chat-box.vue 22 KB

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