ChatFrame.vue 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  1. <template>
  2. <div>
  3. <div class="chat-wrap" v-if="selected">
  4. <div class="chat-title">
  5. {{ selected.username || selected.name }}
  6. </div>
  7. <div class="record-warp">
  8. <!-- 私聊 -->
  9. <el-scrollbar
  10. v-if="tab === 'friends'"
  11. style="height: 95%"
  12. ref="myScrollbar"
  13. >
  14. <div
  15. class="record-list"
  16. v-for="(item, i) in friend_record_list"
  17. :key="i"
  18. >
  19. <!-- receive msg record -->
  20. <div class="record-item" v-if="item.toid === socket.id">
  21. <div class="avatar">
  22. <el-avatar
  23. shape="square"
  24. src="https://img2.baidu.com/it/u=1077360284,2857506492&fm=26&fmt=auto&gp=0.jpg"
  25. ></el-avatar>
  26. </div>
  27. <div class="group-record-item">
  28. <!-- 链接 -->
  29. <a
  30. class="message-content-text receive-record-item"
  31. v-if="_isUrl(item.msg)"
  32. :href="item.msg"
  33. target="_blank"
  34. style="margin-left: 10px; margin-top: 15px;"
  35. >
  36. {{ item.msg }}
  37. </a>
  38. <!-- 文本 -->
  39. <div
  40. class="message-content-text receive-record-item"
  41. v-if="item.type === 'text'"
  42. v-html="_parseHtml(item.msg)"
  43. style="margin-left: 10px; margin-top: 15px;"
  44. ></div>
  45. <!-- 图片 -->
  46. <div
  47. class="message-content-image receive-record-item"
  48. v-if="item.type === 'image'"
  49. :style="getImageStyle(item.msg)"
  50. style="margin-left: 10px; margin-top: 15px;"
  51. >
  52. <viewer style="display: flex; align-items: center">
  53. <img :src="baseUrl + item.msg" alt="" />
  54. </viewer>
  55. </div>
  56. <!-- 视频格式文件 -->
  57. <div
  58. class="message-content-image receive-record-item"
  59. v-if="item.type === 'video'"
  60. style="margin-left: 10px; margin-top: 15px;"
  61. >
  62. <video
  63. style="width: 255px"
  64. src="https://media.w3.org/2010/05/sintel/trailer.mp4"
  65. controls="controls"
  66. >
  67. 您的浏览器不支持 video 标签。
  68. </video>
  69. </div>
  70. </div>
  71. </div>
  72. <!-- self send msg record -->
  73. <div v-if="item.toid !== socket.id" class="record-item send-record">
  74. <!-- 链接 -->
  75. <a
  76. class="message-content-text"
  77. v-if="_isUrl(item.msg)"
  78. :href="item.msg"
  79. target="_blank"
  80. >
  81. {{ item.msg }}
  82. </a>
  83. <!-- 文本 -->
  84. <div
  85. class="message-content-text"
  86. v-if="item.type === 'text'"
  87. v-html="_parseHtml(item.msg)"
  88. ></div>
  89. <!-- 图片 -->
  90. <div
  91. class="message-content-image"
  92. v-if="item.type === 'image'"
  93. :style="getImageStyle(item.msg)"
  94. >
  95. <viewer style="display: flex; align-items: center">
  96. <img :src="baseUrl + item.msg" alt="" />
  97. </viewer>
  98. </div>
  99. <!-- 附件类型消息 -->
  100. <!-- <div class="message-content-file" v-else-if="item.type === 'file'" @click="download(item)">
  101. <img class="message-content-icon" :src="getFileIcon(item)" alt="" />
  102. <div class="message-content-detail">
  103. <div class="file-name">
  104. {{ getFileName(item).name }}
  105. </div>
  106. <div class="file-size">
  107. {{ getFileName(item).size }}
  108. </div>
  109. </div>
  110. </div> -->
  111. <!-- 视频格式文件 -->
  112. <div class="message-content-image" v-if="item.type === 'video'">
  113. <video
  114. style="width: 255px"
  115. src="https://media.w3.org/2010/05/sintel/trailer.mp4"
  116. controls="controls"
  117. >
  118. 您的浏览器不支持 video 标签。
  119. </video>
  120. </div>
  121. <!-- <div class="avatar">
  122. <el-avatar
  123. shape="square"
  124. src="https://img2.baidu.com/it/u=1077360284,2857506492&fm=26&fmt=auto&gp=0.jpg"
  125. ></el-avatar>
  126. </div> -->
  127. </div>
  128. </div>
  129. </el-scrollbar>
  130. <!-- 群聊 -->
  131. <el-scrollbar
  132. v-if="tab === 'groups'"
  133. style="height: 95%"
  134. ref="myScrollbar"
  135. >
  136. <div
  137. class="record-list"
  138. v-for="(item, i) in room_record_list"
  139. :key="i"
  140. >
  141. <!-- receive msg record -->
  142. <div class="record-item" v-if="item.id !== socket.id">
  143. <div class="avatar">
  144. <el-avatar
  145. shape="square"
  146. src="https://img2.baidu.com/it/u=1077360284,2857506492&fm=26&fmt=auto&gp=0.jpg"
  147. ></el-avatar>
  148. </div>
  149. <div class="group-record-item">
  150. <div class="username">
  151. {{ item.username }}
  152. </div>
  153. <!-- 链接 -->
  154. <a
  155. class="message-content-text receive-record-item"
  156. v-if="_isUrl(item.msg)"
  157. :href="item.msg"
  158. target="_blank"
  159. >
  160. {{ item.msg }}
  161. </a>
  162. <!-- 文本 -->
  163. <div
  164. class="message-content-text receive-record-item"
  165. v-if="item.type === 'text'"
  166. v-html="_parseHtml(item.msg)"
  167. ></div>
  168. <!-- 图片 -->
  169. <div
  170. class="message-content-image receive-record-item"
  171. v-if="item.type === 'image'"
  172. :style="getImageStyle(item.msg)"
  173. >
  174. <viewer style="display: flex; align-items: center">
  175. <img :src="baseUrl + item.msg" alt="" />
  176. </viewer>
  177. </div>
  178. <!-- 附件类型消息 -->
  179. <!-- <div class="message-content-file" v-else-if="item.type === 'file'" @click="download(item)">
  180. <img class="message-content-icon" :src="getFileIcon(item)" alt="" />
  181. <div class="message-content-detail">
  182. <div class="file-name">
  183. {{ getFileName(item).name }}
  184. </div>
  185. <div class="file-size">
  186. {{ getFileName(item).size }}
  187. </div>
  188. </div>
  189. </div> -->
  190. <!-- 视频格式文件 -->
  191. <div
  192. class="message-content-image receive-record-item"
  193. v-if="item.type === 'video'"
  194. >
  195. <video
  196. style="width: 255px"
  197. src="https://media.w3.org/2010/05/sintel/trailer.mp4"
  198. controls="controls"
  199. >
  200. 您的浏览器不支持 video 标签。
  201. </video>
  202. </div>
  203. </div>
  204. </div>
  205. <!-- self send msg record -->
  206. <div v-if="item.id === socket.id" class="record-item send-record">
  207. <!-- 链接 -->
  208. <a
  209. class="message-content-text"
  210. v-if="_isUrl(item.msg)"
  211. :href="item.msg"
  212. target="_blank"
  213. >
  214. {{ item.msg }}
  215. </a>
  216. <!-- 文本 -->
  217. <div
  218. class="message-content-text"
  219. v-if="item.type === 'text'"
  220. v-html="_parseHtml(item.msg)"
  221. ></div>
  222. <!-- 图片 -->
  223. <div
  224. class="message-content-image"
  225. v-if="item.type === 'image'"
  226. :style="getImageStyle(item.msg)"
  227. >
  228. <viewer style="display: flex; align-items: center">
  229. <img :src="baseUrl + item.msg" alt="" />
  230. </viewer>
  231. </div>
  232. <!-- 附件类型消息 -->
  233. <!-- <div class="message-content-file" v-else-if="item.type === 'file'" @click="download(item)">
  234. <img class="message-content-icon" :src="getFileIcon(item)" alt="" />
  235. <div class="message-content-detail">
  236. <div class="file-name">
  237. {{ getFileName(item).name }}
  238. </div>
  239. <div class="file-size">
  240. {{ getFileName(item).size }}
  241. </div>
  242. </div>
  243. </div> -->
  244. <!-- 视频格式文件 -->
  245. <div class="message-content-image" v-if="item.type === 'video'">
  246. <video
  247. style="width: 255px"
  248. src="https://media.w3.org/2010/05/sintel/trailer.mp4"
  249. controls="controls"
  250. >
  251. 您的浏览器不支持 video 标签。
  252. </video>
  253. </div>
  254. <!-- <div class="avatar">
  255. <el-avatar
  256. shape="square"
  257. src="https://img2.baidu.com/it/u=1077360284,2857506492&fm=26&fmt=auto&gp=0.jpg"
  258. ></el-avatar>
  259. </div> -->
  260. </div>
  261. </div>
  262. </el-scrollbar>
  263. </div>
  264. <div class="send-msg-wrap">
  265. <div class="send-header">
  266. <div class="tools" style="margin: 10px">
  267. <el-popover placement="top" width="300" trigger="click">
  268. <Emoji @addEmoji="addEmoji" />
  269. <el-button
  270. class="iconfont open_emoji_icon"
  271. style="color:rgb(44,60,80)"
  272. type="text"
  273. slot="reference"
  274. >&#xe612;</el-button
  275. >
  276. </el-popover>
  277. <el-upload
  278. ref="upload"
  279. class="file-uploader"
  280. action="/api/upload/"
  281. :show-file-list="false"
  282. :on-success="handleSuccess"
  283. :before-upload="beforeUpload"
  284. style="display: inline"
  285. >
  286. <i
  287. id="upload-btn"
  288. class="el-icon-folder-opened"
  289. @click="upload"
  290. ></i>
  291. </el-upload>
  292. <i class="el-icon-folder-add"></i>
  293. <i class="el-icon-folder-opened"></i>
  294. </div>
  295. </div>
  296. <div class="input-box">
  297. <div
  298. id="charInput"
  299. @click="saveRangeLocal"
  300. @focus="saveRangeLocal"
  301. @input="saveRangeLocal"
  302. @keydown.enter="stopPropagation"
  303. @keyup.enter="sendMsg"
  304. class="chatframe_input_con scrollbar"
  305. contenteditable="true"
  306. ></div>
  307. <div class="send-footer">
  308. <el-button
  309. type="info"
  310. plain
  311. class="send-btn"
  312. size="small"
  313. @click="sendMsg"
  314. >发送</el-button
  315. >
  316. </div>
  317. </div>
  318. </div>
  319. </div>
  320. <div class="chat-wrap" v-if="!selected">
  321. <el-empty description="请选择聊天对象"></el-empty>
  322. </div>
  323. </div>
  324. </template>
  325. <script>
  326. import { isUrl, parseHtml } from "@/utils/common";
  327. import Emoji from "./Emoji.vue";
  328. import { mapState, mapActions } from "vuex";
  329. let emojiData = require("./../assets/emoji/emoji.json");
  330. import request from "@/api/require";
  331. export default {
  332. name: "ChatFrame",
  333. props: ["selected", "tab"],
  334. components: {
  335. Emoji
  336. },
  337. data() {
  338. return {
  339. baseUrl: "http://192.168.100.8:3000/",
  340. emojiIcon: emojiData.icon,
  341. inputRange: "", // 光标
  342. emojiPath: new Map(), // emoji表情地址map对象,
  343. msg: "",
  344. friend_record_list: [],
  345. room_record_list: [],
  346. messageType: "",
  347. uploadProgress: false
  348. };
  349. },
  350. computed: {
  351. ...mapState({
  352. self: state => state.app.user
  353. }),
  354. ...mapState({
  355. socket: state => state.chat.socket
  356. }),
  357. ...mapState({
  358. friendRecord: state => state.chat.friendRecord
  359. }),
  360. ...mapState({
  361. roomRecord: state => state.chat.roomRecord
  362. })
  363. },
  364. watch: {
  365. selected(newVal, o) {
  366. if (this.tab === "friends") {
  367. if (this.friendRecord[newVal.id]) {
  368. this.friend_record_list = this.friendRecord[newVal.id];
  369. } else {
  370. this.$store.commit("chat/set_friend_record", [newVal.id]);
  371. this.friend_record_list = this.friendRecord[newVal.id];
  372. console.log(`this.friendRecord`, this.friendRecord);
  373. }
  374. } else {
  375. if (this.roomRecord[newVal.id]) {
  376. this.room_record_list = this.roomRecord[newVal.id];
  377. } else {
  378. this.$store.commit("chat/set_room_record", newVal.id);
  379. this.room_record_list = this.roomRecord[newVal.id];
  380. console.log(
  381. `this.room_record_list[newVal.id]`,
  382. this.room_record_list
  383. );
  384. }
  385. }
  386. this.$nextTick(() => {
  387. document.getElementById("charInput").innerText = "";
  388. this.msg = "";
  389. this.$refs["myScrollbar"].wrap.scrollTop = this.$refs[
  390. "myScrollbar"
  391. ].wrap.scrollHeight;
  392. });
  393. }
  394. },
  395. methods: {
  396. handleSuccess(res, file) {
  397. console.log(`res`, res);
  398. this.imageUrl = URL.createObjectURL(file.raw);
  399. },
  400. beforeUpload(file) {
  401. this.uploadProgress = true;
  402. this.fileData = file;
  403. // 调用函数分割文件 我这里是分割成不超过20M的文件快
  404. this.fileDataList = this.createFileChunk(file, 1024 * 1024 * 20);
  405. console.log(`this.fileDataList`, this.fileDataList);
  406. // return new Promise((reslove, reject)=>{
  407. // this.fileUpLoad(reslove, reject);
  408. // })
  409. let fd = new FormData();
  410. fd.append("filename", file);
  411. // upload_file(fd).then(
  412. request
  413. .upload_file(file)
  414. .then(res => {
  415. console.log(`res5555`, res);
  416. })
  417. .catch(err => {
  418. console.log(`err`, err);
  419. });
  420. this.$http.post(this.baseUrl + "api/upload/", fd).then(
  421. res => {
  422. if (
  423. file.type === "image/jpeg" ||
  424. file.type === "image/png" ||
  425. file.type === "image/gif"
  426. ) {
  427. this.messageType = "image";
  428. this.sendMsg(null, res.data.data);
  429. console.log(`res.data.data`, res.data.data);
  430. console.log("========is image =====");
  431. } else {
  432. this.messageType = "file";
  433. console.log("=========not image=========");
  434. }
  435. // this.importDataBtnText='导入成功';
  436. console.log(`res1111`, res);
  437. console.log("======success====");
  438. },
  439. res => {
  440. this.$message.error("文件发送失败");
  441. console.log("===========shibai====");
  442. // this.importDataBtnText='导入失败';
  443. console.log(res);
  444. }
  445. );
  446. return false;
  447. },
  448. submitUpload() {
  449. this.$refs.upload.submit();
  450. },
  451. /**
  452. * 文本转译/校验
  453. * @params text
  454. */
  455. _parseHtml(text) {
  456. const regex2 = /!!\[(.+?)\]!!/g;
  457. const codeList = text.match(regex2);
  458. let html = text;
  459. if (codeList) {
  460. codeList.map(item => {
  461. const code = item.match(/\[(.+?)\]/g)[0];
  462. html = html.replace(
  463. item,
  464. "<img style='width: 20px; height: 20px;vertical-align: sub;' src='" +
  465. this.getIconPic(code) +
  466. "' unicode = '" +
  467. code +
  468. "' alt='' >"
  469. );
  470. });
  471. }
  472. return parseHtml(html);
  473. },
  474. /**
  475. * 是否URL
  476. * @params text
  477. */
  478. _isUrl(text) {
  479. return isUrl(text);
  480. },
  481. // // 初始化上传接口的函数,再上面上传之前调用的
  482. // fileUpLoad(reslove, reject) {
  483. // const paramsData = {
  484. // multipartUpload: true, // 是否是文件分步,分块上传
  485. // name: this.fileData.name,
  486. // size: this.fileData.size,
  487. // }
  488. // initFileUpload(paramsData).then(res => {
  489. // const uploadUrl = res.data.data.uploadUrl
  490. // const completeMultipartUrl = res.data.data.completeMultipartUrl
  491. // // 文件上传的请求地址
  492. // this.uploadUrl = uploadUrl
  493. // // 合并文件上传的请求地址
  494. // this.completeMultipartUrl = completeMultipartUrl
  495. // reslove();
  496. // }).catch((err)=>{
  497. // reject(err)
  498. // })
  499. // },
  500. // 文件分割的方法
  501. createFileChunk(file, size = chunkSize) {
  502. const fileChunkList = [];
  503. let count = 0;
  504. let num = 1;
  505. while (count < file.size) {
  506. fileChunkList.push({
  507. file: file.slice(count, count + size),
  508. partNumber: num
  509. });
  510. count += size;
  511. num++;
  512. }
  513. return fileChunkList;
  514. },
  515. /**
  516. * 根据图片url设置图片框宽高, 注意是图片框
  517. */
  518. getImageStyle(src) {
  519. const arr = src.split("$");
  520. let width = Number(arr[2]);
  521. let height = Number(arr[3]);
  522. if (this.mobile) {
  523. // 如果是移动端,图片最大宽度138, 返回值加12是因为设置的是图片框的宽高要加入padding值
  524. if (width > 138) {
  525. height = (height * 138) / width;
  526. width = 138;
  527. return {
  528. width: `${width + 12}px`,
  529. height: `${height + 12}px`
  530. };
  531. }
  532. }
  533. return {
  534. width: `${width + 12}px`,
  535. height: `${height + 12}px`
  536. };
  537. },
  538. /**
  539. * 是否URL
  540. * @params text
  541. */
  542. _isUrl(text) {
  543. return isUrl(text);
  544. },
  545. upload() {},
  546. // 添加表情, 到输入框
  547. addEmoji(code) {
  548. let inputNode = document.getElementById("charInput");
  549. let html =
  550. "<img style='width: 20px; height: 20px;vertical-align: sub;' src='" +
  551. this.getIconPic(code) +
  552. "' unicode = '" +
  553. code +
  554. "' alt='' >";
  555. let sel = window.getSelection();
  556. let range = this.inputRange;
  557. let el = document.createElement("div");
  558. let frag = document.createDocumentFragment(),
  559. node,
  560. lastNode;
  561. if (!inputNode) {
  562. return;
  563. }
  564. if (!range) {
  565. inputNode.focus();
  566. range = window.getSelection().getRangeAt(0);
  567. }
  568. range.deleteContents();
  569. el.innerHTML = html;
  570. while ((node = el.firstChild)) {
  571. lastNode = frag.appendChild(node);
  572. }
  573. range.insertNode(frag);
  574. if (lastNode) {
  575. range = range.cloneRange();
  576. range.setStartAfter(lastNode);
  577. range.collapse(true);
  578. sel.removeAllRanges();
  579. sel.addRange(range);
  580. }
  581. },
  582. // 初始化emoji的map对象
  583. initEmojiPic() {
  584. let map = new Map();
  585. for (const key in emojiData.icon) {
  586. emojiData.icon[key].forEach(item => {
  587. map.set(
  588. item.unicode,
  589. require("./../assets/emoji/icon/" + item.unicode + ".png")
  590. );
  591. });
  592. }
  593. this.emojiPath = map;
  594. },
  595. // 通过Unicode码从map中获取emoji
  596. getIconPic(unicode) {
  597. return this.emojiPath.get(unicode);
  598. },
  599. // ===============================================================
  600. // 将输入框中的图片替换为emoji表情
  601. formatInputCon() {
  602. let inputValue = document.getElementById("charInput").innerHTML;
  603. inputValue = inputValue.replace(/<img.*?(?:>|\/>)/gi, val => {
  604. let unicode =
  605. "!!" + val.match(/unicode=[\'\"]?([^\'\"]*)[\'\"]?/i)[1] + "!!";
  606. return unicode;
  607. });
  608. return inputValue;
  609. },
  610. // 将emoji表情转换为图片
  611. changeEmojiCon(str) {
  612. let patt = /[\ud800-\udbff][\udc00-\udfff]/g; // 检测utf16字符正则
  613. str = str.replace(patt, char => {
  614. let H, L, code;
  615. if (char.length === 2) {
  616. H = char.charCodeAt(0); // 取出高位
  617. L = char.charCodeAt(1); // 取出低位
  618. code = (H - 0xd800) * 0x400 + 0x10000 + L - 0xdc00; // 转换算法
  619. return "&#" + code + ";";
  620. } else {
  621. return char;
  622. }
  623. });
  624. str = str.replace(/&#{1}[0-9]+;{1}/gi, a => {
  625. let unicode = a.replace(/^&#{1}/gi, "");
  626. unicode = unicode.replace(/;{1}$/gi, "");
  627. unicode =
  628. "U+" +
  629. parseFloat(unicode)
  630. .toString(16)
  631. .toUpperCase();
  632. return "<img src='" + this.getIconPic(unicode) + "'/>";
  633. });
  634. return str;
  635. },
  636. // 延时记录光标到位置
  637. chartFramRange(type) {
  638. try {
  639. if (type === "click") {
  640. this.inputRange = window.getSelection().getRangeAt(0);
  641. }
  642. if (type === "focus") {
  643. this.inputRange = window.getSelection().getRangeAt(0);
  644. }
  645. } catch (err) {
  646. console.log(type);
  647. }
  648. return new Promise((resolve, reject) => {
  649. setTimeout(() => {
  650. this.inputRange = window.getSelection().getRangeAt(0);
  651. resolve(this.inputRange);
  652. }, 0);
  653. });
  654. },
  655. // 延时记录光标到位置
  656. saveRangeLocal() {
  657. setTimeout(() => {
  658. this.inputRange = window.getSelection().getRangeAt(0);
  659. }, 0);
  660. },
  661. // ================================================================
  662. // 阻止回车键默认事件
  663. stopPropagation(e) {
  664. if (e && e.keyCode === 13) {
  665. e.cancelBubble = true;
  666. e.preventDefault();
  667. e.stopPropagation();
  668. }
  669. },
  670. sendMsg(e, fileurl) {
  671. console.log(`e============`, e);
  672. if (fileurl) {
  673. // 上传文件路径
  674. this.msg = fileurl;
  675. } else {
  676. // 输入框信息
  677. this.msg = this.formatInputCon().replace(/<br>/g, "\r\n");
  678. console.log(`this.msg`, this.msg);
  679. }
  680. if (this.msg.trim() === "") {
  681. this.msg = "";
  682. return;
  683. }
  684. if (!this.messageType) {
  685. this.messageType = "text";
  686. }
  687. const msgInfo = {
  688. ...this.self,
  689. id: this.socket.id,
  690. msg: this.msg,
  691. type: this.messageType
  692. };
  693. if (this.tab === "friends") {
  694. msgInfo.toid = this.selected.id;
  695. this.socket.emit("private message", this.selected.id, msgInfo);
  696. const msgData = [this.selected.id, msgInfo];
  697. this.$store.commit("chat/set_friend_record", msgData);
  698. this.friend_record_list = this.friendRecord[this.selected.id];
  699. } else {
  700. console.log("=======ROOM CHAT========");
  701. msgInfo.roomId = this.selected.id;
  702. this.socket.emit("chat-room", this.selected.id, msgInfo);
  703. this.$store.commit("chat/set_room_record", msgInfo);
  704. this.room_record_list = this.roomRecord[this.selected.id];
  705. }
  706. this.$nextTick(() => {
  707. // 输入enter后置空
  708. document.getElementById("charInput").innerText = "";
  709. this.msg = "";
  710. this.messageType = "";
  711. // 滚动到底部
  712. this.$refs["myScrollbar"].wrap.scrollTop = this.$refs["myScrollbar"].wrap.scrollHeight;
  713. });
  714. },
  715. closeScoket() {
  716. // 断开连接
  717. this.$socket.close();
  718. }
  719. },
  720. created() {
  721. // const room_record_str = getCookie("room_record");
  722. // const friend_record_str = getCookie("friend_record");
  723. // if (room_record_str) {
  724. // this.room_record = JSON.parse(room_record_str);
  725. // }
  726. // if (friend_record_str) {
  727. // this.friend_record = JSON.parse(friend_record_str);
  728. // }
  729. },
  730. mounted() {
  731. this.initEmojiPic();
  732. }
  733. };
  734. </script>
  735. <style lang="scss" scoped>
  736. .chat-wrap {
  737. height: 90vh;
  738. border: 1px solid #ddd;
  739. border-radius: 2px;
  740. }
  741. .chat-title {
  742. padding: 20px;
  743. font-weight: 600;
  744. }
  745. .record-warp {
  746. padding: 10px;
  747. height: 63vh;
  748. background: rgb(245, 245, 245);
  749. }
  750. /* .record-list {
  751. } */
  752. .record-item {
  753. display: flex;
  754. padding: 15px 5px;
  755. margin: 5px;
  756. }
  757. .avatar {
  758. margin: 10px 5px;
  759. }
  760. .record-item > span {
  761. padding: 8px 12px;
  762. border-radius: 5px;
  763. }
  764. .username {
  765. padding: 4px;
  766. margin-bottom: 2px;
  767. font-size: 14px;
  768. color: gray;
  769. }
  770. .group-record-item > span {
  771. padding: 8px 12px;
  772. border-radius: 5px;
  773. background-color: rgb(199, 206, 200);
  774. }
  775. .send-record {
  776. position: relative;
  777. justify-content: flex-end;
  778. }
  779. .send-record > span {
  780. display: inline-block;
  781. text-align: right;
  782. }
  783. .message-content-text,
  784. .message-content-image {
  785. max-width: 600px;
  786. display: inline-block;
  787. margin-left: 35px;
  788. overflow: hidden;
  789. margin-top: 4px;
  790. padding: 6px;
  791. background: rgb(117, 197, 128);
  792. font-size: 16px;
  793. border-radius: 5px;
  794. text-align: left;
  795. word-break: break-word;
  796. }
  797. .message-content-image {
  798. max-height: 255px;
  799. max-width: 255px;
  800. background: rgb(204, 197, 197);
  801. }
  802. .message-content-image img {
  803. cursor: pointer;
  804. max-width: 225px;
  805. max-height: 225px;
  806. }
  807. .receive-record-item {
  808. margin: 2px;
  809. background: #fff;
  810. }
  811. .input-box {
  812. display: flex;
  813. overflow: hidden;
  814. position: relative;
  815. }
  816. .send-footer {
  817. position: absolute;
  818. bottom: 0;
  819. right: 20px;
  820. padding: 10px;
  821. text-align: right;
  822. }
  823. .chatframe_input_con {
  824. display: inline-block;
  825. width: 92%;
  826. height: 95px;
  827. resize: none;
  828. padding: 5px 15px;
  829. line-height: 1.5;
  830. box-sizing: border-box;
  831. font-size: inherit;
  832. color: #606266;
  833. background-color: #fff;
  834. outline: none;
  835. overflow-y: scroll;
  836. margin-bottom: 20px;
  837. // 超出自动换行 -- 火狐
  838. word-wrap: break-word;
  839. word-break: break-all;
  840. // 滚动样式 细 - 火狐
  841. scrollbar-width: thin;
  842. }
  843. .chatframe-text {
  844. min-height: 200px;
  845. width: 500px;
  846. box-sizing: border-box;
  847. padding: 10px 15px;
  848. margin-top: 5px;
  849. background: #eaedf1;
  850. color: #282828;
  851. border-radius: 4px;
  852. word-wrap: break-word;
  853. word-break: break-all;
  854. line-height: 18px;
  855. white-space: pre-wrap;
  856. position: relative;
  857. }
  858. .text_emoji {
  859. img {
  860. display: inline-block;
  861. width: 20px;
  862. height: 20px;
  863. vertical-align: bottom;
  864. }
  865. }
  866. </style>