chat.vue 10 KB

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