From dd58c1d3a27ba48a5df050aab7c586bb9b988914 Mon Sep 17 00:00:00 2001
From: wujingjing <gersonwu@qq.com>
Date: 星期三, 09 四月 2025 18:01:22 +0800
Subject: [PATCH] activeTopMenuStyle

---
 src/components/chat/components/playBar/hook/useDigitalHuman.ts |  114 ++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 81 insertions(+), 33 deletions(-)

diff --git a/src/components/chat/components/playBar/hook/useDigitalHuman.ts b/src/components/chat/components/playBar/hook/useDigitalHuman.ts
index e6b8baf..6d48b3b 100644
--- a/src/components/chat/components/playBar/hook/useDigitalHuman.ts
+++ b/src/components/chat/components/playBar/hook/useDigitalHuman.ts
@@ -1,10 +1,12 @@
-import { nextTick, onDeactivated, onMounted, ref } from 'vue';
 import { SignJWT } from 'jose';
+import { nextTick, onDeactivated, onMounted, ref } from 'vue';
 
+import { markdownToTxt } from 'markdown-to-txt';
 import './libs/duix.js';
 import { questionStreamByPost } from '/@/api/ai/chat';
 import { activeGroupType, activeRoomId } from '/@/stores/chatRoom';
-import { markdownToTxt } from 'markdown-to-txt';
+import axios from 'axios';
+import { ElMessage } from 'element-plus';
 
 export type UseDigitalHumanProps = {
 	container: string;
@@ -23,14 +25,42 @@
 
 	// 鏄惁宸叉帴鍙g浉搴�
 	const isReceiveRes = ref(false);
+	const humanIsLoading = ref(true);
 
 	const digitalHumanIsShow = ref(false);
 	const closeDigitalHuman = () => {
 		digitalHumanIsShow.value = false;
 		resetDuixStatus();
 	};
+	/**
+	 * 妫�鏌ユ暟瀛椾汉鏄惁鍙敤
+	 */
+	const checkIsUseable = async () => {
+		const config = {
+			method: 'get',
+			url: `https://duix.guiji.ai/duix-openapi-v2/v1/getconcurrentNumber?appId=${duixConfig.appId}`,
+			headers: {
+				priority: 'u=1, i',
+				sig: duixConfig.sign,
+			},
+		};
+
+		const response = await axios(config);
+		const data = response.data.data;
+		const total = data.totalConcurrentNumber;
+		const user = data.userConcurrentNumber;
+		if (total === null || total === 0) {
+			return false;
+		}
+		if (total !== null && total === user) {
+			return false;
+		}
+
+		return true;
+	};
 
 	const resetDuixStatus = () => {
+		humanIsLoading.value = true;
 		isReceiveRes.value = false;
 		// isSpeaking.value = false;
 		digitalHumanIsShow.value = false;
@@ -46,7 +76,6 @@
 			}
 			return acc + mdText;
 		}, '');
-		console.log('馃殌 ~ knowledgeText:', knowledgeText);
 		// const conclusionText =
 		// 	item.conclusion
 		// 		?.filter((item) => !!item.report)
@@ -58,6 +87,29 @@
 	};
 
 	let isWaitingSpeak = false;
