chat.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 (this.msgData.list[index + 1]?.timestamp - message.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. async getServiceData(){
  165. var formData = {};
  166. const tokenPoi = uni.getStorageSync("contact");
  167. formData.poi = tokenPoi.poi_id;
  168. formData.page = app.msgData.page;
  169. formData.size = app.msgData.rows;
  170. var resp = await this.$api.chat.service.get(formData);
  171. if (resp.code == 0) {
  172. show(resp.msg);
  173. uni.navigateBack();
  174. return ;
  175. }
  176. uni.setNavigationBarTitle({
  177. title:resp.data.store.shopName
  178. })
  179. app.chatData.userId = resp.data.userId;
  180. app.chatData.self = resp.data.avatar;
  181. app.chatData.service = resp.data.service;
  182. var msgData = resp.data.msg.data;
  183. var pushData = [];
  184. var quickData = resp.data.quick;
  185. pushData.push({
  186. content: quickData.star_msg,
  187. userType: 'friend',
  188. avatar: resp.data.service,
  189. msgType:"quick",
  190. list:quickData.quick,
  191. messageId: Date.now(),
  192. timestamp: Date.now(),
  193. })
  194. if (msgData.length > 0) {
  195. msgData.forEach(item => pushData.push(app.formatMsg(item)))
  196. }
  197. this.msgData.list = pushData;
  198. app.msgData.page = 2;
  199. setTimeout(()=>{
  200. this.scrollToBottom()
  201. }, 150)
  202. app.initPush();
  203. },
  204. initPush(){
  205. var _this = this;
  206. this.pushData.obj = new Push({
  207. "url":this.pushData.url,
  208. "app_key":"265c33b73d5c04f918978577df2c48d2",
  209. "auth":this.pushData.auth
  210. });
  211. var user_channel = this.pushData.obj.subscribe('user-' + this.chatData.userId);
  212. user_channel.on('message', function (data) {
  213. console.log(data);
  214. // _this.formatMsg(data);
  215. })
  216. },
  217. quickQs(data){
  218. if (!data.answer) return ;
  219. app.msgData.list.unshift({
  220. content: data.answer,
  221. userType: 'friend',
  222. avatar: app.chatData.service,
  223. msgType:"text",
  224. list:[],
  225. messageId: Date.now(),
  226. timestamp: Date.now(),
  227. })
  228. app.scrollToBottom()
  229. },
  230. focus(){
  231. this.setCursor();
  232. },
  233. setCursor() {},
  234. scroll(){},
  235. // 滚动到顶部
  236. scrolltoupper(){
  237. if (this.msgData.allLoaded) return;
  238. this.getMessage()
  239. },
  240. scrolltolower() {},
  241. keyboardheightchange(e){
  242. const height = e.detail.height;
  243. if (height > 0) {
  244. this.isEmoji = false;
  245. this.isMore = false;
  246. this.isKeyboard = true;
  247. } else {
  248. this.isKeyboard = false;
  249. }
  250. this.keyboardHeight = height;
  251. setTimeout(()=>{
  252. this.scrollToBottom()
  253. }, 150)
  254. },
  255. formatMsg(item){
  256. var payload = "";
  257. if (item.type == 'text') {
  258. payload = item.content
  259. }
  260. if (item.type == 'order') {
  261. var order = JSON.parse(item.content)
  262. payload = order
  263. }
  264. if (item.type == 'pay') {
  265. var order = JSON.parse(item.content)
  266. payload = order
  267. }
  268. if (item.type == 'address') {
  269. var order = JSON.parse(item.content)
  270. payload = order
  271. }
  272. if (item.type == 'image') {
  273. payload = item.content
  274. }
  275. const message = {
  276. content: payload,
  277. userType: item.userType,
  278. avatar: item.avatar,
  279. msgType:item.type,
  280. list:[],
  281. messageId: item.msgId,
  282. timestamp: item.time,
  283. };
  284. return message
  285. },
  286. tapEmoji() {
  287. this.isEmoji = !this.isEmoji;
  288. if (this.isEmoji) {
  289. this.isKeyboard = true;
  290. }
  291. this.isMore = false;
  292. setTimeout(()=>{
  293. this.scrollToBottom()
  294. }, 150)
  295. },
  296. tapMore(){
  297. this.isMore = !this.isMore;
  298. if (this.isMore) {
  299. this.isKeyboard = true;
  300. }
  301. this.isEmoji = false;
  302. setTimeout(()=>{
  303. this.scrollToBottom()
  304. }, 150)
  305. },
  306. sendingMore(data){
  307. switch (data.type) {
  308. case 'img':
  309. show("暂未开通");
  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. getMessage(){
  394. let get = async ()=>{
  395. this.msgData.allLoaded = true;
  396. var formData = {};
  397. const tokenPoi = uni.getStorageSync("contact");
  398. formData.poi = tokenPoi.poi_id;
  399. formData.page = app.msgData.page;
  400. formData.size = app.msgData.rows;
  401. let data = await app.$api.chat.msg.get(formData);
  402. // var msgData = resp.data.msg.data;
  403. // var pushData = [];
  404. // if (msgData.length > 0) {
  405. // msgData.forEach(item => pushData.push(app.formatMsg(item)))
  406. // }
  407. // let data = await getHistoryMsg({
  408. // page:this.msgData.page,
  409. // rows: this.msgData.page == 3 ? 5 : this.msgData.rows
  410. // });
  411. // const selector = `msg-${data?.[0]?.messageId}`;;
  412. // this.msgData.list = this.msgData.list.concat(data);
  413. // // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  414. // this.$nextTick(()=>{
  415. // // 设置当前滚动的位置
  416. // this.scrollIntoViewId = selector;
  417. // if(data.length < this.msgData.rows){
  418. // this.msgData.allLoaded = true;
  419. // this.msgData.flag = false;
  420. // // 当前消息数据条数小于请求要求条数时,则无更多消息,不再允许请求。
  421. // // 可在此处编写无更多消息数据时的逻辑
  422. // }else{
  423. // this.msgData.flag = true;
  424. // this.msgData.allLoaded = false;
  425. // this.msgData.page ++;
  426. // }
  427. // })
  428. }
  429. get();
  430. },
  431. renderTextMessage(text) {
  432. if (!text) return "";
  433. text = text.replace(/\n/g, '');
  434. if (!text) return '<span>' + '[未知内容]' + '</span>';
  435. return '<span>' + decoder.decode(text) + '</span>';
  436. },
  437. }
  438. }
  439. </script>
  440. <style lang="scss" scoped>
  441. .chat-content{
  442. position: fixed;
  443. z-index: 1;
  444. top: 0;
  445. left: 0;
  446. bottom: 0;
  447. right: 0;
  448. background-color: #ededed;
  449. display: flex;
  450. flex-direction: column;
  451. z-index: 98;
  452. }
  453. .m-address{
  454. display: flex;align-items: center;gap: 20rpx;min-width: 70vw;
  455. &.self{
  456. transform: rotate(180);
  457. }
  458. .image {
  459. image{width: 120upx;}
  460. }
  461. .infos{
  462. flex: 1;overflow: hidden;
  463. .title{font-size: 28upx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}
  464. .desc{font-size: 20upx;color: #999;margin-top: 20rpx;}
  465. .price{font-size: 28upx;color: #f00;margin-top: 20rpx;}
  466. }
  467. }
  468. .send-load {
  469. position: fixed;
  470. top: 0;
  471. left: 0;
  472. right: 0;
  473. z-index: 99;
  474. text-align: center;
  475. font-size: 28rpx;
  476. color: $main-text-color;
  477. background-color: #f4f4f5;
  478. padding: 10rpx 0;
  479. }
  480. .msgLoading{
  481. text-align: center;
  482. padding: 10rpx 0;
  483. }
  484. .chat-body{
  485. height: 0;
  486. flex: 1;
  487. .scroll-view-msg{
  488. display: flex;
  489. flex-direction: column-reverse;
  490. flex-wrap: nowrap;
  491. align-content: flex-start;
  492. justify-content: flex-end;
  493. align-items: stretch;
  494. }
  495. .message {
  496. display: flex;
  497. align-items: flex-start;
  498. margin-bottom: 30rpx;
  499. .quickList{
  500. margin-top: 10upx;
  501. .list-items {
  502. color: blue;
  503. font-size: 28upx;
  504. line-height: 48upx;
  505. }
  506. }
  507. .avatar {
  508. width: 80rpx;
  509. height: 80rpx;
  510. border-radius: 10rpx;
  511. margin-right: 30rpx;
  512. }
  513. .message-con {
  514. min-height: 80rpx;
  515. max-width: 80vw;
  516. box-sizing: border-box;
  517. font-size: 28rpx;
  518. line-height: 1.3;
  519. padding: 20rpx;
  520. border-radius: 10rpx;
  521. background: #fff;
  522. image {
  523. // width: 200rpx;
  524. }
  525. }
  526. &.self {
  527. justify-content: flex-end;
  528. .avatar {
  529. margin: 0 0 0 30rpx;
  530. }
  531. .message-con {
  532. position: relative;
  533. &::after {
  534. position: absolute;
  535. content: '';
  536. width: 0;
  537. height: 0;
  538. border: 16rpx solid transparent;
  539. border-left: 16rpx solid #fff;
  540. right: -28rpx;
  541. top: 24rpx;
  542. }
  543. }
  544. }
  545. &.friend {
  546. .message-con {
  547. position: relative;
  548. &::after {
  549. position: absolute;
  550. content: '';
  551. width: 0;
  552. height: 0;
  553. border: 16rpx solid transparent;
  554. border-right: 16rpx solid #fff;
  555. left: -28rpx;
  556. top: 24rpx;
  557. }
  558. }
  559. }
  560. }
  561. }
  562. .tool {
  563. background: #fff;
  564. padding: 20rpx 0 20rpx 0;
  565. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom)/2) !important;
  566. padding-bottom: calc(20rpx + env(safe-area-inset-bottom)/2) !important;
  567. .tool-option{
  568. display: flex;
  569. align-items: flex-start;
  570. box-sizing: border-box;
  571. padding: 0 20rpx;
  572. }
  573. .tool-bar {
  574. display: flex;
  575. align-items: center;
  576. gap: 20rpx;
  577. padding: 0 20rpx;
  578. .bar-item {
  579. display: flex;
  580. align-items: center;
  581. font-size: 28rpx;
  582. gap: 10rpx;
  583. color: #333;
  584. margin-bottom: 20rpx;
  585. image{
  586. width: 40rpx;
  587. height: 40rpx;
  588. }
  589. }
  590. }
  591. .input-text{
  592. flex: 1;width: 100%;
  593. box-sizing: border-box;
  594. padding: 10rpx 14rpx;
  595. min-height: 84rpx;
  596. max-height: 300rpx;
  597. overflow: auto;
  598. border-radius: 10rpx;
  599. background-color: #eee;
  600. .input{
  601. margin: 10rpx 0;
  602. width: 100%;
  603. font-size: 28upx;
  604. height: 100%;
  605. background-color: #eee;
  606. min-height: 48rpx;
  607. max-height: 300rpx;
  608. }
  609. }
  610. .check-img{
  611. display: flex;align-items: center;justify-content: center;
  612. .check-icon{width: 84rpx;height: 84rpx;display: flex;align-items: center;justify-content: center;}
  613. image{width: 75%;height: 75%;}
  614. }
  615. .thumb {
  616. width: 64rpx;
  617. }
  618. }
  619. .time {
  620. width: 100%;
  621. color: #a3a3a3;
  622. line-height: 100rpx;
  623. text-align: center;
  624. font-size: 24rpx;
  625. }
  626. .isrobot{
  627. color: blue;
  628. font-size: 28upx;
  629. line-height: 48upx;
  630. margin-top: 10rpx;
  631. }
  632. </style>