chat-box.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  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" @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" :headImage="headImage(msgInfo)" @call="onRtCall(msgInfo)"
  13. :showName="showName(msgInfo)" @recall="onRecallMessage" @delete="onDeleteMessage"
  14. @longPressHead="onLongPressHead(msgInfo)" @download="onDownloadFile" :id="'chat-item-'+idx"
  15. :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="onVoiceInput()">
  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. <!-- #endif -->
  92. </view>
  93. <scroll-view v-if="chatTabBox==='emo'" class="chat-emotion" scroll-y="true">
  94. <view class="emotion-item-list">
  95. <image class="emotion-item" :title="emoText" :src="$emo.textToPath(emoText)"
  96. v-for="(emoText, i) in $emo.emoTextList" :key="i" @click="selectEmoji(emoText)" mode="aspectFit"
  97. lazy-load="true"></image>
  98. </view>
  99. </scroll-view>
  100. <view v-if="showKeyBoard"></view>
  101. </view>
  102. <chat-at-box ref="atBox" :ownerId="group.ownerId" :members="groupMembers"
  103. @complete="onAtComplete"></chat-at-box>
  104. </view>
  105. </template>
  106. <script>
  107. import UNI_APP from '@/.env.js';
  108. export default {
  109. data() {
  110. return {
  111. chat: {},
  112. friend: {},
  113. group: {},
  114. groupMembers: [],
  115. sendText: "",
  116. isReceipt: false, // 是否回执消息
  117. scrollMsgIdx: 0, // 滚动条定位为到哪条消息
  118. chatTabBox: 'none',
  119. showKeyBoard: false,
  120. showRecord: false,
  121. keyboardHeight: 322,
  122. atUserIds: [],
  123. recordText: "",
  124. showMinIdx: 0 // 下标小于showMinIdx的消息不显示,否则可能很卡
  125. }
  126. },
  127. methods: {
  128. onRecorderInput() {
  129. this.showRecord = true;
  130. this.switchChatTabBox('none',true);
  131. },
  132. onKeyboardInput() {
  133. this.showRecord = false;
  134. this.switchChatTabBox('none',false);
  135. },
  136. onSendRecord(data) {
  137. console.log(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. // #ifdef MP
  496. // 防止滚动条定格在顶部,不能一直往上滚
  497. this.scrollToMsgIdx(this.showMinIdx);
  498. // #endif
  499. // 多展示10条信息
  500. this.showMinIdx = this.showMinIdx > 10 ? this.showMinIdx - 10 : 0;
  501. },
  502. onShowMore() {
  503. if (this.chat.type == "GROUP") {
  504. uni.navigateTo({
  505. url: "/pages/group/group-info?id=" + this.group.id
  506. })
  507. } else {
  508. uni.navigateTo({
  509. url: "/pages/common/user-info?id=" + this.friend.id
  510. })
  511. }
  512. },
  513. onTextInput(e) {
  514. let idx = e.detail.cursor - 1;
  515. if (this.chat.type == 'GROUP' && e.detail.value[idx] == '@') {
  516. this.openAtBox();
  517. let sendText = e.detail.value.replace("@", '');
  518. this.$nextTick(() => {
  519. this.sendText = sendText;
  520. })
  521. }
  522. },
  523. loadReaded(fId) {
  524. this.$http({
  525. url: `/message/private/maxReadedId?friendId=${fId}`,
  526. method: 'get'
  527. }).then((id) => {
  528. this.$store.commit("readedMessage", {
  529. friendId: fId,
  530. maxId: id
  531. });
  532. });
  533. },
  534. readedMessage() {
  535. if (this.chat.type == "GROUP") {
  536. var url = `/message/group/readed?groupId=${this.chat.targetId}`
  537. } else {
  538. url = `/message/private/readed?friendId=${this.chat.targetId}`
  539. }
  540. this.$http({
  541. url: url,
  542. method: 'PUT'
  543. }).then(() => {
  544. this.$store.commit("resetUnreadCount", this.chat)
  545. this.scrollToBottom();
  546. })
  547. },
  548. loadGroup(groupId) {
  549. this.$http({
  550. url: `/group/find/${groupId}`,
  551. method: 'GET'
  552. }).then((group) => {
  553. this.group = group;
  554. this.$store.commit("updateChatFromGroup", group);
  555. this.$store.commit("updateGroup", group);
  556. });
  557. this.$http({
  558. url: `/group/members/${groupId}`,
  559. method: 'get'
  560. }).then((groupMembers) => {
  561. this.groupMembers = groupMembers;
  562. });
  563. },
  564. loadFriend(friendId) {
  565. // 获取对方最新信息
  566. this.$http({
  567. url: `/user/find/${friendId}`,
  568. method: 'GET'
  569. }).then((friend) => {
  570. this.friend = friend;
  571. this.$store.commit("updateChatFromFriend", friend);
  572. this.$store.commit("updateFriend", friend);
  573. })
  574. },
  575. rpxTopx(rpx) {
  576. // px转换成rpx
  577. let info = uni.getSystemInfoSync()
  578. let px = info.windowWidth * rpx / 750;
  579. return Math.floor(rpx);
  580. }
  581. },
  582. computed: {
  583. mine() {
  584. return this.$store.state.userStore.userInfo;
  585. },
  586. title() {
  587. if (!this.chat) {
  588. return "";
  589. }
  590. let title = this.chat.showName;
  591. if (this.chat.type == "GROUP") {
  592. let size = this.groupMembers.filter(m => !m.quit).length;
  593. title += `(${size})`;
  594. }
  595. return title;
  596. },
  597. messageAction() {
  598. return `/message/${this.chat.type.toLowerCase()}/send`;
  599. },
  600. messageSize() {
  601. if (!this.chat || !this.chat.messages) {
  602. return 0;
  603. }
  604. return this.chat.messages.length;
  605. },
  606. unreadCount() {
  607. return this.chat.unreadCount;
  608. },
  609. atUserItems() {
  610. let atUsers = [];
  611. this.atUserIds.forEach((id) => {
  612. if (id == -1) {
  613. atUsers.push({
  614. id: -1,
  615. aliasName: "全体成员"
  616. })
  617. return;
  618. }
  619. let member = this.groupMembers.find((m) => m.userId == id);
  620. if (member) {
  621. atUsers.push(member);
  622. }
  623. })
  624. return atUsers;
  625. }
  626. },
  627. watch: {
  628. messageSize: function(newSize, oldSize) {
  629. // 接收到消息时滚动到底部
  630. if (newSize > oldSize) {
  631. this.scrollToBottom();
  632. }
  633. },
  634. unreadCount: {
  635. handler(newCount, oldCount) {
  636. if (newCount > 0) {
  637. // 消息已读
  638. this.readedMessage()
  639. }
  640. }
  641. }
  642. },
  643. onLoad(options) {
  644. // 聊天数据
  645. this.chat = this.$store.state.chatStore.chats[options.chatIdx];
  646. // 初始状态只显示30条消息
  647. let size = this.chat.messages.length;
  648. this.showMinIdx = size > 30 ? size - 30 : 0;
  649. // 激活当前会话
  650. this.$store.commit("activeChat", options.chatIdx);
  651. // 消息已读
  652. this.readedMessage()
  653. // 加载好友或群聊信息
  654. if (this.chat.type == "GROUP") {
  655. this.loadGroup(this.chat.targetId);
  656. } else {
  657. this.loadFriend(this.chat.targetId);
  658. this.loadReaded(this.chat.targetId)
  659. }
  660. // 复位回执消息
  661. this.isReceipt = false;
  662. },
  663. onShow() {
  664. // 页面滚到底部
  665. this.scrollToBottom();
  666. },
  667. onUnload() {
  668. this.$store.commit("activeChat", -1);
  669. }
  670. }
  671. </script>
  672. <style lang="scss" scoped>
  673. .chat-box {
  674. position: relative;
  675. border: #dddddd solid 1px;
  676. display: flex;
  677. flex-direction: column;
  678. .header {
  679. display: flex;
  680. justify-content: center;
  681. align-items: center;
  682. height: 60rpx;
  683. padding: 5px;
  684. background-color: white;
  685. line-height: 50px;
  686. font-size: 40rpx;
  687. font-weight: 600;
  688. border: #dddddd solid 1px;
  689. .btn-side {
  690. position: absolute;
  691. line-height: 60rpx;
  692. font-size: 28rpx;
  693. cursor: pointer;
  694. &.left {
  695. left: 30rpx;
  696. }
  697. &.right {
  698. right: 30rpx;
  699. }
  700. }
  701. }
  702. .chat-msg {
  703. flex: 1;
  704. padding: 0;
  705. border: #dddddd solid 1px;
  706. overflow: hidden;
  707. position: relative;
  708. background-color: #f8f8f8;
  709. .scroll-box {
  710. height: 100%;
  711. }
  712. }
  713. .chat-at-bar {
  714. display: flex;
  715. align-items: center;
  716. padding: 0 10rpx;
  717. border: #dddddd solid 1px;
  718. .icon-at {
  719. font-size: 35rpx;
  720. color: darkblue;
  721. font-weight: 600;
  722. }
  723. .chat-at-scroll-box {
  724. flex: 1;
  725. width: 80%;
  726. .chat-at-items {
  727. display: flex;
  728. align-items: center;
  729. height: 70rpx;
  730. .chat-at-item {
  731. padding: 0 3rpx;
  732. }
  733. }
  734. }
  735. }
  736. .send-bar {
  737. display: flex;
  738. align-items: center;
  739. padding: 10rpx;
  740. margin-bottom: 10rpx;
  741. border: #dddddd solid 1px;
  742. background-color: white;
  743. .iconfont {
  744. font-size: 60rpx;
  745. margin: 3rpx;
  746. }
  747. .chat-record {
  748. flex: 1;
  749. }
  750. .send-text {
  751. flex: 1;
  752. background-color: #f8f8f8 !important;
  753. overflow: auto;
  754. padding: 20rpx;
  755. background-color: #fff;
  756. border-radius: 20rpx;
  757. font-size: 30rpx;
  758. box-sizing: border-box;
  759. .send-text-area {
  760. width: 100%;
  761. }
  762. }
  763. .btn-send {
  764. margin: 5rpx;
  765. }
  766. }
  767. .chat-tab-bar {
  768. height: 500rpx;
  769. padding: 20rpx;
  770. background-color: whitesmoke;
  771. .chat-tools {
  772. display: flex;
  773. flex-wrap: wrap;
  774. .chat-tools-item {
  775. width: 140rpx;
  776. padding: 16rpx;
  777. display: flex;
  778. flex-direction: column;
  779. align-items: center;
  780. .tool-icon {
  781. padding: 28rpx;
  782. font-size: 60rpx;
  783. background-color: white;
  784. border-radius: 20%;
  785. &.active {
  786. background-color: #ddd;
  787. }
  788. }
  789. .tool-name {
  790. height: 60rpx;
  791. line-height: 60rpx;
  792. font-size: 25rpx;
  793. }
  794. }
  795. }
  796. .chat-emotion {
  797. height: 100%;
  798. .emotion-item-list {
  799. display: flex;
  800. flex-wrap: wrap;
  801. .emotion-item {
  802. width: 40px;
  803. height: 40px;
  804. text-align: center;
  805. cursor: pointer;
  806. padding: 6px;
  807. }
  808. }
  809. }
  810. }
  811. }
  812. </style>