chat.vue 12 KB

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