+
+	const speakContent = (content: string) => {
+		// 鎵撴柇涔嬪墠鐨勫凡鏀跺埌xxx
+		duix.break();
+		isWaitingSpeak = false;
+		duix.speak({
+			content,
+		});
+	};
+
+	const startDuix = () => {
+		const conversationId = duixConfig.conversationId; // duix骞冲彴浼氳瘽id
+
+		duix
+			.start({ conversationId, openAsr: true, wipeGreen: true })
+			.then((res) => {
+				console.info('start', res);
+			})
+			.catch((err) => {
+				console.error('start error', err);
+			});
+	};
+
 	const initDuix = () => {
 		const sign = duixConfig.sign; // sign鐢辨湇鍔$鐢熸垚
 		const conversationId = duixConfig.conversationId; // duix骞冲彴浼氳瘽id
@@ -70,9 +122,7 @@
 		duix.on('intialSucccess', () => {
 			console.info('intialSucccess');
 			// 姝ゆ椂鍒濆鍖栨垚鍔燂紝鍙皟鐢╯tart
-			duix.start({ conversationId, openAsr: true }).then((res) => {
-				console.info('start', res);
-			});
+			startDuix();
 		});
 		duix.on('bye', (data) => {
 			console.info('bye', data);
@@ -82,6 +132,7 @@
 		});
 		duix.on('show', () => {
 			console.info('show');
+			humanIsLoading.value = false;
 			// 姝ゆ椂鍙‘璁よ棰戝凡
 			// (document.querySelector('#modal') as HTMLElement).style.display = 'none';
 		});
@@ -96,7 +147,10 @@
 			console.info('speakStart', data);
 		});
 		duix.on('speakEnd', (data) => {
-			isReceiveRes.value = false;
+			if (!isWaitingSpeak) {
+				isReceiveRes.value = false;
+				duix.openAsr().then((...a) => {});
+			}
 		});
 		duix.on('speakSection', (data) => {
 			console.info('speakSection', data);
@@ -106,18 +160,18 @@
 		});
 		duix.on('asrResult', (data) => {
 			console.info('asrResult', data);
-
 			if (isReceiveRes.value) {
 				return;
 			}
+			duix.closeAsr().then((...a) => {});
 
 			let hasResult = false;
 			isReceiveRes.value = true;
 			try {
-				// isWaitingSpeak = true;
-				// duix.speak({
-				// 	content: '宸叉敹鍒版偍鐨勯棶棰橈紝姝e湪鎬濊�冧腑...璇风◢绛�',
-				// });
+				isWaitingSpeak = true;
+				duix.speak({
+					content: '宸叉敹鍒版偍鐨勯棶棰橈紝姝e湪鎬濊�冧腑...璇风◢绛�',
+				});
 				questionStreamByPost(
 					{
 						question: data,
@@ -130,26 +184,16 @@
 						if (chunkRes.mode === 'result' && chunkRes.value?.answer_type === 'knowledge') {
 							const plainText = getPlainText(chunkRes.value);
 							hasResult = true;
-							duix.speak({
-								content: plainText,
-							});
-						}
-
-						if (!chunkRes.value?.json_ok && chunkRes.value?.err_code === 'MESSAGE') {
+							speakContent(plainText);
+						} else if (!chunkRes.value?.json_ok && chunkRes.value?.err_code === 'MESSAGE') {
+							if (hasResult) return;
 							hasResult = true;
-							isWaitingSpeak = false;
-
-							duix.speak({
-								content: chunkRes.value.json_msg,
-							});
+							speakContent(chunkRes.value.json_msg);
 						}
 
 						if (chunkRes.mode === 'finish') {
 							if (!hasResult) {
-								isWaitingSpeak = false;
-								duix.speak({
-									content: '鏆傛椂鏃犳硶鍙eご鎻忚堪浣犳墍璇寸殑闂',
-								});
+								speakContent('鏆傛椂鏃犳硶鍙eご鎻忚堪浣犳墍璇寸殑闂');
 							} else {
 								hasResult = false;
 							}
@@ -177,20 +221,23 @@
 
 	let hasInitDuix = false;
 	let duix: any;
-	const openDigitalHuman = () => {
+	const openDigitalHuman = async () => {
+		duixConfig.sign = await createSig(duixConfig.appId, duixConfig.appKey, 60 * 60 * duixConfig.expired);
+		const isUsable = await checkIsUseable();
+		if (!isUsable) {
+			ElMessage.warning('"璧勬簮鍗犵敤涓紝璇锋鏌ュ悗鍐嶈瘯~"');
+			return;
+		}
 		digitalHumanIsShow.value = true;
 
-		nextTick(async () => {
-			duixConfig.sign = await createSig(duixConfig.appId, duixConfig.appKey, 60 * 60 * duixConfig.expired);
+		nextTick(() => {
 			if (!hasInitDuix) {
 				hasInitDuix = true;
 
 				duix = new DUIX();
 				initDuix();
 			} else {
-				duix.start({ conversationId: duixConfig.conversationId, openAsr: true }).then((res) => {
-					console.info('start', res);
-				});
+				startDuix();
 			}
 		});
 	};
@@ -222,5 +269,6 @@
 		openDigitalHuman,
 		isHumanTalking: isReceiveRes,
 		closeDigitalHuman,
+		humanIsLoading,
 	};
 };

--
Gitblit v1.9.3