index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <s-layout class="chat-wrap" :title="!isReconnecting ? '连接客服成功' : '会话重连中'" navbar="inner">
  3. <view class="dropdownClass">
  4. <u-dropdown>
  5. <u-dropdown-item v-model="value1" :title="options1[value1 - 1].label" :options="options1"></u-dropdown-item>
  6. </u-dropdown>
  7. </view>
  8. <!-- 覆盖头部导航栏背景颜色 -->
  9. <view class="page-bg" :style="{ height: sys_navBar + 'px' }"></view>
  10. <!-- 聊天区域 -->
  11. <MessageList ref="messageListRef">
  12. <template #bottom>
  13. <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage"></message-input>
  14. </template>
  15. </MessageList>
  16. <!-- 聊天工具 -->
  17. <tools-popup :show-tools="chat.showTools" :tools-mode="chat.toolsMode" @close="handleToolsClose" @on-emoji="onEmoji"
  18. @image-select="onSelect" @on-show-select="onShowSelect">
  19. <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage"></message-input>
  20. </tools-popup>
  21. <!-- 产品订单选择 -->
  22. <SelectPopup :mode="chat.selectMode" :show="chat.showSelect" @select="onSelect" @close="chat.showSelect = false" />
  23. </s-layout>
  24. </template>
  25. <script setup>
  26. import MessageList from '@/pages/chat/components/messageList.vue';
  27. import { reactive, ref, toRefs } from 'vue';
  28. import sheep from '@/sheep';
  29. import ToolsPopup from '@/pages/chat/components/toolsPopup.vue';
  30. import MessageInput from '@/pages/chat/components/messageInput.vue';
  31. import SelectPopup from '@/pages/chat/components/select-popup.vue';
  32. import {
  33. KeFuMessageContentTypeEnum,
  34. WebSocketMessageTypeConstants,
  35. } from '@/pages/chat/util/constants';
  36. import FileApi from '@/sheep/api/infra/file';
  37. import KeFuApi from '@/sheep/api/promotion/kefu';
  38. import { useWebSocket } from '@/sheep/hooks/useWebSocket';
  39. import { jsonParse } from '@/sheep/util';
  40. import { onLoad } from '@dcloudio/uni-app';
  41. const sys_navBar = sheep.$platform.navbar;
  42. const options1 = [{
  43. label: 'ai',
  44. value: 1,
  45. },
  46. {
  47. label: '律师咨询',
  48. value: 2,
  49. },
  50. {
  51. label: '金融相关',
  52. value: 3,
  53. }
  54. ]
  55. const value1 = ref(1)
  56. const chat = reactive({
  57. msg: '',
  58. scrollInto: '',
  59. showTools: false,
  60. toolsMode: '',
  61. showSelect: false,
  62. selectMode: '',
  63. });
  64. const route = ref({})
  65. onLoad((options) => {
  66. route.value = options
  67. });
  68. // 发送消息
  69. async function onSendMessage() {
  70. if (!chat.msg) return;
  71. try {
  72. const data = {
  73. conversationId: route.value.conversationId,
  74. contentType: KeFuMessageContentTypeEnum.TEXT,
  75. content: JSON.stringify({ text: chat.msg }),
  76. };
  77. await KeFuApi.sendKefuMessage(data);
  78. await messageListRef.value.refreshMessageList();
  79. chat.msg = '';
  80. } finally {
  81. chat.showTools = false;
  82. }
  83. }
  84. const messageListRef = ref();
  85. //======================= 聊天工具相关 start =======================
  86. function handleToolsClose() {
  87. chat.showTools = false;
  88. chat.toolsMode = '';
  89. }
  90. function onEmoji(item) {
  91. chat.msg += item.name;
  92. }
  93. // 点击工具栏开关
  94. function onTools(mode) {
  95. if (isReconnecting.value) {
  96. sheep.$helper.toast('您已掉线!请返回重试');
  97. return;
  98. }
  99. if (!chat.toolsMode || chat.toolsMode === mode) {
  100. chat.showTools = !chat.showTools;
  101. }
  102. chat.toolsMode = mode;
  103. if (!chat.showTools) {
  104. chat.toolsMode = '';
  105. }
  106. }
  107. function onShowSelect(mode) {
  108. chat.showTools = false;
  109. chat.showSelect = true;
  110. chat.selectMode = mode;
  111. }
  112. async function onSelect({ type, data }) {
  113. let msg;
  114. switch (type) {
  115. case 'image':
  116. const res = await FileApi.uploadFile(data.tempFiles[0].path);
  117. msg = {
  118. contentType: KeFuMessageContentTypeEnum.IMAGE,
  119. content: JSON.stringify({ picUrl: res.data }),
  120. };
  121. break;
  122. case 'goods':
  123. msg = {
  124. contentType: KeFuMessageContentTypeEnum.PRODUCT,
  125. content: JSON.stringify(data),
  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. },
  167. });
  168. const isReconnecting = toRefs(options).isReconnecting; // 重连状态
  169. </script>
  170. <style scoped lang="scss">
  171. :deep(.z-paging-content-fixed) {
  172. // background: red;
  173. top: 40px;
  174. }
  175. :deep(.zp-paging-container-content) {
  176. padding: 0px 10px;
  177. }
  178. .dropdownClass {
  179. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  180. }
  181. .chat-wrap {
  182. .page-bg {
  183. width: 100%;
  184. position: absolute;
  185. top: 0;
  186. left: 0;
  187. background-color: var(--ui-BG-Main);
  188. z-index: 1;
  189. }
  190. .status {
  191. position: relative;
  192. box-sizing: border-box;
  193. z-index: 3;
  194. height: 70rpx;
  195. padding: 0 30rpx;
  196. background: var(--ui-BG-Main-opacity-1);
  197. display: flex;
  198. align-items: center;
  199. font-size: 30rpx;
  200. font-weight: 400;
  201. color: var(--ui-BG-Main);
  202. }
  203. }
  204. </style>