ChatBox.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <template>
  2. <el-container class="chat-box">
  3. <el-header height="60px">
  4. <span>{{title}}</span>
  5. <span title="群聊信息" v-show="this.chat.type=='GROUP'" class="btn-side el-icon-more"
  6. @click="showSide=!showSide"></span>
  7. </el-header>
  8. <el-main style="padding: 0;">
  9. <el-container>
  10. <el-container class="content-box">
  11. <el-main class="im-chat-main" id="chatScrollBox">
  12. <div class="im-chat-box">
  13. <ul>
  14. <li v-for="(msgInfo,idx) in chat.messages" :key="idx">
  15. <message-item :mine="msgInfo.sendId == mine.id" :headImage="headImage(msgInfo)"
  16. :showName="showName(msgInfo)" :msgInfo="msgInfo">
  17. </message-item>
  18. </li>
  19. </ul>
  20. </div>
  21. </el-main>
  22. <el-footer height="200px" class="im-chat-footer">
  23. <div class="chat-tool-bar">
  24. <div title="表情" class="el-icon-eleme" ref="emotion" @click="switchEmotionBox()">
  25. </div>
  26. <div title="发送图片">
  27. <file-upload :action="imageAction" :maxSize="5*1024*1024"
  28. :fileTypes="['image/jpeg', 'image/png', 'image/jpg', 'image/webp','image/gif']"
  29. @before="handleImageBefore" @success="handleImageSuccess" @fail="handleImageFail">
  30. <i class="el-icon-picture-outline"></i>
  31. </file-upload>
  32. </div>
  33. <div title="发送文件">
  34. <file-upload :action="fileAction" :maxSize="10*1024*1024" @before="handleFileBefore"
  35. @success="handleFileSuccess" @fail="handleFileFail">
  36. <i class="el-icon-wallet"></i>
  37. </file-upload>
  38. </div>
  39. <div title="发送语音" class="el-icon-microphone" @click="showVoiceBox()">
  40. </div>
  41. <div title="聊天记录" class="el-icon-chat-dot-round"></div>
  42. </div>
  43. <textarea v-model="sendText" ref="sendBox" class="send-text-area"
  44. @keydown.enter="sendTextMessage()"></textarea>
  45. <div class="im-chat-send">
  46. <el-button type="primary" @click="sendTextMessage()">发送</el-button>
  47. </div>
  48. </el-footer>
  49. </el-container>
  50. <el-aside class="chat-group-side-box" width="300px" v-show="showSide">
  51. <chat-group-side :group="group" :groupMembers="groupMembers" @reload="loadGroup(group.id)">
  52. </chat-group-side>
  53. </el-aside>
  54. </el-container>
  55. </el-main>
  56. <emotion v-show="showEmotion" :pos="emoBoxPos" @emotion="handleEmotion"></Emotion>
  57. <chat-voice :visible="showVoice" @close="closeVoiceBox" @send="handleSendVoice"></chat-voice>
  58. </el-container>
  59. </template>
  60. <script>
  61. import ChatGroupSide from "./ChatGroupSide.vue";
  62. import MessageItem from "./MessageItem.vue";
  63. import FileUpload from "../common/FileUpload.vue";
  64. import Emotion from "../common/Emotion.vue";
  65. import ChatVoice from "./ChatVoice.vue";
  66. export default {
  67. name: "chatPrivate",
  68. components: {
  69. MessageItem,
  70. FileUpload,
  71. ChatGroupSide,
  72. Emotion,
  73. ChatVoice
  74. },
  75. props: {
  76. chat: {
  77. type: Object
  78. }
  79. },
  80. data() {
  81. return {
  82. friend: {},
  83. group: {},
  84. groupMembers: [],
  85. sendText: "",
  86. showVoice: false, // 是否显示语音录制弹窗
  87. showSide: false, // 是否显示群聊信息栏
  88. showEmotion: false, // 是否显示emoji表情
  89. emoBoxPos: { // emoji表情弹出位置
  90. x: 0,
  91. y: 0
  92. }
  93. }
  94. },
  95. methods: {
  96. handleImageSuccess(res, file) {
  97. let msgInfo = {
  98. recvId: file.raw.targetId,
  99. content: JSON.stringify(res.data),
  100. type: 1
  101. }
  102. // 填充对方id
  103. this.setTargetId(msgInfo, this.chat.targetId);
  104. this.$http({
  105. url: this.messageAction,
  106. method: 'post',
  107. data: msgInfo
  108. }).then((data) => {
  109. let info = {
  110. type: this.chat.type,
  111. targetId: file.raw.targetId,
  112. fileId: file.raw.uid,
  113. content: JSON.stringify(res.data),
  114. loadStatus: "ok"
  115. }
  116. this.$store.commit("handleFileUpload", info);
  117. })
  118. },
  119. handleImageFail(res, file) {
  120. let info = {
  121. type: this.chat.type,
  122. targetId: file.raw.targetId,
  123. fileId: file.raw.uid,
  124. loadStatus: "fail"
  125. }
  126. this.$store.commit("handleFileUpload", info);
  127. },
  128. handleImageBefore(file) {
  129. let url = URL.createObjectURL(file);
  130. let data = {
  131. originUrl: url,
  132. thumbUrl: url
  133. }
  134. let msgInfo = {
  135. fileId: file.uid,
  136. sendId: this.mine.id,
  137. content: JSON.stringify(data),
  138. sendTime: new Date().getTime(),
  139. selfSend: true,
  140. type: 1,
  141. loadStatus: "loading"
  142. }
  143. // 填充对方id
  144. this.setTargetId(msgInfo, this.chat.targetId);
  145. // 插入消息
  146. this.$store.commit("insertMessage", msgInfo);
  147. // 滚动到底部
  148. this.scrollToBottom();
  149. // 借助file对象保存对方id
  150. file.targetId = this.chat.targetId;
  151. },
  152. handleFileSuccess(res, file) {
  153. let data = {
  154. name: file.name,
  155. size: file.size,
  156. url: res.data
  157. }
  158. let msgInfo = {
  159. content: JSON.stringify(data),
  160. type: 2
  161. }
  162. // 填充对方id
  163. this.setTargetId(msgInfo, this.chat.targetId);
  164. this.$http({
  165. url: this.messageAction,
  166. method: 'post',
  167. data: msgInfo
  168. }).then(() => {
  169. let info = {
  170. type: this.chat.type,
  171. targetId: file.raw.targetId,
  172. fileId: file.raw.uid,
  173. content: JSON.stringify(data),
  174. loadStatus: "ok"
  175. }
  176. this.$store.commit("handleFileUpload", info);
  177. })
  178. },
  179. handleFileFail(res, file) {
  180. let info = {
  181. type: this.chat.type,
  182. targetId: file.raw.targetId,
  183. fileId: file.raw.uid,
  184. loadStatus: "fail"
  185. }
  186. this.$store.commit("handleFileUpload", info);
  187. },
  188. handleFileBefore(file) {
  189. let url = URL.createObjectURL(file);
  190. let data = {
  191. name: file.name,
  192. size: file.size,
  193. url: url
  194. }
  195. let msgInfo = {
  196. fileId: file.uid,
  197. sendId: this.mine.id,
  198. content: JSON.stringify(data),
  199. sendTime: new Date().getTime(),
  200. selfSend: true,
  201. type: 2,
  202. loadStatus: "loading"
  203. }
  204. // 填充对方id
  205. this.setTargetId(msgInfo, this.chat.targetId);
  206. // 插入消息
  207. this.$store.commit("insertMessage", msgInfo);
  208. // 滚动到底部
  209. this.scrollToBottom();
  210. // 借助file对象保存对方id
  211. file.targetId = this.chat.targetId;
  212. },
  213. handleCloseSide() {
  214. this.showSide = false;
  215. },
  216. switchEmotionBox() {
  217. this.showEmotion = !this.showEmotion;
  218. let width = this.$refs.emotion.offsetWidth;
  219. let left = this.$elm.fixLeft(this.$refs.emotion);
  220. let top = this.$elm.fixTop(this.$refs.emotion);
  221. this.emoBoxPos.y = top;
  222. this.emoBoxPos.x = left + width / 2;
  223. },
  224. handleEmotion(emoText) {
  225. this.sendText += emoText;
  226. this.showEmotion = false;
  227. // 保持输入框焦点
  228. this.$refs.sendBox.focus();
  229. },
  230. showVoiceBox() {
  231. this.showVoice = true;
  232. },
  233. closeVoiceBox() {
  234. this.showVoice = false;
  235. },
  236. handleSendVoice(data) {
  237. let msgInfo = {
  238. content: JSON.stringify(data),
  239. type: 3
  240. }
  241. // 填充对方id
  242. this.setTargetId(msgInfo, this.chat.targetId);
  243. this.$http({
  244. url: this.messageAction,
  245. method: 'post',
  246. data: msgInfo
  247. }).then(() => {
  248. this.$message.success("发送成功");
  249. msgInfo.sendTime = new Date().getTime();
  250. msgInfo.sendId = this.$store.state.userStore.userInfo.id;
  251. msgInfo.selfSend = true;
  252. this.$store.commit("insertMessage", msgInfo);
  253. // 保持输入框焦点
  254. this.$refs.sendBox.focus();
  255. // 滚动到底部
  256. this.scrollToBottom();
  257. // 关闭录音窗口
  258. this.showVoice = false;
  259. })
  260. },
  261. setTargetId(msgInfo, targetId) {
  262. if (this.chat.type == "GROUP") {
  263. msgInfo.groupId = targetId;
  264. } else {
  265. msgInfo.recvId = targetId;
  266. }
  267. },
  268. sendTextMessage() {
  269. if (!this.sendText.trim()) {
  270. this.$message.error("不能发送空白信息");
  271. return
  272. }
  273. let msgInfo = {
  274. content: this.sendText,
  275. type: 0
  276. }
  277. // 填充对方id
  278. this.setTargetId(msgInfo, this.chat.targetId);
  279. this.$http({
  280. url: this.messageAction,
  281. method: 'post',
  282. data: msgInfo
  283. }).then((data) => {
  284. this.$message.success("发送成功");
  285. this.sendText = "";
  286. msgInfo.sendTime = new Date().getTime();
  287. msgInfo.sendId = this.$store.state.userStore.userInfo.id;
  288. msgInfo.selfSend = true;
  289. this.$store.commit("insertMessage", msgInfo);
  290. // 保持输入框焦点
  291. this.$refs.sendBox.focus();
  292. // 滚动到底部
  293. this.scrollToBottom();
  294. })
  295. const e = window.event || arguments[0];
  296. if (e.key === 'Enter' || e.code === 'Enter' || e.keyCode === 13) {
  297. e.returnValue = false;
  298. e.preventDefault();
  299. return false;
  300. }
  301. },
  302. loadGroup(groupId) {
  303. this.$http({
  304. url: `/group/find/${groupId}`,
  305. method: 'get'
  306. }).then((group) => {
  307. this.group = group;
  308. this.$store.commit("updateChatFromGroup", group);
  309. this.$store.commit("updateGroup", group);
  310. });
  311. this.$http({
  312. url: `/group/members/${groupId}`,
  313. method: 'get'
  314. }).then((groupMembers) => {
  315. this.groupMembers = groupMembers;
  316. });
  317. },
  318. loadFriend(friendId) {
  319. // 获取对方最新信息
  320. this.$http({
  321. url: `/user/find/${friendId}`,
  322. method: 'get'
  323. }).then((friend) => {
  324. this.friend = friend;
  325. this.$store.commit("updateChatFromFriend", friend);
  326. this.$store.commit("updateFriend", friend);
  327. })
  328. },
  329. showName(msgInfo) {
  330. if (this.chat.type == 'GROUP') {
  331. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  332. return member ? member.aliasName : "";
  333. } else {
  334. return msgInfo.sendId == this.mine.id ? this.mine.nickName : this.chat.showName
  335. }
  336. },
  337. headImage(msgInfo) {
  338. if (this.chat.type == 'GROUP') {
  339. let member = this.groupMembers.find((m) => m.userId == msgInfo.sendId);
  340. return member ? member.headImage : "";
  341. } else {
  342. return msgInfo.sendId == this.mine.id ? this.mine.headImageThumb : this.chat.headImage
  343. }
  344. },
  345. scrollToBottom() {
  346. this.$nextTick(() => {
  347. const div = document.getElementById("chatScrollBox");
  348. div.scrollTop = div.scrollHeight;
  349. });
  350. }
  351. },
  352. computed: {
  353. mine() {
  354. return this.$store.state.userStore.userInfo;
  355. },
  356. title() {
  357. let title = this.chat.showName;
  358. if (this.chat.type == "GROUP") {
  359. let size = this.groupMembers.filter(m => !m.quit).length;
  360. title += `(${size})`;
  361. }
  362. return title;
  363. },
  364. imageAction() {
  365. return `${process.env.VUE_APP_BASE_API}/image/upload`;
  366. },
  367. fileAction() {
  368. return `${process.env.VUE_APP_BASE_API}/file/upload`;
  369. },
  370. messageAction() {
  371. return `/message/${this.chat.type.toLowerCase()}/send`;
  372. }
  373. },
  374. watch: {
  375. chat: {
  376. handler(newChat, oldChat) {
  377. if (newChat.targetId > 0 && (newChat.type != oldChat.type || newChat.targetId != oldChat.targetId)) {
  378. if (this.chat.type == "GROUP") {
  379. this.loadGroup(this.chat.targetId);
  380. } else {
  381. this.loadFriend(this.chat.targetId);
  382. }
  383. this.scrollToBottom();
  384. this.sendText = "";
  385. // 保持输入框焦点
  386. this.$refs.sendBox.focus();
  387. }
  388. },
  389. deep: true
  390. }
  391. }
  392. }
  393. </script>
  394. <style lang="scss">
  395. .chat-box {
  396. background: white;
  397. border: #dddddd solid 1px;
  398. .el-header {
  399. padding: 5px;
  400. background-color: white;
  401. line-height: 50px;
  402. font-size: 20px;
  403. font-weight: 600;
  404. border: #dddddd solid 1px;
  405. .btn-side {
  406. position: absolute;
  407. right: 20px;
  408. line-height: 60px;
  409. font-size: 22px;
  410. cursor: pointer;
  411. &:hover {
  412. font-size: 30px;
  413. }
  414. }
  415. }
  416. .im-chat-main {
  417. padding: 0;
  418. border: #dddddd solid 1px;
  419. .im-chat-box {
  420. ul {
  421. padding: 20px;
  422. li {
  423. list-style-type: none;
  424. }
  425. }
  426. }
  427. }
  428. .im-chat-footer {
  429. display: flex;
  430. flex-direction: column;
  431. padding: 0;
  432. border: #dddddd solid 1px;
  433. .chat-tool-bar {
  434. display: flex;
  435. position: relative;
  436. width: 100%;
  437. height: 40px;
  438. text-align: left;
  439. box-sizing: border-box;
  440. border: #dddddd solid 1px;
  441. >div {
  442. margin-left: 10px;
  443. font-size: 22px;
  444. cursor: pointer;
  445. color: #333333;
  446. line-height: 40px;
  447. &:hover {
  448. color: black;
  449. }
  450. }
  451. }
  452. .send-text-area {
  453. box-sizing: border-box;
  454. padding: 5px;
  455. width: 100%;
  456. flex: 1;
  457. resize: none;
  458. background-color: #f8f8f8 !important;
  459. outline-color: rgba(83, 160, 231, 0.61);
  460. }
  461. .im-chat-send {
  462. text-align: right;
  463. padding: 7px;
  464. }
  465. }
  466. .chat-group-side-box {
  467. border: #dddddd solid 1px;
  468. animation: rtl-drawer-in .3s 1ms;
  469. }
  470. }
  471. </style>