ChatBox.vue 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. <template>
  2. <div class="chat-box" @click="closeRefBox()" @mousemove="readedMessage()">
  3. <el-container>
  4. <el-header height="50px">
  5. <span>{{ title }}</span>
  6. <span title="群聊信息" v-show="this.chat.type == 'GROUP'" class="btn-side el-icon-more"
  7. @click="showSide = !showSide"></span>
  8. </el-header>
  9. <el-main style="padding: 0;">
  10. <el-container>
  11. <el-container class="content-box">
  12. <el-main class="im-chat-main" id="chatScrollBox" @scroll="onScroll">
  13. <div class="im-chat-box">
  14. <ul>
  15. <li v-for="(msgInfo, idx) in chat.messages" :key="idx">
  16. <chat-message-item v-if="idx >= showMinIdx" @call="onCall(msgInfo.type)"
  17. :mine="msgInfo.sendId == mine.id" :headImage="headImage(msgInfo)"
  18. :showName="showName(msgInfo)" :msgInfo="msgInfo"
  19. :groupMembers="groupMembers" @delete="deleteMessage"
  20. @recall="recallMessage">
  21. </chat-message-item>
  22. </li>
  23. </ul>
  24. </div>
  25. </el-main>
  26. <el-footer height="220px" class="im-chat-footer">
  27. <div class="chat-tool-bar">
  28. <div title="表情" class="icon iconfont icon-emoji" ref="emotion"
  29. @click.stop="showEmotionBox()">
  30. </div>
  31. <div title="发送图片">
  32. <file-upload :action="'/image/upload'" :maxSize="5 * 1024 * 1024"
  33. :fileTypes="['image/jpeg', 'image/png', 'image/jpg', 'image/webp', 'image/gif']"
  34. @before="onImageBefore" @success="onImageSuccess" @fail="onImageFail">
  35. <i class="el-icon-picture-outline"></i>
  36. </file-upload>
  37. </div>
  38. <div title="发送文件">
  39. <file-upload ref="fileUpload" :action="'/file/upload'" :maxSize="10 * 1024 * 1024"
  40. @before="onFileBefore" @success="onFileSuccess" @fail="onFileFail">
  41. <i class="el-icon-wallet"></i>
  42. </file-upload>
  43. </div>
  44. <div title="回执消息" v-show="chat.type == 'GROUP'" class="icon iconfont icon-receipt"
  45. :class="isReceipt ? 'chat-tool-active' : ''" @click="onSwitchReceipt">
  46. </div>
  47. <div title="发送语音" class="el-icon-microphone" @click="showRecordBox()">
  48. </div>
  49. <div title="语音通话" v-show="chat.type == 'PRIVATE'" class="el-icon-phone-outline"
  50. @click="showPrivateVideo('voice')">
  51. </div>
  52. <div title="语音通话" v-show="chat.type == 'GROUP'" class="el-icon-phone-outline"
  53. @click="onGroupVideo()">
  54. </div>
  55. <div title="视频通话" v-show="chat.type == 'PRIVATE'" class="el-icon-video-camera"
  56. @click="showPrivateVideo('video')">
  57. </div>
  58. <div title="聊天记录" class="el-icon-chat-dot-round" @click="showHistoryBox()"></div>
  59. </div>
  60. <div class="send-content-area">
  61. <ChatInput :ownerId="group.ownerId" ref="chatInputEditor" :group-members="groupMembers"
  62. @submit="sendMessage" />
  63. <div class="send-btn-area">
  64. <el-button type="primary" icon="el-icon-s-promotion"
  65. @click="notifySend()">发送</el-button>
  66. </div>
  67. </div>
  68. </el-footer>
  69. </el-container>
  70. <el-aside class="chat-group-side-box" width="260px" v-if="showSide">
  71. <chat-group-side :group="group" :groupMembers="groupMembers" @reload="loadGroup(group.id)">
  72. </chat-group-side>
  73. </el-aside>
  74. </el-container>
  75. </el-main>
  76. <emotion ref="emoBox" @emotion="onEmotion"></Emotion>
  77. <chat-record :visible="showRecord" @close="closeRecordBox" @send="onSendRecord"></chat-record>
  78. <group-member-selector ref="rtcSel" :groupId="group.id" @complete="onInviteOk"></group-member-selector>
  79. <rtc-group-join ref="rtcJoin" :groupId="group.id"></rtc-group-join>
  80. <chat-history :visible="showHistory" :chat="chat" :friend="friend" :group="group"
  81. :groupMembers="groupMembers" @close="closeHistoryBox"></chat-history>
  82. </el-container>
  83. </div>
  84. </template>
  85. <script>
  86. import ChatGroupSide from "./ChatGroupSide.vue";
  87. import ChatMessageItem from "./ChatMessageItem.vue";
  88. import FileUpload from "../common/FileUpload.vue";
  89. import Emotion from "../common/Emotion.vue";
  90. import ChatRecord from "./ChatRecord.vue";
  91. import ChatHistory from "./ChatHistory.vue";
  92. import ChatAtBox from "./ChatAtBox.vue"
  93. import GroupMemberSelector from "../group/GroupMemberSelector.vue"
  94. import RtcGroupJoin from "../rtc/RtcGroupJoin.vue"
  95. import ChatInput from "./ChatInput";
  96. export default {
  97. name: "chatPrivate",
  98. components: {
  99. ChatInput,
  100. ChatMessageItem,
  101. FileUpload,
  102. ChatGroupSide,
  103. Emotion,
  104. ChatRecord,
  105. ChatHistory,
  106. ChatAtBox,
  107. GroupMemberSelector,
  108. RtcGroupJoin
  109. },
  110. props: {
  111. chat: {
  112. type: Object
  113. }
  114. },
  115. data() {
  116. return {
  117. friend: {},
  118. group: {},
  119. groupMembers: [],
  120. sendImageUrl: "",
  121. sendImageFile: "",
  122. placeholder: "",
  123. isReceipt: true,
  124. showRecord: false, // 是否显示语音录制弹窗
  125. showSide: false, // 是否显示群聊信息栏
  126. showHistory: false, // 是否显示历史聊天记录
  127. lockMessage: false, // 是否锁定发送,
  128. showMinIdx: 0, // 下标低于showMinIdx的消息不显示,否则页面会很卡置
  129. reqQueue: [],
  130. isSending: false
  131. }
  132. },
  133. methods: {
  134. moveChatToTop() {
  135. let chatIdx = this.$store.getters.findChatIdx(this.chat);
  136. this.$store.commit("moveTop", chatIdx);
  137. },
  138. closeRefBox() {
  139. this.$refs.emoBox.close();
  140. // this.$refs.atBox.close();
  141. },
  142. onCall(type) {
  143. if (type == this.$enums.MESSAGE_TYPE.ACT_RT_VOICE) {
  144. this.showPrivateVideo('voice');
  145. } else if (type == this.$enums.MESSAGE_TYPE.ACT_RT_VIDEO) {
  146. this.showPrivateVideo('video');
  147. }
  148. },
  149. onSwitchReceipt() {
  150. this.isReceipt = !this.isReceipt;
  151. this.refreshPlaceHolder();
  152. },
  153. onImageSuccess(data, file) {
  154. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  155. msgInfo.content = JSON.stringify(data);
  156. msgInfo.receipt = this.isReceipt;
  157. this.sendMessageRequest(msgInfo).then((m) => {
  158. msgInfo.loadStatus = 'ok';
  159. msgInfo.id = m.id;
  160. this.isReceipt = false;
  161. this.$store.commit("insertMessage", [msgInfo, this.chat]);
  162. })
  163. },
  164. onImageFail(e, file) {
  165. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  166. msgInfo.loadStatus = 'fail';
  167. this.$store.commit("insertMessage", [msgInfo, this.chat]);
  168. },
  169. onImageBefore(file) {
  170. // 被封禁提示
  171. if (this.isBanned) {
  172. this.showBannedTip();
  173. return;
  174. }
  175. let url = URL.createObjectURL(file);
  176. let data = {
  177. originUrl: url,
  178. thumbUrl: url
  179. }
  180. let msgInfo = {
  181. id: 0,
  182. tmpId: this.generateId(),
  183. fileId: file.uid,
  184. sendId: this.mine.id,
  185. content: JSON.stringify(data),
  186. sendTime: new Date().getTime(),
  187. selfSend: true,
  188. type: 1,
  189. readedCount: 0,
  190. loadStatus: "loading",
  191. status: this.$enums.MESSAGE_STATUS.UNSEND
  192. }
  193. // 填充对方id
  194. this.fillTargetId(msgInfo, this.chat.targetId);
  195. // 插入消息
  196. this.$store.commit("insertMessage", [msgInfo, this.chat]);
  197. // 会话置顶
  198. this.moveChatToTop();
  199. // 滚动到底部
  200. this.scrollToBottom();
  201. // 借助file对象保存
  202. file.msgInfo = msgInfo;
  203. },
  204. onFileSuccess(url, file) {
  205. let data = {
  206. name: file.name,
  207. size: file.size,
  208. url: url
  209. }
  210. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  211. msgInfo.content = JSON.stringify(data);
  212. msgInfo.receipt = this.isReceipt
  213. this.sendMessageRequest(msgInfo).then((m) => {
  214. msgInfo.loadStatus = 'ok';
  215. msgInfo.id = m.id;
  216. this.isReceipt = false;
  217. this.refreshPlaceHolder();
  218. this.$store.commit("insertMessage", [msgInfo, this.chat]);
  219. })
  220. },
  221. onFileFail(e, file) {
  222. let msgInfo = JSON.parse(JSON.stringify(file.msgInfo));
  223. msgInfo.loadStatus = 'fail';
  224. this.$store.commit("insertMessage", [msgInfo, this.chat]);
  225. },
  226. onFileBefore(file) {
  227. // 被封禁提示
  228. if (this.isBanned) {
  229. this.showBannedTip();
  230. return;
  231. }
  232. let url = URL.createObjectURL(file);
  233. let data = {
  234. name: file.name,
  235. size: file.size,
  236. url: url
  237. }
  238. let msgInfo = {
  239. id: 0,
  240. tmpId: this.generateId(),
  241. sendId: this.mine.id,
  242. content: JSON.stringify(data),
  243. sendTime: new Date().getTime(),
  244. selfSend: true,
  245. type: 2,
  246. loadStatus: "loading",
  247. readedCount: 0,
  248. status: this.$enums.MESSAGE_STATUS.UNSEND
  249. }
  250. // 填充对方id
  251. this.fillTargetId(msgInfo, this.chat.targetId);
  252. // 插入消息
  253. this.$store.commit("insertMessage", [msgInfo, this.chat]);
  254. // 会话置顶
  255. this.moveChatToTop();
  256. // 滚动到底部
  257. this.scrollToBottom();
  258. // 借助file对象透传
  259. file.msgInfo = msgInfo;
  260. },
  261. onCloseSide() {
  262. this.showSide = false;
  263. },
  264. onScrollToTop() {
  265. // 多展示10条信息
  266. this.showMinIdx = this.showMinIdx > 10 ? this.showMinIdx - 10 : 0;
  267. },
  268. onScroll(e) {
  269. let scrollElement = e.target
  270. let scrollTop = scrollElement.scrollTop
  271. if (scrollTop < 30) { // 在顶部,不滚动的情况
  272. // 多展示20条信息
  273. this.showMinIdx = this.showMinIdx > 20 ? this.showMinIdx - 20 : 0;
  274. }
  275. },
  276. showEmotionBox() {
  277. let width = this.$refs.emotion.offsetWidth;
  278. let left = this.$elm.fixLeft(this.$refs.emotion);
  279. let top = this.$elm.fixTop(this.$refs.emotion);
  280. this.$refs.emoBox.open({
  281. x: left + width / 2,
  282. y: top
  283. })
  284. },
  285. onEmotion(emoText) {
  286. this.$refs.chatInputEditor.insertEmoji(emoText);
  287. },
  288. showRecordBox() {
  289. this.showRecord = true;
  290. },
  291. closeRecordBox() {
  292. this.showRecord = false;
  293. },
  294. showPrivateVideo(mode) {
  295. // 检查是否被封禁
  296. if (this.isBanned) {
  297. this.showBannedTip();
  298. return;
  299. }
  300. let rtcInfo = {
  301. mode: mode,
  302. isHost: true,
  303. friend: this.friend,
  304. }
  305. // 通过home.vue打开单人视频窗口
  306. this.$eventBus.$emit("openPrivateVideo", rtcInfo);
  307. },
  308. onGroupVideo() {
  309. // 检查是否被封禁
  310. if (this.isBanned) {
  311. this.showBannedTip();
  312. return;
  313. }
  314. // 邀请成员发起通话
  315. let ids = [this.mine.id];
  316. let maxChannel = this.$store.state.configStore.webrtc.maxChannel;
  317. this.$refs.rtcSel.open(maxChannel, ids, ids);
  318. },
  319. onInviteOk(members) {
  320. if (members.length < 2) {
  321. return;
  322. }
  323. let userInfos = [];
  324. members.forEach(m => {
  325. userInfos.push({
  326. id: m.userId,
  327. nickName: m.showNickName,
  328. headImage: m.headImage,
  329. isCamera: false,
  330. isMicroPhone: true
  331. })
  332. })
  333. let rtcInfo = {
  334. isHost: true,
  335. groupId: this.group.id,
  336. inviterId: this.mine.id,
  337. userInfos: userInfos
  338. }
  339. // 通过home.vue打开多人视频窗口
  340. this.$eventBus.$emit("openGroupVideo", rtcInfo);
  341. },
  342. showHistoryBox() {
  343. this.showHistory = true;
  344. },
  345. closeHistoryBox() {
  346. this.showHistory = false;
  347. },
  348. onSendRecord(data) {
  349. // 检查是否被封禁
  350. if (this.isBanned) {
  351. this.showBannedTip();
  352. return;
  353. }
  354. let msgInfo = {
  355. content: JSON.stringify(data),
  356. type: 3,
  357. receipt: this.isReceipt
  358. }
  359. // 填充对方id
  360. this.fillTargetId(msgInfo, this.chat.targetId);
  361. this.sendMessageRequest(msgInfo).then((m) => {
  362. m.selfSend = true;
  363. this.$store.commit("insertMessage", [m, this.chat]);
  364. // 会话置顶
  365. this.moveChatToTop();
  366. // 保持输入框焦点
  367. this.$refs.chatInputEditor.focus();
  368. // 滚动到底部
  369. this.scrollToBottom();
  370. // 关闭录音窗口
  371. this.showRecord = false;
  372. this.isReceipt = false;
  373. this.refreshPlaceHolder();
  374. })
  375. },
  376. fillTargetId(msgInfo, targetId) {
  377. if (this.chat.type == "GROUP") {
  378. msgInfo.groupId = targetId;
  379. } else {
  380. msgInfo.recvId = targetId;
  381. }
  382. },
  383. notifySend() {
  384. this.$refs.chatInputEditor.submit();
  385. },
  386. async sendMessage(fullList) {
  387. this.resetEditor();
  388. this.readedMessage();
  389. // 检查是否被封禁
  390. if (this.isBanned) {
  391. this.showBannedTip();
  392. return;
  393. }
  394. let sendText = this.isReceipt ? "【回执消息】" : "";
  395. let promiseList = [];
  396. for (let i = 0; i < fullList.length; i++) {
  397. let msg = fullList[i];
  398. switch (msg.type) {
  399. case "text":
  400. await this.sendTextMessage(sendText + msg.content, msg.atUserIds);
  401. break;
  402. case "image":
  403. await this.sendImageMessage(msg.content.file);
  404. break;
  405. case "file":
  406. await this.sendFileMessage(msg.content.file);
  407. break;
  408. }
  409. }
  410. },
  411. sendImageMessage(file) {
  412. return new Promise((resolve, reject) => {
  413. this.onImageBefore(file);
  414. let formData = new FormData()
  415. formData.append('file', file)
  416. this.$http.post("/image/upload", formData, {
  417. headers: {
  418. 'Content-Type': 'multipart/form-data'
  419. }
  420. }).then((data) => {
  421. this.onImageSuccess(data, file);
  422. resolve();
  423. }).catch((res) => {
  424. this.onImageFail(res, file);
  425. reject();
  426. })
  427. this.$nextTick(() => this.$refs.chatInputEditor.focus());
  428. this.scrollToBottom();
  429. });
  430. },
  431. sendTextMessage(sendText, atUserIds) {
  432. return new Promise((resolve, reject) => {
  433. if (!sendText.trim()) {
  434. reject();
  435. }
  436. let msgInfo = {
  437. content: sendText,
  438. type: 0
  439. }
  440. // 填充对方id
  441. this.fillTargetId(msgInfo, this.chat.targetId);
  442. // 被@人员列表
  443. if (this.chat.type == "GROUP") {
  444. msgInfo.atUserIds = atUserIds;
  445. msgInfo.receipt = this.isReceipt;
  446. }
  447. this.lockMessage = true;
  448. this.sendMessageRequest(msgInfo).then((m) => {
  449. m.selfSend = true;
  450. this.$store.commit("insertMessage", [m, this.chat]);
  451. // 会话置顶
  452. this.moveChatToTop();
  453. }).finally(() => {
  454. // 解除锁定
  455. this.scrollToBottom();
  456. this.isReceipt = false;
  457. resolve();
  458. });
  459. });
  460. },
  461. sendFileMessage(file) {
  462. return new Promise((resolve, reject) => {
  463. let check = this.$refs.fileUpload.beforeUpload(file);
  464. if (check) {
  465. this.$refs.fileUpload.onFileUpload({ file });
  466. }
  467. })
  468. },
  469. deleteMessage(msgInfo) {
  470. this.$confirm('确认删除消息?', '删除消息', {
  471. confirmButtonText: '确定',
  472. cancelButtonText: '取消',
  473. type: 'warning'
  474. }).then(() => {
  475. this.$store.commit("deleteMessage", msgInfo);
  476. });
  477. },
  478. recallMessage(msgInfo) {
  479. this.$confirm('确认撤回消息?', '撤回消息', {
  480. confirmButtonText: '确定',
  481. cancelButtonText: '取消',
  482. type: 'warning'
  483. }).then(() => {
  484. let url = `/message/${this.chat.type.toLowerCase()}/recall/${msgInfo.id}`
  485. this.$http({
  486. url: url,
  487. method: 'delete'
  488. }).then((m) => {
  489. this.$message.success("消息已撤回");
  490. m.selfSend = true;
  491. this.$store.commit("recallMessage", [m, this.chat]);
  492. })
  493. });
  494. },
  495. readedMessage() {
  496. if (this.chat.unreadCount == 0) {
  497. return;
  498. }
  499. this.$store.commit("resetUnreadCount", this.chat)
  500. if (this.chat.type == "GROUP") {
  501. var url = `/message/group/readed?groupId=${this.chat.targetId}`
  502. } else {
  503. url = `/message/private/readed?friendId=${this.chat.targetId}`
  504. }
  505. this.$http({
  506. url: url,
  507. method: 'put'
  508. }).then(() => {})
  509. },
  510. loadReaded(fId) {
  511. this.$http({
  512. url: `/message/private/maxReadedId?friendId=${fId}`,
  513. method: 'get'
  514. }).then((id) => {
  515. this.$store.commit("readedMessage", {
  516. friendId: fId,
  517. maxId: id
  518. });
  519. });
  520. },
  521. loadGroup(groupId) {
  522. this.$http({
  523. url: `/group/find/${groupId}`,
  524. method: 'get'
  525. }).then((group) => {
  526. this.group = group;
  527. this.$store.commit("updateChatFromGroup", group);
  528. this.$store.commit("updateGroup", group);
  529. });
  530. this.$http({
  531. url: `/group/members/${groupId}`,
  532. method: 'get'
  533. }).then((groupMembers) => {
  534. this.groupMembers = groupMembers;
  535. });
  536. },
  537. loadFriend(friendId) {
  538. // 获取对方最新信息
  539. this.$http({
  540. url: `/user/find/${friendId}`,
  541. method: 'get'
  542. }).then((friend) => {
  543. this.friend = friend;
  544. this.$store.commit("updateChatFromFriend", friend);
  545. this.$store.commit("updateFriend", friend);
  546. })
  547. },
  548. showName(msgInfo) {
  549. if (this.chat.type == 'GROUP') {
  550. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  551. return member ? member.showNickName : "";
  552. } else {
  553. return msgInfo.sendId == this.mine.id ? this.mine.nickName : this.chat.showName
  554. }
  555. },
  556. headImage(msgInfo) {
  557. if (this.chat.type == 'GROUP') {
  558. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  559. return member ? member.headImage : "";
  560. } else {
  561. return msgInfo.sendId == this.mine.id ? this.mine.headImageThumb : this.chat.headImage
  562. }
  563. },
  564. resetEditor() {
  565. this.$nextTick(() => {
  566. this.$refs.chatInputEditor.clear();
  567. this.$refs.chatInputEditor.focus();
  568. });
  569. },
  570. scrollToBottom() {
  571. this.$nextTick(() => {
  572. let div = document.getElementById("chatScrollBox");
  573. div.scrollTop = div.scrollHeight;
  574. });
  575. },
  576. refreshPlaceHolder() {
  577. if (this.isReceipt) {
  578. this.placeholder = "【回执消息】"
  579. } else if (this.$refs.editBox && this.$refs.editBox.innerHTML) {
  580. this.placeholder = ""
  581. } else {
  582. this.placeholder = "聊点什么吧~";
  583. }
  584. },
  585. sendMessageRequest(msgInfo) {
  586. return new Promise((resolve, reject) => {
  587. // 请求入队列,防止请求"后发先至",导致消息错序
  588. this.reqQueue.push({ msgInfo, resolve, reject });
  589. this.processReqQueue();
  590. })
  591. },
  592. processReqQueue() {
  593. if (this.reqQueue.length && !this.isSending) {
  594. this.isSending = true;
  595. const reqData = this.reqQueue.shift();
  596. this.$http({
  597. url: this.messageAction,
  598. method: 'post',
  599. data: reqData.msgInfo
  600. }).then((res) => {
  601. reqData.resolve(res)
  602. }).catch((e) => {
  603. reqData.reject(e)
  604. }).finally(() => {
  605. this.isSending = false;
  606. // 发送下一条请求
  607. this.processReqQueue();
  608. })
  609. }
  610. },
  611. showBannedTip() {
  612. let msgInfo = {
  613. tmpId: this.generateId(),
  614. sendId: this.mine.id,
  615. sendTime: new Date().getTime(),
  616. type: this.$enums.MESSAGE_TYPE.TIP_TEXT
  617. }
  618. if (this.chat.type == "PRIVATE") {
  619. msgInfo.recvId = this.mine.id
  620. msgInfo.content = "该用户已被管理员封禁,原因:" + this.friend.reason
  621. } else {
  622. msgInfo.groupId = this.group.id;
  623. msgInfo.content = "本群聊已被管理员封禁,原因:" + this.group.reason
  624. }
  625. this.$store.commit("insertMessage", [msgInfo, this.chat]);
  626. },
  627. generateId() {
  628. // 生成临时id
  629. return String(new Date().getTime()) + String(Math.floor(Math.random() * 1000));
  630. }
  631. },
  632. computed: {
  633. mine() {
  634. return this.$store.state.userStore.userInfo;
  635. },
  636. title() {
  637. let title = this.chat.showName;
  638. if (this.chat.type == "GROUP") {
  639. let size = this.groupMembers.filter(m => !m.quit).length;
  640. title += `(${size})`;
  641. }
  642. return title;
  643. },
  644. messageAction() {
  645. return `/message/${this.chat.type.toLowerCase()}/send`;
  646. },
  647. unreadCount() {
  648. return this.chat.unreadCount;
  649. },
  650. messageSize() {
  651. if (!this.chat || !this.chat.messages) {
  652. return 0;
  653. }
  654. return this.chat.messages.length;
  655. },
  656. isBanned() {
  657. return (this.chat.type == "PRIVATE" && this.friend.isBanned) ||
  658. (this.chat.type == "GROUP" && this.group.isBanned)
  659. }
  660. },
  661. watch: {
  662. chat: {
  663. handler(newChat, oldChat) {
  664. if (newChat.targetId > 0 && (!oldChat || newChat.type != oldChat.type ||
  665. newChat.targetId != oldChat.targetId)) {
  666. if (this.chat.type == "GROUP") {
  667. this.loadGroup(this.chat.targetId);
  668. } else {
  669. this.loadFriend(this.chat.targetId);
  670. // 加载已读状态
  671. this.loadReaded(this.chat.targetId)
  672. }
  673. // 滚到底部
  674. this.scrollToBottom();
  675. this.showSide = false;
  676. // 消息已读
  677. this.readedMessage()
  678. // 初始状态只显示30条消息
  679. let size = this.chat.messages.length;
  680. this.showMinIdx = size > 30 ? size - 30 : 0;
  681. // 重置输入框
  682. this.resetEditor();
  683. // 复位回执消息
  684. this.isReceipt = false;
  685. // 更新placeholder
  686. this.refreshPlaceHolder();
  687. }
  688. },
  689. immediate: true
  690. },
  691. messageSize: {
  692. handler(newSize, oldSize) {
  693. if (newSize > oldSize) {
  694. // 拉至底部
  695. this.scrollToBottom();
  696. }
  697. }
  698. }
  699. },
  700. mounted() {
  701. let div = document.getElementById("chatScrollBox");
  702. div.addEventListener('scroll', this.onScroll)
  703. }
  704. }
  705. </script>
  706. <style lang="scss">
  707. .chat-box {
  708. position: relative;
  709. width: 100%;
  710. background: #fff;
  711. .el-header {
  712. display: flex;
  713. justify-content: space-between;
  714. padding: 0 12px;
  715. line-height: 50px;
  716. font-size: var(--im-font-size-larger);
  717. border-bottom: var(--im-border);
  718. .btn-side {
  719. position: absolute;
  720. right: 20px;
  721. line-height: 50px;
  722. font-size: 20px;
  723. cursor: pointer;
  724. color: var(--im-text-color-light);
  725. }
  726. }
  727. .im-chat-main {
  728. padding: 0;
  729. background-color: #fff;
  730. .im-chat-box {
  731. >ul {
  732. padding: 0 20px;
  733. li {
  734. list-style-type: none;
  735. }
  736. }
  737. }
  738. }
  739. .im-chat-footer {
  740. display: flex;
  741. flex-direction: column;
  742. padding: 0;
  743. .chat-tool-bar {
  744. display: flex;
  745. position: relative;
  746. width: 100%;
  747. height: 36px;
  748. text-align: left;
  749. box-sizing: border-box;
  750. border-top: var(--im-border);
  751. padding: 4px 2px 2px 8px;
  752. >div {
  753. font-size: 22px;
  754. cursor: pointer;
  755. line-height: 30px;
  756. width: 30px;
  757. height: 30px;
  758. text-align: center;
  759. border-radius: 2px;
  760. margin-right: 8px;
  761. color: #999;
  762. transition: 0.3s;
  763. &.chat-tool-active {
  764. font-weight: 600;
  765. color: var(--im-color-primary);
  766. background-color: #ddd;
  767. }
  768. }
  769. >div:hover {
  770. color: #333;
  771. }
  772. }
  773. .send-content-area {
  774. position: relative;
  775. display: flex;
  776. flex-direction: column;
  777. height: 100%;
  778. background-color: white !important;
  779. .send-text-area {
  780. box-sizing: border-box;
  781. padding: 5px;
  782. width: 100%;
  783. flex: 1;
  784. resize: none;
  785. font-size: 16px;
  786. outline: none;
  787. text-align: left;
  788. line-height: 30px;
  789. &:before {
  790. content: attr(placeholder);
  791. color: gray;
  792. }
  793. .at {
  794. color: blue;
  795. font-weight: 600;
  796. }
  797. .receipt {
  798. color: darkblue;
  799. font-size: 15px;
  800. font-weight: 600;
  801. }
  802. .emo {
  803. width: 30px;
  804. height: 30px;
  805. vertical-align: bottom;
  806. }
  807. }
  808. .send-image-area {
  809. text-align: left;
  810. border: #53a0e7 solid 1px;
  811. .send-image-box {
  812. position: relative;
  813. display: inline-block;
  814. .send-image {
  815. max-height: 180px;
  816. border: 1px solid #ccc;
  817. border-radius: 2%;
  818. margin: 2px;
  819. }
  820. .send-image-close {
  821. position: absolute;
  822. padding: 3px;
  823. right: 7px;
  824. top: 7px;
  825. color: white;
  826. cursor: pointer;
  827. font-size: 15px;
  828. font-weight: 600;
  829. background-color: #aaa;
  830. border-radius: 50%;
  831. border: 1px solid #ccc;
  832. }
  833. }
  834. }
  835. .send-btn-area {
  836. padding: 10px;
  837. position: absolute;
  838. bottom: 4px;
  839. right: 6px;
  840. }
  841. }
  842. }
  843. .chat-group-side-box {
  844. border-left: var(--im-border);
  845. //animation: rtl-drawer-in .3s 1ms;
  846. }
  847. }
  848. </style>