index1.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. <template>
  2. <s-layout class="chat-wrap" title="客服" navbar="inner">
  3. <div class="status">
  4. {{ socketState.isConnect ? customerServiceInfo.title : '网络已断开,请检查网络后刷新重试' }}
  5. </div>
  6. <div class="page-bg" :style="{ height: sys_navBar + 'px' }"></div>
  7. <view class="chat-box" :style="{ height: pageHeight + 'px' }">
  8. <scroll-view
  9. :style="{ height: pageHeight + 'px' }"
  10. scroll-y="true"
  11. :scroll-with-animation="false"
  12. :enable-back-to-top="true"
  13. :scroll-into-view="chat.scrollInto"
  14. >
  15. <button
  16. class="loadmore-btn ss-reset-button"
  17. v-if="
  18. chatList.length &&
  19. chatHistoryPagination.lastPage > 1 &&
  20. loadingMap[chatHistoryPagination.loadStatus].title
  21. "
  22. @click="onLoadMore"
  23. >
  24. {{ loadingMap[chatHistoryPagination.loadStatus].title }}
  25. <i
  26. class="loadmore-icon sa-m-l-6"
  27. :class="loadingMap[chatHistoryPagination.loadStatus].icon"
  28. ></i>
  29. </button>
  30. <view class="message-item ss-flex-col" v-for="(item, index) in chatList" :key="index">
  31. <view class="ss-flex ss-row-center ss-col-center">
  32. <!-- 日期 -->
  33. <view v-if="item.from !== 'system' && showTime(item, index)" class="date-message">
  34. {{ formatTime(item.date) }}
  35. </view>
  36. <!-- 系统消息 -->
  37. <view v-if="item.from === 'system'" class="system-message">
  38. {{ item.content.text }}
  39. </view>
  40. </view>
  41. <!-- 常见问题 -->
  42. <view v-if="item.mode === 'template' && item.content.list.length" class="template-wrap">
  43. <view class="title">猜你想问</view>
  44. <view
  45. class="item"
  46. v-for="(item, index) in item.content.list"
  47. :key="index"
  48. @click="onTemplateList(item)"
  49. >
  50. * {{ item.title }}
  51. </view>
  52. </view>
  53. <view
  54. v-if="
  55. (item.from === 'customer_service' && item.mode !== 'template') ||
  56. item.from === 'customer'
  57. "
  58. class="ss-flex ss-col-top"
  59. :class="[
  60. item.from === 'customer_service'
  61. ? `ss-row-left`
  62. : item.from === 'customer'
  63. ? `ss-row-right`
  64. : '',
  65. ]"
  66. >
  67. <!-- 客服头像 -->
  68. <image
  69. v-show="item.from === 'customer_service'"
  70. class="chat-avatar ss-m-r-24"
  71. :src="
  72. sheep.$url.cdn(item?.sender?.avatar) ||
  73. sheep.$url.static('/static/img/shop/chat/default.png')
  74. "
  75. mode="aspectFill"
  76. ></image>
  77. <!-- 发送状态 -->
  78. <span
  79. v-if="
  80. item.from === 'customer' &&
  81. index == chatData.chatList.length - 1 &&
  82. chatData.isSendSucces !== 0
  83. "
  84. class="send-status"
  85. >
  86. <image
  87. v-if="chatData.isSendSucces == -1"
  88. class="loading"
  89. :src="sheep.$url.static('/static/img/shop/chat/loading.png')"
  90. mode="aspectFill"
  91. ></image>
  92. <!-- <image
  93. v-if="chatData.isSendSucces == 1"
  94. class="warning"
  95. :src="sheep.$url.static('/static/img/shop/chat/warning.png')"
  96. mode="aspectFill"
  97. @click="onAgainSendMessage(item)"
  98. ></image> -->
  99. </span>
  100. <!-- 内容 -->
  101. <template v-if="item.mode === 'text'">
  102. <view class="message-box" :class="[item.from]">
  103. <div
  104. class="message-text ss-flex ss-flex-wrap"
  105. @click="onRichtext"
  106. v-html="replaceEmoji(item.content.text)"
  107. ></div>
  108. </view>
  109. </template>
  110. <template v-if="item.mode === 'image'">
  111. <view class="message-box" :class="[item.from]" :style="{ width: '200rpx' }">
  112. <su-image
  113. class="message-img"
  114. isPreview
  115. :previewList="[sheep.$url.cdn(item.content.url)]"
  116. :current="0"
  117. :src="sheep.$url.cdn(item.content.url)"
  118. :height="200"
  119. :width="200"
  120. mode="aspectFill"
  121. ></su-image>
  122. </view>
  123. </template>
  124. <template v-if="item.mode === 'goods'">
  125. <GoodsItem
  126. :goodsData="item.content.item"
  127. @tap="
  128. sheep.$router.go('/pages/goods/index', {
  129. id: item.content.item.id,
  130. })
  131. "
  132. />
  133. </template>
  134. <template v-if="item.mode === 'order'">
  135. <OrderItem
  136. from="msg"
  137. :orderData="item.content.item"
  138. @tap="
  139. sheep.$router.go('/pages/order/detail', {
  140. id: item.content.item.id,
  141. })
  142. "
  143. />
  144. </template>
  145. <!-- user头像 -->
  146. <image
  147. v-show="item.from === 'customer'"
  148. class="chat-avatar ss-m-l-24"
  149. :src="sheep.$url.cdn(customerUserInfo.avatar)"
  150. mode="aspectFill"
  151. >
  152. </image>
  153. </view>
  154. </view>
  155. <view id="scrollBottom"></view>
  156. </scroll-view>
  157. </view>
  158. <su-fixed bottom>
  159. <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage"></message-input>
  160. </su-fixed>
  161. <!-- 聊天工具 -->
  162. <tools-popup :show-tools="chat.showTools" :tools-mode="chat.toolsMode" @close="handleToolsClose"
  163. @on-emoji="onEmoji" @image-select="onSelect" @on-show-select="onShowSelect">
  164. <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage"></message-input>
  165. </tools-popup>
  166. <SelectPopup
  167. :mode="chat.selectMode"
  168. :show="chat.showSelect"
  169. @select="onSelect"
  170. @close="chat.showSelect = false"
  171. />
  172. </s-layout>
  173. </template>
  174. <script setup>
  175. import sheep from '@/sheep';
  176. import { computed, reactive, toRefs } from 'vue';
  177. import { onLoad } from '@dcloudio/uni-app';
  178. import { emojiList } from './emoji.js';
  179. import SelectPopup from './components/select-popup.vue';
  180. import GoodsItem from './components/goods.vue';
  181. import OrderItem from './components/order.vue';
  182. import MessageInput from './components/messageInput.vue';
  183. import ToolsPopup from './components/toolsPopup.vue';
  184. import { useChatWebSocket } from './socket';
  185. import { useWebSocket } from '@/sheep/hooks/useWebSocket';
  186. const {
  187. socketInit,
  188. state: chatData,
  189. socketSendMsg,
  190. formatChatInput,
  191. socketHistoryList,
  192. onDrop,
  193. onPaste,
  194. getFocus,
  195. // upload,
  196. getUserToken,
  197. // socketTest,
  198. showTime,
  199. formatTime,
  200. } = useChatWebSocket();
  201. const chatList = toRefs(chatData).chatList;
  202. const customerServiceInfo = toRefs(chatData).customerServerInfo;
  203. const chatHistoryPagination = toRefs(chatData).chatHistoryPagination;
  204. const customerUserInfo = toRefs(chatData).customerUserInfo;
  205. const socketState = toRefs(chatData).socketState;
  206. const sys_navBar = sheep.$platform.navbar;
  207. const chatConfig = computed(() => sheep.$store('app').chat);
  208. const { screenHeight, safeAreaInsets, safeArea, screenWidth } = sheep.$platform.device;
  209. const pageHeight = safeArea.height - 44 - 35 - 50;
  210. const chatStatus = {
  211. online: {
  212. text: '在线',
  213. colorVariate: '#46c55f',
  214. },
  215. offline: {
  216. text: '离线',
  217. colorVariate: '#b5b5b5',
  218. },
  219. busy: {
  220. text: '忙碌',
  221. colorVariate: '#ff0e1b',
  222. },
  223. };
  224. // 加载更多
  225. const loadingMap = {
  226. loadmore: {
  227. title: '查看更多',
  228. icon: 'el-icon-d-arrow-left',
  229. },
  230. nomore: {
  231. title: '没有更多了',
  232. icon: '',
  233. },
  234. loading: {
  235. title: '加载中... ',
  236. icon: 'el-icon-loading',
  237. },
  238. };
  239. const onLoadMore = () => {
  240. chatHistoryPagination.value.page < chatHistoryPagination.value.lastPage && socketHistoryList();
  241. };
  242. const chat = reactive({
  243. msg: '',
  244. scrollInto: '',
  245. showTools: false,
  246. toolsMode: '',
  247. showSelect: false,
  248. selectMode: '',
  249. chatStyle: {
  250. mode: 'inner',
  251. color: '#F8270F',
  252. type: 'color',
  253. alwaysShow: 1,
  254. src: '',
  255. list: {},
  256. },
  257. });
  258. //======================= 聊天工具相关 =======================
  259. function handleToolsClose() {
  260. chat.showTools = false;
  261. chat.toolsMode = '';
  262. }
  263. function onEmoji(item) {
  264. chat.msg += item.name;
  265. }
  266. // 点击工具栏开关
  267. function onTools(mode) {
  268. // if (!socketState.value.isConnect) {
  269. // sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  270. // return;
  271. // }
  272. if (!chat.toolsMode || chat.toolsMode === mode) {
  273. chat.showTools = !chat.showTools;
  274. }
  275. chat.toolsMode = mode;
  276. if (!chat.showTools) {
  277. chat.toolsMode = '';
  278. }
  279. }
  280. function onShowSelect(mode) {
  281. chat.showTools = false;
  282. chat.showSelect = true;
  283. chat.selectMode = mode;
  284. }
  285. async function onSelect({ type, data }) {
  286. let msg = '';
  287. switch (type) {
  288. case 'image':
  289. const { path, fullurl } = await sheep.$api.app.upload(data.tempFiles[0].path, 'default');
  290. msg = {
  291. from: 'customer',
  292. mode: 'image',
  293. date: new Date().getTime(),
  294. content: {
  295. url: fullurl,
  296. path: path,
  297. },
  298. };
  299. break;
  300. case 'goods':
  301. msg = {
  302. from: 'customer',
  303. mode: 'goods',
  304. date: new Date().getTime(),
  305. content: {
  306. item: {
  307. id: data.goods.id,
  308. title: data.goods.title,
  309. image: data.goods.image,
  310. price: data.goods.price,
  311. stock: data.goods.stock,
  312. },
  313. },
  314. };
  315. break;
  316. case 'order':
  317. msg = {
  318. from: 'customer',
  319. mode: 'order',
  320. date: new Date().getTime(),
  321. content: {
  322. item: {
  323. id: data.id,
  324. order_sn: data.order_sn,
  325. create_time: data.create_time,
  326. pay_fee: data.pay_fee,
  327. items: data.items.filter((item) => ({
  328. goods_id: item.goods_id,
  329. goods_title: item.goods_title,
  330. goods_image: item.goods_image,
  331. goods_price: item.goods_price,
  332. })),
  333. status_text: data.status_text,
  334. },
  335. },
  336. };
  337. break;
  338. }
  339. if (msg) {
  340. socketSendMsg(msg, () => {
  341. scrollBottom();
  342. });
  343. // scrollBottom();
  344. chat.showTools = false;
  345. chat.showSelect = false;
  346. chat.selectMode = '';
  347. }
  348. }
  349. function onAgainSendMessage(item) {
  350. if (!socketState.value.isConnect) {
  351. sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  352. return;
  353. }
  354. if (!item) return;
  355. const data = {
  356. from: 'customer',
  357. mode: 'text',
  358. date: new Date().getTime(),
  359. content: item.content,
  360. };
  361. socketSendMsg(data, () => {
  362. scrollBottom();
  363. });
  364. }
  365. function onSendMessage() {
  366. if (!socketState.value.isConnect) {
  367. sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  368. return;
  369. }
  370. if (!chat.msg) return;
  371. const data = {
  372. from: 'customer',
  373. mode: 'text',
  374. date: new Date().getTime(),
  375. content: {
  376. text: chat.msg,
  377. },
  378. };
  379. socketSendMsg(data, () => {
  380. scrollBottom();
  381. });
  382. chat.showTools = false;
  383. // scrollBottom();
  384. setTimeout(() => {
  385. chat.msg = '';
  386. }, 100);
  387. }
  388. // 点击猜你想问
  389. function onTemplateList(e) {
  390. if (!socketState.value.isConnect) {
  391. sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  392. return;
  393. }
  394. const data = {
  395. from: 'customer',
  396. mode: 'text',
  397. date: new Date().getTime(),
  398. content: {
  399. text: e.title,
  400. },
  401. customData: {
  402. question_id: e.id,
  403. },
  404. };
  405. socketSendMsg(data, () => {
  406. scrollBottom();
  407. });
  408. // scrollBottom();
  409. }
  410. function selEmojiFile(name) {
  411. for (let index in emojiList) {
  412. if (emojiList[index].name === name) {
  413. return emojiList[index].file;
  414. }
  415. }
  416. return false;
  417. }
  418. function replaceEmoji(data) {
  419. let newData = data;
  420. if (typeof newData !== 'object') {
  421. let reg = /\[(.+?)\]/g; // [] 中括号
  422. let zhEmojiName = newData.match(reg);
  423. if (zhEmojiName) {
  424. zhEmojiName.forEach((item) => {
  425. let emojiFile = selEmojiFile(item);
  426. newData = newData.replace(
  427. item,
  428. `<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${sheep.$url.cdn(
  429. '/static/img/chat/emoji/' + emojiFile,
  430. )}"/>`,
  431. );
  432. });
  433. }
  434. }
  435. return newData;
  436. }
  437. function scrollBottom() {
  438. let timeout = null;
  439. chat.scrollInto = '';
  440. clearTimeout(timeout);
  441. timeout = setTimeout(() => {
  442. chat.scrollInto = 'scrollBottom';
  443. }, 100);
  444. }
  445. const websocket = useWebSocket()
  446. onLoad(async () => {
  447. websocket.socketInit({}, () => {
  448. scrollBottom();
  449. });
  450. });
  451. </script>
  452. <style lang="scss" scoped>
  453. .page-bg {
  454. width: 100%;
  455. position: absolute;
  456. top: 0;
  457. left: 0;
  458. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  459. background-size: 750rpx 100%;
  460. z-index: 1;
  461. }
  462. .chat-wrap {
  463. // :deep() {
  464. // .ui-navbar-box {
  465. // background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  466. // }
  467. // }
  468. .status {
  469. position: relative;
  470. box-sizing: border-box;
  471. z-index: 3;
  472. height: 70rpx;
  473. padding: 0 30rpx;
  474. background: var(--ui-BG-Main-opacity-1);
  475. display: flex;
  476. align-items: center;
  477. font-size: 30rpx;
  478. font-weight: 400;
  479. color: var(--ui-BG-Main);
  480. }
  481. .chat-box {
  482. padding: 0 20rpx 0;
  483. .loadmore-btn {
  484. width: 98%;
  485. height: 40px;
  486. font-size: 12px;
  487. color: #8c8c8c;
  488. .loadmore-icon {
  489. transform: rotate(90deg);
  490. }
  491. }
  492. .message-item {
  493. margin-bottom: 33rpx;
  494. }
  495. .date-message,
  496. .system-message {
  497. width: fit-content;
  498. border-radius: 12rpx;
  499. padding: 8rpx 16rpx;
  500. margin-bottom: 16rpx;
  501. background-color: var(--ui-BG-3);
  502. color: #999;
  503. font-size: 24rpx;
  504. }
  505. .chat-avatar {
  506. width: 70rpx;
  507. height: 70rpx;
  508. border-radius: 50%;
  509. }
  510. .send-status {
  511. color: #333;
  512. height: 80rpx;
  513. margin-right: 8rpx;
  514. display: flex;
  515. align-items: center;
  516. .loading {
  517. width: 32rpx;
  518. height: 32rpx;
  519. -webkit-animation: rotating 2s linear infinite;
  520. animation: rotating 2s linear infinite;
  521. @-webkit-keyframes rotating {
  522. 0% {
  523. transform: rotateZ(0);
  524. }
  525. 100% {
  526. transform: rotateZ(360deg);
  527. }
  528. }
  529. @keyframes rotating {
  530. 0% {
  531. transform: rotateZ(0);
  532. }
  533. 100% {
  534. transform: rotateZ(360deg);
  535. }
  536. }
  537. }
  538. .warning {
  539. width: 32rpx;
  540. height: 32rpx;
  541. color: #ff3000;
  542. }
  543. }
  544. .message-box {
  545. max-width: 50%;
  546. font-size: 16px;
  547. line-height: 20px;
  548. // max-width: 500rpx;
  549. white-space: normal;
  550. word-break: break-all;
  551. word-wrap: break-word;
  552. padding: 20rpx;
  553. border-radius: 10rpx;
  554. color: #fff;
  555. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  556. &.customer_service {
  557. background: #fff;
  558. color: #333;
  559. }
  560. :deep() {
  561. .imgred {
  562. width: 100%;
  563. }
  564. .imgred,
  565. img {
  566. width: 100%;
  567. }
  568. }
  569. }
  570. :deep() {
  571. .goods,
  572. .order {
  573. max-width: 500rpx;
  574. }
  575. }
  576. .message-img {
  577. width: 100px;
  578. height: 100px;
  579. border-radius: 6rpx;
  580. }
  581. .template-wrap {
  582. // width: 100%;
  583. padding: 20rpx 24rpx;
  584. background: #fff;
  585. border-radius: 10rpx;
  586. .title {
  587. font-size: 26rpx;
  588. font-weight: 500;
  589. color: #333;
  590. margin-bottom: 29rpx;
  591. }
  592. .item {
  593. font-size: 24rpx;
  594. color: var(--ui-BG-Main);
  595. margin-bottom: 16rpx;
  596. &:last-of-type {
  597. margin-bottom: 0;
  598. }
  599. }
  600. }
  601. .error-img {
  602. width: 400rpx;
  603. height: 400rpx;
  604. }
  605. #scrollBottom {
  606. height: 120rpx;
  607. }
  608. }
  609. }
  610. </style>
  611. <style>
  612. .chat-img {
  613. width: 24px;
  614. height: 24px;
  615. margin: 0 3px;
  616. }
  617. .full-img {
  618. object-fit: cover;
  619. width: 100px;
  620. height: 100px;
  621. border-radius: 6px;
  622. }
  623. </style>