chat-box.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. <template>
  2. <view class="page chat-box">
  3. <view class="header">
  4. <uni-icons class="btn-side left" type="back" size="30" @click="onNavBack()"></uni-icons>
  5. <text class="title">{{title}}</text>
  6. <uni-icons class="btn-side right" type="more-filled" size="30" @click="onShowMore()"></uni-icons>
  7. </view>
  8. <view class="chat-msg" @click="switchChatTabBox('none',true)">
  9. <scroll-view class="scroll-box" scroll-y="true" upper-threshold="200" @scrolltoupper="onScrollToTop"
  10. :scroll-into-view="'chat-item-'+scrollMsgIdx">
  11. <view v-for="(msgInfo,idx) in chat.messages" :key="idx">
  12. <chat-message-item v-if="idx>=showMinIdx&&!msgInfo.delete" :headImage="headImage(msgInfo)"
  13. @call="onRtCall(msgInfo)" :showName="showName(msgInfo)" @recall="onRecallMessage"
  14. @delete="onDeleteMessage" @longPressHead="onLongPressHead(msgInfo)" @download="onDownloadFile"
  15. :id="'chat-item-'+idx" :msgInfo="msgInfo" :groupMembers="groupMembers">
  16. </chat-message-item>
  17. </view>
  18. </scroll-view>
  19. </view>
  20. <view v-if="atUserIds.length>0" class="chat-at-bar" @click="openAtBox()">
  21. <view class="iconfont icon-at">:&nbsp;</view>
  22. <scroll-view v-if="atUserIds.length>0" class="chat-at-scroll-box" scroll-x="true" scroll-left="120">
  23. <view class="chat-at-items">
  24. <view v-for="m in atUserItems" class="chat-at-item">
  25. <head-image :name="m.aliasName" :url="m.headImage" :size="50"></head-image>
  26. </view>
  27. </view>
  28. </scroll-view>
  29. </view>
  30. <view class="send-bar">
  31. <view v-if="!showRecord" class="iconfont icon-voice-circle" @click="onRecorderInput()"></view>
  32. <view v-else class="iconfont icon-keyboard" @click="onKeyboardInput()"></view>
  33. <chat-record v-if="showRecord" class="chat-record" @send="onSendRecord"></chat-record>
  34. <view v-else class="send-text">
  35. <textarea class="send-text-area" v-model="sendText" auto-height :show-confirm-bar="false"
  36. :placeholder="isReceipt?'[回执消息]':''" :adjust-position="false" @confirm="sendTextMessage()"
  37. @keyboardheightchange="onKeyboardheightchange" @input="onTextInput" confirm-type="send" confirm-hold
  38. :hold-keyboard="true"></textarea>
  39. </view>
  40. <view v-if="chat.type=='GROUP'" class="iconfont icon-at" @click="openAtBox()"></view>
  41. <view class="iconfont icon-icon_emoji" @click="onShowEmoChatTab()"></view>
  42. <view v-if="sendText==''" class="iconfont icon-add" @click="onShowToolsChatTab()">
  43. </view>
  44. <button v-if="sendText!=''||atUserIds.length>0" class="btn-send" type="primary"
  45. @touchend.prevent="sendTextMessage()" size="mini">发送</button>
  46. </view>
  47. <view class="chat-tab-bar" v-show="chatTabBox!='none' ||showKeyBoard " :style="{height:`${keyboardHeight}px`}">
  48. <view v-if="chatTabBox == 'tools'" class="chat-tools">
  49. <view class="chat-tools-item">
  50. <image-upload :maxCount="9" sourceType="album" :onBefore="onUploadImageBefore"
  51. :onSuccess="onUploadImageSuccess" :onError="onUploadImageFail">
  52. <view class="tool-icon iconfont icon-picture"></view>
  53. </image-upload>
  54. <view class="tool-name">相册</view>
  55. </view>
  56. <view class="chat-tools-item">
  57. <image-upload sourceType="camera" :onBefore="onUploadImageBefore" :onSuccess="onUploadImageSuccess"
  58. :onError="onUploadImageFail">
  59. <view class="tool-icon iconfont icon-camera"></view>
  60. </image-upload>
  61. <view class="tool-name">拍摄</view>
  62. </view>
  63. <!-- #ifndef APP-PLUS -->
  64. <!-- APP 暂时不支持选择文件 -->
  65. <view class="chat-tools-item">
  66. <file-upload :onBefore="onUploadFileBefore" :onSuccess="onUploadFileSuccess"
  67. :onError="onUploadFileFail">
  68. <view class="tool-icon iconfont icon-folder"></view>
  69. </file-upload>
  70. <view class="tool-name">文件</view>
  71. </view>
  72. <!-- #endif -->
  73. <view class="chat-tools-item" @click="onRecorderInput()">
  74. <view class="tool-icon iconfont icon-microphone"></view>
  75. <view class="tool-name">语音消息</view>
  76. </view>
  77. <view v-if="chat.type == 'GROUP'" class="chat-tools-item" @click="switchReceipt()">
  78. <view class="tool-icon iconfont icon-receipt" :class="isReceipt?'active':''"></view>
  79. <view class="tool-name">回执消息</view>
  80. </view>
  81. <!-- #ifndef MP-WEIXIN -->
  82. <!-- 音视频不支持小程序 -->
  83. <view v-if="chat.type == 'PRIVATE'" class="chat-tools-item" @click="onPriviteVideo()">
  84. <view class="tool-icon iconfont icon-video"></view>
  85. <view class="tool-name">视频通话</view>
  86. </view>
  87. <view v-if="chat.type == 'PRIVATE'" class="chat-tools-item" @click="onPriviteVoice()">
  88. <view class="tool-icon iconfont icon-call"></view>
  89. <view class="tool-name">语音通话</view>
  90. </view>
  91. <view v-if="chat.type == 'GROUP'" class="chat-tools-item" @click="onGroupVideo()">
  92. <view class="tool-icon iconfont icon-call"></view>
  93. <view class="tool-name">语音通话</view>
  94. </view>
  95. <!-- #endif -->
  96. </view>
  97. <scroll-view v-if="chatTabBox==='emo'" class="chat-emotion" scroll-y="true">
  98. <view class="emotion-item-list">
  99. <image class="emotion-item" :title="emoText" :src="$emo.textToPath(emoText)"
  100. v-for="(emoText, i) in $emo.emoTextList" :key="i" @click="selectEmoji(emoText)" mode="aspectFit"
  101. lazy-load="true"></image>
  102. </view>
  103. </scroll-view>
  104. <view v-if="showKeyBoard"></view>
  105. </view>
  106. <!-- @用户时选择成员 -->
  107. <chat-at-box ref="atBox" :ownerId="group.ownerId" :members="groupMembers"
  108. @complete="onAtComplete"></chat-at-box>
  109. <!-- 群语音通话时选择成员 -->
  110. <group-member-selector ref="selBox" :members="groupMembers"
  111. :maxSize="$store.state.configStore.webrtc.maxChannel"
  112. @complete="onInviteOk"></group-member-selector>
  113. <group-rtc-join ref="rtcJoin" :groupId="group.id"></group-rtc-join>
  114. </view>
  115. </template>
  116. <script>
  117. import UNI_APP from '@/.env.js';
  118. export default {
  119. data() {
  120. return {
  121. chat: {},
  122. friend: {},
  123. group: {},
  124. groupMembers: [],
  125. sendText: "",
  126. isReceipt: false, // 是否回执消息
  127. scrollMsgIdx: 0, // 滚动条定位为到哪条消息
  128. chatTabBox: 'none',
  129. showKeyBoard: false,
  130. showRecord: false,
  131. keyboardHeight: 322,
  132. atUserIds: [],
  133. recordText: "",
  134. showMinIdx: 0 // 下标小于showMinIdx的消息不显示,否则可能很卡
  135. }
  136. },
  137. methods: {
  138. onRecorderInput() {
  139. this.showRecord = true;
  140. this.switchChatTabBox('none', true);
  141. },
  142. onKeyboardInput() {
  143. this.showRecord = false;
  144. this.switchChatTabBox('none', false);
  145. },
  146. onSendRecord(data) {
  147. let msgInfo = {
  148. content: JSON.stringify(data),
  149. type: this.$enums.MESSAGE_TYPE.AUDIO,
  150. receipt: this.isReceipt
  151. }
  152. // 填充对方id
  153. this.fillTargetId(msgInfo, this.chat.targetId);
  154. this.$http({
  155. url: this.messageAction,
  156. method: 'POST',
  157. data: msgInfo
  158. }).then((id) => {
  159. msgInfo.id = id;
  160. msgInfo.sendTime = new Date().getTime();
  161. msgInfo.sendId = this.$store.state.userStore.userInfo.id;
  162. msgInfo.selfSend = true;
  163. msgInfo.status = this.$enums.MESSAGE_STATUS.UNSEND;
  164. msgInfo.readedCount = 0;
  165. this.$store.commit("insertMessage", msgInfo);
  166. // 会话置顶
  167. this.moveChatToTop();
  168. // 滚动到底部
  169. this.scrollToBottom();
  170. this.isReceipt = false;
  171. })
  172. },
  173. onRtCall(msgInfo) {
  174. if (msgInfo.type == this.$enums.MESSAGE_TYPE.RT_VOICE) {
  175. this.onPriviteVoice();
  176. } else if (msgInfo.type == this.$enums.MESSAGE_TYPE.RT_VIDEO) {
  177. this.onPriviteVideo();
  178. }
  179. },
  180. onPriviteVideo() {
  181. const friendInfo = encodeURIComponent(JSON.stringify(this.friend));
  182. uni.navigateTo({
  183. url: `/pages/chat/chat-private-video?mode=video&friend=${friendInfo}&isHost=true`
  184. })
  185. },
  186. onPriviteVoice() {
  187. const friendInfo = encodeURIComponent(JSON.stringify(this.friend));
  188. uni.navigateTo({
  189. url: `/pages/chat/chat-private-video?mode=voice&friend=${friendInfo}&isHost=true`
  190. })
  191. },
  192. onGroupVideo() {
  193. this.$http({
  194. url: "/webrtc/group/info?groupId="+this.group.id,
  195. method: 'GET'
  196. }).then((rtcInfo)=>{
  197. if(rtcInfo.isChating){
  198. // 已在通话中,可以直接加入通话
  199. this.$refs.rtcJoin.open(rtcInfo);
  200. }else {
  201. // 邀请成员发起通话
  202. let ids = [this.mine.id];
  203. this.$refs.selBox.init(ids, ids);
  204. this.$refs.selBox.open();
  205. }
  206. })
  207. },
  208. onInviteOk(ids) {
  209. if(ids.length < 2){
  210. return;
  211. }
  212. let users = [];
  213. ids.forEach(id => {
  214. let m = this.groupMembers.find(m => m.userId == id);
  215. // 只取部分字段,压缩url长度
  216. users.push({
  217. id: m.userId,
  218. nickName: m.aliasName,
  219. headImage: m.headImage,
  220. isCamera: false,
  221. isMicroPhone: true
  222. })
  223. })
  224. const groupId = this.group.id;
  225. const inviterId = this.mine.id;
  226. const userInfos = encodeURIComponent(JSON.stringify(users));
  227. uni.navigateTo({
  228. url: `/pages/chat/chat-group-video?groupId=${groupId}&isHost=true
  229. &inviterId=${inviterId}&userInfos=${userInfos}`
  230. })
  231. },
  232. moveChatToTop() {
  233. let chatIdx = this.$store.getters.findChatIdx(this.chat);
  234. this.$store.commit("moveTop", chatIdx);
  235. },
  236. switchReceipt() {
  237. this.isReceipt = !this.isReceipt;
  238. },
  239. openAtBox() {
  240. this.$refs.atBox.init(this.atUserIds);
  241. this.$refs.atBox.open();
  242. },
  243. onAtComplete(atUserIds) {
  244. this.atUserIds = atUserIds;
  245. },
  246. onLongPressHead(msgInfo) {
  247. if (!msgInfo.selfSend && this.chat.type == "GROUP" && this.atUserIds.indexOf(msgInfo.sendId) < 0) {
  248. this.atUserIds.push(msgInfo.sendId);
  249. }
  250. },
  251. headImage(msgInfo) {
  252. if (this.chat.type == 'GROUP') {
  253. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  254. return member ? member.headImage : "";
  255. } else {
  256. return msgInfo.selfSend ? this.mine.headImageThumb : this.chat.headImage
  257. }
  258. },
  259. showName(msgInfo) {
  260. if (this.chat.type == 'GROUP') {
  261. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  262. return member ? member.aliasName : "";
  263. } else {
  264. return msgInfo.selfSend ? this.mine.nickName : this.chat.showName
  265. }
  266. },
  267. sendTextMessage() {
  268. if (!this.sendText.trim() && this.atUserIds.length == 0) {
  269. return uni.showToast({
  270. title: "不能发送空白信息",
  271. icon: "none"
  272. });
  273. }
  274. let receiptText = this.isReceipt ? "【回执消息】" : "";
  275. let atText = this.createAtText();
  276. let msgInfo = {
  277. content: receiptText + this.sendText + atText,
  278. atUserIds: this.atUserIds,
  279. receipt: this.isReceipt,
  280. type: 0
  281. }
  282. // 填充对方id
  283. this.fillTargetId(msgInfo, this.chat.targetId);
  284. this.sendText = "";
  285. this.$http({
  286. url: this.messageAction,
  287. method: 'POST',
  288. data: msgInfo
  289. }).then((id) => {
  290. msgInfo.id = id;
  291. msgInfo.sendTime = new Date().getTime();
  292. msgInfo.sendId = this.$store.state.userStore.userInfo.id;
  293. msgInfo.selfSend = true;
  294. msgInfo.readedCount = 0,
  295. msgInfo.status = this.$enums.MESSAGE_STATUS.UNSEND;
  296. this.$store.commit("insertMessage", msgInfo);
  297. // 会话置顶
  298. this.moveChatToTop();
  299. this.sendText = "";
  300. }).finally(() => {
  301. // 滚动到底部
  302. this.scrollToBottom();
  303. // 清空@用户列表
  304. this.atUserIds = [];
  305. this.isReceipt = false;
  306. });
  307. },
  308. createAtText() {
  309. let atText = "";
  310. this.atUserIds.forEach((id) => {
  311. if (id == -1) {
  312. atText += ` @全体成员`;
  313. } else {
  314. let member = this.groupMembers.find((m) => m.userId == id);
  315. if (member) {
  316. atText += ` @${member.aliasName}`;
  317. }
  318. }
  319. })
  320. return atText;
  321. },
  322. fillTargetId(msgInfo, targetId) {
  323. if (this.chat.type == "GROUP") {
  324. msgInfo.groupId = targetId;
  325. } else {
  326. msgInfo.recvId = targetId;
  327. }
  328. },
  329. scrollToBottom() {
  330. let size = this.chat.messages.length;
  331. if (size > 0) {
  332. this.scrollToMsgIdx(size - 1);
  333. }
  334. },
  335. scrollToMsgIdx(idx) {
  336. // 如果scrollMsgIdx值没变化,滚动条不会移动
  337. if (idx == this.scrollMsgIdx && idx > 0) {
  338. this.$nextTick(() => {
  339. // 先滚动到上一条
  340. this.scrollMsgIdx = idx - 1;
  341. // 再滚动目标位置
  342. this.scrollToMsgIdx(idx);
  343. });
  344. return;
  345. }
  346. this.$nextTick(() => {
  347. this.scrollMsgIdx = idx;
  348. });
  349. },
  350. onShowEmoChatTab() {
  351. this.showRecord = false;
  352. this.switchChatTabBox('emo', true)
  353. },
  354. onShowToolsChatTab() {
  355. this.showRecord = false;
  356. this.switchChatTabBox('tools', true)
  357. },
  358. switchChatTabBox(chatTabBox, hideKeyBoard) {
  359. this.chatTabBox = chatTabBox;
  360. if (hideKeyBoard) {
  361. uni.hideKeyboard();
  362. this.showKeyBoard = false;
  363. }
  364. },
  365. selectEmoji(emoText) {
  366. this.sendText += `#${emoText};`;
  367. },
  368. onNavBack() {
  369. uni.switchTab({
  370. url: "/pages/chat/chat"
  371. })
  372. },
  373. onKeyboardheightchange(e) {
  374. if (e.detail.height > 0) {
  375. this.showKeyBoard = true;
  376. this.switchChatTabBox('none', false)
  377. this.keyboardHeight = this.rpxTopx(e.detail.height);
  378. } else {
  379. this.showKeyBoard = false;
  380. }
  381. },
  382. onUploadImageBefore(file) {
  383. let data = {
  384. originUrl: file.path,
  385. thumbUrl: file.path
  386. }
  387. let msgInfo = {
  388. id: 0,
  389. fileId: file.uid,
  390. sendId: this.mine.id,
  391. content: JSON.stringify(data),
  392. sendTime: new Date().getTime(),
  393. selfSend: true,
  394. type: this.$enums.MESSAGE_TYPE.IMAGE,
  395. readedCount: 0,
  396. loadStatus: "loading",
  397. status: this.$enums.MESSAGE_STATUS.UNSEND
  398. }
  399. // 填充对方id
  400. this.fillTargetId(msgInfo, this.chat.targetId);
  401. // 插入消息
  402. this.$store.commit("insertMessage", msgInfo);
  403. // 会话置顶
  404. this.moveChatToTop();
  405. // 借助file对象保存
  406. file.msgInfo = msgInfo;
  407. // 滚到最低部
  408. this.scrollToBottom();
  409. return true;
  410. },
  411. onUploadImageSuccess(file, res) {
  412. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  413. msgInfo.content = JSON.stringify(res.data);
  414. msgInfo.receipt = this.isReceipt
  415. this.$http({
  416. url: this.messageAction,
  417. method: 'POST',
  418. data: msgInfo
  419. }).then((id) => {
  420. msgInfo.loadStatus = 'ok';
  421. msgInfo.id = id;
  422. this.isReceipt = false;
  423. this.$store.commit("insertMessage", msgInfo);
  424. })
  425. },
  426. onUploadImageFail(file, err) {
  427. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  428. msgInfo.loadStatus = 'fail';
  429. this.$store.commit("insertMessage", msgInfo);
  430. },
  431. onUploadFileBefore(file) {
  432. let data = {
  433. name: file.name,
  434. size: file.size,
  435. url: file.path
  436. }
  437. let msgInfo = {
  438. id: 0,
  439. sendId: this.mine.id,
  440. content: JSON.stringify(data),
  441. sendTime: new Date().getTime(),
  442. selfSend: true,
  443. type: this.$enums.MESSAGE_TYPE.FILE,
  444. readedCount: 0,
  445. loadStatus: "loading",
  446. status: this.$enums.MESSAGE_STATUS.UNSEND
  447. }
  448. // 填充对方id
  449. this.fillTargetId(msgInfo, this.chat.targetId);
  450. // 插入消息
  451. this.$store.commit("insertMessage", msgInfo);
  452. // 会话置顶
  453. this.moveChatToTop();
  454. // 借助file对象保存
  455. file.msgInfo = msgInfo;
  456. // 滚到最低部
  457. this.scrollToBottom();
  458. return true;
  459. },
  460. onUploadFileSuccess(file, res) {
  461. let data = {
  462. name: file.name,
  463. size: file.size,
  464. url: res.data
  465. }
  466. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  467. msgInfo.content = JSON.stringify(data);
  468. msgInfo.receipt = this.isReceipt
  469. this.$http({
  470. url: this.messageAction,
  471. method: 'POST',
  472. data: msgInfo
  473. }).then((id) => {
  474. msgInfo.loadStatus = 'ok';
  475. msgInfo.id = id;
  476. this.isReceipt = false;
  477. this.$store.commit("insertMessage", msgInfo);
  478. })
  479. },
  480. onUploadFileFail(file, res) {
  481. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  482. msgInfo.loadStatus = 'fail';
  483. this.$store.commit("insertMessage", msgInfo);
  484. },
  485. onDeleteMessage(msgInfo) {
  486. uni.showModal({
  487. title: '删除消息',
  488. content: '确认删除消息?',
  489. success: (res) => {
  490. if (!res.cancel) {
  491. this.$store.commit("deleteMessage", msgInfo);
  492. uni.showToast({
  493. title: "删除成功",
  494. icon: "none"
  495. })
  496. }
  497. }
  498. })
  499. },
  500. onRecallMessage(msgInfo) {
  501. uni.showModal({
  502. title: '撤回消息',
  503. content: '确认撤回消息?',
  504. success: (res) => {
  505. if (!res.cancel) {
  506. let url = `/message/${this.chat.type.toLowerCase()}/recall/${msgInfo.id}`
  507. this.$http({
  508. url: url,
  509. method: 'DELETE'
  510. }).then(() => {
  511. msgInfo = JSON.parse(JSON.stringify(msgInfo));
  512. msgInfo.type = this.$enums.MESSAGE_TYPE.RECALL;
  513. msgInfo.content = '你撤回了一条消息';
  514. msgInfo.status = this.$enums.MESSAGE_STATUS.RECALL;
  515. this.$store.commit("insertMessage", msgInfo);
  516. })
  517. }
  518. }
  519. })
  520. },
  521. onDownloadFile(msgInfo) {
  522. let url = JSON.parse(msgInfo.content).url;
  523. uni.downloadFile({
  524. url: url,
  525. success(res) {
  526. if (res.statusCode === 200) {
  527. var filePath = encodeURI(res.tempFilePath);
  528. uni.openDocument({
  529. filePath: filePath,
  530. showMenu: true
  531. });
  532. }
  533. },
  534. fail(e) {
  535. console.log(e);
  536. uni.showToast({
  537. title: "文件下载失败",
  538. icon: "none"
  539. })
  540. }
  541. });
  542. },
  543. onScrollToTop() {
  544. if (this.showMinIdx == 0) {
  545. console.log("消息已滚动到顶部")
  546. return;
  547. }
  548. // #ifndef H5
  549. // 防止滚动条定格在顶部,不能一直往上滚
  550. this.scrollToMsgIdx(this.showMinIdx);
  551. // #endif
  552. // 多展示0条信息
  553. this.showMinIdx = this.showMinIdx > 20 ? this.showMinIdx - 20 : 0;
  554. },
  555. onShowMore() {
  556. if (this.chat.type == "GROUP") {
  557. uni.navigateTo({
  558. url: "/pages/group/group-info?id=" + this.group.id
  559. })
  560. } else {
  561. uni.navigateTo({
  562. url: "/pages/common/user-info?id=" + this.friend.id
  563. })
  564. }
  565. },
  566. onTextInput(e) {
  567. let idx = e.detail.cursor - 1;
  568. if (this.chat.type == 'GROUP' && e.detail.value[idx] == '@') {
  569. this.openAtBox();
  570. let sendText = e.detail.value.replace("@", '');
  571. this.$nextTick(() => {
  572. this.sendText = sendText;
  573. })
  574. }
  575. },
  576. loadReaded(fId) {
  577. this.$http({
  578. url: `/message/private/maxReadedId?friendId=${fId}`,
  579. method: 'get'
  580. }).then((id) => {
  581. this.$store.commit("readedMessage", {
  582. friendId: fId,
  583. maxId: id
  584. });
  585. });
  586. },
  587. readedMessage() {
  588. console.log("readedMessage")
  589. if (this.unreadCount == 0) {
  590. return;
  591. }
  592. let url = ""
  593. if (this.chat.type == "GROUP") {
  594. url = `/message/group/readed?groupId=${this.chat.targetId}`
  595. } else {
  596. url = `/message/private/readed?friendId=${this.chat.targetId}`
  597. }
  598. this.$http({
  599. url: url,
  600. method: 'PUT'
  601. }).then(() => {
  602. this.$store.commit("resetUnreadCount", this.chat)
  603. this.scrollToBottom();
  604. })
  605. },
  606. loadGroup(groupId) {
  607. this.$http({
  608. url: `/group/find/${groupId}`,
  609. method: 'GET'
  610. }).then((group) => {
  611. this.group = group;
  612. this.$store.commit("updateChatFromGroup", group);
  613. this.$store.commit("updateGroup", group);
  614. });
  615. this.$http({
  616. url: `/group/members/${groupId}`,
  617. method: 'GET'
  618. }).then((groupMembers) => {
  619. this.groupMembers = groupMembers;
  620. });
  621. },
  622. loadFriend(friendId) {
  623. // 获取对方最新信息
  624. this.$http({
  625. url: `/user/find/${friendId}`,
  626. method: 'GET'
  627. }).then((friend) => {
  628. this.friend = friend;
  629. this.$store.commit("updateChatFromFriend", friend);
  630. this.$store.commit("updateFriend", friend);
  631. })
  632. },
  633. rpxTopx(rpx) {
  634. // px转换成rpx
  635. let info = uni.getSystemInfoSync()
  636. let px = info.windowWidth * rpx / 750;
  637. return Math.floor(rpx);
  638. }
  639. },
  640. computed: {
  641. mine() {
  642. return this.$store.state.userStore.userInfo;
  643. },
  644. title() {
  645. if (!this.chat) {
  646. return "";
  647. }
  648. let title = this.chat.showName;
  649. if (this.chat.type == "GROUP") {
  650. let size = this.groupMembers.filter(m => !m.quit).length;
  651. title += `(${size})`;
  652. }
  653. return title;
  654. },
  655. messageAction() {
  656. return `/message/${this.chat.type.toLowerCase()}/send`;
  657. },
  658. messageSize() {
  659. if (!this.chat || !this.chat.messages) {
  660. return 0;
  661. }
  662. return this.chat.messages.length;
  663. },
  664. unreadCount() {
  665. return this.chat.unreadCount;
  666. },
  667. atUserItems() {
  668. let atUsers = [];
  669. this.atUserIds.forEach((id) => {
  670. if (id == -1) {
  671. atUsers.push({
  672. id: -1,
  673. aliasName: "全体成员"
  674. })
  675. return;
  676. }
  677. let member = this.groupMembers.find((m) => m.userId == id);
  678. if (member) {
  679. atUsers.push(member);
  680. }
  681. })
  682. return atUsers;
  683. }
  684. },
  685. watch: {
  686. messageSize: function(newSize, oldSize) {
  687. // 接收到消息时滚动到底部
  688. if (newSize > oldSize) {
  689. this.scrollToBottom();
  690. }
  691. },
  692. unreadCount: {
  693. handler(newCount, oldCount) {
  694. if (newCount > 0) {
  695. // 消息已读
  696. this.readedMessage()
  697. }
  698. }
  699. }
  700. },
  701. onLoad(options) {
  702. // 聊天数据
  703. this.chat = this.$store.state.chatStore.chats[options.chatIdx];
  704. // 初始状态只显示20条消息
  705. let size = this.chat.messages.length;
  706. this.showMinIdx = size > 20 ? size - 20 : 0;
  707. // 消息已读
  708. this.readedMessage()
  709. // 加载好友或群聊信息
  710. if (this.chat.type == "GROUP") {
  711. this.loadGroup(this.chat.targetId);
  712. } else {
  713. this.loadFriend(this.chat.targetId);
  714. this.loadReaded(this.chat.targetId)
  715. }
  716. // 激活当前会话
  717. this.$store.commit("activeChat", options.chatIdx);
  718. // 复位回执消息
  719. this.isReceipt = false;
  720. // 页面滚到底部
  721. this.scrollToBottom();
  722. },
  723. onUnload() {
  724. this.$store.commit("activeChat", -1);
  725. }
  726. }
  727. </script>
  728. <style lang="scss" scoped>
  729. .chat-box {
  730. position: relative;
  731. border: #dddddd solid 1px;
  732. display: flex;
  733. flex-direction: column;
  734. .header {
  735. display: flex;
  736. justify-content: center;
  737. align-items: center;
  738. height: 60rpx;
  739. padding: 5px;
  740. background-color: white;
  741. line-height: 50px;
  742. font-size: 40rpx;
  743. font-weight: 600;
  744. border: #dddddd solid 1px;
  745. .btn-side {
  746. position: absolute;
  747. line-height: 60rpx;
  748. font-size: 28rpx;
  749. cursor: pointer;
  750. &.left {
  751. left: 30rpx;
  752. }
  753. &.right {
  754. right: 30rpx;
  755. }
  756. }
  757. }
  758. .chat-msg {
  759. flex: 1;
  760. padding: 0;
  761. border: #dddddd solid 1px;
  762. overflow: hidden;
  763. position: relative;
  764. background-color: #f8f8f8;
  765. .scroll-box {
  766. height: 100%;
  767. }
  768. }
  769. .chat-at-bar {
  770. display: flex;
  771. align-items: center;
  772. padding: 0 10rpx;
  773. border: #dddddd solid 1px;
  774. .icon-at {
  775. font-size: 35rpx;
  776. color: darkblue;
  777. font-weight: 600;
  778. }
  779. .chat-at-scroll-box {
  780. flex: 1;
  781. width: 80%;
  782. .chat-at-items {
  783. display: flex;
  784. align-items: center;
  785. height: 70rpx;
  786. .chat-at-item {
  787. padding: 0 3rpx;
  788. }
  789. }
  790. }
  791. }
  792. .send-bar {
  793. display: flex;
  794. align-items: center;
  795. padding: 10rpx;
  796. margin-bottom: 10rpx;
  797. border: #dddddd solid 1px;
  798. background-color: white;
  799. .iconfont {
  800. font-size: 60rpx;
  801. margin: 3rpx;
  802. }
  803. .chat-record {
  804. flex: 1;
  805. }
  806. .send-text {
  807. flex: 1;
  808. background-color: #f8f8f8 !important;
  809. overflow: auto;
  810. padding: 20rpx;
  811. background-color: #fff;
  812. border-radius: 20rpx;
  813. font-size: 30rpx;
  814. box-sizing: border-box;
  815. .send-text-area {
  816. width: 100%;
  817. }
  818. }
  819. .btn-send {
  820. margin: 5rpx;
  821. }
  822. }
  823. .chat-tab-bar {
  824. height: 500rpx;
  825. padding: 20rpx;
  826. background-color: whitesmoke;
  827. .chat-tools {
  828. display: flex;
  829. flex-wrap: wrap;
  830. .chat-tools-item {
  831. width: 140rpx;
  832. padding: 16rpx;
  833. display: flex;
  834. flex-direction: column;
  835. align-items: center;
  836. .tool-icon {
  837. padding: 28rpx;
  838. font-size: 60rpx;
  839. background-color: white;
  840. border-radius: 20%;
  841. &.active {
  842. background-color: #ddd;
  843. }
  844. }
  845. .tool-name {
  846. height: 60rpx;
  847. line-height: 60rpx;
  848. font-size: 25rpx;
  849. }
  850. }
  851. }
  852. .chat-emotion {
  853. height: 100%;
  854. .emotion-item-list {
  855. display: flex;
  856. flex-wrap: wrap;
  857. .emotion-item {
  858. width: 40px;
  859. height: 40px;
  860. text-align: center;
  861. cursor: pointer;
  862. padding: 6px;
  863. }
  864. }
  865. }
  866. }
  867. }
  868. </style>