chat-box.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902
  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="onVideoCall()">
  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="onVoiceCall()">
  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="onSelectMember"></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.onVoiceCall();
  176. } else if (msgInfo.type == this.$enums.MESSAGE_TYPE.RT_VIDEO) {
  177. this.onVideoCall();
  178. }
  179. },
  180. onVideoCall() {
  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. onVoiceCall() {
  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. onSelectMember(ids) {
  209. let users = [];
  210. ids.forEach(id => {
  211. let m = this.groupMembers.find(m => m.userId == id);
  212. // 只取部分字段,压缩url长度
  213. users.push({
  214. id: m.userId,
  215. nickName: m.aliasName,
  216. headImage: m.headImage,
  217. isCamera: false
  218. })
  219. })
  220. const groupId = this.group.id;
  221. const inviterId = this.mine.id;
  222. const userInfos = encodeURIComponent(JSON.stringify(users));
  223. uni.navigateTo({
  224. url: `/pages/chat/chat-group-video?groupId=${groupId}&isHost=true
  225. &inviterId=${inviterId}&userInfos=${userInfos}`
  226. })
  227. },
  228. moveChatToTop() {
  229. let chatIdx = this.$store.getters.findChatIdx(this.chat);
  230. this.$store.commit("moveTop", chatIdx);
  231. },
  232. switchReceipt() {
  233. this.isReceipt = !this.isReceipt;
  234. },
  235. openAtBox() {
  236. this.$refs.atBox.init(this.atUserIds);
  237. this.$refs.atBox.open();
  238. },
  239. onAtComplete(atUserIds) {
  240. this.atUserIds = atUserIds;
  241. },
  242. onLongPressHead(msgInfo) {
  243. if (!msgInfo.selfSend && this.chat.type == "GROUP" && this.atUserIds.indexOf(msgInfo.sendId) < 0) {
  244. this.atUserIds.push(msgInfo.sendId);
  245. }
  246. },
  247. headImage(msgInfo) {
  248. if (this.chat.type == 'GROUP') {
  249. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  250. return member ? member.headImage : "";
  251. } else {
  252. return msgInfo.selfSend ? this.mine.headImageThumb : this.chat.headImage
  253. }
  254. },
  255. showName(msgInfo) {
  256. if (this.chat.type == 'GROUP') {
  257. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  258. return member ? member.aliasName : "";
  259. } else {
  260. return msgInfo.selfSend ? this.mine.nickName : this.chat.showName
  261. }
  262. },
  263. sendTextMessage() {
  264. if (!this.sendText.trim() && this.atUserIds.length == 0) {
  265. return uni.showToast({
  266. title: "不能发送空白信息",
  267. icon: "none"
  268. });
  269. }
  270. let receiptText = this.isReceipt ? "【回执消息】" : "";
  271. let atText = this.createAtText();
  272. let msgInfo = {
  273. content: receiptText + this.sendText + atText,
  274. atUserIds: this.atUserIds,
  275. receipt: this.isReceipt,
  276. type: 0
  277. }
  278. // 填充对方id
  279. this.fillTargetId(msgInfo, this.chat.targetId);
  280. this.sendText = "";
  281. this.$http({
  282. url: this.messageAction,
  283. method: 'POST',
  284. data: msgInfo
  285. }).then((id) => {
  286. msgInfo.id = id;
  287. msgInfo.sendTime = new Date().getTime();
  288. msgInfo.sendId = this.$store.state.userStore.userInfo.id;
  289. msgInfo.selfSend = true;
  290. msgInfo.readedCount = 0,
  291. msgInfo.status = this.$enums.MESSAGE_STATUS.UNSEND;
  292. this.$store.commit("insertMessage", msgInfo);
  293. // 会话置顶
  294. this.moveChatToTop();
  295. this.sendText = "";
  296. }).finally(() => {
  297. // 滚动到底部
  298. this.scrollToBottom();
  299. // 清空@用户列表
  300. this.atUserIds = [];
  301. this.isReceipt = false;
  302. });
  303. },
  304. createAtText() {
  305. let atText = "";
  306. this.atUserIds.forEach((id) => {
  307. if (id == -1) {
  308. atText += ` @全体成员`;
  309. } else {
  310. let member = this.groupMembers.find((m) => m.userId == id);
  311. if (member) {
  312. atText += ` @${member.aliasName}`;
  313. }
  314. }
  315. })
  316. return atText;
  317. },
  318. fillTargetId(msgInfo, targetId) {
  319. if (this.chat.type == "GROUP") {
  320. msgInfo.groupId = targetId;
  321. } else {
  322. msgInfo.recvId = targetId;
  323. }
  324. },
  325. scrollToBottom() {
  326. let size = this.chat.messages.length;
  327. if (size > 0) {
  328. this.scrollToMsgIdx(size - 1);
  329. }
  330. },
  331. scrollToMsgIdx(idx) {
  332. // 如果scrollMsgIdx值没变化,滚动条不会移动
  333. if (idx == this.scrollMsgIdx && idx > 0) {
  334. this.$nextTick(() => {
  335. // 先滚动到上一条
  336. this.scrollMsgIdx = idx - 1;
  337. // 再滚动目标位置
  338. this.scrollToMsgIdx(idx);
  339. });
  340. return;
  341. }
  342. this.$nextTick(() => {
  343. this.scrollMsgIdx = idx;
  344. });
  345. },
  346. onShowEmoChatTab() {
  347. this.showRecord = false;
  348. this.switchChatTabBox('emo', true)
  349. },
  350. onShowToolsChatTab() {
  351. this.showRecord = false;
  352. this.switchChatTabBox('tools', true)
  353. },
  354. switchChatTabBox(chatTabBox, hideKeyBoard) {
  355. this.chatTabBox = chatTabBox;
  356. if (hideKeyBoard) {
  357. uni.hideKeyboard();
  358. this.showKeyBoard = false;
  359. }
  360. },
  361. selectEmoji(emoText) {
  362. this.sendText += `#${emoText};`;
  363. },
  364. onNavBack() {
  365. uni.switchTab({
  366. url: "/pages/chat/chat"
  367. })
  368. },
  369. onKeyboardheightchange(e) {
  370. if (e.detail.height > 0) {
  371. this.showKeyBoard = true;
  372. this.switchChatTabBox('none', false)
  373. this.keyboardHeight = this.rpxTopx(e.detail.height);
  374. } else {
  375. this.showKeyBoard = false;
  376. }
  377. },
  378. onUploadImageBefore(file) {
  379. let data = {
  380. originUrl: file.path,
  381. thumbUrl: file.path
  382. }
  383. let msgInfo = {
  384. id: 0,
  385. fileId: file.uid,
  386. sendId: this.mine.id,
  387. content: JSON.stringify(data),
  388. sendTime: new Date().getTime(),
  389. selfSend: true,
  390. type: this.$enums.MESSAGE_TYPE.IMAGE,
  391. readedCount: 0,
  392. loadStatus: "loading",
  393. status: this.$enums.MESSAGE_STATUS.UNSEND
  394. }
  395. // 填充对方id
  396. this.fillTargetId(msgInfo, this.chat.targetId);
  397. // 插入消息
  398. this.$store.commit("insertMessage", msgInfo);
  399. // 会话置顶
  400. this.moveChatToTop();
  401. // 借助file对象保存
  402. file.msgInfo = msgInfo;
  403. // 滚到最低部
  404. this.scrollToBottom();
  405. return true;
  406. },
  407. onUploadImageSuccess(file, res) {
  408. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  409. msgInfo.content = JSON.stringify(res.data);
  410. msgInfo.receipt = this.isReceipt
  411. this.$http({
  412. url: this.messageAction,
  413. method: 'POST',
  414. data: msgInfo
  415. }).then((id) => {
  416. msgInfo.loadStatus = 'ok';
  417. msgInfo.id = id;
  418. this.isReceipt = false;
  419. this.$store.commit("insertMessage", msgInfo);
  420. })
  421. },
  422. onUploadImageFail(file, err) {
  423. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  424. msgInfo.loadStatus = 'fail';
  425. this.$store.commit("insertMessage", msgInfo);
  426. },
  427. onUploadFileBefore(file) {
  428. let data = {
  429. name: file.name,
  430. size: file.size,
  431. url: file.path
  432. }
  433. let msgInfo = {
  434. id: 0,
  435. sendId: this.mine.id,
  436. content: JSON.stringify(data),
  437. sendTime: new Date().getTime(),
  438. selfSend: true,
  439. type: this.$enums.MESSAGE_TYPE.FILE,
  440. readedCount: 0,
  441. loadStatus: "loading",
  442. status: this.$enums.MESSAGE_STATUS.UNSEND
  443. }
  444. // 填充对方id
  445. this.fillTargetId(msgInfo, this.chat.targetId);
  446. // 插入消息
  447. this.$store.commit("insertMessage", msgInfo);
  448. // 会话置顶
  449. this.moveChatToTop();
  450. // 借助file对象保存
  451. file.msgInfo = msgInfo;
  452. // 滚到最低部
  453. this.scrollToBottom();
  454. return true;
  455. },
  456. onUploadFileSuccess(file, res) {
  457. let data = {
  458. name: file.name,
  459. size: file.size,
  460. url: res.data
  461. }
  462. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  463. msgInfo.content = JSON.stringify(data);
  464. msgInfo.receipt = this.isReceipt
  465. this.$http({
  466. url: this.messageAction,
  467. method: 'POST',
  468. data: msgInfo
  469. }).then((id) => {
  470. msgInfo.loadStatus = 'ok';
  471. msgInfo.id = id;
  472. this.isReceipt = false;
  473. this.$store.commit("insertMessage", msgInfo);
  474. })
  475. },
  476. onUploadFileFail(file, res) {
  477. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  478. msgInfo.loadStatus = 'fail';
  479. this.$store.commit("insertMessage", msgInfo);
  480. },
  481. onDeleteMessage(msgInfo) {
  482. uni.showModal({
  483. title: '删除消息',
  484. content: '确认删除消息?',
  485. success: (res) => {
  486. if (!res.cancel) {
  487. this.$store.commit("deleteMessage", msgInfo);
  488. uni.showToast({
  489. title: "删除成功",
  490. icon: "none"
  491. })
  492. }
  493. }
  494. })
  495. },
  496. onRecallMessage(msgInfo) {
  497. uni.showModal({
  498. title: '撤回消息',
  499. content: '确认撤回消息?',
  500. success: (res) => {
  501. if (!res.cancel) {
  502. let url = `/message/${this.chat.type.toLowerCase()}/recall/${msgInfo.id}`
  503. this.$http({
  504. url: url,
  505. method: 'DELETE'
  506. }).then(() => {
  507. msgInfo = JSON.parse(JSON.stringify(msgInfo));
  508. msgInfo.type = this.$enums.MESSAGE_TYPE.RECALL;
  509. msgInfo.content = '你撤回了一条消息';
  510. msgInfo.status = this.$enums.MESSAGE_STATUS.RECALL;
  511. this.$store.commit("insertMessage", msgInfo);
  512. })
  513. }
  514. }
  515. })
  516. },
  517. onDownloadFile(msgInfo) {
  518. let url = JSON.parse(msgInfo.content).url;
  519. uni.downloadFile({
  520. url: url,
  521. success(res) {
  522. if (res.statusCode === 200) {
  523. var filePath = encodeURI(res.tempFilePath);
  524. uni.openDocument({
  525. filePath: filePath,
  526. showMenu: true
  527. });
  528. }
  529. },
  530. fail(e) {
  531. console.log(e);
  532. uni.showToast({
  533. title: "文件下载失败",
  534. icon: "none"
  535. })
  536. }
  537. });
  538. },
  539. onScrollToTop() {
  540. if (this.showMinIdx == 0) {
  541. console.log("消息已滚动到顶部")
  542. return;
  543. }
  544. // #ifndef H5
  545. // 防止滚动条定格在顶部,不能一直往上滚
  546. this.scrollToMsgIdx(this.showMinIdx);
  547. // #endif
  548. // 多展示0条信息
  549. this.showMinIdx = this.showMinIdx > 20 ? this.showMinIdx - 20 : 0;
  550. },
  551. onShowMore() {
  552. if (this.chat.type == "GROUP") {
  553. uni.navigateTo({
  554. url: "/pages/group/group-info?id=" + this.group.id
  555. })
  556. } else {
  557. uni.navigateTo({
  558. url: "/pages/common/user-info?id=" + this.friend.id
  559. })
  560. }
  561. },
  562. onTextInput(e) {
  563. let idx = e.detail.cursor - 1;
  564. if (this.chat.type == 'GROUP' && e.detail.value[idx] == '@') {
  565. this.openAtBox();
  566. let sendText = e.detail.value.replace("@", '');
  567. this.$nextTick(() => {
  568. this.sendText = sendText;
  569. })
  570. }
  571. },
  572. loadReaded(fId) {
  573. this.$http({
  574. url: `/message/private/maxReadedId?friendId=${fId}`,
  575. method: 'get'
  576. }).then((id) => {
  577. this.$store.commit("readedMessage", {
  578. friendId: fId,
  579. maxId: id
  580. });
  581. });
  582. },
  583. readedMessage() {
  584. console.log("readedMessage")
  585. if (this.unreadCount == 0) {
  586. return;
  587. }
  588. let url = ""
  589. if (this.chat.type == "GROUP") {
  590. url = `/message/group/readed?groupId=${this.chat.targetId}`
  591. } else {
  592. url = `/message/private/readed?friendId=${this.chat.targetId}`
  593. }
  594. this.$http({
  595. url: url,
  596. method: 'PUT'
  597. }).then(() => {
  598. this.$store.commit("resetUnreadCount", this.chat)
  599. this.scrollToBottom();
  600. })
  601. },
  602. loadGroup(groupId) {
  603. this.$http({
  604. url: `/group/find/${groupId}`,
  605. method: 'GET'
  606. }).then((group) => {
  607. this.group = group;
  608. this.$store.commit("updateChatFromGroup", group);
  609. this.$store.commit("updateGroup", group);
  610. });
  611. this.$http({
  612. url: `/group/members/${groupId}`,
  613. method: 'GET'
  614. }).then((groupMembers) => {
  615. this.groupMembers = groupMembers;
  616. });
  617. },
  618. loadFriend(friendId) {
  619. // 获取对方最新信息
  620. this.$http({
  621. url: `/user/find/${friendId}`,
  622. method: 'GET'
  623. }).then((friend) => {
  624. this.friend = friend;
  625. this.$store.commit("updateChatFromFriend", friend);
  626. this.$store.commit("updateFriend", friend);
  627. })
  628. },
  629. rpxTopx(rpx) {
  630. // px转换成rpx
  631. let info = uni.getSystemInfoSync()
  632. let px = info.windowWidth * rpx / 750;
  633. return Math.floor(rpx);
  634. }
  635. },
  636. computed: {
  637. mine() {
  638. return this.$store.state.userStore.userInfo;
  639. },
  640. title() {
  641. if (!this.chat) {
  642. return "";
  643. }
  644. let title = this.chat.showName;
  645. if (this.chat.type == "GROUP") {
  646. let size = this.groupMembers.filter(m => !m.quit).length;
  647. title += `(${size})`;
  648. }
  649. return title;
  650. },
  651. messageAction() {
  652. return `/message/${this.chat.type.toLowerCase()}/send`;
  653. },
  654. messageSize() {
  655. if (!this.chat || !this.chat.messages) {
  656. return 0;
  657. }
  658. return this.chat.messages.length;
  659. },
  660. unreadCount() {
  661. return this.chat.unreadCount;
  662. },
  663. atUserItems() {
  664. let atUsers = [];
  665. this.atUserIds.forEach((id) => {
  666. if (id == -1) {
  667. atUsers.push({
  668. id: -1,
  669. aliasName: "全体成员"
  670. })
  671. return;
  672. }
  673. let member = this.groupMembers.find((m) => m.userId == id);
  674. if (member) {
  675. atUsers.push(member);
  676. }
  677. })
  678. return atUsers;
  679. }
  680. },
  681. watch: {
  682. messageSize: function(newSize, oldSize) {
  683. // 接收到消息时滚动到底部
  684. if (newSize > oldSize) {
  685. this.scrollToBottom();
  686. }
  687. },
  688. unreadCount: {
  689. handler(newCount, oldCount) {
  690. if (newCount > 0) {
  691. // 消息已读
  692. this.readedMessage()
  693. }
  694. }
  695. }
  696. },
  697. onLoad(options) {
  698. // 聊天数据
  699. this.chat = this.$store.state.chatStore.chats[options.chatIdx];
  700. // 初始状态只显示20条消息
  701. let size = this.chat.messages.length;
  702. this.showMinIdx = size > 20 ? size - 20 : 0;
  703. // 消息已读
  704. this.readedMessage()
  705. // 加载好友或群聊信息
  706. if (this.chat.type == "GROUP") {
  707. this.loadGroup(this.chat.targetId);
  708. } else {
  709. this.loadFriend(this.chat.targetId);
  710. this.loadReaded(this.chat.targetId)
  711. }
  712. // 激活当前会话
  713. this.$store.commit("activeChat", options.chatIdx);
  714. // 复位回执消息
  715. this.isReceipt = false;
  716. // 页面滚到底部
  717. this.scrollToBottom();
  718. },
  719. onUnload() {
  720. this.$store.commit("activeChat", -1);
  721. }
  722. }
  723. </script>
  724. <style lang="scss" scoped>
  725. .chat-box {
  726. position: relative;
  727. border: #dddddd solid 1px;
  728. display: flex;
  729. flex-direction: column;
  730. .header {
  731. display: flex;
  732. justify-content: center;
  733. align-items: center;
  734. height: 60rpx;
  735. padding: 5px;
  736. background-color: white;
  737. line-height: 50px;
  738. font-size: 40rpx;
  739. font-weight: 600;
  740. border: #dddddd solid 1px;
  741. .btn-side {
  742. position: absolute;
  743. line-height: 60rpx;
  744. font-size: 28rpx;
  745. cursor: pointer;
  746. &.left {
  747. left: 30rpx;
  748. }
  749. &.right {
  750. right: 30rpx;
  751. }
  752. }
  753. }
  754. .chat-msg {
  755. flex: 1;
  756. padding: 0;
  757. border: #dddddd solid 1px;
  758. overflow: hidden;
  759. position: relative;
  760. background-color: #f8f8f8;
  761. .scroll-box {
  762. height: 100%;
  763. }
  764. }
  765. .chat-at-bar {
  766. display: flex;
  767. align-items: center;
  768. padding: 0 10rpx;
  769. border: #dddddd solid 1px;
  770. .icon-at {
  771. font-size: 35rpx;
  772. color: darkblue;
  773. font-weight: 600;
  774. }
  775. .chat-at-scroll-box {
  776. flex: 1;
  777. width: 80%;
  778. .chat-at-items {
  779. display: flex;
  780. align-items: center;
  781. height: 70rpx;
  782. .chat-at-item {
  783. padding: 0 3rpx;
  784. }
  785. }
  786. }
  787. }
  788. .send-bar {
  789. display: flex;
  790. align-items: center;
  791. padding: 10rpx;
  792. margin-bottom: 10rpx;
  793. border: #dddddd solid 1px;
  794. background-color: white;
  795. .iconfont {
  796. font-size: 60rpx;
  797. margin: 3rpx;
  798. }
  799. .chat-record {
  800. flex: 1;
  801. }
  802. .send-text {
  803. flex: 1;
  804. background-color: #f8f8f8 !important;
  805. overflow: auto;
  806. padding: 20rpx;
  807. background-color: #fff;
  808. border-radius: 20rpx;
  809. font-size: 30rpx;
  810. box-sizing: border-box;
  811. .send-text-area {
  812. width: 100%;
  813. }
  814. }
  815. .btn-send {
  816. margin: 5rpx;
  817. }
  818. }
  819. .chat-tab-bar {
  820. height: 500rpx;
  821. padding: 20rpx;
  822. background-color: whitesmoke;
  823. .chat-tools {
  824. display: flex;
  825. flex-wrap: wrap;
  826. .chat-tools-item {
  827. width: 140rpx;
  828. padding: 16rpx;
  829. display: flex;
  830. flex-direction: column;
  831. align-items: center;
  832. .tool-icon {
  833. padding: 28rpx;
  834. font-size: 60rpx;
  835. background-color: white;
  836. border-radius: 20%;
  837. &.active {
  838. background-color: #ddd;
  839. }
  840. }
  841. .tool-name {
  842. height: 60rpx;
  843. line-height: 60rpx;
  844. font-size: 25rpx;
  845. }
  846. }
  847. }
  848. .chat-emotion {
  849. height: 100%;
  850. .emotion-item-list {
  851. display: flex;
  852. flex-wrap: wrap;
  853. .emotion-item {
  854. width: 40px;
  855. height: 40px;
  856. text-align: center;
  857. cursor: pointer;
  858. padding: 6px;
  859. }
  860. }
  861. }
  862. }
  863. }
  864. </style>