index.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <template>
  2. <view>
  3. <view class="notice">
  4. <up-notice-bar text="为了您的交易安全和售后保障,请您务必在平台内完成交易!" icon=""></up-notice-bar>
  5. </view>
  6. <view class="container" :style="pageHeight">
  7. <view class="box-1">
  8. <scroll-view scroll-y refresher-background="transparent" style="height: 100%;"
  9. @refresherrefresh="refresherrefresh" :refresher-enabled="true" :scroll-with-animation="false"
  10. :refresher-triggered="scrollView.refresherTriggered" :scroll-into-view="scrollView.intoView">
  11. <view class="talk-list">
  12. <view v-for="(item, index) in talkList" :key="item.msgSendTime" :id="'msg-' + item.msgSendTime">
  13. <view v-if="showTimeLabel(item, index)" class="time-label"> {{ showTimeLabel(item, index) }}</view>
  14. <view class="item flex-col" :class="item.senderId == this.userId ? 'push' : 'pull'">
  15. <!-- <image :src="item.pic" mode="aspectFill" class="pic"></image> -->
  16. <img v-if="item.senderId !== this.userId"
  17. :src="orderInfo.conversationAvatar || '/static/serverImg/mine/user.png'" alt=""
  18. class="pic" mode="aspectFill" />
  19. <img src="/static/serverImg/mine/user.png" alt="" class="pic" mode="aspectFill"
  20. v-else />
  21. <view class="content">
  22. <template v-if="item.msgType === '2'">
  23. <image :src="item.msgContent" mode="widthFix" style="width: 400rpx;"></image>
  24. </template>
  25. <template v-else>
  26. {{ item.msgContent }}
  27. </template>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </scroll-view>
  33. </view>
  34. <view class="box-2">
  35. <view class="flex-col">
  36. <view style="margin-right: 20rpx;">
  37. <uni-icons type="image" size="24" color="#42b983" @tap="handleImageClick"></uni-icons>
  38. </view>
  39. <view class="flex-grow">
  40. <input type="text" class="content" v-model="content" placeholder="请输入聊天内容"
  41. placeholder-style="color:#DDD;" :cursor-spacing="6">
  42. </view>
  43. <button class="send" @tap="handleSendClick">发送</button>
  44. </view>
  45. </view>
  46. </view>
  47. </view>
  48. </template>
  49. <script>
  50. import { wxUploadFile, wxMakePhoneCall } from '@/utils/wxRequest.js'
  51. import { sendMsg, getOrderConversationInfo, getListConversationInfo, setRead, getHistoryMsg,getHelpConversation } from '@/api/conversation.js';
  52. import { onMounted } from "vue";
  53. import dayjs from 'dayjs/esm/index'
  54. import store from "@/store";
  55. import { handleTime } from '../../request/dateTime';
  56. export default {
  57. data() {
  58. return {
  59. // 滚动容器
  60. scrollView: {
  61. refresherTriggered: false,
  62. intoView: "",
  63. safeAreaHeight: 0
  64. },
  65. // 聊天列表数据
  66. talkList: [],
  67. // 请求参数
  68. ajax: {
  69. rows: 20, //每页数量
  70. page: 1, //页码
  71. flag: true, // 请求开关
  72. },
  73. // 发送内容
  74. content: '',
  75. orderId: '',
  76. orderInfo: {},//订单信息
  77. userType: uni.getStorageSync('userType'), //读取本地存储
  78. system: 2,
  79. userId: uni.getStorageSync('userId'), //读取本地存储
  80. conversationRecordId: null,//会话id
  81. storeUser: store.state.user,
  82. store: store,
  83. isKg:false,
  84. conversationType:'2'
  85. }
  86. },
  87. computed: {
  88. // 页面高度
  89. pageHeight() {
  90. const safeAreaHeight = this.scrollView.safeAreaHeight;
  91. console.log('safeAreaHeight', safeAreaHeight);
  92. if (safeAreaHeight > 0) {
  93. return `height: calc(${safeAreaHeight}px - var(--window-top));`
  94. }
  95. return "";
  96. }
  97. },
  98. onLoad(options) {
  99. console.log("TCL: onLoad -> options", options)
  100. this.conversationType = options.conversationType;
  101. if (options.orderId) {
  102. this.orderId = options.orderId;
  103. }
  104. if (options.conversationRecordId) {
  105. this.conversationRecordId = options.conversationRecordId;
  106. }
  107. //1用户2志愿者3后台
  108. this.system = this.userType === 1 ? 1 : 2;
  109. //客服
  110. if(options.customerService){
  111. // this.system = 3;
  112. this.isKg = true;
  113. }
  114. // #ifdef H5
  115. this.scrollView.safeAreaHeight = uni.getSystemInfoSync().safeArea.height;
  116. // #endif
  117. this.getMessage();
  118. },
  119. onMounted() {
  120. // uni.onSocketMessage(function (parmas) {
  121. // const res = JSON.parse(parmas.data);
  122. // console.log("TCL: get -> res", res)
  123. // if (res.type === 'msgNew') {
  124. // this.talkList.unshift({ ...res.data, code: 200 });
  125. // }
  126. // console.log(' onMounted-收到服务器消息:', res, this.talkList);
  127. // });
  128. },
  129. methods: {
  130. // 下拉刷新
  131. refresherrefresh(e) {
  132. console.log("TCL: 下拉刷新 -> e", e)
  133. this.scrollView.refresherTriggered = true;
  134. // this.getMessage();
  135. const conversationMsgRecordId = this.talkList[this.talkList.length - 1].conversationMsgRecordId;
  136. console.log("TCL: refresherrefresh -> conversationMsgRecordId", conversationMsgRecordId, this.talkList[this.talkList.length - 1])
  137. const msgSendTime = this.talkList[this.talkList.length - 1].msgSendTime
  138. getHistoryMsg({ conversationMsgRecordId, conversationRecordId: this.conversationRecordId }).then(res => {
  139. console.log("TCL: refresherrefresh -> res", res)
  140. if (res.code === 200) {
  141. if (res.data.length === 0) {
  142. uni.showToast({
  143. title: '没有更多消息了',
  144. icon: 'none'
  145. })
  146. this.scrollView.refresherTriggered = false;
  147. return
  148. }
  149. this.talkList = [...this.talkList, ...res.data];
  150. this.scrollView.refresherTriggered = false;
  151. this.scrollView.intoView = `msg-${msgSendTime}`;
  152. }
  153. })
  154. },
  155. listsMessage() {
  156. console.log('-----listsMessage---->', this.storeUser, this.storeUser.wsManager);
  157. this.storeUser.wsManager.onMessage(res => {
  158. console.log('--------->', res)
  159. // 处理消息逻辑
  160. if (res.type === 'msgNew') {
  161. this.talkList.unshift({ ...res.data, code: 200 });
  162. }
  163. });
  164. },
  165. // 获取历史消息
  166. getMessage() {
  167. if (!this.ajax.flag) {
  168. return;
  169. }
  170. console.log('--------->', this.storeUser);
  171. if (!this.storeUser.wsManager) {
  172. store.dispatch('GetInfo').then((res) => {
  173. this.listsMessage();
  174. });
  175. } else {
  176. this.listsMessage();
  177. }
  178. // 此处用到 ES7 的 async/await 知识,为使代码更加优美。不懂的请自行学习。
  179. let get = async () => {
  180. this.ajax.flag = false;
  181. const parmas = {
  182. system: this.system
  183. }
  184. const apiFun = this.orderId ? getOrderConversationInfo : (this.isKg?getHelpConversation:getListConversationInfo)
  185. if (this.orderId) {
  186. parmas.mainOrderId = this.orderId
  187. }
  188. if (this.conversationRecordId) {
  189. parmas.conversationRecordId = this.conversationRecordId
  190. }
  191. const res = await apiFun(parmas);
  192. console.log("TCL: getOrder -> res", res)
  193. await setRead({...parmas,conversationRecordId: res.data.conversationRecordId}); //设置已读
  194. if (res.code === 200) {
  195. const data = res.data.vos;
  196. // 获取待滚动元素选择器,解决插入数据后,滚动条定位时使用。取当前消息数据的第一条信息元素
  197. const selector = `msg-${data?.[0]?.msgSendTime}`;
  198. // uni.setNavigationBarTitle({
  199. // title: userType === 1? res.data. : // 根据业务逻辑调整
  200. // });
  201. // 将获取到的消息数据合并到消息数组中
  202. this.talkList = data;
  203. console.log("TCL: getOrder -> talkList", this.talkList)
  204. this.orderInfo = res.data;
  205. // 数据挂载后执行,不懂的请自行阅读 Vue.js 文档对 Vue.nextTick 函数说明。
  206. this.$nextTick(() => {
  207. // 设置当前滚动的位置
  208. this.scrollView.intoView = selector;
  209. if (data.length < this.ajax.rows) {
  210. // 当前消息数据条数小于请求要求条数时,则无更多消息,不再允许请求。
  211. // 可在此处编写无更多消息数据时的逻辑
  212. } else {
  213. this.ajax.flag = true;
  214. this.ajax.page++;
  215. }
  216. })
  217. }
  218. }
  219. get();
  220. },
  221. // 发送信息
  222. handleSendClick() {
  223. if (!this.content) {
  224. uni.showToast({
  225. title: '请输入有效的内容',
  226. icon: 'none'
  227. })
  228. return;
  229. }
  230. this.sendMessage(this.content, 'text');
  231. // 清空内容框中的内容
  232. this.content = '';
  233. },
  234. // 处理图片点击
  235. async handleImageClick() {
  236. const res = await wxUploadFile('img');
  237. this.sendMessage(res[0].url, 'image');
  238. console.log("TCL: handleImageClick -> res", res)
  239. },
  240. async sendMessage(content, contentType = 'text') {
  241. // 将当前发送信息 添加到消息列表。
  242. let data = {
  243. conversationRecordId: "3",
  244. msgContent: content,
  245. msgSendTime: new Date().getTime(),
  246. senderId: this.userId,
  247. msgType: contentType == 'text' ? '1' : '2',//消息类型 1文字消息 2图片消息
  248. }
  249. console.log('发送消息',this.conversationType);
  250. const res = await sendMsg({
  251. ...this.orderInfo,
  252. msgContent: content,
  253. system: this.system,
  254. msgType: data.msgType,
  255. conversationType: this.conversationType
  256. });
  257. this.talkList.unshift({ ...data, code: res.code });
  258. this.scrollView.intoView = `msg-${data.msgSendTime}`;
  259. },
  260. showTimeLabel(item, index) {
  261. const { talkList } = this;
  262. const isLastItem = index === talkList.length - 1;
  263. // 最后一条消息,强制显示时间
  264. if (isLastItem) {
  265. return handleTime(item.msgSendTime);
  266. }
  267. const nextItem = talkList[index + 1];
  268. // 如果没有下一条,不显示
  269. if (!nextItem) {
  270. return null;
  271. }
  272. const diffInMinutes = dayjs(nextItem.msgSendTime).diff(dayjs(item.msgSendTime), 'minute');
  273. // 如果下一条消息与当前消息间隔 >= 30 分钟,则由下一条显示时间
  274. if (Math.abs(diffInMinutes) >= 30) {
  275. return handleTime(item.msgSendTime);
  276. }
  277. return null;
  278. }
  279. },
  280. }
  281. </script>
  282. <style lang="scss">
  283. @import "../../lib/global.scss";
  284. page {
  285. background-color: #F3F3F3;
  286. font-size: 28rpx;
  287. }
  288. .notice {
  289. height: 40px;
  290. }
  291. .container {
  292. height: calc(100vh - var(--window-top) - 40px);
  293. display: flex;
  294. flex-direction: column;
  295. flex-wrap: nowrap;
  296. align-content: center;
  297. justify-content: space-between;
  298. align-items: stretch;
  299. }
  300. /* 加载数据提示 */
  301. .tips {
  302. position: fixed;
  303. left: 0;
  304. top: var(--window-top);
  305. width: 100%;
  306. z-index: 9;
  307. background-color: rgba(0, 0, 0, 0.15);
  308. height: 72rpx;
  309. line-height: 72rpx;
  310. transform: translateY(-80rpx);
  311. transition: transform 0.3s ease-in-out 0s;
  312. &.show {
  313. transform: translateY(0);
  314. }
  315. }
  316. .box-1 {
  317. width: 100%;
  318. height: 0;
  319. flex: 1 0 auto;
  320. box-sizing: content-box;
  321. }
  322. .box-2 {
  323. height: auto;
  324. z-index: 2;
  325. border-top: #e5e5e5 solid 1px;
  326. box-sizing: content-box;
  327. background-color: #F3F3F3;
  328. /* 兼容iPhoneX */
  329. padding-bottom: 0;
  330. padding-bottom: constant(safe-area-inset-bottom);
  331. padding-bottom: env(safe-area-inset-bottom);
  332. >view {
  333. padding: 0 20rpx;
  334. height: 100rpx;
  335. }
  336. .content {
  337. background-color: #fff;
  338. height: 64rpx;
  339. padding: 0 20rpx;
  340. border-radius: 32rpx;
  341. font-size: 28rpx;
  342. }
  343. .send {
  344. background-color: #42b983;
  345. color: #fff;
  346. height: 64rpx;
  347. margin-left: 20rpx;
  348. border-radius: 32rpx;
  349. padding: 0;
  350. width: 120rpx;
  351. line-height: 62rpx;
  352. &:active {
  353. background-color: #5fc496;
  354. }
  355. }
  356. }
  357. .talk-list {
  358. padding-bottom: 20rpx;
  359. display: flex;
  360. flex-direction: column-reverse;
  361. flex-wrap: nowrap;
  362. align-content: flex-start;
  363. justify-content: flex-end;
  364. align-items: stretch;
  365. // 添加弹性容器,让内容自动在顶部
  366. &::before {
  367. content: '.';
  368. display: inline;
  369. visibility: hidden;
  370. line-height: 0;
  371. font-size: 0;
  372. flex: 1 0 auto;
  373. height: 1px;
  374. }
  375. /* 消息项,基础类 */
  376. .item {
  377. padding: 20rpx 20rpx 0 20rpx;
  378. align-items: flex-start;
  379. align-content: flex-start;
  380. color: #333;
  381. .pic {
  382. width: 92rpx;
  383. height: 92rpx;
  384. border-radius: 50%;
  385. border: #fff solid 1px;
  386. }
  387. .content {
  388. padding: 20rpx;
  389. border-radius: 4px;
  390. max-width: 500rpx;
  391. word-break: break-all;
  392. line-height: 52rpx;
  393. position: relative;
  394. }
  395. /* 收到的消息 */
  396. &.pull {
  397. .content {
  398. margin-left: 32rpx;
  399. background-color: #fff;
  400. &::after {
  401. content: '';
  402. display: block;
  403. width: 0;
  404. height: 0;
  405. border-top: 16rpx solid transparent;
  406. border-bottom: 16rpx solid transparent;
  407. border-right: 20rpx solid #fff;
  408. position: absolute;
  409. top: 30rpx;
  410. left: -18rpx;
  411. }
  412. }
  413. }
  414. /* 发出的消息 */
  415. &.push {
  416. /* 主轴为水平方向,起点在右端。使不修改DOM结构,也能改变元素排列顺序 */
  417. flex-direction: row-reverse;
  418. .content {
  419. margin-right: 32rpx;
  420. background-color: #a0e959;
  421. &::after {
  422. content: '';
  423. display: block;
  424. width: 0;
  425. height: 0;
  426. border-top: 16rpx solid transparent;
  427. border-bottom: 16rpx solid transparent;
  428. border-left: 20rpx solid #a0e959;
  429. position: absolute;
  430. top: 30rpx;
  431. right: -18rpx;
  432. }
  433. }
  434. }
  435. }
  436. }
  437. .time-label {
  438. text-align: center;
  439. margin-top: 16rpx;
  440. }
  441. </style>