index.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, computed } 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. relUserId: route.value.relUserId
  77. };
  78. await KeFuApi.sendKefuMessageNew(data);
  79. await messageListRef.value.refreshMessageList();
  80. chat.msg = '';
  81. } finally {
  82. chat.showTools = false;
  83. }
  84. }
  85. const messageListRef = ref();
  86. //======================= 聊天工具相关 start =======================
  87. function handleToolsClose() {
  88. chat.showTools = false;
  89. chat.toolsMode = '';
  90. }
  91. function onEmoji(item) {
  92. chat.msg += item.name;
  93. }
  94. // 点击工具栏开关
  95. function onTools(mode) {
  96. if (isReconnecting.value) {
  97. sheep.$helper.toast('您已掉线!请返回重试');
  98. return;
  99. }
  100. if (!chat.toolsMode || chat.toolsMode === mode) {
  101. chat.showTools = !chat.showTools;
  102. }
  103. chat.toolsMode = mode;
  104. if (!chat.showTools) {
  105. chat.toolsMode = '';
  106. }
  107. }
  108. function onShowSelect(mode) {
  109. chat.showTools = false;
  110. chat.showSelect = true;
  111. chat.selectMode = mode;
  112. }
  113. async function onSelect({ type, data }) {
  114. let msg;
  115. switch (type) {
  116. case 'image':
  117. const res = await FileApi.uploadFile(data.tempFiles[0].path);
  118. msg = {
  119. contentType: KeFuMessageContentTypeEnum.IMAGE,
  120. content: JSON.stringify({ picUrl: res.data }),
  121. };
  122. break;
  123. case 'goods':
  124. msg = {
  125. contentType: KeFuMessageContentTypeEnum.PRODUCT,
  126. content: JSON.stringify(data),
  127. };
  128. break;
  129. case 'order':
  130. msg = {
  131. contentType: KeFuMessageContentTypeEnum.ORDER,
  132. content: JSON.stringify(data),
  133. };
  134. break;
  135. }
  136. if (msg) {
  137. // 发送消息
  138. // scrollBottom();
  139. await KeFuApi.sendKefuMessage(msg);
  140. await messageListRef.value.refreshMessageList();
  141. chat.showTools = false;
  142. chat.showSelect = false;
  143. chat.selectMode = '';
  144. }
  145. }
  146. //======================= 聊天工具相关 end =======================
  147. const { options } = useWebSocket({
  148. // 连接成功
  149. onConnected: async () => { },
  150. // 收到消息
  151. onMessage: async (data) => {
  152. const type = data.type;
  153. if (!type) {
  154. console.error('未知的消息类型:' + data);
  155. return;
  156. }
  157. // 2.2 消息类型:KEFU_MESSAGE_TYPE
  158. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_TYPE) {
  159. // 刷新消息列表
  160. await messageListRef.value.refreshMessageList(jsonParse(data.content));
  161. return;
  162. }
  163. // 2.3 消息类型:KEFU_MESSAGE_ADMIN_READ
  164. if (type === WebSocketMessageTypeConstants.KEFU_MESSAGE_ADMIN_READ) {
  165. console.log('管理员已读消息');
  166. }
  167. },
  168. });
  169. const isReconnecting = toRefs(options).isReconnecting; // 重连状态
  170. </script>
  171. <style scoped lang="scss">
  172. :deep(.z-paging-content-fixed) {
  173. // background: red;
  174. top: 40px;
  175. }
  176. :deep(.zp-paging-container-content) {
  177. padding: 0px 10px;
  178. }
  179. .dropdownClass {
  180. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  181. }
  182. .chat-wrap {
  183. .page-bg {
  184. width: 100%;
  185. position: absolute;
  186. top: 0;
  187. left: 0;
  188. background-color: var(--ui-BG-Main);
  189. z-index: 1;
  190. }
  191. .status {
  192. position: relative;
  193. box-sizing: border-box;
  194. z-index: 3;
  195. height: 70rpx;
  196. padding: 0 30rpx;
  197. background: var(--ui-BG-Main-opacity-1);
  198. display: flex;
  199. align-items: center;
  200. font-size: 30rpx;
  201. font-weight: 400;
  202. color: var(--ui-BG-Main);
  203. }
  204. }
  205. </style>