index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. <template>
  2. <s-layout ref="layoutRef" class="chat-wrap" title="ai" navbar="inner">
  3. <EasyHover :iconUrl="sheep.$url.static('/static/home/xq.png')" :circle="false" :height="80" :initMarginTop="1000"
  4. :width="80" @taped="triged" :stickSide="true" initSide="right">
  5. </EasyHover>
  6. <!-- <view class="dropdownClass">
  7. <u-dropdown>
  8. <u-dropdown-item v-model="value1" :title="options1[value1 - 1].label" :options="options1"></u-dropdown-item>
  9. </u-dropdown>
  10. </view> -->
  11. <!-- 覆盖头部导航栏背景颜色 -->
  12. <view class="page-bg" :style="{ height: sys_navBar + 'px' }"></view>
  13. <!-- 聊天区域 -->
  14. <MessageList ref="messageListRef" :queryShow="false">
  15. <template #bottom>
  16. <message-input :loading="loadingInput" v-model="chat.msg" @on-tools="onTools"
  17. @send-message="onSendMessage"></message-input>
  18. </template>
  19. </MessageList>
  20. <!-- 聊天工具 -->
  21. <tools-popup :show-tools="chat.showTools" :tools-mode="chat.toolsMode" @close="handleToolsClose" @on-emoji="onEmoji"
  22. @image-select="onSelect" @on-show-select="onShowSelect">
  23. <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage"></message-input>
  24. </tools-popup>
  25. <!-- 产品订单选择 -->
  26. <SelectPopup :mode="chat.selectMode" :show="chat.showSelect" @select="onSelect" @close="chat.showSelect = false" />
  27. <EventSource ref="EventSourceRef" :url="eventSourceUrl" :options="eventSourceOptions" @callback="handleCallback" />
  28. <template #right>
  29. <view class="lsdhClass">
  30. <view class="lsdhDetailClass addClass" style="" @click="addChat">
  31. <u-icon name="plus"></u-icon>
  32. 新增对话
  33. </view>
  34. <view @click="detailCLick(item)" class="lsdhDetailClass" v-for="item in lsjlList" :key="item">
  35. {{ item.title }}
  36. </view>
  37. </view>
  38. </template>
  39. </s-layout>
  40. </template>
  41. <script setup>
  42. import MessageList from '@/pages/chat/components/messageList.vue';
  43. import EventSource from './eventSource.vue';
  44. import { reactive, ref, toRefs, computed, provide, onMounted } from 'vue';
  45. import sheep from '@/sheep';
  46. import ToolsPopup from '@/pages/chat/components/toolsPopup.vue';
  47. import MessageInput from '@/pages/chat/components/messageInput.vue';
  48. import SelectPopup from '@/pages/chat/components/select-popup.vue';
  49. import EasyHover from '@/components/easy-hover/easy-hover.vue'
  50. import {
  51. KeFuMessageContentTypeEnum
  52. } from '../util/constants';
  53. import FileApi from '@/sheep/api/infra/file';
  54. import KeFuApi from '@/sheep/api/promotion/kefu';
  55. import { useWebSocket } from '@/sheep/hooks/useWebSocket';
  56. import { jsonParse } from '@/sheep/util';
  57. import { onLoad } from '@dcloudio/uni-app';
  58. const EventSourceRef = ref(null); //AIref
  59. const chatMsgData = ref({});
  60. const messageListRef = ref();
  61. const eventSourceUrl = import.meta.env.SHOPRO_BASE_URL + '/app-api/ai/chat/message/dify-stream'; //ai客服URL
  62. // const eventSourceUrl = "https://192.168.10.17:9095/app-api/infra/ai-dify/chat-messages-stream"; //ai客服URL
  63. // ai客服流式上传参数
  64. const eventSourceOptions = computed(() => {
  65. return {
  66. headers: {
  67. 'content-type': 'application/json',
  68. Accept: 'text/event-stream',
  69. 'tenant-id': 1,
  70. authorization: uni.getStorageSync('token'),
  71. },
  72. method: 'POST',
  73. body: JSON.stringify({
  74. contentType: 1,
  75. content: chatMsgData.value.content || chat.msg,
  76. relUserId: route.value.relUserId,
  77. stateId: messageListRef.value ? messageListRef.value.messageList.length + 2 : 2,
  78. conversationId: reateDifyParams.value.id,
  79. difyConversationId: difyConversationId.value,
  80. // type: "律师咨询",
  81. // query: chat.msg
  82. }), // 请求体
  83. };
  84. });
  85. const layoutRef = ref(null); // 布局组件引用
  86. const lsjlList = ref([]);
  87. const detailCLick = (item) => {
  88. KeFuApi.conversationId({
  89. tenantId: item.id,
  90. conversationId: item.id,
  91. }).then((res) => {
  92. messageListRef.value.messageList = res.data.reverse().map((item) => {
  93. if (item.type == 'user') {
  94. item.senderId = userInfo.value.id;
  95. }
  96. return item;
  97. });
  98. layoutRef.value.show = false;
  99. });
  100. };
  101. const lsjlClick = () => {
  102. KeFuApi.conversationMyList().then((res) => {
  103. lsjlList.value = res.data;
  104. layoutRef.value.show = true;
  105. console.log(layoutRef.value, 222221111)
  106. });
  107. };
  108. const triged = () => {
  109. console.log(7777)
  110. lsjlClick()
  111. }
  112. const answerArr = ref('');
  113. const loadingInput = ref(false); // 加载状态
  114. provide('loadingInput', loadingInput); // 依赖注入加载状态
  115. const EventSourceFun = async (data, is = true) => {
  116. console.log('张耀文2', data)
  117. loadingInput.value = true;
  118. chatMsgData.value = data;
  119. const avatarObj = messageListRef.value.getAvatar() || {};
  120. const params = {
  121. // id: 1,
  122. conversationId: reateDifyParams.value.id,
  123. senderId: userInfo.value.id,
  124. senderAvatar: avatarObj.senderAvatar || '',
  125. senderType: 1,
  126. stateId: messageListRef.value.messageList.length + 2,
  127. receiverId: route.value.relUserId,
  128. receiverAvatar: avatarObj.receiverAvatar || '',
  129. receiverType: null,
  130. contentType: 1,
  131. content: is ? JSON.stringify({ text: chat.msg }) : data.content,
  132. readStatus: true,
  133. createTime: 1745546275000,
  134. };
  135. if (data.content != 'ecologicalValueCalculate') {
  136. await messageListRef.value.refreshMessageList(JSON.parse(JSON.stringify(params)));
  137. }
  138. const params1 = {
  139. ids: 1,
  140. isAi: true,
  141. isLoading: true,
  142. conversationId: reateDifyParams.value.id,
  143. senderId: route.value.relUserId,
  144. senderAvatar: avatarObj.receiverAvatar || '',
  145. stateId: messageListRef.value.messageList.length + 2,
  146. senderType: 1,
  147. receiverId: userInfo.value.id,
  148. receiverAvatar: avatarObj.senderAvatar || '',
  149. receiverType: null,
  150. contentType: 22,
  151. content: '',
  152. readStatus: true,
  153. createTime: 1745546275000,
  154. };
  155. await messageListRef.value.refreshMessageList(JSON.parse(JSON.stringify(params1)));
  156. await EventSourceRef.value.send(data);
  157. };
  158. provide('EventSourceFun', EventSourceFun); // 依赖注入加载状态
  159. const loadingId = ref(''); // 加载的id
  160. // ai客服发送消息接收回调
  161. const difyConversationId = ref('');
  162. const handleCallback = async (e) => {
  163. const { type, msg, data } = e || {};
  164. if (type == 'onmessage') {
  165. const datas = JSON.parse(data);
  166. console.log(datas, 66666);
  167. difyConversationId.value = datas.data.receive.difyConversationId;
  168. if (datas.data.receive.event === null) {
  169. await messageListRef.value.updateMessage({ ...datas.data.receive, contentType: 1 });
  170. } else if (datas.data.receive.event == 'message') {
  171. await messageListRef.value.updateMessage(datas.data.receive);
  172. }
  173. }
  174. if (type == 'onclose') {
  175. loadingInput.value = false;
  176. // await messageListRef.value.updateMessage({}, loadingId.value);
  177. answerArr.value = '';
  178. }
  179. };
  180. const sys_navBar = sheep.$platform.navbar;
  181. const options1 = [
  182. {
  183. label: 'ai',
  184. value: 1,
  185. },
  186. {
  187. label: '律师咨询',
  188. value: 2,
  189. },
  190. {
  191. label: '金融相关',
  192. value: 3,
  193. },
  194. ];
  195. const value1 = ref(1);
  196. const chat = reactive({
  197. msg: '',
  198. scrollInto: '',
  199. showTools: false,
  200. toolsMode: '',
  201. showSelect: false,
  202. selectMode: '',
  203. });
  204. const route = ref({});
  205. const reateDifyParams = ref({});
  206. const init = async () => {
  207. const res = await KeFuApi.sendCreateDify({
  208. roleId: 666,
  209. knowledgeId: 1204,
  210. });
  211. reateDifyParams.value = res.data;
  212. };
  213. onLoad((options) => {
  214. route.value = options;
  215. });
  216. onMounted(async () => {
  217. await init();
  218. loadingInput.value = true;
  219. const data = {
  220. conversationId: reateDifyParams.value.id,
  221. contentType: KeFuMessageContentTypeEnum.TEXT,
  222. stateId: messageListRef.value ? messageListRef.value.messageList.length + 2 : 2,
  223. content: 'ecologicalValueCalculate',
  224. relUserId: route.value.relUserId,
  225. };
  226. await EventSourceFun(data, false);
  227. });
  228. const userInfo = computed(() => sheep.$store('user').userInfo);
  229. const addChat = async () => {
  230. await init();
  231. loadingInput.value = true;
  232. messageListRef.value.messageList = [];
  233. layoutRef.value.show = false;
  234. difyConversationId.value = '';
  235. const data = {
  236. conversationId: reateDifyParams.value.id,
  237. contentType: KeFuMessageContentTypeEnum.TEXT,
  238. stateId: messageListRef.value ? messageListRef.value.messageList.length + 2 : 2,
  239. content: 'ecologicalValueCalculate',
  240. relUserId: route.value.relUserId,
  241. };
  242. await EventSourceFun(data, false);
  243. }
  244. // 发送消息
  245. async function onSendMessage() {
  246. if (!chat.msg) return;
  247. try {
  248. loadingInput.value = true;
  249. const data = {
  250. conversationId: reateDifyParams.value.id,
  251. contentType: KeFuMessageContentTypeEnum.TEXT,
  252. stateId: messageListRef.value.messageList.length + 2,
  253. content: JSON.stringify({ text: chat.msg }),
  254. relUserId: route.value.relUserId,
  255. };
  256. await EventSourceFun(data);
  257. chat.msg = '';
  258. } finally {
  259. chat.showTools = false;
  260. }
  261. }
  262. //======================= 聊天工具相关 start =======================
  263. function handleToolsClose() {
  264. chat.showTools = false;
  265. chat.toolsMode = '';
  266. }
  267. function onEmoji(item) {
  268. chat.msg += item.name;
  269. }
  270. // 点击工具栏开关
  271. function onTools(mode) {
  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. console.log(data, 555222233);
  287. let msg;
  288. switch (type) {
  289. case 'image':
  290. const res = await FileApi.uploadFile(data.tempFiles[0].path);
  291. msg = {
  292. contentType: KeFuMessageContentTypeEnum.IMAGE,
  293. content: JSON.stringify({ picUrl: res.data }),
  294. conversationId: reateDifyParams.value.id,
  295. };
  296. break;
  297. case 'goods':
  298. msg = {
  299. contentType: KeFuMessageContentTypeEnum.PRODUCT,
  300. content: JSON.stringify(data),
  301. conversationId: reateDifyParams.value.id,
  302. };
  303. break;
  304. case 'order':
  305. msg = {
  306. contentType: KeFuMessageContentTypeEnum.ORDER,
  307. content: JSON.stringify(data),
  308. conversationId: reateDifyParams.value.id,
  309. };
  310. break;
  311. }
  312. if (msg) {
  313. // 发送消息
  314. // scrollBottom();
  315. // await KeFuApi.sendKefuMessage(msg);
  316. await KeFuApi.listSendNew(msg);
  317. await messageListRef.value.refreshMessageList();
  318. chat.showTools = false;
  319. chat.showSelect = false;
  320. chat.selectMode = '';
  321. }
  322. }
  323. </script>
  324. <style scoped lang="scss">
  325. :deep(.z-paging-content-fixed) {
  326. // background: red;
  327. // top: 40px;
  328. }
  329. :deep(.zp-paging-container-content) {
  330. padding: 0px 10px;
  331. }
  332. .dropdownClass {
  333. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  334. }
  335. .chat-wrap {
  336. .page-bg {
  337. width: 100%;
  338. position: absolute;
  339. top: 0;
  340. left: 0;
  341. background-color: #3a74f2;
  342. z-index: 1;
  343. }
  344. .status {
  345. position: relative;
  346. box-sizing: border-box;
  347. z-index: 3;
  348. height: 70rpx;
  349. padding: 0 30rpx;
  350. background: var(--ui-BG-Main-opacity-1);
  351. display: flex;
  352. align-items: center;
  353. font-size: 30rpx;
  354. font-weight: 400;
  355. color: var(--ui-BG-Main);
  356. }
  357. }
  358. .lsdhClass {
  359. margin: 20px 0;
  360. height: calc(100vh - 40px);
  361. overflow: auto;
  362. .addClass {
  363. background: #C1D6F7;
  364. border: #C1D6F7 2px solid;
  365. color: #4076FE;
  366. border-radius: 20px;
  367. font-weight: 800;
  368. padding: 16px;
  369. margin: 20px;
  370. }
  371. .lsdhDetailClass {
  372. background: #f5f7f9;
  373. padding: 16px;
  374. margin: 20px;
  375. width: 60vw;
  376. }
  377. }
  378. a {
  379. color: red !important;
  380. }
  381. </style>