chat.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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="item.avatar" 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="item.avatar" 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. import emoji from '@/components/chat/emoji';
  95. import more from '@/components/chat/more';
  96. import { EmojiDecoder, emojiMap } from '@/utils/js/EmojiDecoder.js';
  97. const emojiUrl = 'https://imgcache.qq.com/open/qcloud/tim/assets/emoji/';
  98. const decoder = new EmojiDecoder(emojiUrl, emojiMap);
  99. export default {
  100. components: {
  101. emoji,more
  102. },
  103. data() {
  104. return {
  105. scrollIntoViewId:"",
  106. keyboardHeight:0,
  107. isEmoji:false,
  108. isMore:false,
  109. isKeyboard:false,
  110. text:"",
  111. scrollTop:0,
  112. msgData: {
  113. list:[],
  114. allLoaded:false, // 请求开关
  115. rows:20, //每页数量
  116. page:1, //页码
  117. flag:true, // 请求开关
  118. isSend:false,
  119. last:0
  120. },
  121. screenWidth:0,
  122. pushData: {
  123. obj:null,
  124. auth:"https://tran.jsshuita.cn/plugin/webman/push/auth",
  125. url: 'wss://tran.jsshuita.cn/ws',
  126. },
  127. isFocus:false
  128. }
  129. },
  130. onLoad() {
  131. app = this;
  132. const systemInfo = uni.getSystemInfoSync();
  133. app.screenWidth = systemInfo.screenWidth;
  134. this.getMessage()
  135. uni.setNavigationBarTitle({
  136. title:"与xxx对话"
  137. })
  138. },
  139. computed: mapState({
  140. //显示时间
  141. renderMessageDate() {
  142. return (message, index) => {
  143. if (this.msgData.list[index + 1]?.timestamp - message.timestamp > 3 * 60 * 1000) {
  144. return formatDate(message.timestamp, 'timestamp');
  145. }
  146. return '';
  147. };
  148. },
  149. }),
  150. beforeDestroy() {
  151. cursor = 0;
  152. clearInterval(getSelectedTextRangeSetInterval);
  153. },
  154. methods: {
  155. focus(){
  156. this.setCursor();
  157. },
  158. setCursor() {
  159. },
  160. scroll(){},
  161. // 滚动到顶部
  162. scrolltoupper(){
  163. if (this.msgData.allLoaded) return;
  164. this.getMessage()
  165. },
  166. scrolltolower() {},
  167. keyboardheightchange(e){
  168. const height = e.detail.height;
  169. if (height > 0) {
  170. this.isEmoji = false;
  171. this.isMore = false;
  172. this.isKeyboard = true;
  173. } else {
  174. this.isKeyboard = false;
  175. }
  176. this.keyboardHeight = height;
  177. setTimeout(()=>{
  178. this.scrollToBottom()
  179. }, 150)
  180. },
  181. tapEmoji() {
  182. this.isEmoji = !this.isEmoji;
  183. if (this.isEmoji) {
  184. this.isKeyboard = true;
  185. }
  186. this.isMore = false;
  187. setTimeout(()=>{
  188. this.scrollToBottom()
  189. }, 150)
  190. },
  191. tapMore(){
  192. this.isMore = !this.isMore;
  193. if (this.isMore) {
  194. this.isKeyboard = true;
  195. }
  196. this.isEmoji = false;
  197. setTimeout(()=>{
  198. this.scrollToBottom()
  199. }, 150)
  200. },
  201. sendingMore(data){
  202. switch (data.type) {
  203. case 'img':
  204. show("暂未开通");
  205. // this.sendImageMessage();
  206. break;
  207. case 'video':
  208. show("暂未开通");
  209. break;
  210. case 'order':
  211. show("暂未开通");
  212. // this.showOrder(1);
  213. break;
  214. }
  215. },
  216. scrollToBottom() {
  217. if (this.isEmoji) {
  218. var px = 600 * (app.screenWidth / 750)
  219. this.scrollTop = (this.msgData.list.length + px) * 1000
  220. } else if (this.isMore) {
  221. var px = 430 * (app.screenWidth / 750)
  222. this.scrollTop = (this.msgData.list.length + px) * 1000
  223. } else {
  224. this.scrollTop = (this.msgData.list.length + parseInt(this.keyboardHeight)) * 1000
  225. }
  226. },
  227. touchmove(){
  228. this.isFocus = false;
  229. this.isFocisEmojius = false;
  230. this.isEmoji = false;
  231. this.isMore = false;
  232. this.isKeyboard = false;
  233. this.keyboardHeight = 0
  234. },
  235. onEmoji(key) {
  236. const text = `${this.text.slice(0, cursor)}${key}${this.text.slice(cursor)}`;
  237. this.text = text;
  238. },
  239. sendingEmojiPack(){},
  240. // 删除表情
  241. deleteFn() {
  242. const str = this.text.charAt(this.text.length - 1);
  243. if (str === ']') {
  244. let metaChars = /\[.*?(\u4e00*\u597d*)\]/g;
  245. let xstr = '';
  246. this.text.replace(metaChars, (match) => {
  247. xstr = match;
  248. });
  249. var text = this.text;
  250. function del(str) {
  251. return text.slice(0, text.length - str.length);
  252. }
  253. this.text = del(xstr);
  254. } else {
  255. this.text = this.text.substring(0, this.text.length - 1);
  256. }
  257. },
  258. sendingText(){
  259. if (app.text === '') return ;
  260. app.msgData.isSend = true;
  261. const sendMsg = app.text;
  262. app.text = ''
  263. setTimeout(function(){
  264. app.msgData.isSend = false;
  265. app.sendMessage(sendMsg,"text");
  266. app.scrollToBottom()
  267. },2000)
  268. },
  269. sendMessage(content,type){
  270. const message = {
  271. content: content,
  272. userType: 'self',
  273. avatar: "/static/logo.jpg",
  274. msgType:type,
  275. list:[],
  276. messageId: Date.now(),
  277. timestamp: Date.now(),
  278. };
  279. app.msgData.list.unshift(message);
  280. },
  281. getMessage(){
  282. let get = async ()=>{
  283. this.msgData.allLoaded = true;
  284. let data = await getHistoryMsg({
  285. page:this.msgData.page,
  286. rows: this.msgData.page == 3 ? 5 : this.msgData.rows
  287. });
  288. const selector = `msg-${data?.[0]?.messageId}`;;
  289. this.msgData.list = this.msgData.list.concat(data);
  290. // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  291. this.$nextTick(()=>{
  292. // 设置当前滚动的位置
  293. this.scrollIntoViewId = selector;
  294. if(data.length < this.msgData.rows){
  295. this.msgData.allLoaded = true;
  296. this.msgData.flag = false;
  297. // 当前消息数据条数小于请求要求条数时,则无更多消息,不再允许请求。
  298. // 可在此处编写无更多消息数据时的逻辑
  299. }else{
  300. this.msgData.flag = true;
  301. this.msgData.allLoaded = false;
  302. this.msgData.page ++;
  303. }
  304. })
  305. }
  306. get();
  307. },
  308. renderTextMessage(text) {
  309. if (!text) return "";
  310. text = text.replace(/\n/g, '');
  311. if (!text) return '<span>' + '[未知内容]' + '</span>';
  312. return '<span>' + decoder.decode(text) + '</span>';
  313. },
  314. }
  315. }
  316. </script>
  317. <style lang="scss" scoped>
  318. .chat-content{
  319. position: fixed;
  320. z-index: 1;
  321. top: 0;
  322. left: 0;
  323. bottom: 0;
  324. right: 0;
  325. background-color: #ededed;
  326. display: flex;
  327. flex-direction: column;
  328. z-index: 98;
  329. }
  330. .m-address{
  331. display: flex;align-items: center;gap: 20rpx;min-width: 70vw;
  332. &.self{
  333. transform: rotate(180);
  334. }
  335. .image {
  336. image{width: 120upx;}
  337. }
  338. .infos{
  339. flex: 1;overflow: hidden;
  340. .title{font-size: 28upx;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;}
  341. .desc{font-size: 20upx;color: #999;margin-top: 20rpx;}
  342. .price{font-size: 28upx;color: #f00;margin-top: 20rpx;}
  343. }
  344. }
  345. .send-load {
  346. position: fixed;
  347. top: 0;
  348. left: 0;
  349. right: 0;
  350. z-index: 99;
  351. text-align: center;
  352. font-size: 28rpx;
  353. color: $main-text-color;
  354. background-color: #f4f4f5;
  355. padding: 10rpx 0;
  356. }
  357. .msgLoading{
  358. text-align: center;
  359. padding: 10rpx 0;
  360. }
  361. .chat-body{
  362. height: 0;
  363. flex: 1;
  364. .scroll-view-msg{
  365. display: flex;
  366. flex-direction: column-reverse;
  367. flex-wrap: nowrap;
  368. align-content: flex-start;
  369. justify-content: flex-end;
  370. align-items: stretch;
  371. }
  372. .message {
  373. display: flex;
  374. align-items: flex-start;
  375. margin-bottom: 30rpx;
  376. .quickList{
  377. margin-top: 10upx;
  378. .list-items {
  379. color: blue;
  380. font-size: 28upx;
  381. line-height: 48upx;
  382. }
  383. }
  384. .avatar {
  385. width: 80rpx;
  386. height: 80rpx;
  387. border-radius: 10rpx;
  388. margin-right: 30rpx;
  389. }
  390. .message-con {
  391. min-height: 80rpx;
  392. max-width: 80vw;
  393. box-sizing: border-box;
  394. font-size: 28rpx;
  395. line-height: 1.3;
  396. padding: 20rpx;
  397. border-radius: 10rpx;
  398. background: #fff;
  399. image {
  400. // width: 200rpx;
  401. }
  402. }
  403. &.self {
  404. justify-content: flex-end;
  405. .avatar {
  406. margin: 0 0 0 30rpx;
  407. }
  408. .message-con {
  409. position: relative;
  410. &::after {
  411. position: absolute;
  412. content: '';
  413. width: 0;
  414. height: 0;
  415. border: 16rpx solid transparent;
  416. border-left: 16rpx solid #fff;
  417. right: -28rpx;
  418. top: 24rpx;
  419. }
  420. }
  421. }
  422. &.friend {
  423. .message-con {
  424. position: relative;
  425. &::after {
  426. position: absolute;
  427. content: '';
  428. width: 0;
  429. height: 0;
  430. border: 16rpx solid transparent;
  431. border-right: 16rpx solid #fff;
  432. left: -28rpx;
  433. top: 24rpx;
  434. }
  435. }
  436. }
  437. }
  438. }
  439. .tool {
  440. background: #fff;
  441. padding: 20rpx 0 20rpx 0;
  442. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom)/2) !important;
  443. padding-bottom: calc(20rpx + env(safe-area-inset-bottom)/2) !important;
  444. .tool-option{
  445. display: flex;
  446. align-items: flex-start;
  447. box-sizing: border-box;
  448. padding: 0 20rpx;
  449. }
  450. .tool-bar {
  451. display: flex;
  452. align-items: center;
  453. gap: 20rpx;
  454. padding: 0 20rpx;
  455. .bar-item {
  456. display: flex;
  457. align-items: center;
  458. font-size: 28rpx;
  459. gap: 10rpx;
  460. color: #333;
  461. margin-bottom: 20rpx;
  462. image{
  463. width: 40rpx;
  464. height: 40rpx;
  465. }
  466. }
  467. }
  468. .input-text{
  469. flex: 1;width: 100%;
  470. box-sizing: border-box;
  471. padding: 10rpx 14rpx;
  472. min-height: 84rpx;
  473. max-height: 300rpx;
  474. overflow: auto;
  475. border-radius: 10rpx;
  476. background-color: #eee;
  477. .input{
  478. margin: 10rpx 0;
  479. width: 100%;
  480. font-size: 28upx;
  481. height: 100%;
  482. background-color: #eee;
  483. min-height: 48rpx;
  484. max-height: 300rpx;
  485. }
  486. }
  487. .check-img{
  488. display: flex;align-items: center;justify-content: center;
  489. .check-icon{width: 84rpx;height: 84rpx;display: flex;align-items: center;justify-content: center;}
  490. image{width: 75%;height: 75%;}
  491. }
  492. .thumb {
  493. width: 64rpx;
  494. }
  495. }
  496. .time {
  497. width: 100%;
  498. color: #a3a3a3;
  499. line-height: 100rpx;
  500. text-align: center;
  501. font-size: 24rpx;
  502. }
  503. </style>