index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. const {
  186. socketInit,
  187. state: chatData,
  188. socketSendMsg,
  189. formatChatInput,
  190. socketHistoryList,
  191. onDrop,
  192. onPaste,
  193. getFocus,
  194. // upload,
  195. getUserToken,
  196. // socketTest,
  197. showTime,
  198. formatTime,
  199. } = useChatWebSocket();
  200. const chatList = toRefs(chatData).chatList;
  201. const customerServiceInfo = toRefs(chatData).customerServerInfo;
  202. const chatHistoryPagination = toRefs(chatData).chatHistoryPagination;
  203. const customerUserInfo = toRefs(chatData).customerUserInfo;
  204. const socketState = toRefs(chatData).socketState;
  205. const sys_navBar = sheep.$platform.navbar;
  206. const chatConfig = computed(() => sheep.$store('app').chat);
  207. const { screenHeight, safeAreaInsets, safeArea, screenWidth } = sheep.$platform.device;
  208. const pageHeight = safeArea.height - 44 - 35 - 50;
  209. const chatStatus = {
  210. online: {
  211. text: '在线',
  212. colorVariate: '#46c55f',
  213. },
  214. offline: {
  215. text: '离线',
  216. colorVariate: '#b5b5b5',
  217. },
  218. busy: {
  219. text: '忙碌',
  220. colorVariate: '#ff0e1b',
  221. },
  222. };
  223. // 加载更多
  224. const loadingMap = {
  225. loadmore: {
  226. title: '查看更多',
  227. icon: 'el-icon-d-arrow-left',
  228. },
  229. nomore: {
  230. title: '没有更多了',
  231. icon: '',
  232. },
  233. loading: {
  234. title: '加载中... ',
  235. icon: 'el-icon-loading',
  236. },
  237. };
  238. const onLoadMore = () => {
  239. chatHistoryPagination.value.page < chatHistoryPagination.value.lastPage && socketHistoryList();
  240. };
  241. const chat = reactive({
  242. msg: '',
  243. scrollInto: '',
  244. showTools: false,
  245. toolsMode: '',
  246. showSelect: false,
  247. selectMode: '',
  248. chatStyle: {
  249. mode: 'inner',
  250. color: '#F8270F',
  251. type: 'color',
  252. alwaysShow: 1,
  253. src: '',
  254. list: {},
  255. },
  256. });
  257. //======================= 聊天工具相关 =======================
  258. function handleToolsClose() {
  259. chat.showTools = false;
  260. chat.toolsMode = '';
  261. }
  262. function onEmoji(item) {
  263. chat.msg += item.name;
  264. }
  265. // 点击工具栏开关
  266. function onTools(mode) {
  267. // if (!socketState.value.isConnect) {
  268. // sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  269. // return;
  270. // }
  271. if (!chat.toolsMode || chat.toolsMode === mode) {
  272. chat.showTools = !chat.showTools;
  273. }
  274. chat.toolsMode = mode;
  275. if (!chat.showTools) {
  276. chat.toolsMode = '';
  277. }
  278. }
  279. function onShowSelect(mode) {
  280. chat.showTools = false;
  281. chat.showSelect = true;
  282. chat.selectMode = mode;
  283. }
  284. async function onSelect({ type, data }) {
  285. let msg = '';
  286. switch (type) {
  287. case 'image':
  288. const { path, fullurl } = await sheep.$api.app.upload(data.tempFiles[0].path, 'default');
  289. msg = {
  290. from: 'customer',
  291. mode: 'image',
  292. date: new Date().getTime(),
  293. content: {
  294. url: fullurl,
  295. path: path,
  296. },
  297. };
  298. break;
  299. case 'goods':
  300. msg = {
  301. from: 'customer',
  302. mode: 'goods',
  303. date: new Date().getTime(),
  304. content: {
  305. item: {
  306. id: data.goods.id,
  307. title: data.goods.title,
  308. image: data.goods.image,
  309. price: data.goods.price,
  310. stock: data.goods.stock,
  311. },
  312. },
  313. };
  314. break;
  315. case 'order':
  316. msg = {
  317. from: 'customer',
  318. mode: 'order',
  319. date: new Date().getTime(),
  320. content: {
  321. item: {
  322. id: data.id,
  323. order_sn: data.order_sn,
  324. create_time: data.create_time,
  325. pay_fee: data.pay_fee,
  326. items: data.items.filter((item) => ({
  327. goods_id: item.goods_id,
  328. goods_title: item.goods_title,
  329. goods_image: item.goods_image,
  330. goods_price: item.goods_price,
  331. })),
  332. status_text: data.status_text,
  333. },
  334. },
  335. };
  336. break;
  337. }
  338. if (msg) {
  339. socketSendMsg(msg, () => {
  340. scrollBottom();
  341. });
  342. // scrollBottom();
  343. chat.showTools = false;
  344. chat.showSelect = false;
  345. chat.selectMode = '';
  346. }
  347. }
  348. function onAgainSendMessage(item) {
  349. if (!socketState.value.isConnect) {
  350. sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  351. return;
  352. }
  353. if (!item) return;
  354. const data = {
  355. from: 'customer',
  356. mode: 'text',
  357. date: new Date().getTime(),
  358. content: item.content,
  359. };
  360. socketSendMsg(data, () => {
  361. scrollBottom();
  362. });
  363. }
  364. function onSendMessage() {
  365. if (!socketState.value.isConnect) {
  366. sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  367. return;
  368. }
  369. if (!chat.msg) return;
  370. const data = {
  371. from: 'customer',
  372. mode: 'text',
  373. date: new Date().getTime(),
  374. content: {
  375. text: chat.msg,
  376. },
  377. };
  378. socketSendMsg(data, () => {
  379. scrollBottom();
  380. });
  381. chat.showTools = false;
  382. // scrollBottom();
  383. setTimeout(() => {
  384. chat.msg = '';
  385. }, 100);
  386. }
  387. // 点击猜你想问
  388. function onTemplateList(e) {
  389. if (!socketState.value.isConnect) {
  390. sheep.$helper.toast(socketState.value.tip || '您已掉线!请返回重试');
  391. return;
  392. }
  393. const data = {
  394. from: 'customer',
  395. mode: 'text',
  396. date: new Date().getTime(),
  397. content: {
  398. text: e.title,
  399. },
  400. customData: {
  401. question_id: e.id,
  402. },
  403. };
  404. socketSendMsg(data, () => {
  405. scrollBottom();
  406. });
  407. // scrollBottom();
  408. }
  409. function selEmojiFile(name) {
  410. for (let index in emojiList) {
  411. if (emojiList[index].name === name) {
  412. return emojiList[index].file;
  413. }
  414. }
  415. return false;
  416. }
  417. function replaceEmoji(data) {
  418. let newData = data;
  419. if (typeof newData !== 'object') {
  420. let reg = /\[(.+?)\]/g; // [] 中括号
  421. let zhEmojiName = newData.match(reg);
  422. if (zhEmojiName) {
  423. zhEmojiName.forEach((item) => {
  424. let emojiFile = selEmojiFile(item);
  425. newData = newData.replace(
  426. item,
  427. `<img class="chat-img" style="width: 24px;height: 24px;margin: 0 3px;" src="${sheep.$url.cdn(
  428. '/static/img/chat/emoji/' + emojiFile,
  429. )}"/>`,
  430. );
  431. });
  432. }
  433. }
  434. return newData;
  435. }
  436. function scrollBottom() {
  437. let timeout = null;
  438. chat.scrollInto = '';
  439. clearTimeout(timeout);
  440. timeout = setTimeout(() => {
  441. chat.scrollInto = 'scrollBottom';
  442. }, 100);
  443. }
  444. onLoad(async () => {
  445. const { error } = await getUserToken();
  446. if (error === 0) {
  447. socketInit(chatConfig.value, () => {
  448. scrollBottom();
  449. });
  450. } else {
  451. socketState.value.isConnect = false;
  452. }
  453. });
  454. </script>
  455. <style lang="scss" scoped>
  456. .page-bg {
  457. width: 100%;
  458. position: absolute;
  459. top: 0;
  460. left: 0;
  461. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  462. background-size: 750rpx 100%;
  463. z-index: 1;
  464. }
  465. .chat-wrap {
  466. // :deep() {
  467. // .ui-navbar-box {
  468. // background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  469. // }
  470. // }
  471. .status {
  472. position: relative;
  473. box-sizing: border-box;
  474. z-index: 3;
  475. height: 70rpx;
  476. padding: 0 30rpx;
  477. background: var(--ui-BG-Main-opacity-1);
  478. display: flex;
  479. align-items: center;
  480. font-size: 30rpx;
  481. font-weight: 400;
  482. color: var(--ui-BG-Main);
  483. }
  484. .chat-box {
  485. padding: 0 20rpx 0;
  486. .loadmore-btn {
  487. width: 98%;
  488. height: 40px;
  489. font-size: 12px;
  490. color: #8c8c8c;
  491. .loadmore-icon {
  492. transform: rotate(90deg);
  493. }
  494. }
  495. .message-item {
  496. margin-bottom: 33rpx;
  497. }
  498. .date-message,
  499. .system-message {
  500. width: fit-content;
  501. border-radius: 12rpx;
  502. padding: 8rpx 16rpx;
  503. margin-bottom: 16rpx;
  504. background-color: var(--ui-BG-3);
  505. color: #999;
  506. font-size: 24rpx;
  507. }
  508. .chat-avatar {
  509. width: 70rpx;
  510. height: 70rpx;
  511. border-radius: 50%;
  512. }
  513. .send-status {
  514. color: #333;
  515. height: 80rpx;
  516. margin-right: 8rpx;
  517. display: flex;
  518. align-items: center;
  519. .loading {
  520. width: 32rpx;
  521. height: 32rpx;
  522. -webkit-animation: rotating 2s linear infinite;
  523. animation: rotating 2s linear infinite;
  524. @-webkit-keyframes rotating {
  525. 0% {
  526. transform: rotateZ(0);
  527. }
  528. 100% {
  529. transform: rotateZ(360deg);
  530. }
  531. }
  532. @keyframes rotating {
  533. 0% {
  534. transform: rotateZ(0);
  535. }
  536. 100% {
  537. transform: rotateZ(360deg);
  538. }
  539. }
  540. }
  541. .warning {
  542. width: 32rpx;
  543. height: 32rpx;
  544. color: #ff3000;
  545. }
  546. }
  547. .message-box {
  548. max-width: 50%;
  549. font-size: 16px;
  550. line-height: 20px;
  551. // max-width: 500rpx;
  552. white-space: normal;
  553. word-break: break-all;
  554. word-wrap: break-word;
  555. padding: 20rpx;
  556. border-radius: 10rpx;
  557. color: #fff;
  558. background: linear-gradient(90deg, var(--ui-BG-Main), var(--ui-BG-Main-gradient));
  559. &.customer_service {
  560. background: #fff;
  561. color: #333;
  562. }
  563. :deep() {
  564. .imgred {
  565. width: 100%;
  566. }
  567. .imgred,
  568. img {
  569. width: 100%;
  570. }
  571. }
  572. }
  573. :deep() {
  574. .goods,
  575. .order {
  576. max-width: 500rpx;
  577. }
  578. }
  579. .message-img {
  580. width: 100px;
  581. height: 100px;
  582. border-radius: 6rpx;
  583. }
  584. .template-wrap {
  585. // width: 100%;
  586. padding: 20rpx 24rpx;
  587. background: #fff;
  588. border-radius: 10rpx;
  589. .title {
  590. font-size: 26rpx;
  591. font-weight: 500;
  592. color: #333;
  593. margin-bottom: 29rpx;
  594. }
  595. .item {
  596. font-size: 24rpx;
  597. color: var(--ui-BG-Main);
  598. margin-bottom: 16rpx;
  599. &:last-of-type {
  600. margin-bottom: 0;
  601. }
  602. }
  603. }
  604. .error-img {
  605. width: 400rpx;
  606. height: 400rpx;
  607. }
  608. #scrollBottom {
  609. height: 120rpx;
  610. }
  611. }
  612. }
  613. </style>
  614. <style>
  615. .chat-img {
  616. width: 24px;
  617. height: 24px;
  618. margin: 0 3px;
  619. }
  620. .full-img {
  621. object-fit: cover;
  622. width: 100px;
  623. height: 100px;
  624. border-radius: 6px;
  625. }
  626. </style>