chat-box.vue 22 KB

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