msg.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. <template>
  2. <view class="page">
  3. <view class="flex_c pages" @touchmove="touchmove">
  4. <scroll-view class="scroll-view flex1" scroll-y scroll-with-animation :scroll-top="top" scroll-with-animation @scroll="scroll" @scrolltoupper="scrolltoupper" @scrolltolower="scrolltolower">
  5. <view style="padding: 30rpx 30rpx">
  6. <block v-for="(item,index) in list" :key="index">
  7. <view class="text_26 color__ time">
  8. {{ renderMessageDate(item, index) }}
  9. </view>
  10. <view class="message" :class="[item.userType]">
  11. <image :src="item.avatar" v-if="item.userType === 'friend'" class="avatar" mode="widthFix"></image>
  12. <view class="content" v-if="item.messageType === 'image'">
  13. <image :src="item.content" mode="widthFix"></image>
  14. </view>
  15. <view class="content" v-else>
  16. <view class="" v-if="item.msgType == 'quick'">
  17. <view class="quickCon">{{item.content}}</view>
  18. <view class="quickList" v-if="item.list.length > 0">
  19. <view class="list-items" v-for="(qItem,indx) in item.list" @click="quickQs(qItem)">{{qItem.question}}</view>
  20. </view>
  21. </view>
  22. <view class="" v-else-if="item.msgType == 'address'">
  23. 1
  24. </view>
  25. <view class="" v-else-if="item.msgType == 'order'">
  26. 2
  27. </view>
  28. <view class="" v-else-if="item.msgType == 'image'">
  29. 3
  30. </view>
  31. <view class="" v-else-if="item.msgType == 'pay'">
  32. 4
  33. </view>
  34. <view v-else>
  35. <view v-html="renderTextMessage(item.content)"></view>
  36. </view>
  37. </view>
  38. <image :src="item.avatar" v-if="item.userType === 'self'" class="avatar" mode="widthFix"></image>
  39. </view>
  40. </block>
  41. </view>
  42. </scroll-view>
  43. <view class="tool">
  44. <view class="tool-option">
  45. <view class="input-text">
  46. <textarea class="input" placeholder="输入点什么呢..." auto-height="true" confirm-type="send" type="text"
  47. :maxlength="-1" :adjust-position="false" v-model="text" confirm-hold
  48. :show-confirm-bar="false" @confirm="sendingText" @keyboardheightchange="keyboardheightchange" />
  49. </view>
  50. <view class="check-img">
  51. <view class="check-icon" @click="tapEmoji"><image src="@/static/image/face.svg" mode="aspectFill"></image></view>
  52. <view class="check-icon" @click="tapMore"><image src="@/static/image/more.svg" mode="aspectFill"></image></view>
  53. </view>
  54. </view>
  55. </view>
  56. <template v-if="keyboardHeight>0">
  57. <view class="keyboardheight" :style="{ height: keyboardHeight + 'px' }"></view>
  58. </template>
  59. <view>
  60. <emoji v-model="isEmoji" @onEmoji="onEmoji" @deleteFn="deleteFn" @sendingText="sendingText" @sendingEmojiPack="sendingEmojiPack"></emoji>
  61. </view>
  62. </view>
  63. </view>
  64. </template>
  65. <script>
  66. var {Push} = require("@/static/js/push.js")
  67. import * as Api from "@/static/api/msg.js";
  68. import form from "@/static/js/form.js";
  69. let cursor = 0; //输入框光标
  70. var app;
  71. import emoji from '@/components/bottom-operation/emoji.vue';
  72. import { mapState } from 'vuex';
  73. import {
  74. show,
  75. formatDate,
  76. throttle,
  77. openimg,
  78. getLocation,
  79. to as tofn
  80. } from '@/utils/index.js';
  81. import { EmojiDecoder, emojiMap } from '@/utils/EmojiDecoder.js';
  82. const emojiUrl = 'https://imgcache.qq.com/open/qcloud/tim/assets/emoji/';
  83. const decoder = new EmojiDecoder(emojiUrl, emojiMap);
  84. export default {
  85. components: {
  86. emoji
  87. },
  88. data() {
  89. return {
  90. text: '',
  91. list: [],
  92. allLoaded:false,
  93. msgPage:2,
  94. top: 0,
  95. isEmoji:false,
  96. isMore:false,
  97. keyboardHeight:0,
  98. pushObj:null,
  99. wsAuth:"https://tran.jsshuita.cn/plugin/webman/push/auth",
  100. wsUrl: 'wss://tran.jsshuita.cn/ws',
  101. _friendAvatar:"",
  102. _selfAvatar:"",
  103. isKeyboard:false,
  104. screenWidth:0
  105. };
  106. },
  107. computed: mapState({
  108. //显示时间
  109. renderMessageDate() {
  110. return (message, index) => {
  111. if (message.timestamp - this.list[index + 1]?.timestamp > 3 * 60 * 1000) {
  112. return formatDate(message.timestamp, 'timestamp');
  113. }
  114. return '';
  115. };
  116. },
  117. }),
  118. onLoad(options) {
  119. app = this;
  120. app.optionsData = options;
  121. uni.setNavigationBarTitle({
  122. title:"加载中..."
  123. });
  124. const systemInfo = uni.getSystemInfoSync();
  125. app.screenWidth = systemInfo.screenWidth;
  126. app.getService(options)
  127. },
  128. beforeDestroy() {
  129. cursor = 0;
  130. this.pushObj.disconnect()
  131. },
  132. methods: {
  133. scroll(){},
  134. // 滚动到底部
  135. scrolltolower() {
  136. // if (this.history.allLoaded) return;
  137. // console.log('触底')
  138. // this.getMoreMsg()
  139. // this.loadHistoryMessage();
  140. },
  141. // 滚动到顶部
  142. scrolltoupper() {
  143. if (this.allLoaded) return;
  144. console.log('滚动到顶部')
  145. this.getMoreMsg()
  146. },
  147. getMoreMsg(){
  148. this.allLoaded = true;
  149. var formData = this.optionsData;
  150. const tokenPoi = uni.getStorageSync("contact");
  151. formData.page = this.msgPage;
  152. formData.poi = tokenPoi.poi_id;
  153. Api.msg(formData).then((res)=>{
  154. if(res.code == 0) {
  155. return this.$dialog.showSuccess(res.msg)
  156. }
  157. if (res.data.rows.length > 0) {
  158. this.msgPage ++;
  159. this.allLoaded = false;
  160. var msgData = res.data.rows;
  161. var pushData = [];
  162. if (msgData.length > 0) {
  163. msgData.forEach(item => pushData.push(app.formatMsg(item)))
  164. }
  165. // console.log("pushData",pushData)
  166. // app.list = [...app.list,...pushData]
  167. app.list.unshift(...pushData)
  168. // this.scrollToBottom()
  169. } else {
  170. this.allLoaded = true;
  171. }
  172. })
  173. },
  174. quickQs(data){
  175. if (!data.answer) return ;
  176. app.list.push({
  177. content: data.answer,
  178. userType: 'self',
  179. avatar: app._selfAvatar,
  180. msgType:"text",
  181. list:[]
  182. })
  183. app.scrollToBottom()
  184. },
  185. renderTextMessage(text) {
  186. if (!text) return "";
  187. console.log("text",text)
  188. text = text.replace(/\n/g, '');
  189. if (!text) return '<span>' + '[未知内容]' + '</span>';
  190. return '<span>' + decoder.decode(text) + '</span>';
  191. },
  192. getService(options){
  193. Api.checkIn(options).then((res)=>{
  194. uni.setNavigationBarTitle({
  195. title:"与"+res.data.store.name+"对话中"
  196. })
  197. app._selfAvatar = res.data.avatar.self;
  198. app._friendAvatar = res.data.avatar.friend;
  199. var msgData = res.data.msg.rows;
  200. var pushData = [];
  201. if (msgData.length > 0) {
  202. msgData.forEach(item => pushData.push(app.formatMsg(item)))
  203. }
  204. var quickData = res.data.quick;
  205. pushData.push({
  206. content: quickData.star_msg,
  207. userType: 'friend',
  208. avatar: res.data.avatar.friend,
  209. msgType:"quick",
  210. list:quickData.quick
  211. })
  212. console.log("pushData",pushData)
  213. app.list = [...app.list,...pushData]
  214. this.scrollToBottom()
  215. })
  216. },
  217. formatMsg(item){
  218. var payload = "";
  219. if (item.type == 'text') {
  220. payload = item.content
  221. }
  222. if (item.type == 'order') {
  223. var order = JSON.parse(item.content)
  224. payload = order
  225. }
  226. if (item.type == 'pay') {
  227. var order = JSON.parse(item.content)
  228. payload = order
  229. }
  230. if (item.type == 'address') {
  231. var order = JSON.parse(item.content)
  232. payload = order
  233. }
  234. if (item.type == 'image') {
  235. payload = item.content
  236. }
  237. const message = {
  238. content: payload,
  239. userType: item.userType,
  240. avatar: item.avatar,
  241. msgType:item.type,
  242. list:[],
  243. timestamp: item.time,
  244. };
  245. return message;
  246. },
  247. tapMore(){
  248. },
  249. keyboardheightchange(e){
  250. // app.$dialog.showSuccess('asdasdasd')
  251. const height = e.detail.height;
  252. if (height > 0) {
  253. this.isEmoji = false;
  254. this.isMore = false;
  255. this.isKeyboard = true;
  256. } else {
  257. this.isKeyboard = false;
  258. }
  259. this.keyboardHeight = height;
  260. setTimeout(()=>{
  261. this.scrollToBottom()
  262. }, 150)
  263. },
  264. touchmove(){
  265. this.isFocus = false;
  266. this.isFocisEmojius = false;
  267. this.isEmoji = false;
  268. this.isMore = false;
  269. this.isKeyboard = false;
  270. this.keyboardHeight = 0
  271. },
  272. onEmoji(key) {
  273. const text = `${this.text.slice(0, cursor)}${key}${this.text.slice(cursor)}`;
  274. this.text = text;
  275. },
  276. sendingEmojiPack(){},
  277. // 删除表情
  278. deleteFn() {
  279. const str = this.text.charAt(this.text.length - 1);
  280. if (str === ']') {
  281. let metaChars = /\[.*?(\u4e00*\u597d*)\]/g;
  282. let xstr = '';
  283. this.text.replace(metaChars, (match) => {
  284. xstr = match;
  285. });
  286. var text = this.text;
  287. function del(str) {
  288. return text.slice(0, text.length - str.length);
  289. }
  290. this.text = del(xstr);
  291. } else {
  292. this.text = this.text.substring(0, this.text.length - 1);
  293. }
  294. },
  295. tapEmoji() {
  296. this.isEmoji = !this.isEmoji;
  297. if (this.isEmoji) {
  298. this.isKeyboard = true;
  299. }
  300. this.isMore = false;
  301. setTimeout(()=>{
  302. this.scrollToBottom()
  303. }, 150)
  304. },
  305. sendingText() {
  306. if (!this.text) return ;
  307. this.list.push({
  308. content: this.text,
  309. userType: 'self',
  310. avatar: this._selfAvatar
  311. })
  312. this.text = ''
  313. this.scrollToBottom()
  314. // 模拟对方回复
  315. // setTimeout(()=>{
  316. // this.list.push({
  317. // content: '周末什么安排',
  318. // userType: 'friend',
  319. // avatar: this._friendAvatar
  320. // })
  321. // this.scrollToBottom()
  322. // }, 1500)
  323. },
  324. chooseImage() {
  325. uni.chooseImage({
  326. // sourceType: 'album',
  327. success: (res) => {
  328. this.list.push({
  329. content: res.tempFilePaths[0],
  330. userType: 'self',
  331. messageType: 'image',
  332. avatar: this._selfAvatar
  333. })
  334. this.scrollToBottom()
  335. // 模拟对方回复
  336. setTimeout(()=>{
  337. this.list.push({
  338. content: '你好呀,朋友~',
  339. userType: 'friend',
  340. avatar: this._friendAvatar
  341. })
  342. this.scrollToBottom()
  343. }, 1500)
  344. }
  345. })
  346. },
  347. scrollToBottom() {
  348. if (this.isEmoji) {
  349. var px = 600 * (app.screenWidth / 750)
  350. this.top = (this.list.length + px) * 1000
  351. } else {
  352. this.top = (this.list.length + parseInt(this.keyboardHeight)) * 1000
  353. }
  354. }
  355. }
  356. }
  357. </script>
  358. <style lang="scss" scoped>
  359. .pages {
  360. position: fixed;
  361. z-index: 1;
  362. top: 0;
  363. left: 0;
  364. bottom: 0;
  365. right: 0;
  366. background-color: #ededed;
  367. }
  368. .safe-box{width: 100%;height: env(safe-area-inset-bottom);}
  369. .scroll-view {
  370. /* #ifdef H5 */
  371. height: calc(100vh - 44px);
  372. /* #endif */
  373. /* #ifndef H5 */
  374. height: 0;
  375. /* #endif */
  376. background: #eee;
  377. box-sizing: border-box;
  378. }
  379. .message {
  380. display: flex;
  381. align-items: flex-start;
  382. margin-bottom: 30rpx;
  383. .quickList{
  384. margin-top: 10upx;
  385. .list-items {
  386. color: blue;
  387. font-size: 28upx;
  388. line-height: 48upx;
  389. }
  390. }
  391. .avatar {
  392. width: 80rpx;
  393. height: 80rpx;
  394. border-radius: 10rpx;
  395. margin-right: 30rpx;
  396. }
  397. .content {
  398. min-height: 80rpx;
  399. max-width: 60vw;
  400. box-sizing: border-box;
  401. font-size: 28rpx;
  402. line-height: 1.3;
  403. padding: 20rpx;
  404. border-radius: 10rpx;
  405. background: #fff;
  406. image {
  407. width: 200rpx;
  408. }
  409. }
  410. &.self {
  411. justify-content: flex-end;
  412. .avatar {
  413. margin: 0 0 0 30rpx;
  414. }
  415. .content {
  416. position: relative;
  417. &::after {
  418. position: absolute;
  419. content: '';
  420. width: 0;
  421. height: 0;
  422. border: 16rpx solid transparent;
  423. border-left: 16rpx solid #fff;
  424. right: -28rpx;
  425. top: 24rpx;
  426. }
  427. }
  428. }
  429. &.friend {
  430. .content {
  431. position: relative;
  432. &::after {
  433. position: absolute;
  434. content: '';
  435. width: 0;
  436. height: 0;
  437. border: 16rpx solid transparent;
  438. border-right: 16rpx solid #fff;
  439. left: -28rpx;
  440. top: 24rpx;
  441. }
  442. }
  443. }
  444. }
  445. .tool {
  446. // min-height: 120rpx;
  447. // position: fixed;
  448. // right: 0;
  449. // left: 0;
  450. // bottom: 0;
  451. background: #fff;
  452. padding: 20rpx 0 20rpx 0;
  453. padding-bottom: calc(20rpx + constant(safe-area-inset-bottom)/2) !important;
  454. padding-bottom: calc(20rpx + env(safe-area-inset-bottom)/2) !important;
  455. .tool-option{
  456. display: flex;
  457. align-items: flex-start;
  458. box-sizing: border-box;
  459. padding: 0 20rpx;
  460. }
  461. .input-text{
  462. flex: 1;width: 100%;
  463. box-sizing: border-box;
  464. padding: 10rpx 14rpx;
  465. min-height: 84rpx;
  466. max-height: 300rpx;
  467. overflow: auto;
  468. border-radius: 10rpx;
  469. background-color: #eee;
  470. .input{
  471. margin: 10rpx 0;
  472. width: 100%;
  473. font-size: 28upx;
  474. height: 100%;
  475. background-color: #eee;
  476. min-height: 48rpx;
  477. max-height: 300rpx;
  478. }
  479. }
  480. .check-img{
  481. display: flex;align-items: center;justify-content: center;
  482. .check-icon{width: 84rpx;height: 84rpx;display: flex;align-items: center;justify-content: center;}
  483. image{width: 75%;height: 75%;}
  484. }
  485. .thumb {
  486. width: 64rpx;
  487. }
  488. }
  489. .time {
  490. width: 100%;
  491. color: #a3a3a3;
  492. line-height: 100rpx;
  493. text-align: center;
  494. font-size: 24rpx;
  495. }
  496. </style>