chat-box.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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="onRecorderInput()">
  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. return;
  541. }
  542. let url = ""
  543. if (this.chat.type == "GROUP") {
  544. url = `/message/group/readed?groupId=${this.chat.targetId}`
  545. } else {
  546. url = `/message/private/readed?friendId=${this.chat.targetId}`
  547. }
  548. this.$http({
  549. url: url,
  550. method: 'PUT'
  551. }).then(() => {
  552. this.$store.commit("resetUnreadCount", this.chat)
  553. this.scrollToBottom();
  554. })
  555. },
  556. loadGroup(groupId) {
  557. this.$http({
  558. url: `/group/find/${groupId}`,
  559. method: 'GET'
  560. }).then((group) => {
  561. this.group = group;
  562. this.$store.commit("updateChatFromGroup", group);
  563. this.$store.commit("updateGroup", group);
  564. });
  565. this.$http({
  566. url: `/group/members/${groupId}`,
  567. method: 'GET'
  568. }).then((groupMembers) => {
  569. this.groupMembers = groupMembers;
  570. });
  571. },
  572. loadFriend(friendId) {
  573. // 获取对方最新信息
  574. this.$http({
  575. url: `/user/find/${friendId}`,
  576. method: 'GET'
  577. }).then((friend) => {
  578. this.friend = friend;
  579. this.$store.commit("updateChatFromFriend", friend);
  580. this.$store.commit("updateFriend", friend);
  581. })
  582. },
  583. rpxTopx(rpx) {
  584. // px转换成rpx
  585. let info = uni.getSystemInfoSync()
  586. let px = info.windowWidth * rpx / 750;
  587. return Math.floor(rpx);
  588. }
  589. },
  590. computed: {
  591. mine() {
  592. return this.$store.state.userStore.userInfo;
  593. },
  594. title() {
  595. if (!this.chat) {
  596. return "";
  597. }
  598. let title = this.chat.showName;
  599. if (this.chat.type == "GROUP") {
  600. let size = this.groupMembers.filter(m => !m.quit).length;
  601. title += `(${size})`;
  602. }
  603. return title;
  604. },
  605. messageAction() {
  606. return `/message/${this.chat.type.toLowerCase()}/send`;
  607. },
  608. messageSize() {
  609. if (!this.chat || !this.chat.messages) {
  610. return 0;
  611. }
  612. return this.chat.messages.length;
  613. },
  614. unreadCount() {
  615. return this.chat.unreadCount;
  616. },
  617. atUserItems() {
  618. let atUsers = [];
  619. this.atUserIds.forEach((id) => {
  620. if (id == -1) {
  621. atUsers.push({
  622. id: -1,
  623. aliasName: "全体成员"
  624. })
  625. return;
  626. }
  627. let member = this.groupMembers.find((m) => m.userId == id);
  628. if (member) {
  629. atUsers.push(member);
  630. }
  631. })
  632. return atUsers;
  633. }
  634. },
  635. watch: {
  636. messageSize: function(newSize, oldSize) {
  637. // 接收到消息时滚动到底部
  638. if (newSize > oldSize) {
  639. this.scrollToBottom();
  640. }
  641. },
  642. unreadCount: {
  643. handler(newCount, oldCount) {
  644. if (newCount > 0) {
  645. // 消息已读
  646. this.readedMessage()
  647. }
  648. }
  649. }
  650. },
  651. onLoad(options) {
  652. // 聊天数据
  653. this.chat = this.$store.state.chatStore.chats[options.chatIdx];
  654. // 初始状态只显示20条消息
  655. let size = this.chat.messages.length;
  656. this.showMinIdx = size > 20 ? size - 20 : 0;
  657. // 消息已读
  658. this.readedMessage()
  659. // 加载好友或群聊信息
  660. if (this.chat.type == "GROUP") {
  661. this.loadGroup(this.chat.targetId);
  662. } else {
  663. this.loadFriend(this.chat.targetId);
  664. this.loadReaded(this.chat.targetId)
  665. }
  666. // 激活当前会话
  667. this.$store.commit("activeChat", options.chatIdx);
  668. // 复位回执消息
  669. this.isReceipt = false;
  670. // 页面滚到底部
  671. this.scrollToBottom();
  672. },
  673. onUnload() {
  674. this.$store.commit("activeChat", -1);
  675. }
  676. }
  677. </script>
  678. <style lang="scss" scoped>
  679. .chat-box {
  680. position: relative;
  681. border: #dddddd solid 1px;
  682. display: flex;
  683. flex-direction: column;
  684. .header {
  685. display: flex;
  686. justify-content: center;
  687. align-items: center;
  688. height: 60rpx;
  689. padding: 5px;
  690. background-color: white;
  691. line-height: 50px;
  692. font-size: 40rpx;
  693. font-weight: 600;
  694. border: #dddddd solid 1px;
  695. .btn-side {
  696. position: absolute;
  697. line-height: 60rpx;
  698. font-size: 28rpx;
  699. cursor: pointer;
  700. &.left {
  701. left: 30rpx;
  702. }
  703. &.right {
  704. right: 30rpx;
  705. }
  706. }
  707. }
  708. .chat-msg {
  709. flex: 1;
  710. padding: 0;
  711. border: #dddddd solid 1px;
  712. overflow: hidden;
  713. position: relative;
  714. background-color: #f8f8f8;
  715. .scroll-box {
  716. height: 100%;
  717. }
  718. }
  719. .chat-at-bar {
  720. display: flex;
  721. align-items: center;
  722. padding: 0 10rpx;
  723. border: #dddddd solid 1px;
  724. .icon-at {
  725. font-size: 35rpx;
  726. color: darkblue;
  727. font-weight: 600;
  728. }
  729. .chat-at-scroll-box {
  730. flex: 1;
  731. width: 80%;
  732. .chat-at-items {
  733. display: flex;
  734. align-items: center;
  735. height: 70rpx;
  736. .chat-at-item {
  737. padding: 0 3rpx;
  738. }
  739. }
  740. }
  741. }
  742. .send-bar {
  743. display: flex;
  744. align-items: center;
  745. padding: 10rpx;
  746. margin-bottom: 10rpx;
  747. border: #dddddd solid 1px;
  748. background-color: white;
  749. .iconfont {
  750. font-size: 60rpx;
  751. margin: 3rpx;
  752. }
  753. .chat-record {
  754. flex: 1;
  755. }
  756. .send-text {
  757. flex: 1;
  758. background-color: #f8f8f8 !important;
  759. overflow: auto;
  760. padding: 20rpx;
  761. background-color: #fff;
  762. border-radius: 20rpx;
  763. font-size: 30rpx;
  764. box-sizing: border-box;
  765. .send-text-area {
  766. width: 100%;
  767. }
  768. }
  769. .btn-send {
  770. margin: 5rpx;
  771. }
  772. }
  773. .chat-tab-bar {
  774. height: 500rpx;
  775. padding: 20rpx;
  776. background-color: whitesmoke;
  777. .chat-tools {
  778. display: flex;
  779. flex-wrap: wrap;
  780. .chat-tools-item {
  781. width: 140rpx;
  782. padding: 16rpx;
  783. display: flex;
  784. flex-direction: column;
  785. align-items: center;
  786. .tool-icon {
  787. padding: 28rpx;
  788. font-size: 60rpx;
  789. background-color: white;
  790. border-radius: 20%;
  791. &.active {
  792. background-color: #ddd;
  793. }
  794. }
  795. .tool-name {
  796. height: 60rpx;
  797. line-height: 60rpx;
  798. font-size: 25rpx;
  799. }
  800. }
  801. }
  802. .chat-emotion {
  803. height: 100%;
  804. .emotion-item-list {
  805. display: flex;
  806. flex-wrap: wrap;
  807. .emotion-item {
  808. width: 40px;
  809. height: 40px;
  810. text-align: center;
  811. cursor: pointer;
  812. padding: 6px;
  813. }
  814. }
  815. }
  816. }
  817. }
  818. </style>