index.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <template>
  2. <s-layout class="chat-wrap" :title="!isReconnecting ? '连接客服成功' : '会话重连中'" navbar="inner">
  3. <!-- 覆盖头部导航栏背景颜色 -->
  4. <view class="page-bg" :style="{ height: sys_navBar + 'px' }"></view>
  5. <!-- 聊天区域 -->
  6. <MessageList ref="messageListRef">
  7. <template #bottom>
  8. <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage" :auto-focus="false"
  9. :show-char-count="true" :max-length="500"></message-input>
  10. </template>
  11. </MessageList>
  12. <!-- 聊天工具 -->
  13. <tools-popup :show-tools="chat.showTools" :tools-mode="chat.toolsMode" @close="handleToolsClose" @on-emoji="onEmoji"
  14. @image-select="onSelect" @on-show-select="onShowSelect">
  15. <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage" :auto-focus="false"
  16. :show-char-count="true" :max-length="500"></message-input>
  17. </tools-popup>
  18. <!-- 商品订单选择 -->
  19. <SelectPopup :mode="chat.selectMode" :show="chat.showSelect" @select="onSelect" @close="chat.showSelect = false" />
  20. </s-layout>
  21. </template>
  22. <script setup>
  23. import MessageList from '@/pages/chat/components/messageList.vue';
  24. import { reactive, ref, toRefs } from 'vue';
  25. import sheep from '@/sheep';
  26. import ToolsPopup from '@/pages/chat/components/toolsPopup.vue';
  27. import MessageInput from '@/pages/chat/components/messageInput.vue';
  28. import SelectPopup from '@/pages/chat/components/select-popup.vue';
  29. import {
  30. KeFuMessageContentTypeEnum,
  31. WebSocketMessageTypeConstants,
  32. } from '@/pages/chat/util/constants';
  33. import FileApi from '@/sheep/api/infra/file';
  34. import KeFuApi from '@/sheep/api/promotion/kefu';
  35. import { useWebSocket } from '@/sheep/hooks/useWebSocket';
  36. import { jsonParse } from '@/sheep/util';
  37. import { onLoad } from '@dcloudio/uni-app';
  38. const sys_navBar = sheep.$platform.navbar;
  39. const route = ref({})
  40. onLoad((options) => {
  41. route.value = options
  42. });
  43. const chat = reactive({
  44. msg: '',
  45. scrollInto: '',
  46. showTools: false,
  47. toolsMode: '',
  48. showSelect: false,
  49. selectMode: '',
  50. });
  51. // 发送消息
  52. async function onSendMessage() {
  53. if (!chat.msg) return;
  54. try {
  55. const data = {
  56. contentType: KeFuMessageContentTypeEnum.TEXT,
  57. content: JSON.stringify({ text: chat.msg }),
  58. conversationId: route.value.conversationId,
  59. };
  60. await KeFuApi.sendKefuMessage(data);
  61. await messageListRef.value.refreshMessageList();
  62. chat.msg = '';
  63. } finally {
  64. chat.showTools = false;
  65. }
  66. }
  67. const messageListRef = ref();
  68. //======================= 聊天工具相关 start =======================
  69. function handleToolsClose() {
  70. chat.showTools = false;
  71. chat.toolsMode = '';
  72. }
  73. function onEmoji(item) {
  74. chat.msg += item.name;
  75. }
  76. // 点击工具栏开关
  77. function onTools(mode) {
  78. if (isReconnecting.value) {
  79. sheep.$helper.toast('您已掉线!请返回重试');
  80. return;
  81. }
  82. // 第二次点击关闭
  83. if (chat.showTools && chat.toolsMode === mode) {
  84. handleToolsClose();
  85. return;
  86. }
  87. // 切换工具栏
  88. if (chat.showTools && chat.toolsMode !== mode) {
  89. chat.showTools = false;
  90. chat.toolsMode = '';
  91. }
  92. // 延迟打开等一下过度效果
  93. setTimeout(() => {
  94. chat.toolsMode = mode;
  95. chat.showTools = true;
  96. }, 200);
  97. }
  98. function onShowSelect(mode) {
  99. chat.showTools = false;
  100. chat.showSelect = true;
  101. chat.selectMode = mode;
  102. }
  103. async function onSelect({ type, data }) {
  104. let msg;
  105. switch (type) {
  106. case 'image':
  107. const res = await FileApi.uploadFile(data.tempFiles[0].path);
  108. msg = {
  109. contentType: KeFuMessageContentTypeEnum.IMAGE,
  110. content: JSON.stringify({ picUrl: res.data }),
  111. conversationId: route.value.conversationId,
  112. };
  113. break;
  114. case 'video':
  115. msg = {
  116. contentType: KeFuMessageContentTypeEnum.VIDEO,
  117. content: JSON.stringify({ videoUrl: data }),
  118. conversationId: route.value.conversationId,
  119. };
  120. break;
  121. case 'goods':
  122. msg = {
  123. contentType: KeFuMessageContentTypeEnum.PRODUCT,
  124. content: JSON.stringify(data),
  125. conversationId: route.value.conversationId,
  126. };
  127. break;
  128. case 'order':
  129. msg = {
  130. contentType: KeFuMessageContentTypeEnum.ORDER,
  131. content: JSON.stringify(data),
  132. };
  133. break;
  134. }
  135. if (msg) {
  136. // 发送消息
  137. // scrollBottom();
  138. await KeFuApi.sendKefuMessage(msg);
  139. await messageListRef.value.refreshMessageList();
  140. chat.showTools = false;
  141. chat.showSelect = false;
  142. chat.selectMode = '';
  143. }
  144. }
  145. //======================= 聊天工具相关 end =======================
  146. const { options } = useWebSocket({
  147. // 连接成功
  148. onConnected: async () => { },
  149. // 收到消息
  150. onMessage: async (data) => {
  151. const type = data.type;
  152. if (!type) {
  153. console.error('未知的消息类型:' + data);
  154. return;
  155. }
  156. // 2.2 消息类型:KEFU_MESSAGE_TYPE
  157. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
  158. // 刷新消息列表
  159. await messageListRef.value.refreshMessageList(jsonParse(data.content));
  160. return;
  161. }
  162. // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
  163. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
  164. console.log('管理员已读消息');
  165. // 更新消息已读状态
  166. sheep.$helper.toast('客服已读您的消息');
  167. }
  168. },
  169. });
  170. const isReconnecting = toRefs(options).isReconnecting; // 重连状态
  171. </script>
  172. <style scoped lang="scss">
  173. .chat-wrap {
  174. .page-bg {
  175. width: 100%;
  176. position: absolute;
  177. top: 0;
  178. left: 0;
  179. background-color: var(--ui-BG-Main);
  180. z-index: 1;
  181. }
  182. .status {
  183. position: relative;
  184. box-sizing: border-box;
  185. z-index: 3;
  186. height: 70rpx;
  187. padding: 0 30rpx;
  188. background: var(--ui-BG-Main-opacity-1);
  189. display: flex;
  190. align-items: center;
  191. font-size: 30rpx;
  192. font-weight: 400;
  193. color: var(--ui-BG-Main);
  194. }
  195. }
  196. </style>