chat.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. <template>
  2. <view class="content">
  3. <!-- <view class="send-load" v-if="msgData.flag">数据加载中...</view> -->
  4. <view class="send-load" v-if="msgData.isSend">发送中...</view>
  5. <view class="chat-content" @touchmove="touchmove">
  6. <scroll-view class="chat-body" scroll-y scroll-with-animation :scroll-top="scrollTop" scroll-with-animation @scroll="scroll" @scrolltoupper="scrolltoupper" @scrolltolower="scrolltolower" upper-threshold="80" :scroll-into-view="scrollIntoViewId" refresher-background="transparent">
  7. <!-- <view class="msgLoading" v-if="msgData.flag">数据加载中...</view> -->
  8. <view class="scroll-view-msg" style="padding: 30rpx 30rpx;">
  9. <template v-for="(item, index) in msgData.list">
  10. <view class="msgContent" :id="'msg-' + item.messageId">
  11. <view class="text_26 color__ time">
  12. {{ renderMessageDate(item, index) }}
  13. </view>
  14. <view class="message" :class="[item.userType]">
  15. <image :src="chatData.service" v-if="item.userType === 'friend'" class="avatar" mode="widthFix"></image>
  16. <view class="message-con">
  17. <view v-if="item.msgType == 'text'">
  18. <view v-html="renderTextMessage(item.content)"></view>
  19. </view>
  20. <view class="m-address" v-if="item.msgType == 'address'">
  21. <view class="image"><image :src="item.content.img" mode="widthFix"></image></view>
  22. <view class="infos">
  23. <view class="title">{{item.content.name}}</view>
  24. <view class="desc">{{item.content.order?item.content.order:'-'}}</view>
  25. </view>
  26. </view>
  27. <view class="m-address" v-if="item.msgType == 'order'">
  28. <view class="image"><image :src="item.content.img" mode="widthFix"></image></view>
  29. <view class="infos">
  30. <view class="title">{{item.content.name}}</view>
  31. <view class="desc">{{item.content.order?item.content.order:'-'}}</view>
  32. </view>
  33. </view>
  34. <view class="m-image" v-if="item.msgType == 'image'">
  35. <image :src="item.content" mode="widthFix" @click="previewImg(item.content)"></image>
  36. </view>
  37. <view class="m-address" v-if="item.msgType == 'pay'">
  38. <view class="image"><image :src="item.content.img" mode="widthFix"></image></view>
  39. <view class="infos">
  40. <view class="title">{{item.content.name}}</view>
  41. <view class="price">¥ {{item.content.price?item.content.price:'-'}}</view>
  42. </view>
  43. </view>
  44. <view class="" v-if="item.msgType == 'quick'">
  45. <view class="quickCon">{{item.content}}</view>
  46. <view class="quickList" v-if="item.list.length > 0">
  47. <view class="list-items" v-for="(qItem,indx) in item.list" @click="quickQs(qItem)">
  48. {{qItem.question}}
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. <image :src="chatData.self" v-if="item.userType === 'self'" class="avatar" mode="widthFix"></image>
  54. </view>
  55. </view>
  56. </template>
  57. </view>
  58. </scroll-view>
  59. <view class="tool">
  60. <view class="tool-option">
  61. <view class="input-text">
  62. <textarea class="input" placeholder="输入点什么呢..." auto-height="true" confirm-type="send" type="text"
  63. :maxlength="-1" :adjust-position="false" v-model="text" confirm-hold @focus="focus" :focus="isFocus" @blur="isFocus = false"
  64. :show-confirm-bar="false" @confirm="sendingText" @keyboardheightchange="keyboardheightchange" />
  65. </view>
  66. <view class="check-img">
  67. <view class="check-icon" @click="tapEmoji"><image src="@/static/image/face.svg" mode="aspectFill"></image></view>
  68. <view class="check-icon" @click="tapMore"><image src="@/static/image/more.svg" mode="aspectFill"></image></view>
  69. </view>
  70. </view>
  71. </view>
  72. <template v-if="keyboardHeight>0">
  73. <view class="keyboardheight" :style="{ height: keyboardHeight + 'px' }"></view>
  74. </template>
  75. <emoji v-model="isEmoji" @onEmoji="onEmoji" @deleteFn="deleteFn" @sendingText="sendingText" @sendingEmojiPack="sendingEmojiPack"></emoji>
  76. <more v-model="isMore" @onMore="sendingMore"></more>
  77. </view>
  78. </view>
  79. </template>
  80. <script>
  81. var app;
  82. let cursor = 0; //输入框光标
  83. let getSelectedTextRangeSetInterval = null;
  84. import { mapState } from 'vuex';
  85. import {
  86. show,
  87. formatDate,
  88. throttle,
  89. openimg,
  90. getLocation,
  91. to as tofn
  92. } from '@/utils/js/index.js';
  93. import { getHistoryMsg } from "@/utils/js/history-msg.js";
  94. var {Push} = require("@/static/js/push.js")
  95. import emoji from '@/components/chat/emoji';
  96. import more from '@/components/chat/more';
  97. import { EmojiDecoder, emojiMap } from '@/utils/js/EmojiDecoder.js';
  98. const emojiUrl = 'https://imgcache.qq.com/open/qcloud/tim/assets/emoji/';
  99. const decoder = new EmojiDecoder(emojiUrl, emojiMap);
  100. export default {
  101. components: {
  102. emoji,more
  103. },
  104. data() {
  105. return {
  106. scrollIntoViewId:"",
  107. keyboardHeight:0,
  108. isEmoji:false,
  109. isMore:false,
  110. isKeyboard:false,
  111. text:"",
  112. scrollTop:0,
  113. msgData: {
  114. list:[],
  115. allLoaded:false, // 请求开关
  116. rows:20, //每页数量
  117. page:1, //页码
  118. flag:true, // 请求开关
  119. isSend:false,
  120. last:0
  121. },
  122. screenWidth:0,
  123. pushData: {
  124. obj:null,
  125. auth:"http://192.168.3.16:9881/plugin/webman/push/auth",
  126. url: 'ws://192.168.3.16:3131',
  127. },
  128. isFocus:false,
  129. chatData:{
  130. userId:"",
  131. self:"",
  132. service:""
  133. },
  134. isServiceUser:0, // 1已转人工 0未转人工
  135. }
  136. },
  137. onLoad() {
  138. app = this;
  139. const systemInfo = uni.getSystemInfoSync();
  140. app.screenWidth = systemInfo.screenWidth;
  141. // this.getMessage()
  142. this.getServiceData();
  143. uni.setNavigationBarTitle({
  144. title:"加载中..."
  145. })
  146. },
  147. computed: mapState({
  148. //显示时间
  149. renderMessageDate() {
  150. return (message, index) => {
  151. if (message.timestamp - this.msgData.list[index + 1]?.timestamp > 3 * 60 * 1000) {
  152. return formatDate(message.timestamp, 'timestamp');
  153. }
  154. return '';
  155. };
  156. },
  157. }),
  158. beforeDestroy() {
  159. cursor = 0;
  160. this.pushData.obj.disconnect()
  161. clearInterval(getSelectedTextRangeSetInterval);
  162. },
  163. methods: {
  164. previewImg(data){
  165. return openimg(1,[data])
  166. },
  167. async getServiceData(){
  168. var formData = {};
  169. const tokenPoi = uni.getStorageSync("contact");
  170. formData.poi = tokenPoi.poi_id;
  171. formData.page = app.msgData.page;
  172. formData.size = app.msgData.rows;
  173. var resp = await this.$api.chat.service.get(formData);
  174. if (resp.code == 0) {
  175. show(resp.msg);
  176. uni.navigateBack();
  177. return ;
  178. }
  179. uni.setNavigationBarTitle({
  180. title:resp.data.store.shopName
  181. })
  182. app.chatData.userId = resp.data.userId;
  183. app.chatData.self = resp.data.avatar;
  184. app.chatData.service = resp.data.service;
  185. var msgData = resp.data.msg.data;
  186. var pushData = [];
  187. var quickData = resp.data.quick;
  188. pushData.push({
  189. content: quickData.star_msg,
  190. userType: 'friend',
  191. avatar: resp.data.service,
  192. msgType:"quick",
  193. list:quickData.quick,
  194. messageId: Date.now(),
  195. timestamp: Date.now(),
  196. })
  197. if (msgData.length > 0) {
  198. msgData.forEach(item => pushData.push(app.formatMsg(item)))
  199. }
  200. this.msgData.list = pushData;
  201. app.msgData.page = 2;
  202. setTimeout(()=>{
  203. this.scrollToBottom()
  204. }, 150)
  205. app.initPush();
  206. },
  207. initPush(){
  208. var _this = this;
  209. this.pushData.obj = new Push({
  210. "url":this.pushData.url,
  211. "app_key":"265c33b73d5c04f918978577df2c48d2",
  212. "auth":this.pushData.auth
  213. });
  214. var user_channel = this.pushData.obj.subscribe('user-' + this.chatData.userId);
  215. user_channel.on('message', function (data) {
  216. const pushData = _this.formatMsg(data);
  217. _this.msgData.list.unshift(pushData);
  218. _this.scrollToBottom()
  219. })
  220. },
  221. quickQs(data){
  222. if (!data.answer) return ;
  223. app.msgData.list.unshift({
  224. content: data.answer,
  225. userType: 'friend',
  226. avatar: app.chatData.service,
  227. msgType:"text",
  228. list:[],
  229. messageId: Date.now(),
  230. timestamp: Date.now(),
  231. })
  232. app.scrollToBottom()
  233. },
  234. focus(){
  235. this.setCursor();
  236. },
  237. setCursor() {},
  238. scroll(){},
  239. // 滚动到顶部
  240. scrolltoupper(){
  241. if (this.msgData.allLoaded) return;
  242. this.getMessage()
  243. },
  244. scrolltolower() {},
  245. keyboardheightchange(e){
  246. const height = e.detail.height;
  247. if (height > 0) {
  248. this.isEmoji = false;
  249. this.isMore = false;
  250. this.isKeyboard = true;
  251. } else {
  252. this.isKeyboard = false;
  253. }
  254. this.keyboardHeight = height;
  255. setTimeout(()=>{
  256. this.scrollToBottom()
  257. }, 150)
  258. },
  259. formatMsg(item){
  260. var payload = "";
  261. if (item.type == 'text') {
  262. payload = item.content
  263. }
  264. if (item.type == 'order') {
  265. payload = JSON.parse(item.content)
  266. }
  267. if (item.type == 'pay') {
  268. payload = JSON.parse(item.content)
  269. }
  270. if (item.type == 'address') {
  271. payload = JSON.parse(item.content)
  272. }
  273. if (item.type == 'image') {
  274. payload = item.content
  275. }
  276. const message = {
  277. content: payload,
  278. userType: item.userType,
  279. avatar: item.avatar,
  280. msgType:item.type,
  281. list:[],
  282. messageId: item.msgId,
  283. timestamp: item.time,
  284. };
  285. return message
  286. },
  287. tapEmoji() {
  288. this.isEmoji = !this.isEmoji;
  289. if (this.isEmoji) {
  290. this.isKeyboard = true;
  291. }
  292. this.isMore = false;
  293. setTimeout(()=>{
  294. this.scrollToBottom()
  295. }, 150)
  296. },
  297. tapMore(){
  298. this.isMore = !this.isMore;
  299. if (this.isMore) {
  300. this.isKeyboard = true;
  301. }
  302. this.isEmoji = false;
  303. setTimeout(()=>{
  304. this.scrollToBottom()
  305. }, 150)
  306. },
  307. sendingMore(data){
  308. switch (data.type) {
  309. case 'img':
  310. this.sendImageMessage();
  311. break;
  312. case 'video':
  313. show("暂未开通");
  314. break;
  315. case 'order':
  316. show("暂未开通");
  317. // this.showOrder(1);
  318. break;
  319. }
  320. },
  321. scrollToBottom() {
  322. if (this.isEmoji) {
  323. var px = 600 * (app.screenWidth / 750)
  324. this.scrollTop = (this.msgData.list.length + px) * 1000
  325. } else if (this.isMore) {
  326. var px = 430 * (app.screenWidth / 750)
  327. this.scrollTop = (this.msgData.list.length + px) * 1000
  328. } else {
  329. this.scrollTop = (this.msgData.list.length + parseInt(this.keyboardHeight)) * 1000
  330. }
  331. },
  332. touchmove(){
  333. this.isFocus = false;
  334. this.isFocisEmojius = false;
  335. this.isEmoji = false;
  336. this.isMore = false;
  337. this.isKeyboard = false;
  338. this.keyboardHeight = 0
  339. },
  340. onEmoji(key) {
  341. const text = `${this.text.slice(0, cursor)}${key}${this.text.slice(cursor)}`;
  342. this.text = text;
  343. },
  344. sendingEmojiPack(){},
  345. // 删除表情
  346. deleteFn() {
  347. const str = this.text.charAt(this.text.length - 1);
  348. if (str === ']') {
  349. let metaChars = /\[.*?(\u4e00*\u597d*)\]/g;
  350. let xstr = '';
  351. this.text.replace(metaChars, (match) => {
  352. xstr = match;
  353. });
  354. var text = this.text;
  355. function del(str) {
  356. return text.slice(0, text.length - str.length);
  357. }
  358. this.text = del(xstr);
  359. } else {
  360. this.text = this.text.substring(0, this.text.length - 1);
  361. }
  362. },
  363. async sendingText(){
  364. if (app.text === '') return ;
  365. app.msgData.isSend = true;
  366. const sendMsg = app.text;
  367. app.text = ''
  368. var formData = {};
  369. const tokenPoi = uni.getStorageSync("contact");
  370. formData.groupId = tokenPoi.poi_id;
  371. formData.type = "text";
  372. formData.content = sendMsg;
  373. var resp = await app.$api.chat.sendText.post(formData);
  374. app.msgData.isSend = false;
  375. if (resp.code == 0) {
  376. return show(resp.msg);
  377. }
  378. app.sendMessage(sendMsg,"text");
  379. app.scrollToBottom()
  380. },
  381. sendMessage(content,type){
  382. const message = {
  383. content: content,
  384. userType: 'self',
  385. avatar: "/static/logo.jpg",
  386. msgType:type,
  387. list:[],
  388. messageId: Date.now(),
  389. timestamp: Date.now(),
  390. };
  391. app.msgData.list.unshift(message);
  392. },
  393. // 创建发送照片内容
  394. sendImageMessage() {
  395. uni.chooseImage({
  396. count: 1,
  397. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  398. success: async (res) => {
  399. res.tempFiles.forEach((file) => {
  400. console.log(file);
  401. this.createImageMessage(file);
  402. });
  403. },
  404. fail(e) {
  405. console.log("选择失败",e)
  406. }
  407. });
  408. },
  409. // 创建发送照片内容
  410. async createImageMessage(file) {
  411. var formData = {};
  412. const tokenPoi = uni.getStorageSync("contact");
  413. formData.groupId = tokenPoi.poi_id;
  414. formData.type = "image";
  415. var resp = await app.$api.chat.sendImage.post({'path':file.path,"param":formData});
  416. var respData = JSON.parse(resp);
  417. app.msgData.isSend = false;
  418. if (respData.code !== 1) {
  419. return show(respData.msg);
  420. }
  421. app.sendMessage(respData.data.path,"image");
  422. app.scrollToBottom()
  423. },
  424. getMessage(){
  425. let get = async ()=>{
  426. this.msgData.allLoaded = true;
  427. var formData = {};
  428. const tokenPoi = uni.getStorageSync("contact");
  429. formData.poi = tokenPoi.poi_id;
  430. formData.page = app.msgData.page;
  431. formData.size = app.msgData.rows;
  432. let resp = await app.$api.chat.msg.get(formData);
  433. var msgData = resp.data.data.data;
  434. var pushData = [];
  435. if (msgData.length > 0) {
  436. msgData.forEach(item => pushData.push(app.formatMsg(item)))
  437. }
  438. const selector = `msg-${msgData?.[0]?.messageId}`;;
  439. this.msgData.list = this.msgData.list.concat(pushData);
  440. // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  441. this.$nextTick(()=>{
  442. // 设置当前滚动的位置
  443. this.scrollIntoViewId = selector;
  444. if(msgData.length < this.msgData.rows){
  445. this.msgData.allLoaded = true;
  446. this.msgData.flag = false;
  447. // 当前消息数据条数小于请求要求条数时,则无更多消息,不再允许请求。
  448. // 可在此处编写无更多消息数据时的逻辑
  449. }else{
  450. this.msgData.flag = true;
  451. this.msgData.allLoaded = false;
  452. this.msgData.page ++;
  453. }
  454. })
  455. }
  456. get();
  457. },
  458. renderTextMessage(text) {
  459. if (!text) return "";
  460. text = text.replace(/\n/g, '');
  461. if (!text) return '<span>' + '[未知内容]' + '</span>';
  462. return '<span>' + decoder.decode(text) + '</span>';
  463. },
  464. }
  465. }
  466. </script>
  467. <style lang="scss" scoped>
  468. .chat-content{
  469. position: fixed;
  470. z-index: 1;
  471. top: 0;
  472. left: 0;
  473. bottom: 0;
  474. right: 0;
  475. background-color: #ededed;
  476. display: flex;
  477. flex-direction: column;
  478. z-index: 98;
  479. }
  480. .m-image {
  481. image {
  482. width: 140rpx;
  483. }
  484. }
  485. .m-address{
  486. display: flex;align-items: center;gap: 20rpx;min-width: 70vw;
  487. &.self{
  488. transform: rotate(180);
  489. }
  490. .image {
  491. image{width: 120upx;}
  492. }
  493. .infos{
  494. flex: 1;overflow: hidden;
  495. .title{font-size: 28upx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}
  496. .desc{font-size: 20upx;color: #999;margin-top: 20rpx;}
  497. .price{font-size: 28upx;color: #f00;margin-top: 20rpx;}
  498. }
  499. }
  500. .send-load {
  501. position: fixed;
  502. top: 0;
  503. left: 0;
  504. right: 0;
  505. z-index: 99;
  506. text-align: center;
  507. font-size: 28rpx;
  508. color: $main-text-color;
  509. background-color: #f4f4f5;
  510. padding: 10rpx 0;
  511. }
  512. .msgLoading{
  513. text-align: center;
  514. padding: 10rpx 0;
  515. }
  516. .chat-body{
  517. height: 0;
  518. flex: 1;
  519. .scroll-view-msg{
  520. display: flex;
  521. flex-direction: column-reverse;
  522. flex-wrap: nowrap;
  523. align-content: flex-start;
  524. justify-content: flex-end;
  525. align-items: stretch;
  526. }
  527. .message {
  528. display: flex;
  529. align-items: flex-start;
  530. margin-bottom: 30rpx;
  531. .quickList{
  532. margin-top: 10upx;
  533. .list-items {
  534. color: blue;
  535. font-size: 28upx;
  536. line-height: 48upx;
  537. }
  538. }
  539. .avatar {
  540. width: 80rpx;
  541. height: 80rpx;
  542. border-radius: 10rpx;
  543. margin-right: 30rpx;
  544. }
  545. .message-con {
  546. min-height: 80rpx;
  547. max-width: 80vw;
  548. box-sizing: border-box;
  549. font-size: 28rpx;
  550. line-height: 1.3;
  551. padding: 20rpx;
  552. border-radius: 10rpx;
  553. background: #fff;
  554. image {
  555. // width: 200rpx;
  556. }
  557. }
  558. &.self {
  559. justify-content: flex-end;
  560. .avatar {
  561. margin: 0 0 0 30rpx;
  562. }
  563. .message-con {
  564. position: relative;
  565. &::after {
  566. position: absolute;
  567. content: '';
  568. width: 0;
  569. height: 0;
  570. border: 16rpx solid transparent;
  571. border-left: 16rpx solid #fff;
  572. right: -28rpx;
  573. top: 24rpx;
  574. }
  575. }
  576. }
  577. &.friend {
  578. .message-con {
  579. position: relative;
  580. &::after {
  581. position: absolute;
  582. content: '';
  583. width: 0;
  584. height: 0;
  585. border: 16rpx solid transparent;
  586. border-right: 16rpx solid #fff;
  587. left: -28rpx;
  588. top: 24rpx;
  589. }
  590. }
  591. }
  592. }
  593. }
  594. .tool {
  595. background: #fff;
  596. padding: 20rpx 0 20rpx 0;
  597. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom)/2) !important;
  598. padding-bottom: calc(20rpx + env(safe-area-inset-bottom)/2) !important;
  599. .tool-option{
  600. display: flex;
  601. align-items: flex-start;
  602. box-sizing: border-box;
  603. padding: 0 20rpx;
  604. }
  605. .tool-bar {
  606. display: flex;
  607. align-items: center;
  608. gap: 20rpx;
  609. padding: 0 20rpx;
  610. .bar-item {
  611. display: flex;
  612. align-items: center;
  613. font-size: 28rpx;
  614. gap: 10rpx;
  615. color: #333;
  616. margin-bottom: 20rpx;
  617. image{
  618. width: 40rpx;
  619. height: 40rpx;
  620. }
  621. }
  622. }
  623. .input-text{
  624. flex: 1;width: 100%;
  625. box-sizing: border-box;
  626. padding: 10rpx 14rpx;
  627. min-height: 84rpx;
  628. max-height: 300rpx;
  629. overflow: auto;
  630. border-radius: 10rpx;
  631. background-color: #eee;
  632. .input{
  633. margin: 10rpx 0;
  634. width: 100%;
  635. font-size: 28upx;
  636. height: 100%;
  637. background-color: #eee;
  638. min-height: 48rpx;
  639. max-height: 300rpx;
  640. }
  641. }
  642. .check-img{
  643. display: flex;align-items: center;justify-content: center;
  644. .check-icon{width: 84rpx;height: 84rpx;display: flex;align-items: center;justify-content: center;}
  645. image{width: 75%;height: 75%;}
  646. }
  647. .thumb {
  648. width: 64rpx;
  649. }
  650. }
  651. .time {
  652. width: 100%;
  653. color: #a3a3a3;
  654. line-height: 100rpx;
  655. text-align: center;
  656. font-size: 24rpx;
  657. }
  658. .isrobot{
  659. color: blue;
  660. font-size: 28upx;
  661. line-height: 48upx;
  662. margin-top: 10rpx;
  663. }
  664. </style>