|
@@ -10,7 +10,8 @@
|
|
<!-- 聊天区域 -->
|
|
<!-- 聊天区域 -->
|
|
<MessageList ref="messageListRef">
|
|
<MessageList ref="messageListRef">
|
|
<template #bottom>
|
|
<template #bottom>
|
|
- <message-input v-model="chat.msg" @on-tools="onTools" @send-message="onSendMessage"></message-input>
|
|
|
|
|
|
+ <message-input :loading="loadingInput" v-model="chat.msg" @on-tools="onTools"
|
|
|
|
+ @send-message="onSendMessage"></message-input>
|
|
</template>
|
|
</template>
|
|
</MessageList>
|
|
</MessageList>
|
|
<!-- 聊天工具 -->
|
|
<!-- 聊天工具 -->
|
|
@@ -20,12 +21,14 @@
|
|
</tools-popup>
|
|
</tools-popup>
|
|
<!-- 产品订单选择 -->
|
|
<!-- 产品订单选择 -->
|
|
<SelectPopup :mode="chat.selectMode" :show="chat.showSelect" @select="onSelect" @close="chat.showSelect = false" />
|
|
<SelectPopup :mode="chat.selectMode" :show="chat.showSelect" @select="onSelect" @close="chat.showSelect = false" />
|
|
|
|
+ <EventSource ref="EventSourceRef" :url="eventSourceUrl" :options="eventSourceOptions" @callback="handleCallback" />
|
|
</s-layout>
|
|
</s-layout>
|
|
</template>
|
|
</template>
|
|
|
|
|
|
<script setup>
|
|
<script setup>
|
|
import MessageList from '@/pages/chat/components/messageList.vue';
|
|
import MessageList from '@/pages/chat/components/messageList.vue';
|
|
-import { reactive, ref, toRefs, computed } from 'vue';
|
|
|
|
|
|
+import EventSource from '@/pages/chat/components/eventSource.vue';
|
|
|
|
+import { reactive, ref, toRefs, computed, provide } from 'vue';
|
|
import sheep from '@/sheep';
|
|
import sheep from '@/sheep';
|
|
import ToolsPopup from '@/pages/chat/components/toolsPopup.vue';
|
|
import ToolsPopup from '@/pages/chat/components/toolsPopup.vue';
|
|
import MessageInput from '@/pages/chat/components/messageInput.vue';
|
|
import MessageInput from '@/pages/chat/components/messageInput.vue';
|
|
@@ -39,6 +42,48 @@ import KeFuApi from '@/sheep/api/promotion/kefu';
|
|
import { useWebSocket } from '@/sheep/hooks/useWebSocket';
|
|
import { useWebSocket } from '@/sheep/hooks/useWebSocket';
|
|
import { jsonParse } from '@/sheep/util';
|
|
import { jsonParse } from '@/sheep/util';
|
|
import { onLoad } from '@dcloudio/uni-app';
|
|
import { onLoad } from '@dcloudio/uni-app';
|
|
|
|
+const EventSourceRef = ref(null); //AIref
|
|
|
|
+
|
|
|
|
+const eventSourceUrl = import.meta.env.SHOPRO_BASE_URL + "/app-api/promotion/kefu-message/sendStream"; //ai客服URL
|
|
|
|
+// const eventSourceUrl = "https://192.168.10.17:9095/app-api/infra/ai-dify/chat-messages-stream"; //ai客服URL
|
|
|
|
+// ai客服流式上传参数
|
|
|
|
+const eventSourceOptions = computed(() => {
|
|
|
|
+ return {
|
|
|
|
+ headers: {
|
|
|
|
+ "content-type": "application/json",
|
|
|
|
+ Accept: "text/event-stream",
|
|
|
|
+ "tenant-id": 1,
|
|
|
|
+ Authorization: "Bearer " + uni.getStorageSync('token'),
|
|
|
|
+ },
|
|
|
|
+ method: "POST",
|
|
|
|
+ body: JSON.stringify({
|
|
|
|
+ "contentType": 22,
|
|
|
|
+ "content": chat.msg,
|
|
|
|
+ "relUserId": route.value.relUserId
|
|
|
|
+ // type: "律师咨询",
|
|
|
|
+ // query: chat.msg
|
|
|
|
+ }), // 请求体
|
|
|
|
+ };
|
|
|
|
+});
|
|
|
|
+const answerArr = ref('');
|
|
|
|
+const loadingInput = ref(false); // 加载状态
|
|
|
|
+provide('loadingInput', loadingInput); // 依赖注入加载状态
|
|
|
|
+const loadingId = ref(""); // 加载的id
|
|
|
|
+// ai客服发送消息接收回调
|
|
|
|
+const handleCallback = async (e) => {
|
|
|
|
+ const { type, msg, data } = e || {};
|
|
|
|
+ if (type == "onmessage") {
|
|
|
|
+ const datas = JSON.parse(data);
|
|
|
|
+ answerArr.value += datas.content;
|
|
|
|
+ loadingId.value = datas.messageId
|
|
|
|
+ await messageListRef.value.updateMessage(datas);
|
|
|
|
+ }
|
|
|
|
+ if (type == "onclose") {
|
|
|
|
+ loadingInput.value = false;
|
|
|
|
+ await messageListRef.value.updateMessage({}, loadingId.value);
|
|
|
|
+ answerArr.value = ""
|
|
|
|
+ }
|
|
|
|
+};
|
|
const sys_navBar = sheep.$platform.navbar;
|
|
const sys_navBar = sheep.$platform.navbar;
|
|
const options1 = [{
|
|
const options1 = [{
|
|
label: 'ai',
|
|
label: 'ai',
|
|
@@ -66,23 +111,72 @@ const route = ref({})
|
|
onLoad((options) => {
|
|
onLoad((options) => {
|
|
route.value = options
|
|
route.value = options
|
|
});
|
|
});
|
|
-
|
|
|
|
|
|
+const userInfo = computed(() => sheep.$store('user').userInfo);
|
|
// 发送消息
|
|
// 发送消息
|
|
async function onSendMessage() {
|
|
async function onSendMessage() {
|
|
if (!chat.msg) return;
|
|
if (!chat.msg) return;
|
|
try {
|
|
try {
|
|
|
|
+ loadingInput.value = true;
|
|
|
|
+ const idArr = ['1', '2', '3', '4']
|
|
const data = {
|
|
const data = {
|
|
conversationId: route.value.conversationId,
|
|
conversationId: route.value.conversationId,
|
|
contentType: KeFuMessageContentTypeEnum.TEXT,
|
|
contentType: KeFuMessageContentTypeEnum.TEXT,
|
|
content: JSON.stringify({ text: chat.msg }),
|
|
content: JSON.stringify({ text: chat.msg }),
|
|
relUserId: route.value.relUserId
|
|
relUserId: route.value.relUserId
|
|
};
|
|
};
|
|
- await KeFuApi.sendKefuMessageNew(data);
|
|
|
|
- await messageListRef.value.refreshMessageList();
|
|
|
|
|
|
+ // 如果在线就走直接发消息 如果不在线就走ai回复
|
|
|
|
+ const res = await KeFuApi.checkUserId({
|
|
|
|
+ userId: route.value.relUserId
|
|
|
|
+ })
|
|
|
|
+ if (res.data) {
|
|
|
|
+ await KeFuApi.sendKefuMessageNew(data);
|
|
|
|
+ } else {
|
|
|
|
+ if (idArr.includes(route.value.relUserId)) {
|
|
|
|
+ console.log(messageListRef.value.getAvatar(), 555222233)
|
|
|
|
+ const avatarObj = messageListRef.value.getAvatar()
|
|
|
|
+ const params = {
|
|
|
|
+ id: 1,
|
|
|
|
+ conversationId: route.value.conversationId,
|
|
|
|
+ senderId: userInfo.value.id,
|
|
|
|
+ senderAvatar: avatarObj.senderAvatar,
|
|
|
|
+ senderType: 1,
|
|
|
|
+ receiverId: route.value.relUserId,
|
|
|
|
+ receiverAvatar: avatarObj.receiverAvatar,
|
|
|
|
+ receiverType: null,
|
|
|
|
+ contentType: 1,
|
|
|
|
+ content: JSON.stringify({ text: chat.msg }),
|
|
|
|
+ readStatus: true,
|
|
|
|
+ createTime: 1745546275000
|
|
|
|
+ }
|
|
|
|
+ await messageListRef.value.refreshMessageList(params);
|
|
|
|
+ const params1 = {
|
|
|
|
+ ids: 1,
|
|
|
|
+ isAi: true,
|
|
|
|
+ isLoading: true,
|
|
|
|
+ conversationId: route.value.conversationId,
|
|
|
|
+ senderId: route.value.relUserId,
|
|
|
|
+ senderAvatar: avatarObj.receiverAvatar,
|
|
|
|
+ senderType: 1,
|
|
|
|
+ receiverId: userInfo.value.id,
|
|
|
|
+ receiverAvatar: avatarObj.senderAvatar,
|
|
|
|
+ receiverType: null,
|
|
|
|
+ contentType: 22,
|
|
|
|
+ content: '',
|
|
|
|
+ readStatus: true,
|
|
|
|
+ createTime: 1745546275000
|
|
|
|
+ }
|
|
|
|
+ console.log(params1, params, 555222233)
|
|
|
|
+ await messageListRef.value.refreshMessageList(params1);
|
|
|
|
+ await EventSourceRef.value.send(data);
|
|
|
|
+ } else {
|
|
|
|
+ await KeFuApi.sendKefuMessageNew(data);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
chat.msg = '';
|
|
chat.msg = '';
|
|
} finally {
|
|
} finally {
|
|
chat.showTools = false;
|
|
chat.showTools = false;
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
const messageListRef = ref();
|
|
const messageListRef = ref();
|