You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4991 lines
151 KiB

5 months ago
5 months ago
5 months ago
5 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
5 months ago
5 months ago
5 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
5 months ago
1 month ago
1 month ago
1 month ago
2 months ago
2 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
1 month ago
1 month ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
5 months ago
5 months ago
5 months ago
5 months ago
1 month ago
1 month ago
1 month ago
5 months ago
  1. <script setup>
  2. import { ref, onMounted, watch, nextTick, reactive, onUnmounted } from "vue";
  3. import { ElDialog, ElMessage } from "element-plus";
  4. import {
  5. getReplyStreamAPI,
  6. getReplyAPI,
  7. TTSAPI,
  8. qsArpAamClickAPI,
  9. dbqbFirstAPI,
  10. dbqbSecondOneAPI,
  11. dbqbSecondTwoAPI,
  12. dbqbSecondThreeAPI,
  13. dbqbSecondFourAPI,
  14. dataListAPI,
  15. } from "../api/AIxiaocaishen";
  16. import { useUserStore } from "../store/userPessionCode";
  17. import { useChatStore } from "../store/chat";
  18. import { useAudioStore } from "../store/audio";
  19. import { useDataStore } from "@/store/dataList.js";
  20. import { marked } from "marked"; // 引入marked库
  21. import katex from "katex"; // 引入 KaTeX 库
  22. import { htmlToText } from "html-to-text";
  23. import { Howl, Howler } from "howler";
  24. import * as echarts from "echarts";
  25. import _, { add } from "lodash";
  26. import moment from "moment";
  27. import title1 from "@/assets/img/AIchat/核心价值评估.png";
  28. import title2 from "@/assets/img/AIchat/主力作战.png";
  29. import title3 from "@/assets/img/AIchat/攻防三维.png";
  30. import title4 from "@/assets/img/AIchat/综合作战.png";
  31. import logo1 from "@/assets/img/AIchat/夺宝奇兵logo.png";
  32. import logo2 from "@/assets/img/AIchat/开启无限财富.png";
  33. import getCountAll from "../assets/img/homePage/get-count-all.png";
  34. import voice from "../assets/img/homePage/tail/voice.png";
  35. import voiceNoActive from "../assets/img/homePage/tail/voice-no-active.png";
  36. const chatStore = useChatStore();
  37. const audioStore = useAudioStore();
  38. const dataStore = useDataStore();
  39. // 将这些变量移到全局作用域
  40. const audioQueue = ref([]);
  41. const isPlayingAudio = ref(false);
  42. let currentPlayIndex = 0;
  43. let isCallingPlayNext = false;
  44. // 音频预加载状态
  45. const audioPreloadStatus = {
  46. one: { loaded: false, url: null },
  47. two: { loaded: false, url: null },
  48. three: { loaded: false, url: null },
  49. four: { loaded: false, url: null },
  50. };
  51. // 音频队列顺序管理
  52. const audioQueueOrder = {
  53. "API1-第一个": 1,
  54. "API2-第二个": 2,
  55. "API3-第三个": 3,
  56. "API4-第四个": 4,
  57. };
  58. // 播放下一个音频的函数
  59. const playNextAudio = () => {
  60. if (isCallingPlayNext) {
  61. console.log("playNextAudio已在执行中,跳过重复调用");
  62. return;
  63. }
  64. if (currentPlayIndex >= audioQueue.value.length) {
  65. console.log("所有音频播放完成,重置到第一个音频");
  66. // 播放完成后重置到第一个音频,但不自动播放
  67. currentPlayIndex = 0;
  68. audioStore.isPlaying = false;
  69. audioStore.isPaused = false;
  70. audioStore.playbackPosition = 0;
  71. // 清除音频实例,确保下次点击从头开始
  72. audioStore.soundInstance = null;
  73. audioStore.nowSound = null;
  74. if (audioQueue.value.length > 0) {
  75. audioStore.setCurrentAudioUrl(audioQueue.value[0]);
  76. }
  77. return;
  78. }
  79. // if (currentPlayIndex >= audioQueue.value.length) {
  80. // console.log("所有音频播放完成");
  81. // isPlayingAudio.value = false;
  82. // return;
  83. // }
  84. isCallingPlayNext = true;
  85. const audioInfo = audioQueue.value[currentPlayIndex];
  86. if (!audioInfo || !audioInfo.url) {
  87. console.warn(`音频信息无效,跳过索引 ${currentPlayIndex}`);
  88. currentPlayIndex++;
  89. isCallingPlayNext = false;
  90. playNextAudio();
  91. return;
  92. }
  93. console.log(`开始播放 ${audioInfo.name},索引: ${currentPlayIndex}`);
  94. const audio = new Howl({
  95. src: [audioInfo.url],
  96. html5: true,
  97. format: ["mp3", "acc"],
  98. rate: 1.2,
  99. retryCount: 0,
  100. onplay: () => {
  101. audioStore.isPlaying = true;
  102. isPlayingAudio.value = true;
  103. isCallingPlayNext = false;
  104. console.log(`${audioInfo.name}音频开始播放`);
  105. },
  106. onpause: () => {
  107. audioStore.isPlaying = false;
  108. audioStore.isPaused = true;
  109. audioStore.playbackPosition = audio.seek() || 0;
  110. console.log(`${audioInfo.name}音频已暂停`);
  111. },
  112. onresume: () => {
  113. audioStore.isPlaying = true;
  114. audioStore.isPaused = false;
  115. console.log(`${audioInfo.name}音频继续播放`);
  116. },
  117. onend: () => {
  118. console.log(`${audioInfo.name}音频播放完成,准备播放下一个`);
  119. audioStore.isPlaying = false;
  120. audioStore.isPaused = false;
  121. audioStore.playbackPosition = 0;
  122. isPlayingAudio.value = false;
  123. currentPlayIndex++;
  124. console.log(
  125. "currentPlayIndex",
  126. currentPlayIndex,
  127. "audioQueue.value.length",
  128. audioQueue.value.length
  129. );
  130. if (currentPlayIndex < audioQueue.value.length) {
  131. console.log(
  132. `队列中还有音频,500ms后播放下一个 (索引:${currentPlayIndex})`
  133. );
  134. setTimeout(() => {
  135. isCallingPlayNext = false;
  136. playNextAudio();
  137. }, 500);
  138. } else {
  139. console.log("🎉 所有音频播放完成,清除音频实例");
  140. chatStore.messages[chatStore.currentUserIndex].audioStatus = false;
  141. audioStore.nowSound = null;
  142. audioStore.soundInstance = null;
  143. // currentPlayIndex = 0;
  144. isCallingPlayNext = false;
  145. // setTimeout(() => {
  146. // isCallingPlayNext = false;
  147. // playNextAudio();
  148. // }, 500); // 间隔500ms播放下一个
  149. }
  150. },
  151. onstop: () => {
  152. console.log(`${audioInfo.name}音频被停止`);
  153. audioStore.isPlaying = false;
  154. audioStore.isPaused = false;
  155. audioStore.playbackPosition = 0;
  156. },
  157. onloaderror: (id, err) => {
  158. console.error(`${audioInfo.name}音频播放失败:`, err);
  159. isPlayingAudio.value = false;
  160. isCallingPlayNext = false;
  161. setTimeout(() => {
  162. playNextAudio();
  163. }, 100);
  164. },
  165. });
  166. audioStore.setCurrentAudioUrl(audioInfo.url);
  167. audioStore.nowSound = audio;
  168. audioStore.setAudioInstance(audio);
  169. console.log(`尝试播放${audioInfo.name}音频`);
  170. audio.play();
  171. };
  172. // 添加音频到播放队列(确保顺序)
  173. const addToAudioQueue = (url, name) => {
  174. console.log(`=== 添加音频到队列 ===`);
  175. console.log("URL:", url);
  176. console.log("Name:", name);
  177. console.log("音频启用状态:", audioStore.isVoiceEnabled);
  178. if (url && audioStore.isVoiceEnabled) {
  179. const audioItem = {
  180. url,
  181. name,
  182. order: audioQueueOrder[name] || 999,
  183. };
  184. audioQueue.value.push(audioItem);
  185. // 按顺序排序队列
  186. audioQueue.value.sort((a, b) => a.order - b.order);
  187. console.log(`音频${name}已添加到播放队列,顺序:${audioItem.order}`);
  188. console.log(
  189. "当前队列顺序:",
  190. audioQueue.value.map((item) => `${item.name}(${item.order})`)
  191. );
  192. // 只有在确实没有音频在播放且这是第一个音频时才开始播放
  193. if (
  194. !isPlayingAudio.value &&
  195. !audioStore.isPlaying &&
  196. audioQueue.value.length === 1
  197. ) {
  198. console.log("✅ 条件满足:没有音频在播放且这是第一个音频,立即开始播放", {
  199. isPlayingAudio: isPlayingAudio.value,
  200. audioStoreIsPlaying: audioStore.isPlaying,
  201. queueLength: audioQueue.value.length,
  202. });
  203. playNextAudio();
  204. } else {
  205. console.log("⏳ 等待条件:", {
  206. isPlayingAudio: isPlayingAudio.value,
  207. audioStoreIsPlaying: audioStore.isPlaying,
  208. queueLength: audioQueue.value.length,
  209. reason:
  210. audioQueue.value.length > 1 ? "队列中已有其他音频" : "有音频正在播放",
  211. });
  212. }
  213. } else {
  214. console.log("❌ 跳过添加音频:", {
  215. hasUrl: !!url,
  216. voiceEnabled: audioStore.isVoiceEnabled,
  217. });
  218. }
  219. console.log(`=== 添加音频完成 ===`);
  220. };
  221. // 语音播放控制函数
  222. const toggleVoiceForUser = (index) => {
  223. console.log(
  224. "上一个按钮坐标",
  225. chatStore.currentUserIndex,
  226. "当前按钮坐标",
  227. index
  228. );
  229. if (
  230. !chatStore.messages[index].audioArray[0] ||
  231. !chatStore.messages[index].audioArray[1] ||
  232. !chatStore.messages[index].audioArray[2] ||
  233. !chatStore.messages[index].audioArray[3]
  234. ) {
  235. return;
  236. }
  237. // 先把当前按钮状态修改
  238. chatStore.messages[index].audioStatus =
  239. !chatStore.messages[index].audioStatus;
  240. // 如果当前按钮和之前的按钮不是同一个则再修改之前的按钮的状态
  241. if (chatStore.currentUserIndex != index) {
  242. if (chatStore.currentUserIndex != null) {
  243. if (audioStore.isPlaying) {
  244. audioStore.togglePlayPause();
  245. }
  246. chatStore.messages[chatStore.currentUserIndex].audioStatus = false;
  247. }
  248. audioPreloadStatus.one = { loaded: false, url: null };
  249. audioPreloadStatus.two = { loaded: false, url: null };
  250. audioPreloadStatus.three = { loaded: false, url: null };
  251. audioPreloadStatus.four = { loaded: false, url: null };
  252. if (chatStore.messages[index].audioArray[0]) {
  253. audioPreloadStatus.one.loaded = true;
  254. audioPreloadStatus.one.url = chatStore.messages[index].audioArray[0];
  255. }
  256. if (chatStore.messages[index].audioArray[1]) {
  257. audioPreloadStatus.two.loaded = true;
  258. audioPreloadStatus.two.url = chatStore.messages[index].audioArray[1];
  259. }
  260. if (chatStore.messages[index].audioArray[2]) {
  261. audioPreloadStatus.three.loaded = true;
  262. audioPreloadStatus.three.url = chatStore.messages[index].audioArray[2];
  263. }
  264. if (chatStore.messages[index].audioArray[3]) {
  265. audioPreloadStatus.four.loaded = true;
  266. audioPreloadStatus.four.url = chatStore.messages[index].audioArray[3];
  267. }
  268. chatStore.currentUserIndex = index;
  269. audioQueue.value = [];
  270. isPlayingAudio.value = false;
  271. audioStore.soundInstance = null;
  272. currentPlayIndex = 0;
  273. isCallingPlayNext = false;
  274. addToAudioQueue(chatStore.messages[index].audioArray[0], "API1-第一个");
  275. addToAudioQueue(chatStore.messages[index].audioArray[1], "API2-第二个");
  276. addToAudioQueue(chatStore.messages[index].audioArray[2], "API3-第三个");
  277. addToAudioQueue(chatStore.messages[index].audioArray[3], "API4-第四个");
  278. if (!audioStore.isVoiceEnabled) {
  279. audioStore.toggleVoice();
  280. } else {
  281. if (audioStore.currentAudioUrl || audioStore.ttsUrl) {
  282. audioStore.togglePlayPause();
  283. } else {
  284. audioStore.toggleVoice();
  285. }
  286. }
  287. } else {
  288. if (!audioStore.isVoiceEnabled) {
  289. console.log("1111");
  290. audioStore.toggleVoice();
  291. } else {
  292. if (currentPlayIndex >= audioQueue.value.length) {
  293. console.log("重新开始播放音频序列");
  294. currentPlayIndex = 0;
  295. isPlayingAudio.value = false;
  296. isCallingPlayNext = false;
  297. audioStore.soundInstance = null;
  298. // 重新开始播放
  299. if (audioQueue.value.length > 0) {
  300. playNextAudio();
  301. }
  302. } else if (audioStore.currentAudioUrl || audioStore.ttsUrl) {
  303. console.log("2222");
  304. audioStore.togglePlayPause();
  305. } else {
  306. console.log("3333");
  307. audioStore.toggleVoice();
  308. }
  309. }
  310. }
  311. };
  312. // 计算属性:判断语音是否启用
  313. const isVoice = computed(() => {
  314. return audioStore.isVoiceEnabled;
  315. });
  316. // 随机GIF
  317. const currentGif = ref("");
  318. const renderer = new marked.Renderer();
  319. // 重写 del 方法,让删除线不生效
  320. renderer.del = function (text) {
  321. // 处理各种数据类型
  322. console.log("text", text);
  323. return "~" + text.tokens[0].raw + "<br>" + text.tokens[2].raw + "~";
  324. };
  325. // 定义自定义事件
  326. const emit = defineEmits(["updateMessage", "sendMessage", "enableInput"]);
  327. // 音频播放方法
  328. const playAudio = (url) => {
  329. // 添加空值校验
  330. if (!url) {
  331. console.warn("音频URL为空,跳过播放");
  332. audioStore.isPlaying = false;
  333. return;
  334. }
  335. const handlePlay = () => {
  336. if (audioStore.isNewInstance) {
  337. const newSound = new Howl({
  338. src: [url],
  339. html5: true, // 强制HTML5 Audio解决iOS兼容问题
  340. format: ["mp3", "acc"],
  341. rate: 1.2, // 调整播放速度
  342. onplay: () => {
  343. audioStore.isPlaying = true; // 改为更新store状态
  344. newSound.volume(1); // 添加音量设置
  345. },
  346. onend: () => (audioStore.isPlaying = false),
  347. onstop: () => (audioStore.isPlaying = false),
  348. onloaderror: (id, err) => {
  349. console.error("音频加载失败:", err);
  350. ElMessage.error("音频播放失败,请检查网络连接");
  351. },
  352. });
  353. if (audioStore.nowSound) {
  354. audioStore.nowSound.stop();
  355. }
  356. audioStore.nowSound = newSound;
  357. audioStore.isNewInstance = false;
  358. console.log("新音频");
  359. } else {
  360. console.log("已经有音频");
  361. }
  362. const newSound = audioStore.nowSound;
  363. // 添加立即播放逻辑
  364. newSound.play();
  365. audioStore.setAudioInstance(newSound);
  366. Howler._howls.push(newSound); // 强制注册到全局管理
  367. // // 处理浏览器自动播放策略
  368. // if (Howler.autoUnlock) {
  369. // Howler.autoUnlock();
  370. // }
  371. };
  372. // if (/iPhone|iPad|iPod/.test(navigator.userAgent)) {
  373. // document.addEventListener('touchstart', handlePlay, { once: true });
  374. // ElMessage.info('请轻触屏幕以启用音频播放');
  375. // } else {
  376. // handlePlay();
  377. // }
  378. handlePlay();
  379. // Howler.volume(1.0) // 添加全局音量设置
  380. };
  381. // 新增暂停方法
  382. const pauseAudio = () => {
  383. if (audioStore.soundInstance) {
  384. audioStore.soundInstance.pause();
  385. audioStore.isPlaying = false;
  386. }
  387. };
  388. // 音频轮流播放方法
  389. const playAudioSequence = (audioUrls) => {
  390. console.log("playAudioSequence被调用,参数:", audioUrls);
  391. if (!audioUrls || audioUrls.length === 0) {
  392. console.warn("音频URL列表为空,跳过播放");
  393. return;
  394. }
  395. let currentIndex = 0;
  396. let audioSequence = [...audioUrls]; // 保存音频序列
  397. const playNext = () => {
  398. if (currentIndex >= audioSequence.length) {
  399. console.log("所有音频播放完成,重置到第一个音频");
  400. // 播放完成后重置到第一个音频,但不自动播放
  401. currentIndex = 0;
  402. audioStore.isPlaying = false;
  403. audioStore.isPaused = false;
  404. audioStore.playbackPosition = 0;
  405. // 清除音频实例,确保下次点击从头开始
  406. audioStore.soundInstance = null;
  407. audioStore.nowSound = null;
  408. if (audioSequence.length > 0) {
  409. audioStore.setCurrentAudioUrl(audioSequence[0]);
  410. }
  411. return;
  412. }
  413. const currentUrl = audioSequence[currentIndex];
  414. console.log(`正在播放第${currentIndex + 1}个音频:`, currentUrl);
  415. console.log(
  416. "音频URL有效性检查:",
  417. !!currentUrl,
  418. "长度:",
  419. currentUrl?.length
  420. );
  421. // 增强URL验证
  422. if (
  423. !currentUrl ||
  424. typeof currentUrl !== "string" ||
  425. currentUrl.trim() === ""
  426. ) {
  427. console.error(`音频 ${currentIndex + 1} URL无效,跳过该音频`);
  428. currentIndex++;
  429. setTimeout(() => {
  430. playNext();
  431. }, 100);
  432. return;
  433. }
  434. // 检查URL格式
  435. try {
  436. new URL(currentUrl);
  437. } catch (e) {
  438. console.error(`音频 ${currentIndex + 1} URL格式错误:`, currentUrl);
  439. currentIndex++;
  440. setTimeout(() => {
  441. playNext();
  442. }, 100);
  443. return;
  444. }
  445. // 设置当前音频URL
  446. audioStore.setCurrentAudioUrl(currentUrl);
  447. // 停止当前播放的音频
  448. if (audioStore.nowSound) {
  449. audioStore.nowSound.stop();
  450. }
  451. const sound = new Howl({
  452. src: [currentUrl],
  453. html5: true,
  454. format: ["mp3", "acc"],
  455. rate: 1.2,
  456. onplay: () => {
  457. audioStore.isPlaying = true;
  458. audioStore.isPaused = false;
  459. console.log(`开始播放音频 ${currentIndex + 1}`);
  460. console.log("音频播放状态:", {
  461. duration: sound.duration(),
  462. state: sound.state(),
  463. playing: sound.playing(),
  464. });
  465. },
  466. onpause: () => {
  467. audioStore.isPlaying = false;
  468. audioStore.isPaused = true;
  469. audioStore.playbackPosition = sound.seek() || 0;
  470. console.log(`音频 ${currentIndex + 1} 已暂停`);
  471. },
  472. onend: () => {
  473. audioStore.isPlaying = false;
  474. audioStore.isPaused = false;
  475. audioStore.playbackPosition = 0;
  476. console.log(`音频 ${currentIndex + 1} 播放完成`);
  477. currentIndex++;
  478. // 如果是最后一个音频播放完成,立即清除实例
  479. if (currentIndex >= audioSequence.length) {
  480. console.log("最后一个音频播放完成,清除音频实例");
  481. audioStore.soundInstance = null;
  482. audioStore.nowSound = null;
  483. currentIndex = 0; // 立即重置索引
  484. }
  485. // 播放下一个音频
  486. setTimeout(() => {
  487. playNext();
  488. }, 500); // 间隔500ms播放下一个
  489. },
  490. onstop: () => {
  491. audioStore.isPlaying = false;
  492. audioStore.isPaused = false;
  493. audioStore.playbackPosition = 0;
  494. console.log(`音频 ${currentIndex + 1} 已停止`);
  495. },
  496. onloaderror: (id, err) => {
  497. console.error(`音频 ${currentIndex + 1} 加载失败:`, err);
  498. console.error("失败的音频URL:", currentUrl);
  499. console.error("错误详情:", { id, err });
  500. // 增加重试机制
  501. if (!sound.retryCount) {
  502. sound.retryCount = 0;
  503. }
  504. if (sound.retryCount < 2) {
  505. sound.retryCount++;
  506. console.log(
  507. `音频 ${currentIndex + 1}${sound.retryCount}次重试加载`
  508. );
  509. setTimeout(() => {
  510. sound.load();
  511. }, 1000 * sound.retryCount); // 递增延时重试
  512. } else {
  513. console.warn(`音频 ${currentIndex + 1} 重试失败,跳过该音频`);
  514. currentIndex++;
  515. // 跳过失败的音频,播放下一个
  516. setTimeout(() => {
  517. playNext();
  518. }, 100);
  519. }
  520. },
  521. });
  522. audioStore.nowSound = sound;
  523. audioStore.setAudioInstance(sound);
  524. // 添加播放超时检测
  525. const playTimeout = setTimeout(() => {
  526. if (!audioStore.isPlaying && sound.state() === "loading") {
  527. console.warn(`音频 ${currentIndex + 1} 播放超时,可能网络问题`);
  528. sound.stop();
  529. currentIndex++;
  530. playNext();
  531. }
  532. }, 10000); // 10秒超时
  533. // 播放成功后清除超时
  534. sound.once("play", () => {
  535. clearTimeout(playTimeout);
  536. });
  537. console.log(`尝试播放音频 ${currentIndex + 1},URL: ${currentUrl}`);
  538. sound.play();
  539. };
  540. // 重写togglePlayPause方法以支持音频序列控制
  541. const originalTogglePlayPause = audioStore.togglePlayPause;
  542. audioStore.togglePlayPause = () => {
  543. console.log("音频控制按钮被点击 11111111111");
  544. console.log("当前播放状态:", audioStore.isPlaying);
  545. console.log("当前暂停状态:", audioStore.isPaused);
  546. console.log("当前音频实例:", audioStore.soundInstance);
  547. console.log(
  548. "当前索引:",
  549. currentIndex,
  550. "音频序列长度:",
  551. audioSequence.length
  552. );
  553. if (audioStore.soundInstance) {
  554. if (audioStore.isPlaying) {
  555. // 暂停当前音频
  556. console.log("暂停当前音频");
  557. audioStore.pause();
  558. } else if (audioStore.isPaused) {
  559. // 从暂停位置继续播放
  560. console.log("从暂停位置继续播放");
  561. audioStore.play();
  562. } else {
  563. // 重新开始播放当前音频或从头开始播放序列
  564. console.log("重新开始播放,当前索引:", currentIndex);
  565. if (currentIndex >= audioSequence.length) {
  566. console.log("所有音频已播放完成,从头开始");
  567. currentIndex = 0; // 重置到第一个音频
  568. }
  569. playNext();
  570. }
  571. } else {
  572. // 没有音频实例时,从头开始播放
  573. console.log("没有音频实例,从头开始播放");
  574. currentIndex = 0;
  575. playNext();
  576. }
  577. };
  578. // 开始播放第一个音频
  579. playNext();
  580. };
  581. // 获取消息
  582. const chatMsg = computed(() => chatStore.messages);
  583. const props = defineProps({
  584. messages: Array,
  585. chartData: {
  586. type: Object,
  587. default: null,
  588. },
  589. index: {
  590. type: Number,
  591. required: true,
  592. },
  593. });
  594. // 打字机效果
  595. const typewriterContent = ref("");
  596. const isTyping = ref(false);
  597. const typeWriter = (text, callback) => {
  598. let index = 0;
  599. isTyping.value = true;
  600. typewriterContent.value = "";
  601. const typingInterval = setInterval(() => {
  602. if (index < text.length) {
  603. typewriterContent.value += text.charAt(index);
  604. index++;
  605. // 自动滚动到底部
  606. nextTick(() => {
  607. const container = document.querySelector(".message-area");
  608. if (container) container.scrollTop = container.scrollHeight;
  609. });
  610. } else {
  611. clearInterval(typingInterval);
  612. isTyping.value = false;
  613. if (callback) callback();
  614. }
  615. }, 50); // 调整速度(毫秒)
  616. };
  617. const fnGetData = (data) => {
  618. const YaLiZhiChengLuoPan = data.YaLiZhiChengLuoPan;
  619. let sz = ref(5.5); // 进行判断
  620. const yl = YaLiZhiChengLuoPan.Yali; // 压力位
  621. const zc = YaLiZhiChengLuoPan.ZhiCheng; // 支撑位
  622. console.log("yl", yl, "zc", zc);
  623. if (yl == "较大" && zc == "较弱") {
  624. sz.value = 0.5; // 极高风险
  625. } else if (yl == "一般" && zc == "较弱") {
  626. sz.value = 1.5; // 弱撑中压区
  627. } else if (yl == "较弱" && zc == "较弱") {
  628. sz.value = 2.5; // 弱撑弱压区
  629. } else if (yl == "较大" && zc == "较大") {
  630. sz.value = 3.5; // 强撑强压区
  631. } else if (yl == "一般" && zc == "较大") {
  632. sz.value = 4.5; // 强撑中压
  633. } else if (yl == "较弱" && zc == "较大") {
  634. sz.value = 5.5; // 强撑弱压区
  635. } else if (yl == "较大" && zc == "一般") {
  636. sz.value = 0.2;
  637. } else if (yl == "一般" && zc == "一般") {
  638. sz.value = 3;
  639. } else if (yl == "较弱" && zc == "一般") {
  640. sz.value = 5.8;
  641. }
  642. return sz.value;
  643. };
  644. const typingQueue = ref([]);
  645. const isTypingInProgress = ref(false);
  646. // 创建打字机效果的Promise函数
  647. const createTypingEffect = (message, content, speed) => {
  648. return new Promise((resolve) => {
  649. chatStore.messages.push(message);
  650. if (Array.isArray(content) && content.length > 0) {
  651. message.content = "";
  652. message.isTyping = true;
  653. let currentIndex = 0;
  654. const processNextElement = () => {
  655. if (currentIndex >= content.length) {
  656. if (message.isEnd) {
  657. if (message.isEnd == "1") {
  658. apiStatus.one.isEnd = true;
  659. } else if (message.isEnd == "2") {
  660. apiStatus.two.isEnd = true;
  661. } else if (message.isEnd == "3") {
  662. apiStatus.three.isEnd = true;
  663. } else if (message.isEnd == "4") {
  664. apiStatus.four.isEnd = true;
  665. }
  666. }
  667. if (message.error) {
  668. chatStore.messages.push({
  669. class: "ing",
  670. type: "ing",
  671. flag: false,
  672. content: "工作流返回出错,请稍后重试",
  673. });
  674. chatStore.isLoading = false;
  675. emit("enableInput");
  676. if (message.error == "2") {
  677. apiStatus.two.isError = true;
  678. } else if (message.error == "3") {
  679. apiStatus.three.isError = true;
  680. } else if (message.error == "4") {
  681. apiStatus.four.isError = true;
  682. }
  683. }
  684. if (message.end) {
  685. chatStore.getUserCount();
  686. chatStore.isLoading = false;
  687. chatStore.searchRecord = true;
  688. console.log("打印完毕,接触输入框禁用状态");
  689. emit("enableInput");
  690. }
  691. message.isTyping = false;
  692. nextTick(() => {
  693. resolve(); // 完成后resolve
  694. });
  695. return;
  696. }
  697. if (currentIndex % 2 === 0) {
  698. // 偶数下标:直接加入
  699. message.content += content[currentIndex];
  700. currentIndex++;
  701. processNextElement(); // 立即处理下一个元素
  702. } else {
  703. // 奇数下标:打字机效果
  704. const text = content[currentIndex];
  705. let charIndex = 0;
  706. const typingInterval = setInterval(() => {
  707. if (charIndex < text.length) {
  708. message.content += text.charAt(charIndex);
  709. charIndex++;
  710. } else {
  711. clearInterval(typingInterval);
  712. currentIndex++;
  713. processNextElement(); // 处理下一个元素
  714. }
  715. }, speed);
  716. }
  717. };
  718. processNextElement(); // 开始处理
  719. } else {
  720. if (message.kline) {
  721. if (message.klineType == 1) {
  722. console.log("六色罗盘消息已添加到聊天列表");
  723. // 在渲染完成后初始化图表
  724. nextTick(() => {
  725. console.log("nextTick开始 - 准备渲染图表");
  726. console.log("消息列表:", chatStore.messages);
  727. // 寻找最新添加的K线消息索引
  728. let klineIndex = -1;
  729. for (let i = 0; i < chatStore.messages.length; i++) {
  730. if (chatStore.messages[i].messageId === message.messageId) {
  731. klineIndex = i;
  732. break;
  733. }
  734. }
  735. console.log("找到的K线消息索引:", klineIndex);
  736. if (klineIndex !== -1) {
  737. const containerId = `kline-container-${klineIndex}`;
  738. console.log("图表容器ID:", containerId);
  739. // 确保DOM已经渲染完成
  740. setTimeout(() => {
  741. console.log("延时执行,确保DOM已渲染");
  742. KlineCanvsEcharts(containerId);
  743. }, 100); // 短暂延时确保DOM已渲染
  744. } else {
  745. console.warn("未找到K线消息");
  746. }
  747. });
  748. } else {
  749. console.log("K线消息已添加到聊天列表");
  750. // 在渲染完成后初始化图表
  751. nextTick(() => {
  752. console.log("nextTick开始 - 准备渲染图表");
  753. console.log("消息列表:", chatStore.messages);
  754. // 寻找最新添加的K线消息索引
  755. let klineIndex = -1;
  756. for (let i = 0; i < chatStore.messages.length; i++) {
  757. if (chatStore.messages[i].messageId === message.messageId) {
  758. klineIndex = i;
  759. break;
  760. }
  761. }
  762. console.log("找到的K线消息索引:", klineIndex);
  763. if (klineIndex !== -1) {
  764. const containerId = `kline-container-${klineIndex}`;
  765. console.log("图表容器ID:", containerId);
  766. // 确保DOM已经渲染完成
  767. setTimeout(() => {
  768. console.log("延时执行,确保DOM已渲染");
  769. KlineCanvsEcharts(containerId);
  770. }, 100); // 短暂延时确保DOM已渲染
  771. } else {
  772. console.warn("未找到K线消息");
  773. }
  774. });
  775. }
  776. if (message.isEnd) {
  777. if (message.isEnd == "1") {
  778. apiStatus.one.isEnd = true;
  779. } else if (message.isEnd == "2") {
  780. apiStatus.two.isEnd = true;
  781. } else if (message.isEnd == "3") {
  782. apiStatus.three.isEnd = true;
  783. } else if (message.isEnd == "4") {
  784. apiStatus.four.isEnd = true;
  785. }
  786. }
  787. if (message.error) {
  788. chatStore.messages.push({
  789. class: "ing",
  790. type: "ing",
  791. flag: false,
  792. content: "工作流返回出错,请稍后重试",
  793. });
  794. chatStore.isLoading = false;
  795. emit("enableInput");
  796. if (message.error == "2") {
  797. apiStatus.two.isError = true;
  798. } else if (message.error == "3") {
  799. apiStatus.three.isError = true;
  800. } else if (message.error == "4") {
  801. apiStatus.four.isError = true;
  802. }
  803. }
  804. // 延时1秒后resolve
  805. setTimeout(() => {
  806. resolve();
  807. }, 1000);
  808. } else {
  809. if (message.isEnd) {
  810. if (message.isEnd == "1") {
  811. apiStatus.one.isEnd = true;
  812. } else if (message.isEnd == "2") {
  813. apiStatus.two.isEnd = true;
  814. } else if (message.isEnd == "3") {
  815. apiStatus.three.isEnd = true;
  816. } else if (message.isEnd == "4") {
  817. apiStatus.four.isEnd = true;
  818. }
  819. }
  820. if (message.error) {
  821. chatStore.messages.push({
  822. class: "ing",
  823. type: "ing",
  824. flag: false,
  825. content: "工作流返回出错,请稍后重试",
  826. });
  827. chatStore.isLoading = false;
  828. emit("enableInput");
  829. if (message.error == "2") {
  830. apiStatus.two.isError = true;
  831. } else if (message.error == "3") {
  832. apiStatus.three.isError = true;
  833. } else if (message.error == "4") {
  834. apiStatus.four.isError = true;
  835. }
  836. }
  837. // 延时1秒后resolve
  838. setTimeout(() => {
  839. resolve();
  840. }, 1000);
  841. }
  842. }
  843. });
  844. };
  845. let apiStatus = {};
  846. // 队列处理函数
  847. const processTypingQueue = async () => {
  848. if (isTypingInProgress.value || typingQueue.value.length === 0) {
  849. return;
  850. }
  851. isTypingInProgress.value = true;
  852. while (typingQueue.value.length > 0) {
  853. const task = typingQueue.value.shift();
  854. await createTypingEffect(task.message, task.content, task.speed);
  855. }
  856. isTypingInProgress.value = false;
  857. };
  858. // 添加打字机任务到队列
  859. const addTypingTask = (message, content, speed) => {
  860. typingQueue.value.push({ message, content, speed });
  861. processTypingQueue();
  862. };
  863. // 显示思考过程
  864. async function showThinkingProcess(stockName = null) {
  865. // 第一步:正在思考
  866. const thinkingMessage1 = reactive({
  867. sender: "ai",
  868. class: "ing",
  869. type: "ing",
  870. flag: true,
  871. content: "正在思考......",
  872. gif: "/src/assets/img/gif/思考.gif",
  873. });
  874. chatStore.messages.push(thinkingMessage1);
  875. await new Promise((resolve) => setTimeout(resolve, 1500));
  876. chatStore.messages.pop();
  877. // 第二步:正在解析关键数据(持续显示直到获取到股票名称)
  878. const thinkingMessage2 = reactive({
  879. sender: "ai",
  880. class: "ing",
  881. type: "ing",
  882. flag: true,
  883. content: "正在解析关键数据......",
  884. gif: "/src/assets/img/gif/解析.gif",
  885. });
  886. chatStore.messages.push(thinkingMessage2);
  887. // 如果没有股票名称,保持第二步显示
  888. if (!stockName) {
  889. return thinkingMessage2; // 返回消息引用,以便后续更新
  890. }
  891. // 有股票名称后,继续后续步骤
  892. await new Promise((resolve) => setTimeout(resolve, 1500));
  893. chatStore.messages.pop();
  894. // 第三步:生成具体股票的全景作战报告
  895. const thinkingMessage3 = reactive({
  896. sender: "ai",
  897. class: "ing",
  898. type: "ing",
  899. flag: true,
  900. content: `正在生成${stockName}全景作战报告......`,
  901. gif: "/src/assets/img/gif/生成.gif",
  902. });
  903. chatStore.messages.push(thinkingMessage3);
  904. await new Promise((resolve) => setTimeout(resolve, 1500));
  905. chatStore.messages.pop();
  906. // 第四步:报告已生成
  907. const thinkingMessage4 = reactive({
  908. sender: "ai",
  909. class: "ing",
  910. type: "ing",
  911. flag: true,
  912. content: "报告已生成!",
  913. gif: "/src/assets/img/gif/生成.gif",
  914. });
  915. chatStore.messages.push(thinkingMessage4);
  916. await new Promise((resolve) => setTimeout(resolve, 1500));
  917. chatStore.messages.pop();
  918. return null;
  919. }
  920. // 继续思考过程(当获取到股票名称后调用)
  921. async function continueThinkingProcess(thinkingMessageRef, stockName) {
  922. if (!thinkingMessageRef || !stockName) return;
  923. // 等待一段时间后继续
  924. await new Promise((resolve) => setTimeout(resolve, 1500));
  925. // 移除第二步消息
  926. const index = chatStore.messages.indexOf(thinkingMessageRef);
  927. if (index > -1) {
  928. chatStore.messages.splice(index, 1);
  929. }
  930. // 第三步:生成具体股票的全景作战报告
  931. const thinkingMessage3 = reactive({
  932. sender: "ai",
  933. class: "ing",
  934. type: "ing",
  935. flag: true,
  936. content: `正在生成${stockName}全景作战报告......`,
  937. gif: "/src/assets/img/gif/生成.gif",
  938. });
  939. chatStore.messages.push(thinkingMessage3);
  940. await new Promise((resolve) => setTimeout(resolve, 1500));
  941. chatStore.messages.pop();
  942. // 第四步:报告已生成
  943. const thinkingMessage4 = reactive({
  944. sender: "ai",
  945. class: "ing",
  946. type: "ing",
  947. flag: true,
  948. content: "报告已生成!",
  949. });
  950. chatStore.messages.push(thinkingMessage4);
  951. await new Promise((resolve) => setTimeout(resolve, 1500));
  952. chatStore.messages.pop();
  953. }
  954. const hasValidData = ref(false);
  955. // 创建一个非响应式的对象来存储图表实例
  956. const chartInstancesMap = {};
  957. // 存储上一次的消息的length
  958. const previousMessagesLength = ref(0);
  959. watch(
  960. () => props.messages,
  961. async (newVal, oldVal) => {
  962. // // 添加空值判断
  963. if (!newVal?.length || newVal === previousMessagesLength.value) return;
  964. previousMessagesLength.value = newVal.length;
  965. if (newVal.length > 0) {
  966. console.log("消息列表已更新,最新消息:", newVal[newVal.length - 1]);
  967. chatStore.messages.push(newVal[newVal.length - 1]);
  968. chatStore.currentUserIndex = chatStore.messages.length - 1;
  969. console.log(
  970. "消息列表已更新,最新消息:",
  971. chatStore.messages[chatStore.messages.length - 1],
  972. "最新用户坐标",
  973. chatStore.currentUserIndex
  974. );
  975. // 获取权限
  976. const userStore = useUserStore();
  977. const params1 = {
  978. language: "cn",
  979. marketList: "usa,sg,my,hk,cn,can,vi,th,in",
  980. content: newVal[newVal.length - 1].content,
  981. token: localStorage.getItem("localToken"),
  982. model: 1,
  983. // language: "cn",
  984. // marketList: "hk,cn,usa,my,sg,vi,in,gb"
  985. // token: "+SsksARQgUHIbIG3rRnnbZi0+fEeMx8pywnIlrmTxo5EOPR/wjWDV7w7+ZUseiBtf9kFa/atmNx6QfSpv5w",
  986. };
  987. // 标志
  988. let flag = true;
  989. const codeData = ref();
  990. // 开始思考过程(不带股票名称)
  991. const thinkingMessageRef = await showThinkingProcess();
  992. // 第一阶段,意图识别
  993. try {
  994. // 调用工作流获取回复
  995. const result = await dbqbFirstAPI(params1);
  996. codeData.value = result.data;
  997. console.log(codeData.value, "codeData");
  998. // 根据意图识别结果判断
  999. if (result.code == 200) {
  1000. // 获取到股票名称后,继续思考过程
  1001. if (thinkingMessageRef && codeData.value.name) {
  1002. await continueThinkingProcess(
  1003. thinkingMessageRef,
  1004. codeData.value.name
  1005. );
  1006. }
  1007. chatStore.messages.push({
  1008. class: "ing",
  1009. type: "ing",
  1010. flag: flag,
  1011. content: result.data.kaishi,
  1012. });
  1013. } else {
  1014. // 意图识别失败,先清理思考过程消息
  1015. if (thinkingMessageRef) {
  1016. const index = chatStore.messages.indexOf(thinkingMessageRef);
  1017. if (index > -1) {
  1018. chatStore.messages.splice(index, 1);
  1019. }
  1020. }
  1021. flag = false;
  1022. console.log("执行回绝话术");
  1023. const AIcontent = ref(result.msg);
  1024. // 修改后的消息处理逻辑
  1025. const processedContent = marked(AIcontent.value);
  1026. const katexRegex = /\$\$(.*?)\$\$/g;
  1027. const aiContent = processedContent.replace(
  1028. katexRegex,
  1029. (match, formula) => {
  1030. try {
  1031. return katex.renderToString(formula, { throwOnError: false });
  1032. } catch (error) {
  1033. console.error("KaTeX 渲染错误:", error);
  1034. return match;
  1035. }
  1036. }
  1037. );
  1038. console.log(aiContent, "aiContent");
  1039. chatStore.messages.push({
  1040. class: "ing",
  1041. type: "ing",
  1042. flag: flag,
  1043. content: aiContent,
  1044. });
  1045. chatStore.isLoading = false;
  1046. emit("enableInput");
  1047. }
  1048. } catch (e) {
  1049. // 意图识别异常,先清理思考过程消息
  1050. if (thinkingMessageRef) {
  1051. const index = chatStore.messages.indexOf(thinkingMessageRef);
  1052. if (index > -1) {
  1053. chatStore.messages.splice(index, 1);
  1054. }
  1055. }
  1056. console.log(e, "意图识别失败");
  1057. chatStore.messages.push({
  1058. class: "ing",
  1059. type: "ing",
  1060. flag: false,
  1061. content: "工作流返回出错,请稍后重试",
  1062. });
  1063. chatStore.isLoading = false;
  1064. emit("enableInput");
  1065. }
  1066. if (flag) {
  1067. const params2 = {
  1068. language: "cn",
  1069. token: localStorage.getItem("localToken"),
  1070. parentId: codeData.value.parentId,
  1071. stockId: codeData.value.stockId,
  1072. recordId: codeData.value.recordId,
  1073. // content: newVal[newVal.length - 1].content,
  1074. // marketList: "usa,sg,my,hk,cn,can,vi,th,in",
  1075. // name: codeData.value.name,
  1076. // code: codeData.value.code,
  1077. // market: codeData.value.market,
  1078. // language: "cn",
  1079. // marketList: "hk,cn,usa,my,sg,vi,in,gb"
  1080. // token: "+SsksARQgUHIbIG3rRnnbZi0+fEeMx8pywnIlrmTxo5EOPR/wjWDV7w7+ZUseiBtf9kFa/atmNx6QfSpv5w",
  1081. };
  1082. try {
  1083. const env = import.meta.env.VITE_ENV;
  1084. const result20 = await dataListAPI({
  1085. token:
  1086. "8Csj5VVX1UbIb4C3oxrnbZi0+fEeMx8pywnIlrmTm45Cb/EllzWACLto9J9+fCFsfdgBOvKvyY94FvqlvM0",
  1087. // "8nkj4QBV1RPIb4CzoRTnbZi0+fEeMx8pywnIlrmTxdwROKkuwWqAWu9orpkpeXVqL98DPfeonNYpHv+mucA",
  1088. market: codeData.value.market,
  1089. code: codeData.value.code,
  1090. language: "cn", //t.value.suoxie,
  1091. brainPrivilegeState: 1,
  1092. swordPrivilegeState: 1,
  1093. stockForecastPrivilegeState: 1,
  1094. spaceForecastPrivilegeState: 1,
  1095. aibullPrivilegeState: 1,
  1096. aigoldBullPrivilegeState: 1,
  1097. airadarPrivilegeState: 1,
  1098. // marketList: 1,
  1099. // brainPrivilegeState: userStore.brainPerssion,
  1100. // swordPrivilegeState: userStore.swordPerssion,
  1101. // stockForecastPrivilegeState: userStore.pricePerssion,
  1102. // spaceForecastPrivilegeState: userStore.timePerssion,
  1103. // aibullPrivilegeState: userStore.aibullPerssion,
  1104. // aigoldBullPrivilegeState: userStore.aiGnbullPerssion,
  1105. // airadarPrivilegeState: userStore.airadarPerssion,
  1106. marketList: "usa,sg,my,hk,cn,can,vi,th,in,gb",
  1107. });
  1108. // 添加空值检查防止访问null对象的属性
  1109. const HomePage = result20.data?.HomePage || null;
  1110. const AIGoldBull = result20.data?.AIGoldBull || null;
  1111. const isLiuSe = HomePage ? true : false;
  1112. const isAIGoldBull =
  1113. AIGoldBull &&
  1114. AIGoldBull.DNC &&
  1115. AIGoldBull.FCX &&
  1116. AIGoldBull.JN &&
  1117. AIGoldBull.KLine20 &&
  1118. AIGoldBull.QSXH
  1119. ? true
  1120. : false;
  1121. const katexRegex = /\$\$(.*?)\$\$/g;
  1122. let result21;
  1123. let result22;
  1124. let result23;
  1125. let result24;
  1126. // 用于跟踪API完成状态和结果
  1127. apiStatus = {
  1128. one: {
  1129. completed: false,
  1130. result: null,
  1131. error: null,
  1132. isError: false,
  1133. isEnd: false,
  1134. },
  1135. two: {
  1136. completed: false,
  1137. result: null,
  1138. error: null,
  1139. isError: false,
  1140. isEnd: false,
  1141. },
  1142. three: {
  1143. completed: false,
  1144. result: null,
  1145. error: null,
  1146. isError: false,
  1147. isEnd: false,
  1148. },
  1149. four: {
  1150. completed: false,
  1151. result: null,
  1152. error: null,
  1153. isError: false,
  1154. isEnd: false,
  1155. },
  1156. };
  1157. // 音频播放队列管理已移到全局作用域
  1158. // 重写audioStore的togglePlayPause方法以支持队列播放控制
  1159. const originalTogglePlayPause = audioStore.togglePlayPause;
  1160. audioStore.togglePlayPause = () => {
  1161. console.log("主页音频控制按钮被点击22222222222222");
  1162. console.log(
  1163. "当前音频状态 - isPlaying:",
  1164. audioStore.isPlaying,
  1165. "isPaused:",
  1166. audioStore.isPaused
  1167. );
  1168. console.log("当前音频实例:", audioStore.soundInstance);
  1169. console.log(
  1170. "队列播放状态 - isPlayingAudio:",
  1171. isPlayingAudio.value,
  1172. "队列长度:",
  1173. audioQueue.value.length
  1174. );
  1175. console.log(
  1176. "当前播放索引:",
  1177. currentPlayIndex,
  1178. "是否所有音频播放完成:",
  1179. currentPlayIndex >= audioQueue.value.length
  1180. );
  1181. // 检查是否所有音频都播放完成,如果是则重新开始
  1182. if (
  1183. audioStore.soundInstance &&
  1184. currentPlayIndex < audioQueue.value.length &&
  1185. isPlayingAudio.value
  1186. ) {
  1187. if (audioStore.isPlaying) {
  1188. // 暂停当前音频
  1189. console.log("暂停当前音频");
  1190. audioStore.soundInstance.pause();
  1191. } else if (
  1192. audioStore.isPaused &&
  1193. audioStore.playbackPosition > 0
  1194. ) {
  1195. // 从暂停位置继续播放
  1196. console.log(
  1197. "从暂停位置继续播放音频,位置:",
  1198. audioStore.playbackPosition
  1199. );
  1200. audioStore.soundInstance.seek(audioStore.playbackPosition);
  1201. audioStore.soundInstance.play();
  1202. } else {
  1203. // 重新开始播放
  1204. console.log("重新开始播放音频");
  1205. audioStore.soundInstance.play();
  1206. }
  1207. } else {
  1208. console.log("没有音频实例,检查是否需要重新播放队列");
  1209. console.log("重新播放条件检查:", {
  1210. isPlayingAudio: isPlayingAudio.value,
  1211. queueLength: audioQueue.value.length,
  1212. currentPlayIndex: currentPlayIndex,
  1213. condition1: audioQueue.value.length === 0,
  1214. condition2: currentPlayIndex >= audioQueue.value.length,
  1215. finalCondition:
  1216. !isPlayingAudio.value &&
  1217. (audioQueue.value.length === 0 ||
  1218. currentPlayIndex >= audioQueue.value.length),
  1219. });
  1220. // 没有音频实例时,检查是否所有音频都播放完成
  1221. if (
  1222. !isPlayingAudio.value &&
  1223. (audioQueue.value.length === 0 ||
  1224. currentPlayIndex >= audioQueue.value.length)
  1225. ) {
  1226. console.log("所有音频播放完成,重新构建队列从第一个开始播放");
  1227. // 重新构建音频队列,按顺序添加所有音频(不自动播放)
  1228. const audioItems = [];
  1229. if (audioPreloadStatus.one.url) {
  1230. audioItems.push({
  1231. url: audioPreloadStatus.one.url,
  1232. name: "API1-第一个",
  1233. order: 1,
  1234. });
  1235. }
  1236. if (audioPreloadStatus.two.url) {
  1237. audioItems.push({
  1238. url: audioPreloadStatus.two.url,
  1239. name: "API2-第二个",
  1240. order: 2,
  1241. });
  1242. }
  1243. if (audioPreloadStatus.three.url) {
  1244. audioItems.push({
  1245. url: audioPreloadStatus.three.url,
  1246. name: "API3-第三个",
  1247. order: 3,
  1248. });
  1249. }
  1250. if (audioPreloadStatus.four.url) {
  1251. audioItems.push({
  1252. url: audioPreloadStatus.four.url,
  1253. name: "API4-第四个",
  1254. order: 4,
  1255. });
  1256. }
  1257. // 按顺序排序并添加到队列
  1258. audioItems.sort((a, b) => a.order - b.order);
  1259. audioQueue.value = audioItems;
  1260. console.log(
  1261. "队列重建完成,队列内容:",
  1262. audioQueue.value.map((item) => item.name)
  1263. );
  1264. console.log("开始从第一个音频播放");
  1265. // 重置播放状态并开始播放第一个音频
  1266. if (audioQueue.value.length > 0) {
  1267. // 完全重置所有播放相关状态
  1268. isPlayingAudio.value = false;
  1269. isCallingPlayNext = false;
  1270. currentPlayIndex = 0; // 重置播放索引到第一个音频
  1271. audioStore.isPlaying = false;
  1272. audioStore.isPaused = false;
  1273. audioStore.playbackPosition = 0;
  1274. audioStore.nowSound = null;
  1275. audioStore.soundInstance = null;
  1276. console.log("🔄 状态完全重置完成,准备从第一个音频开始播放");
  1277. console.log("✅ 完全重置播放状态,准备播放第一个音频");
  1278. console.log("重置后状态检查:", {
  1279. isPlayingAudio: isPlayingAudio.value,
  1280. isCallingPlayNext: isCallingPlayNext,
  1281. currentPlayIndex: currentPlayIndex,
  1282. audioStoreIsPlaying: audioStore.isPlaying,
  1283. queueLength: audioQueue.value.length,
  1284. });
  1285. setTimeout(() => {
  1286. console.log("🚀 延迟后开始播放第一个音频");
  1287. console.log("播放前最终状态检查:", {
  1288. currentPlayIndex: currentPlayIndex,
  1289. queueLength: audioQueue.value.length,
  1290. queueItems: audioQueue.value.map(
  1291. (item, index) => `${index}: ${item.name}`
  1292. ),
  1293. isPlayingAudio: isPlayingAudio.value,
  1294. isCallingPlayNext: isCallingPlayNext,
  1295. audioStoreIsPlaying: audioStore.isPlaying,
  1296. audioStoreInstance: !!audioStore.soundInstance,
  1297. });
  1298. console.log(
  1299. "🎯 即将调用 playNextAudio,期望播放:",
  1300. audioQueue.value[currentPlayIndex]?.name || "无音频"
  1301. );
  1302. playNextAudio();
  1303. }, 200);
  1304. }
  1305. } else if (!isPlayingAudio.value && audioQueue.value.length > 0) {
  1306. console.log("队列中还有音频,继续播放");
  1307. playNextAudio();
  1308. } else {
  1309. console.log(
  1310. "无法播放 - isPlayingAudio:",
  1311. isPlayingAudio.value,
  1312. "队列长度:",
  1313. audioQueue.value.length
  1314. );
  1315. }
  1316. }
  1317. };
  1318. // 预加载音频函数
  1319. const preloadAudio = (url, apiKey) => {
  1320. if (!url || !audioStore.isVoiceEnabled) {
  1321. audioPreloadStatus[apiKey].loaded = true;
  1322. return Promise.resolve();
  1323. }
  1324. // 立即设置URL,确保即使预加载失败也能使用
  1325. audioPreloadStatus[apiKey].url = url;
  1326. console.log(`设置音频${apiKey}的URL:`, url);
  1327. return new Promise((resolve) => {
  1328. const audio = new Howl({
  1329. src: [url],
  1330. html5: true,
  1331. format: ["mp3", "acc"],
  1332. rate: 1.2,
  1333. preload: true,
  1334. onload: () => {
  1335. console.log(`音频${apiKey}预加载完成:`, url);
  1336. audioPreloadStatus[apiKey].loaded = true;
  1337. resolve();
  1338. },
  1339. onloaderror: (id, err) => {
  1340. console.error(`音频${apiKey}预加载失败:`, err);
  1341. audioPreloadStatus[apiKey].loaded = true; // 标记为已处理,避免阻塞
  1342. // URL已经在上面设置了,即使预加载失败也保留URL
  1343. resolve();
  1344. },
  1345. });
  1346. });
  1347. };
  1348. // 检查第一个接口是否可以开始输出(文本和音频都准备好)
  1349. const canStartFirstOutput = () => {
  1350. return apiStatus.one.completed && audioPreloadStatus.one.loaded;
  1351. };
  1352. // 检查并按顺序执行代码的函数
  1353. const checkAndExecuteInOrder = () => {
  1354. // 检查OneAPI - 只有当文本和音频都准备好时才开始输出
  1355. if (canStartFirstOutput() && !apiStatus.one.executed) {
  1356. if (apiStatus.one.result) {
  1357. apiStatus.one.executed = true;
  1358. console.log(
  1359. "执行OneAPI代码(文本和音频同步开始):",
  1360. apiStatus.one.result
  1361. );
  1362. // 将第一个音频添加到播放队列(确保顺序:API1)
  1363. if (audioPreloadStatus.one.url) {
  1364. chatStore.messages[
  1365. chatStore.currentUserIndex
  1366. ].audioArray.push(audioPreloadStatus.one.url);
  1367. addToAudioQueue(audioPreloadStatus.one.url, "API1-第一个");
  1368. console.log(
  1369. "音频队列:添加API1音频,当前队列长度:",
  1370. audioQueue.value.length
  1371. );
  1372. }
  1373. // 在这里添加OneAPI成功后需要执行的代码
  1374. // 删除正在为您生成信息
  1375. chatStore.messages.pop();
  1376. // 添加报告头和时间
  1377. addTypingTask(
  1378. {
  1379. sender: "ai",
  1380. class: "title1",
  1381. type: "title1",
  1382. content: codeData.value.name + "全景作战报告",
  1383. date: result21.data.date,
  1384. },
  1385. "",
  1386. 50
  1387. );
  1388. // chatStore.messages.push({
  1389. // sender: "ai",
  1390. // class: "title1",
  1391. // type: "title1",
  1392. // content: codeData.value.name + "全景作战报告",
  1393. // date: moment().format("MM/DD/YYYY"),
  1394. // });
  1395. // 添加股票信息框
  1396. const pc1 = marked(
  1397. result21.data.name +
  1398. "\n" +
  1399. result21.data.price +
  1400. "\n" +
  1401. result21.data.date
  1402. );
  1403. const ac1 = pc1.replace(katexRegex, (match, formula) => {
  1404. try {
  1405. return katex.renderToString(formula, {
  1406. throwOnError: false,
  1407. });
  1408. } catch (error) {
  1409. console.error("KaTeX 渲染错误:", error);
  1410. return match;
  1411. }
  1412. });
  1413. // 先推送初始消息
  1414. const aiMessage1 = reactive({
  1415. sender: "ai",
  1416. class: "content1",
  1417. type: "content1",
  1418. content: "",
  1419. isTyping: true,
  1420. });
  1421. // chatStore.messages.push(aiMessage1);
  1422. // let index1 = 0;
  1423. // const typingInterval1 = setInterval(() => {
  1424. // if (index1 < ac1.length) {
  1425. // aiMessage1.content += ac1.charAt(index1);
  1426. // index1++;
  1427. // } else {
  1428. // clearInterval(typingInterval1);
  1429. // aiMessage1.isTyping = false;
  1430. // }
  1431. // }, 50); // 调整速度为50ms/字符
  1432. addTypingTask(aiMessage1, ["", ac1], 130);
  1433. // chatStore.messages.push({
  1434. // sender: "ai",
  1435. // class: "content1",
  1436. // type: "content1",
  1437. // content: ac1,
  1438. // });
  1439. // 添加六色罗盘
  1440. if (HomePage) {
  1441. const LiuSeData = JSON.parse(JSON.stringify(toRaw(HomePage)));
  1442. const sz = fnGetData(LiuSeData);
  1443. if (sz) {
  1444. hasValidData.value = true;
  1445. console.log("hasValidData设置为:", hasValidData.value);
  1446. }
  1447. // 先推送K线图消息
  1448. const klineMessageId1 = `kline-${Date.now()}`;
  1449. console.log("生成K线消息ID:", klineMessageId1);
  1450. addTypingTask(
  1451. {
  1452. sender: "ai",
  1453. class: "content1",
  1454. type: "content1",
  1455. kline: true,
  1456. chartData: sz,
  1457. messageId: klineMessageId1,
  1458. hasValidData: true,
  1459. klineType: 1,
  1460. },
  1461. "",
  1462. 50
  1463. );
  1464. // chatStore.messages.push({
  1465. // sender: "ai",
  1466. // class: "content1",
  1467. // type: "content1",
  1468. // kline: true,
  1469. // chartData: sz,
  1470. // messageId: klineMessageId1,
  1471. // hasValidData: true,
  1472. // klineType: 1,
  1473. // });
  1474. // console.log("六色罗盘消息已添加到聊天列表");
  1475. // // 在渲染完成后初始化图表
  1476. // nextTick(() => {
  1477. // console.log("nextTick开始 - 准备渲染图表");
  1478. // console.log("消息列表:", chatStore.messages);
  1479. // // 寻找最新添加的K线消息索引
  1480. // let klineIndex = -1;
  1481. // for (let i = 0; i < chatStore.messages.length; i++) {
  1482. // if (chatStore.messages[i].messageId === klineMessageId1) {
  1483. // klineIndex = i;
  1484. // break;
  1485. // }
  1486. // }
  1487. // console.log("找到的K线消息索引:", klineIndex);
  1488. // if (klineIndex !== -1) {
  1489. // const containerId = `kline-container-${klineIndex}`;
  1490. // console.log("图表容器ID:", containerId);
  1491. // // 确保DOM已经渲染完成
  1492. // setTimeout(() => {
  1493. // console.log("延时执行,确保DOM已渲染");
  1494. // KlineCanvsEcharts(containerId);
  1495. // }, 100); // 短暂延时确保DOM已渲染
  1496. // } else {
  1497. // console.warn("未找到K线消息");
  1498. // }
  1499. // });
  1500. }
  1501. // 度牛尺K线图
  1502. if (
  1503. AIGoldBull &&
  1504. AIGoldBull.DNC &&
  1505. AIGoldBull.FCX &&
  1506. AIGoldBull.JN &&
  1507. AIGoldBull.KLine20 &&
  1508. AIGoldBull.QSXH
  1509. ) {
  1510. const AIGoldBullData = JSON.parse(
  1511. JSON.stringify(toRaw(AIGoldBull))
  1512. );
  1513. const HomePageData = JSON.parse(
  1514. JSON.stringify(toRaw(HomePage))
  1515. );
  1516. console.log("处理 K 线数据 - 开始");
  1517. console.log("AIGoldBullData", AIGoldBullData);
  1518. console.log("HomePageData", HomePageData);
  1519. const Kline20 = {
  1520. name: HomePageData.StockInformation.Name,
  1521. Kline: AIGoldBullData,
  1522. };
  1523. // 打印K线数据结构
  1524. console.log("K线数据结构:", Kline20);
  1525. console.log("K线数据名称:", Kline20.name);
  1526. console.log("K线数据:", Kline20.Kline ? Kline20.Kline : null);
  1527. // 设置数据有效标志
  1528. hasValidData.value = true;
  1529. console.log("hasValidData设置为:", hasValidData.value);
  1530. // chatStore.messages.pop();
  1531. // 先推送K线图消息
  1532. const klineMessageId2 = `kline-${Date.now() + 1}`;
  1533. console.log("生成K线消息ID:", klineMessageId2);
  1534. // chatStore.messages.push({
  1535. // sender: "ai",
  1536. // class: "content2",
  1537. // type: "content2",
  1538. // kline: true,
  1539. // chartData: Kline20,
  1540. // messageId: klineMessageId2,
  1541. // hasValidData: true, // 添加hasValidData标志
  1542. // klineType: 2,
  1543. // });
  1544. addTypingTask(
  1545. {
  1546. sender: "ai",
  1547. class: "content2",
  1548. type: "content2",
  1549. kline: true,
  1550. chartData: Kline20,
  1551. messageId: klineMessageId2,
  1552. hasValidData: true, // 添加hasValidData标志
  1553. klineType: 2,
  1554. error: apiStatus.two.error ? "2" : "",
  1555. isEnd: "1",
  1556. },
  1557. "",
  1558. 50
  1559. );
  1560. // console.log("K线消息已添加到聊天列表");
  1561. // // 在渲染完成后初始化图表
  1562. // nextTick(() => {
  1563. // console.log("nextTick开始 - 准备渲染图表");
  1564. // console.log("消息列表:", chatStore.messages);
  1565. // // 寻找最新添加的K线消息索引
  1566. // let klineIndex = -1;
  1567. // for (let i = 0; i < chatStore.messages.length; i++) {
  1568. // if (chatStore.messages[i].messageId === klineMessageId2) {
  1569. // klineIndex = i;
  1570. // break;
  1571. // }
  1572. // }
  1573. // console.log("找到的K线消息索引:", klineIndex);
  1574. // if (klineIndex !== -1) {
  1575. // const containerId = `kline-container-${klineIndex}`;
  1576. // console.log("图表容器ID:", containerId);
  1577. // // 确保DOM已经渲染完成
  1578. // setTimeout(() => {
  1579. // console.log("延时执行,确保DOM已渲染");
  1580. // KlineCanvsEcharts(containerId);
  1581. // }, 100); // 短暂延时确保DOM已渲染
  1582. // } else {
  1583. // console.warn("未找到K线消息");
  1584. // }
  1585. // });
  1586. }
  1587. } else {
  1588. chatStore.messages.push({
  1589. class: "ing",
  1590. type: "ing",
  1591. flag: false,
  1592. content: "工作流返回出错,请稍后重试",
  1593. });
  1594. chatStore.isLoading = false;
  1595. emit("enableInput");
  1596. }
  1597. }
  1598. // 检查TwoAPI(需要OneAPI已执行)
  1599. if (
  1600. apiStatus.one.executed &&
  1601. apiStatus.two.completed &&
  1602. !apiStatus.two.executed
  1603. ) {
  1604. if (apiStatus.two.result) {
  1605. apiStatus.two.executed = true;
  1606. console.log("执行TwoAPI代码:", apiStatus.two.result);
  1607. // 将第二个音频添加到播放队列(确保顺序:API2)
  1608. if (audioPreloadStatus.two.url) {
  1609. chatStore.messages[
  1610. chatStore.currentUserIndex
  1611. ].audioArray.push(audioPreloadStatus.two.url);
  1612. addToAudioQueue(audioPreloadStatus.two.url, "API2-第二个");
  1613. console.log(
  1614. "音频队列:添加API2音频,当前队列长度:",
  1615. audioQueue.value.length
  1616. );
  1617. }
  1618. // 在这里添加TwoAPI成功后需要执行的代码
  1619. // 添加标题2
  1620. addTypingTask(
  1621. {
  1622. sender: "ai",
  1623. class: "title2",
  1624. type: "title2",
  1625. content: "",
  1626. },
  1627. "",
  1628. 50
  1629. );
  1630. // chatStore.messages.push({
  1631. // sender: "ai",
  1632. // class: "title2",
  1633. // type: "title2",
  1634. // content: "",
  1635. // });
  1636. // 添加内容框1
  1637. const pc2 = marked(result22.data.hxjzpg);
  1638. console.log(pc2, "pc2");
  1639. const ac2 = pc2.replace(katexRegex, (match, formula) => {
  1640. try {
  1641. return katex.renderToString(formula, {
  1642. throwOnError: false,
  1643. });
  1644. } catch (error) {
  1645. console.error("KaTeX 渲染错误:", error);
  1646. return match;
  1647. }
  1648. });
  1649. // 先推送初始消息
  1650. const aiMessage2 = reactive({
  1651. sender: "ai",
  1652. class: "content3",
  1653. type: "content3",
  1654. content: "",
  1655. isTyping: true,
  1656. error: apiStatus.three.error ? "3" : "",
  1657. isEnd: "2",
  1658. });
  1659. // chatStore.messages.push(aiMessage2);
  1660. // let index2 = 0;
  1661. // const typingInterval2 = setInterval(() => {
  1662. // if (index2 < ac2.length) {
  1663. // aiMessage2.content += ac2.charAt(index2);
  1664. // index2++;
  1665. // } else {
  1666. // clearInterval(typingInterval2);
  1667. // aiMessage2.isTyping = false;
  1668. // }
  1669. // }, 50); // 调整速度为50ms/字符
  1670. addTypingTask(aiMessage2, ["", ac2], 130);
  1671. // chatStore.messages.push({
  1672. // sender: "ai",
  1673. // class: "content3",
  1674. // type: "content3",
  1675. // content: ac2,
  1676. // });
  1677. } else {
  1678. console.log("1111111111111111111");
  1679. if (
  1680. apiStatus.one.isEnd &&
  1681. apiStatus.two.error &&
  1682. !apiStatus.two.isError
  1683. ) {
  1684. apiStatus.two.isError = true;
  1685. chatStore.messages.push({
  1686. class: "ing",
  1687. type: "ing",
  1688. flag: false,
  1689. content: "工作流返回出错,请稍后重试",
  1690. });
  1691. chatStore.isLoading = false;
  1692. emit("enableInput");
  1693. }
  1694. }
  1695. }
  1696. // 检查ThreeAPI(需要TwoAPI已执行)
  1697. if (
  1698. apiStatus.two.executed &&
  1699. apiStatus.three.completed &&
  1700. !apiStatus.three.executed
  1701. ) {
  1702. if (apiStatus.three.result) {
  1703. apiStatus.three.executed = true;
  1704. console.log("执行ThreeAPI代码:", apiStatus.three.result);
  1705. // 将第三个音频添加到播放队列(确保顺序:API3)
  1706. if (audioPreloadStatus.three.url) {
  1707. chatStore.messages[
  1708. chatStore.currentUserIndex
  1709. ].audioArray.push(audioPreloadStatus.three.url);
  1710. addToAudioQueue(audioPreloadStatus.three.url, "API3-第三个");
  1711. console.log(
  1712. "音频队列:添加API3音频,当前队列长度:",
  1713. audioQueue.value.length
  1714. );
  1715. }
  1716. // 在这里添加ThreeAPI成功后需要执行的代码
  1717. // 添加标题3-2
  1718. addTypingTask(
  1719. {
  1720. sender: "ai",
  1721. class: "title3",
  1722. type: "title3",
  1723. content: title2,
  1724. },
  1725. "",
  1726. 50
  1727. );
  1728. // chatStore.messages.push({
  1729. // sender: "ai",
  1730. // class: "title3",
  1731. // type: "title3",
  1732. // content: title2,
  1733. // });
  1734. // 添加内容框2
  1735. // const pc3 = marked(result23.data.zhuli1+'\n'+result23.data.zhuli2+'\n'+result23.data.zhuli3);
  1736. // const ac3 = pc3.replace(
  1737. // katexRegex,
  1738. // (match, formula) => {
  1739. // try {
  1740. // return katex.renderToString(formula, { throwOnError: false });
  1741. // } catch (error) {
  1742. // console.error("KaTeX 渲染错误:", error);
  1743. // return match;
  1744. // }
  1745. // }
  1746. // );
  1747. const ac31 = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【主力行为】</p><p>`;
  1748. const ac32 = `${result23.data.zhuli1}</p><p>${result23.data.zhuli2}</p><p>${result23.data.zhuli3}</p>`;
  1749. const ac3 = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【主力行为】</p><p>${result23.data.zhuli1}</p><p>${result23.data.zhuli2}</p><p>${result23.data.zhuli3}</p>`;
  1750. // 先推送初始消息
  1751. const aiMessage3 = reactive({
  1752. sender: "ai",
  1753. class: "content3",
  1754. type: "content3",
  1755. content: "",
  1756. isTyping: true,
  1757. });
  1758. // chatStore.messages.push(aiMessage3);
  1759. // let index3 = 0;
  1760. // const typingInterval3 = setInterval(() => {
  1761. // if (index3 < ac3.length) {
  1762. // aiMessage3.content += ac3.charAt(index3);
  1763. // index3++;
  1764. // } else {
  1765. // clearInterval(typingInterval3);
  1766. // aiMessage3.isTyping = false;
  1767. // }
  1768. // }, 50); // 调整速度为50ms/字符
  1769. addTypingTask(aiMessage3, [ac31, ac32], 200);
  1770. // chatStore.messages.push({
  1771. // sender: "ai",
  1772. // class: "content3",
  1773. // type: "content3",
  1774. // content: ac3,
  1775. // });
  1776. // 添加标题3-3
  1777. addTypingTask(
  1778. {
  1779. sender: "ai",
  1780. class: "title3",
  1781. type: "title3",
  1782. content: title3,
  1783. },
  1784. "",
  1785. 50
  1786. );
  1787. // chatStore.messages.push({
  1788. // sender: "ai",
  1789. // class: "title3",
  1790. // type: "title3",
  1791. // content: title3,
  1792. // });
  1793. // 添加内容框3
  1794. const arr = result23.data.kongjian.split(",");
  1795. const kongjian = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【空间维度】</p><p style="display:flex;justify-content:center;">${arr[0]},${arr[1]}</p><p style="display:flex;justify-content:center;">${arr[2]},${arr[3]}</p>`;
  1796. const shijian = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【时间维度】</p><p style="display:flex;justify-content:center;">${result23.data.shijian}</p>`;
  1797. const nengliang = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【能量维度】</p><p>${result23.data.nengliang}</p>`;
  1798. const ac4 = kongjian + shijian + nengliang;
  1799. const ac41 = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【空间维度】</p><p style="display:flex;justify-content:center;">`;
  1800. const ac42 = `${arr[0]},${arr[1]}`;
  1801. const ac43 = `</p><p style="display:flex;justify-content:center;">`;
  1802. const ac44 = `${arr[2]},${arr[3]}</p>`;
  1803. const ac45 = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【时间维度】</p><p style="display:flex;justify-content:center;">`;
  1804. const ac46 = `${result23.data.shijian}</p>`;
  1805. const ac47 = `<p style="margin:0;color:#FADC0C;font-weight:bold;display:flex;justify-content:center;font-size:22px">【能量维度】</p><p style="display:flex;justify-content:center;">`;
  1806. const ac48 = `${result23.data.nengliang}</p>`;
  1807. // const pc4 = marked(
  1808. // kongjian +
  1809. // "\n" +
  1810. // result23.data.shijian +
  1811. // "\n" +
  1812. // result23.data.nengliang
  1813. // );
  1814. // const ac4 = pc4.replace(katexRegex, (match, formula) => {
  1815. // try {
  1816. // return katex.renderToString(formula, { throwOnError: false });
  1817. // } catch (error) {
  1818. // console.error("KaTeX 渲染错误:", error);
  1819. // return match;
  1820. // }
  1821. // });
  1822. // 先推送初始消息
  1823. const aiMessage4 = reactive({
  1824. sender: "ai",
  1825. class: "content3",
  1826. type: "content3",
  1827. content: "",
  1828. isTyping: true,
  1829. error: apiStatus.four.error ? "4" : "",
  1830. isEnd: "3",
  1831. });
  1832. // chatStore.messages.push(aiMessage4);
  1833. // let index4 = 0;
  1834. // const typingInterval4 = setInterval(() => {
  1835. // if (index4 < ac4.length) {
  1836. // aiMessage4.content += ac4.charAt(index4);
  1837. // index4++;
  1838. // } else {
  1839. // clearInterval(typingInterval4);
  1840. // aiMessage4.isTyping = false;
  1841. // }
  1842. // }, 50); // 调整速度为50ms/字符
  1843. addTypingTask(
  1844. aiMessage4,
  1845. [ac41, ac42, ac43, ac44, ac45, ac46, ac47, ac48],
  1846. 200
  1847. );
  1848. // chatStore.messages.push({
  1849. // sender: "ai",
  1850. // class: "content3",
  1851. // type: "content3",
  1852. // content: ac4,
  1853. // });
  1854. } else {
  1855. if (
  1856. apiStatus.two.isEnd &&
  1857. apiStatus.three.error &&
  1858. !apiStatus.three.isError
  1859. ) {
  1860. apiStatus.three.isError = true;
  1861. chatStore.messages.push({
  1862. class: "ing",
  1863. type: "ing",
  1864. flag: false,
  1865. content: "工作流返回出错,请稍后重试1111",
  1866. });
  1867. chatStore.isLoading = false;
  1868. emit("enableInput");
  1869. }
  1870. }
  1871. }
  1872. // 检查FourAPI(需要ThreeAPI已执行)
  1873. if (
  1874. apiStatus.three.executed &&
  1875. apiStatus.four.completed &&
  1876. !apiStatus.four.executed
  1877. ) {
  1878. if (apiStatus.four.result) {
  1879. apiStatus.four.executed = true;
  1880. console.log("执行FourAPI代码:", apiStatus.four.result);
  1881. // 将第四个音频添加到播放队列(确保顺序:API4)
  1882. if (audioPreloadStatus.four.url) {
  1883. chatStore.messages[
  1884. chatStore.currentUserIndex
  1885. ].audioArray.push(audioPreloadStatus.four.url);
  1886. addToAudioQueue(audioPreloadStatus.four.url, "API4-第四个");
  1887. console.log(
  1888. "音频队列:添加API4音频,当前队列长度:",
  1889. audioQueue.value.length
  1890. );
  1891. }
  1892. // 在这里添加FourAPI成功后需要执行的代码
  1893. // 添加标题3-4
  1894. addTypingTask(
  1895. {
  1896. sender: "ai",
  1897. class: "title3",
  1898. type: "title3",
  1899. content: title4,
  1900. },
  1901. "",
  1902. 50
  1903. );
  1904. // chatStore.messages.push({
  1905. // sender: "ai",
  1906. // class: "title3",
  1907. // type: "title3",
  1908. // content: title4,
  1909. // });
  1910. // 添加内容框4
  1911. const cftj = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【触发条件】</p><p>${result24.data.cftl}</p>`;
  1912. const gfzl = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【攻防指令】</p><p>${result24.data.gfzl}</p>`;
  1913. const ac5 = cftj + gfzl;
  1914. const ac51 = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【触发条件】</p><p>`;
  1915. const ac52 = `${result24.data.cftl}</p>`;
  1916. const ac53 = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【攻防指令】</p><p>`;
  1917. const ac54 = `${result24.data.gfzl}</p>`;
  1918. // const pc5 = marked(result24.data.cftl + "/n" + result24.data.gfzl);
  1919. // const ac5 = pc5.replace(katexRegex, (match, formula) => {
  1920. // try {
  1921. // return katex.renderToString(formula, { throwOnError: false });
  1922. // } catch (error) {
  1923. // console.error("KaTeX 渲染错误:", error);
  1924. // return match;
  1925. // }
  1926. // });
  1927. // 先推送初始消息
  1928. const aiMessage5 = reactive({
  1929. sender: "ai",
  1930. class: "content3",
  1931. type: "content3",
  1932. content: "",
  1933. isTyping: true,
  1934. });
  1935. // chatStore.messages.push(aiMessage5);
  1936. // let index5 = 0;
  1937. // const typingInterval5 = setInterval(() => {
  1938. // if (index5 < ac5.length) {
  1939. // aiMessage5.content += ac5.charAt(index5);
  1940. // index5++;
  1941. // } else {
  1942. // clearInterval(typingInterval5);
  1943. // aiMessage5.isTyping = false;
  1944. // }
  1945. // }, 50); // 调整速度为50ms/字符
  1946. addTypingTask(aiMessage5, [ac51, ac52, ac53, ac54], 240);
  1947. // chatStore.messages.push({
  1948. // sender: "ai",
  1949. // class: "content3",
  1950. // type: "content3",
  1951. // content: ac5,
  1952. // });
  1953. const ac6 = "该内容由AI生成,请注意甄别";
  1954. // 先推送初始消息
  1955. const aiMessage6 = reactive({
  1956. sender: "ai",
  1957. class: "mianze",
  1958. type: "mianze",
  1959. content: "",
  1960. isTyping: true,
  1961. end: true,
  1962. });
  1963. // chatStore.messages.push(aiMessage6);
  1964. // let index6 = 0;
  1965. // const typingInterval6 = setInterval(() => {
  1966. // if (index6 < ac6.length) {
  1967. // aiMessage6.content += ac6.charAt(index6);
  1968. // index6++;
  1969. // } else {
  1970. // clearInterval(typingInterval6);
  1971. // aiMessage6.isTyping = false;
  1972. // }
  1973. // }, 50); // 调整速度为50ms/字符
  1974. addTypingTask(aiMessage6, ["", ac6], 210);
  1975. // chatStore.isLoading = false;
  1976. // chatStore.messages.push({
  1977. // sender: "ai",
  1978. // class: "mianze",
  1979. // type: "mianze",
  1980. // content: "内容由AI生成,请注意甄别",
  1981. // });
  1982. } else {
  1983. if (
  1984. apiStatus.three.isEnd &&
  1985. apiStatus.four.error &&
  1986. !apiStatus.four.isError
  1987. ) {
  1988. apiStatus.four.isError = true;
  1989. chatStore.messages.push({
  1990. class: "ing",
  1991. type: "ing",
  1992. flag: false,
  1993. content: "工作流返回出错,请稍后重试",
  1994. });
  1995. chatStore.isLoading = false;
  1996. emit("enableInput");
  1997. }
  1998. }
  1999. }
  2000. // 检查是否所有API都已完成并执行
  2001. if (
  2002. apiStatus.one.completed &&
  2003. apiStatus.two.completed &&
  2004. apiStatus.three.completed &&
  2005. apiStatus.four.completed &&
  2006. apiStatus.four.executed
  2007. ) {
  2008. console.log("所有API已完成,开始收集预加载的音频URL");
  2009. // 收集所有预加载的音频URL
  2010. const audioUrls = [];
  2011. console.log("预加载音频状态检查:");
  2012. console.log("audioPreloadStatus:", audioPreloadStatus);
  2013. if (audioPreloadStatus.one.url) {
  2014. console.log(
  2015. "添加预加载音频URL one:",
  2016. audioPreloadStatus.one.url
  2017. );
  2018. audioUrls.push(audioPreloadStatus.one.url);
  2019. }
  2020. if (audioPreloadStatus.two.url) {
  2021. console.log(
  2022. "添加预加载音频URL two:",
  2023. audioPreloadStatus.two.url
  2024. );
  2025. audioUrls.push(audioPreloadStatus.two.url);
  2026. }
  2027. if (audioPreloadStatus.three.url) {
  2028. console.log(
  2029. "添加预加载音频URL three:",
  2030. audioPreloadStatus.three.url
  2031. );
  2032. audioUrls.push(audioPreloadStatus.three.url);
  2033. }
  2034. if (audioPreloadStatus.four.url) {
  2035. console.log(
  2036. "添加预加载音频URL four:",
  2037. audioPreloadStatus.four.url
  2038. );
  2039. audioUrls.push(audioPreloadStatus.four.url);
  2040. }
  2041. console.log("收集到的预加载音频URLs:", audioUrls);
  2042. console.log("语音是否启用:", audioStore.isVoiceEnabled);
  2043. // 音频播放逻辑已移至各个接口的执行代码中
  2044. console.log("所有接口执行完成,音频已在各接口中单独播放");
  2045. }
  2046. };
  2047. const handleOneAPI = async () => {
  2048. try {
  2049. result21 = await dbqbSecondOneAPI(params2);
  2050. if (result21.code == 400) {
  2051. throw new Error("API返回错误码400,请求失败");
  2052. }
  2053. console.log("OneAPI成功返回:", result21);
  2054. apiStatus.one.completed = true;
  2055. apiStatus.one.result = result21;
  2056. // 预加载第一个接口的音频
  2057. if (result21?.data?.url) {
  2058. await preloadAudio(result21.data.url.trim(), "one");
  2059. } else {
  2060. audioPreloadStatus.one.loaded = true;
  2061. }
  2062. // 检查是否可以执行
  2063. checkAndExecuteInOrder();
  2064. } catch (error) {
  2065. console.error("OneAPI失败:", error);
  2066. apiStatus.one.completed = true;
  2067. apiStatus.one.error = error;
  2068. audioPreloadStatus.one.loaded = true; // 失败时也标记为已处理
  2069. // 即使失败也要检查后续执行
  2070. checkAndExecuteInOrder();
  2071. }
  2072. };
  2073. const handleTwoAPI = async () => {
  2074. try {
  2075. result22 = await dbqbSecondTwoAPI(params2);
  2076. if (result22.code == 400) {
  2077. throw new Error("API返回错误码400,请求失败");
  2078. }
  2079. console.log("TwoAPI成功返回:", result22);
  2080. apiStatus.two.completed = true;
  2081. apiStatus.two.result = result22;
  2082. // 预加载第二个接口的音频
  2083. if (result22?.data?.url) {
  2084. await preloadAudio(result22.data.url.trim(), "two");
  2085. } else {
  2086. audioPreloadStatus.two.loaded = true;
  2087. }
  2088. // 检查是否可以执行
  2089. checkAndExecuteInOrder();
  2090. } catch (error) {
  2091. console.error("TwoAPI失败:", error);
  2092. apiStatus.two.completed = true;
  2093. apiStatus.two.error = error;
  2094. audioPreloadStatus.two.loaded = true;
  2095. checkAndExecuteInOrder();
  2096. }
  2097. };
  2098. const handleThreeAPI = async () => {
  2099. try {
  2100. result23 = await dbqbSecondThreeAPI(params2);
  2101. if (result23.code == 400) {
  2102. throw new Error("API返回错误码400,请求失败");
  2103. }
  2104. // result23 = await dbqbSecondThreeAPI();
  2105. console.log("ThreeAPI成功返回:", result23);
  2106. apiStatus.three.completed = true;
  2107. apiStatus.three.result = result23;
  2108. // 预加载第三个接口的音频
  2109. if (result23?.data?.url) {
  2110. await preloadAudio(result23.data.url.trim(), "three");
  2111. } else {
  2112. audioPreloadStatus.three.loaded = true;
  2113. }
  2114. // 检查是否可以执行
  2115. checkAndExecuteInOrder();
  2116. } catch (error) {
  2117. console.error("ThreeAPI失败:", error);
  2118. apiStatus.three.completed = true;
  2119. apiStatus.three.error = error;
  2120. audioPreloadStatus.three.loaded = true;
  2121. checkAndExecuteInOrder();
  2122. }
  2123. };
  2124. const handleFourAPI = async () => {
  2125. try {
  2126. result24 = await dbqbSecondFourAPI(params2);
  2127. if (result24.code == 400) {
  2128. throw new Error("API返回错误码400,请求失败");
  2129. }
  2130. console.log("FourAPI成功返回:", result24);
  2131. apiStatus.four.completed = true;
  2132. apiStatus.four.result = result24;
  2133. // 预加载第四个接口的音频
  2134. if (result24?.data?.url) {
  2135. await preloadAudio(result24.data.url.trim(), "four");
  2136. } else {
  2137. audioPreloadStatus.four.loaded = true;
  2138. }
  2139. // 检查是否可以执行
  2140. checkAndExecuteInOrder();
  2141. } catch (error) {
  2142. console.error("FourAPI失败:", error);
  2143. apiStatus.four.completed = true;
  2144. apiStatus.four.error = error;
  2145. audioPreloadStatus.four.loaded = true;
  2146. checkAndExecuteInOrder();
  2147. }
  2148. };
  2149. if (isLiuSe && isAIGoldBull) {
  2150. handleOneAPI();
  2151. handleTwoAPI();
  2152. handleThreeAPI();
  2153. handleFourAPI();
  2154. } else {
  2155. chatStore.messages.pop();
  2156. chatStore.messages.push({
  2157. class: "ing",
  2158. type: "ing",
  2159. flag: false,
  2160. content: "数据缺失,请稍后重试",
  2161. });
  2162. chatStore.isLoading = false;
  2163. emit("enableInput");
  2164. }
  2165. } catch (e) {
  2166. console.error("请求失败:", e);
  2167. hasValidData.value = false; // 请求失败时设置数据无效
  2168. // chatStore.messages.pop();
  2169. // chatStore.messages.push({
  2170. // sender: "ai",
  2171. // content: "AI思考失败,请稍后再试... ",
  2172. // });
  2173. // chatStore.setLoading(false);
  2174. } finally {
  2175. // chatStore.setLoading(false);
  2176. await chatStore.getUserCount();
  2177. }
  2178. }
  2179. }
  2180. },
  2181. { deep: false }
  2182. );
  2183. // 点击历史记录
  2184. watch(
  2185. () => chatStore.dbqbClickRecord,
  2186. (newValue, oldValue) => {
  2187. console.log("new", newValue);
  2188. if (!newValue || Object.keys(newValue).length === 0) {
  2189. return;
  2190. }
  2191. const clickRecord = ref(newValue);
  2192. console.log("dbqbClickRecord 发生变化:", clickRecord.value);
  2193. // 在下一个事件循环中清空 dbqbClickRecord
  2194. setTimeout(() => {
  2195. chatStore.dbqbClickRecord = {};
  2196. console.log("dbqbClickRecord 已清空");
  2197. }, 0);
  2198. if (
  2199. !clickRecord.value.wokeFlowData.One ||
  2200. !clickRecord.value.wokeFlowData.Two ||
  2201. !clickRecord.value.wokeFlowData.Three ||
  2202. !clickRecord.value.wokeFlowData.Four
  2203. ) {
  2204. return;
  2205. }
  2206. try {
  2207. // 清空聊天框内容
  2208. chatStore.messages = [];
  2209. chatStore.messages.push({
  2210. sender: "user",
  2211. timestamp: clickRecord.value.createdTime,
  2212. content: clickRecord.value.keyword,
  2213. audioArray: [
  2214. clickRecord.value.wokeFlowData.One.url,
  2215. clickRecord.value.wokeFlowData.Two.url,
  2216. clickRecord.value.wokeFlowData.Three.url,
  2217. clickRecord.value.wokeFlowData.Four.url,
  2218. ],
  2219. audioStatus: true,
  2220. });
  2221. chatStore.messages.push({
  2222. sender: "ai",
  2223. class: "title1",
  2224. type: "title1",
  2225. content: clickRecord.value.stockName + "全景作战报告",
  2226. date: clickRecord.value.wokeFlowData.One.date,
  2227. });
  2228. const pc1 = marked(
  2229. clickRecord.value.wokeFlowData.One.name +
  2230. "\n" +
  2231. clickRecord.value.wokeFlowData.One.price +
  2232. "\n" +
  2233. clickRecord.value.wokeFlowData.One.date
  2234. );
  2235. chatStore.messages.push({
  2236. sender: "ai",
  2237. class: "content1",
  2238. type: "content1",
  2239. content: pc1,
  2240. });
  2241. const HomePage = clickRecord.value.stockData.HomePage;
  2242. if (HomePage) {
  2243. const LiuSeData = JSON.parse(JSON.stringify(toRaw(HomePage)));
  2244. const sz = fnGetData(LiuSeData);
  2245. if (sz) {
  2246. hasValidData.value = true;
  2247. console.log("hasValidData设置为:", hasValidData.value);
  2248. }
  2249. // 先推送K线图消息
  2250. const klineMessageId1 = `kline-${Date.now()}`;
  2251. console.log("生成K线消息ID:", klineMessageId1);
  2252. chatStore.messages.push({
  2253. sender: "ai",
  2254. class: "content1",
  2255. type: "content1",
  2256. kline: true,
  2257. chartData: sz,
  2258. messageId: klineMessageId1,
  2259. hasValidData: true,
  2260. klineType: 1,
  2261. });
  2262. // 在渲染完成后初始化图表
  2263. nextTick(() => {
  2264. console.log("nextTick开始 - 准备渲染图表");
  2265. console.log("消息列表:", chatStore.messages);
  2266. // 寻找最新添加的K线消息索引
  2267. let klineIndex = -1;
  2268. for (let i = 0; i < chatStore.messages.length; i++) {
  2269. if (chatStore.messages[i].messageId === klineMessageId1) {
  2270. klineIndex = i;
  2271. break;
  2272. }
  2273. }
  2274. console.log("找到的K线消息索引:", klineIndex);
  2275. if (klineIndex !== -1) {
  2276. const containerId = `kline-container-${klineIndex}`;
  2277. console.log("图表容器ID:", containerId);
  2278. // 确保DOM已经渲染完成
  2279. setTimeout(() => {
  2280. console.log("延时执行,确保DOM已渲染");
  2281. KlineCanvsEcharts(containerId);
  2282. }, 100); // 短暂延时确保DOM已渲染
  2283. } else {
  2284. console.warn("未找到K线消息");
  2285. }
  2286. });
  2287. }
  2288. const AIGoldBull = clickRecord.value.stockData.AIGoldBull;
  2289. // 度牛尺K线图
  2290. if (
  2291. AIGoldBull &&
  2292. AIGoldBull.DNC &&
  2293. AIGoldBull.FCX &&
  2294. AIGoldBull.JN &&
  2295. AIGoldBull.KLine20 &&
  2296. AIGoldBull.QSXH
  2297. ) {
  2298. const AIGoldBullData = JSON.parse(JSON.stringify(toRaw(AIGoldBull)));
  2299. const HomePageData = JSON.parse(JSON.stringify(toRaw(HomePage)));
  2300. console.log("处理 K 线数据 - 开始");
  2301. console.log("AIGoldBullData", AIGoldBullData);
  2302. console.log("HomePageData", HomePageData);
  2303. const Kline20 = {
  2304. name: HomePageData.StockInformation.Name,
  2305. Kline: AIGoldBullData,
  2306. };
  2307. // 打印K线数据结构
  2308. console.log("K线数据结构:", Kline20);
  2309. console.log("K线数据名称:", Kline20.name);
  2310. console.log("K线数据:", Kline20.Kline ? Kline20.Kline : null);
  2311. // 设置数据有效标志
  2312. hasValidData.value = true;
  2313. console.log("hasValidData设置为:", hasValidData.value);
  2314. // chatStore.messages.pop();
  2315. // 先推送K线图消息
  2316. const klineMessageId2 = `kline-${Date.now() + 1}`;
  2317. console.log("生成K线消息ID:", klineMessageId2);
  2318. chatStore.messages.push({
  2319. sender: "ai",
  2320. class: "content2",
  2321. type: "content2",
  2322. kline: true,
  2323. chartData: Kline20,
  2324. messageId: klineMessageId2,
  2325. hasValidData: true, // 添加hasValidData标志
  2326. klineType: 2,
  2327. });
  2328. // 在渲染完成后初始化图表
  2329. nextTick(() => {
  2330. console.log("nextTick开始 - 准备渲染图表");
  2331. console.log("消息列表:", chatStore.messages);
  2332. // 寻找最新添加的K线消息索引
  2333. let klineIndex = -1;
  2334. for (let i = 0; i < chatStore.messages.length; i++) {
  2335. if (chatStore.messages[i].messageId === klineMessageId2) {
  2336. klineIndex = i;
  2337. break;
  2338. }
  2339. }
  2340. console.log("找到的K线消息索引:", klineIndex);
  2341. if (klineIndex !== -1) {
  2342. const containerId = `kline-container-${klineIndex}`;
  2343. console.log("图表容器ID:", containerId);
  2344. // 确保DOM已经渲染完成
  2345. setTimeout(() => {
  2346. console.log("延时执行,确保DOM已渲染");
  2347. KlineCanvsEcharts(containerId);
  2348. }, 100); // 短暂延时确保DOM已渲染
  2349. } else {
  2350. console.warn("未找到K线消息");
  2351. }
  2352. });
  2353. }
  2354. chatStore.messages.push({
  2355. sender: "ai",
  2356. class: "title2",
  2357. type: "title2",
  2358. content: "",
  2359. });
  2360. const pc2 = marked(clickRecord.value.wokeFlowData.Two.hxjzpg);
  2361. // 先推送初始消息
  2362. chatStore.messages.push({
  2363. sender: "ai",
  2364. class: "content3",
  2365. type: "content3",
  2366. content: pc2,
  2367. });
  2368. chatStore.messages.push({
  2369. sender: "ai",
  2370. class: "title3",
  2371. type: "title3",
  2372. content: title2,
  2373. });
  2374. const ac3 = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【主力行为】</p><p>${clickRecord.value.wokeFlowData.Three.zhuli1}</p><p>${clickRecord.value.wokeFlowData.Three.zhuli2}</p><p>${clickRecord.value.wokeFlowData.Three.zhuli3}</p>`;
  2375. // 先推送初始消息
  2376. chatStore.messages.push({
  2377. sender: "ai",
  2378. class: "content3",
  2379. type: "content3",
  2380. content: ac3,
  2381. isTyping: true,
  2382. });
  2383. chatStore.messages.push({
  2384. sender: "ai",
  2385. class: "title3",
  2386. type: "title3",
  2387. content: title3,
  2388. });
  2389. const arr = clickRecord.value.wokeFlowData.Three.kongjian.split(",");
  2390. const kongjian = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【空间维度】</p><p style="display:flex;justify-content:center;">${arr[0]},${arr[1]}</p><p style="display:flex;justify-content:center;">${arr[2]},${arr[3]}</p>`;
  2391. const shijian = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【时间维度】</p><p style="display:flex;justify-content:center;">${clickRecord.value.wokeFlowData.Three.shijian}</p>`;
  2392. const nengliang = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【能量维度】</p><p style="display:flex;justify-content:center;">${clickRecord.value.wokeFlowData.Three.nengliang}</p>`;
  2393. const ac4 = kongjian + shijian + nengliang;
  2394. // 先推送初始消息
  2395. chatStore.messages.push({
  2396. sender: "ai",
  2397. class: "content3",
  2398. type: "content3",
  2399. content: ac4,
  2400. });
  2401. chatStore.messages.push({
  2402. sender: "ai",
  2403. class: "title3",
  2404. type: "title3",
  2405. content: title4,
  2406. });
  2407. const cftj = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【触发条件】</p><p>${clickRecord.value.wokeFlowData.Four.cftl}</p>`;
  2408. const gfzl = `<p style="margin:0;color:#FFD700;font-weight:bold;display:flex;justify-content:center;font-size:22px">【攻防指令】</p><p>${clickRecord.value.wokeFlowData.Four.gfzl}</p>`;
  2409. const ac5 = cftj + gfzl;
  2410. // 先推送初始消息
  2411. chatStore.messages.push({
  2412. sender: "ai",
  2413. class: "content3",
  2414. type: "content3",
  2415. content: ac5,
  2416. });
  2417. chatStore.messages.push({
  2418. sender: "ai",
  2419. class: "mianze",
  2420. type: "mianze",
  2421. content: "该内容由AI生成,请注意甄别",
  2422. end: true,
  2423. });
  2424. } catch (e) {
  2425. ElMessage.error("历史数据获取出错!");
  2426. console.error("e", e);
  2427. }
  2428. },
  2429. {
  2430. deep: true, // 深度监听对象内部属性的变化
  2431. immediate: true, // 立即执行一次回调
  2432. }
  2433. );
  2434. function KlineCanvsEcharts(containerId) {
  2435. function vwToPx(vw) {
  2436. console.log((window.innerWidth * vw) / 100, "vwToPx");
  2437. return (window.innerWidth * vw) / 100;
  2438. }
  2439. // console.log("KLine渲染: 开始处理数据, 容器ID:", containerId);
  2440. // 从 chatStore 中获取数据
  2441. const messages = chatStore.messages;
  2442. // console.log("KLine渲染: 获取到的消息:", messages);
  2443. let klineMessageIndex = -1;
  2444. let klineData = null;
  2445. // 查找最近的 K线 消息
  2446. // for (let i = messages.length - 1; i >= 0; i--) {
  2447. // console.log('KLine渲染: 检查消息:', messages[i]);
  2448. // if (messages[i].type === 'kline' && messages[i].chartData) {
  2449. // klineMessageIndex = i;
  2450. // klineData = messages[i].chartData;
  2451. // console.log('KLine渲染: 找到K线消息索引:', klineMessageIndex);
  2452. // console.log('KLine渲染: 找到K线数据:', klineData);
  2453. // break;
  2454. // }
  2455. // }
  2456. klineMessageIndex = containerId.split("-")[2];
  2457. // console.log("KLine渲染: 找到K线消息索引:", klineMessageIndex);
  2458. if (
  2459. messages[klineMessageIndex].kline &&
  2460. messages[klineMessageIndex].chartData
  2461. ) {
  2462. klineData = messages[klineMessageIndex].chartData;
  2463. }
  2464. var KlineOption = {};
  2465. // 检测设备类型
  2466. const isMobile = window.innerWidth < 768;
  2467. const isTablet = window.innerWidth >= 768 && window.innerWidth < 1024;
  2468. console.log(
  2469. "KLine渲染: 设备类型",
  2470. isMobile ? "移动设备" : isTablet ? "平板设备" : "桌面设备"
  2471. );
  2472. if (messages[klineMessageIndex].klineType == 1) {
  2473. if (!klineData) {
  2474. // console.warn("六色罗盘渲染: 数据无效 - 在chatStore中找不到有效的K线数据");
  2475. return;
  2476. }
  2477. // 获取容器元素
  2478. const container = document.getElementById(containerId);
  2479. if (!container) {
  2480. // console.error("六色罗盘渲染: 找不到容器元素:", containerId);
  2481. return;
  2482. }
  2483. // 创建图表实例
  2484. // console.log("六色罗盘渲染: 创建图表实例");
  2485. try {
  2486. // 如果已有实例,先销毁
  2487. if (chartInstancesMap[containerId]) {
  2488. // console.log("六色罗盘渲染: 销毁已有图表实例");
  2489. chartInstancesMap[containerId].dispose();
  2490. delete chartInstancesMap[containerId];
  2491. }
  2492. // 使用普通变量存储实例
  2493. chartInstancesMap[containerId] = echarts.init(container);
  2494. // console.log("六色罗盘渲染: 图表实例创建成功");
  2495. } catch (error) {
  2496. // console.error("六色罗盘渲染: 图表实例创建失败:", error);
  2497. return;
  2498. }
  2499. const name = ref("六色罗盘");
  2500. const size = ref(16);
  2501. // PC版字体大小
  2502. if (window.innerWidth > 768) {
  2503. size.value = 25;
  2504. }
  2505. KlineOption = {
  2506. tooltip: {
  2507. show: !1,
  2508. },
  2509. series: [
  2510. {
  2511. name: "\u4eea\u8868\u76d8",
  2512. type: "gauge",
  2513. center: ["50%", "50%"],
  2514. radius: "90%",
  2515. startAngle: 140,
  2516. endAngle: -140,
  2517. min: 0,
  2518. max: 6,
  2519. precision: 0,
  2520. splitNumber: 30, // 分成30份
  2521. axisLine: {
  2522. show: !0,
  2523. lineStyle: {
  2524. color: [
  2525. [0.17, "#FC4407"],
  2526. [0.33, "#FDC404"],
  2527. [0.5, "#2D8FFD"],
  2528. [0.67, "#87CCE7"],
  2529. [0.83, "#C1F478"],
  2530. [1, "#8FEB8D"],
  2531. ],
  2532. width: 20,
  2533. },
  2534. },
  2535. axisTick: {
  2536. show: !0,
  2537. splitNumber: 9,
  2538. length: 8,
  2539. lineStyle: {
  2540. color: "#eee",
  2541. width: 1,
  2542. type: "solid",
  2543. },
  2544. },
  2545. axisLabel: {
  2546. show: true,
  2547. formatter: function (v) {},
  2548. textStyle: {
  2549. color: "auto",
  2550. },
  2551. },
  2552. // 中途切割
  2553. splitLine: {
  2554. show: !0,
  2555. length: 20,
  2556. lineStyle: {
  2557. color: "#eee",
  2558. width: 2,
  2559. type: "solid",
  2560. },
  2561. },
  2562. pointer: {
  2563. length: "80%",
  2564. width: 8,
  2565. color: "auto",
  2566. },
  2567. title: {
  2568. show: !0,
  2569. offsetCenter: ["-65%", -10],
  2570. textStyle: {
  2571. color: "#333",
  2572. fontSize: 15,
  2573. },
  2574. },
  2575. detail: {
  2576. show: !0,
  2577. backgroundColor: "rgba(0,0,0,0)",
  2578. borderWidth: 0,
  2579. borderColor: "#ccc",
  2580. width: 100,
  2581. height: 40,
  2582. offsetCenter: ["-90%", 0], // name位置
  2583. formatter: function () {
  2584. return name.value;
  2585. },
  2586. textStyle: {
  2587. color: "auto",
  2588. fontSize: size.value, // 字体尺寸
  2589. },
  2590. },
  2591. data: [{ value: klineData }],
  2592. },
  2593. ],
  2594. };
  2595. } else if (messages[klineMessageIndex].klineType == 2) {
  2596. if (!klineData || !klineData.Kline) {
  2597. // console.warn("KLine渲染: 数据无效 - 在chatStore中找不到有效的K线数据");
  2598. return;
  2599. }
  2600. // 获取容器元素
  2601. const container = document.getElementById(containerId);
  2602. if (!container) {
  2603. // console.error("KLine渲染: 找不到容器元素:", containerId);
  2604. return;
  2605. }
  2606. // 创建图表实例
  2607. // console.log("KLine渲染: 创建图表实例");
  2608. try {
  2609. // 如果已有实例,先销毁
  2610. if (chartInstancesMap[containerId]) {
  2611. // console.log("KLine渲染: 销毁已有图表实例");
  2612. chartInstancesMap[containerId].dispose();
  2613. delete chartInstancesMap[containerId];
  2614. }
  2615. // 使用普通变量存储实例
  2616. chartInstancesMap[containerId] = echarts.init(container);
  2617. // console.log("KLine渲染: 图表实例创建成功");
  2618. } catch (error) {
  2619. // console.error("KLine渲染: 图表实例创建失败:", error);
  2620. return;
  2621. }
  2622. const data = klineData.Kline;
  2623. // console.log("KLine渲染: Kline数据", data);
  2624. // 切割数据方法
  2625. const splitData = (a) => {
  2626. // console.log("KLine渲染: 开始数据切割");
  2627. const categoryData = [];
  2628. let values = [];
  2629. for (let i = 0; i < a.length; i++) {
  2630. categoryData.push(a[i][0]);
  2631. values.push([a[i][1], a[i][2], a[i][3], a[i][4]]);
  2632. }
  2633. // console.log("KLine渲染: 日期数据点数量", categoryData.length);
  2634. // console.log("KLine渲染: 值数据点数量", values.length);
  2635. return { categoryData, values };
  2636. };
  2637. // 给配置项
  2638. // console.log("KLine渲染: 开始配置图表选项");
  2639. const arr1 = [];
  2640. const arr2 = [];
  2641. const arr3 = [];
  2642. const arr4 = [];
  2643. const changeColorKline = (QSXH, KLine20) => {
  2644. if (QSXH) {
  2645. QSXH.map((item) => {
  2646. KLine20.map((kline_item) => {
  2647. if (item[1] == 1 && item[0] == kline_item[0]) {
  2648. arr1.push(kline_item);
  2649. arr2.push([
  2650. kline_item[0],
  2651. null,
  2652. null,
  2653. null,
  2654. null,
  2655. null,
  2656. null,
  2657. null,
  2658. ]);
  2659. arr3.push([
  2660. kline_item[0],
  2661. null,
  2662. null,
  2663. null,
  2664. null,
  2665. null,
  2666. null,
  2667. null,
  2668. ]);
  2669. arr4.push([
  2670. kline_item[0],
  2671. null,
  2672. null,
  2673. null,
  2674. null,
  2675. null,
  2676. null,
  2677. null,
  2678. ]);
  2679. }
  2680. if (item[1] == 2 && item[0] == kline_item[0]) {
  2681. arr2.push(kline_item);
  2682. arr1.push([
  2683. kline_item[0],
  2684. null,
  2685. null,
  2686. null,
  2687. null,
  2688. null,
  2689. null,
  2690. null,
  2691. ]);
  2692. arr3.push([
  2693. kline_item[0],
  2694. null,
  2695. null,
  2696. null,
  2697. null,
  2698. null,
  2699. null,
  2700. null,
  2701. ]);
  2702. arr4.push([
  2703. kline_item[0],
  2704. null,
  2705. null,
  2706. null,
  2707. null,
  2708. null,
  2709. null,
  2710. null,
  2711. ]);
  2712. }
  2713. if (item[1] == 3 && item[0] == kline_item[0]) {
  2714. arr3.push(kline_item);
  2715. arr2.push([
  2716. kline_item[0],
  2717. null,
  2718. null,
  2719. null,
  2720. null,
  2721. null,
  2722. null,
  2723. null,
  2724. ]);
  2725. arr1.push([
  2726. kline_item[0],
  2727. null,
  2728. null,
  2729. null,
  2730. null,
  2731. null,
  2732. null,
  2733. null,
  2734. ]);
  2735. arr4.push([
  2736. kline_item[0],
  2737. null,
  2738. null,
  2739. null,
  2740. null,
  2741. null,
  2742. null,
  2743. null,
  2744. ]);
  2745. }
  2746. if (item[1] == 4 && item[0] == kline_item[0]) {
  2747. arr4.push(kline_item);
  2748. arr2.push([
  2749. kline_item[0],
  2750. null,
  2751. null,
  2752. null,
  2753. null,
  2754. null,
  2755. null,
  2756. null,
  2757. ]);
  2758. arr3.push([
  2759. kline_item[0],
  2760. null,
  2761. null,
  2762. null,
  2763. null,
  2764. null,
  2765. null,
  2766. null,
  2767. ]);
  2768. arr1.push([
  2769. kline_item[0],
  2770. null,
  2771. null,
  2772. null,
  2773. null,
  2774. null,
  2775. null,
  2776. null,
  2777. ]);
  2778. }
  2779. });
  2780. });
  2781. }
  2782. };
  2783. console.log(arr1, arr2, arr3, arr4);
  2784. changeColorKline(data.QSXH, data.KLine20);
  2785. var dealData = splitData(data.KLine20);
  2786. var dealData1 = splitData(arr1);
  2787. var dealData2 = splitData(arr2);
  2788. var dealData3 = splitData(arr3);
  2789. var dealData4 = splitData(arr4);
  2790. var dealGnBullData = data.JN;
  2791. function processMAData(data) {
  2792. let processedData = [];
  2793. data.forEach((item, idx) => {
  2794. processedData.push({
  2795. date: item[0],
  2796. value: item[1],
  2797. type: item[2],
  2798. });
  2799. });
  2800. // 当某一种type只存在一天,设置另一种type透明
  2801. let singleTypeRed = [{ min: 0, max: 0, color: "#000" }];
  2802. let singleTypeYellow = [{ min: 0, max: 0, color: "#000" }];
  2803. let singleTypeGreen = [{ min: 0, max: 0, color: "#000" }];
  2804. for (let i = 1; i < processedData.length; i++) {
  2805. if (processedData[i].type !== processedData[i - 1].type) {
  2806. if (
  2807. i == processedData.length - 1 ||
  2808. (processedData[i].type !== processedData[i + 1].type &&
  2809. processedData[i - 1].type === processedData[i + 1].type)
  2810. ) {
  2811. if (processedData[i - 1].type === 0) {
  2812. singleTypeGreen.push({
  2813. min: i - 1,
  2814. max: i,
  2815. color: "rgba(0,0,0,0)",
  2816. });
  2817. } else if (processedData[i - 1].type === 1) {
  2818. singleTypeRed.push({
  2819. min: i - 1,
  2820. max: i,
  2821. color: "rgba(0,0,0,0)",
  2822. });
  2823. } else if (processedData[i - 1].type === 2) {
  2824. singleTypeYellow.push({
  2825. min: i - 1,
  2826. max: i,
  2827. color: "rgba(0,0,0,0)",
  2828. });
  2829. }
  2830. }
  2831. }
  2832. if (processedData[i].type !== processedData[i - 1].type) {
  2833. if (processedData[i].type == 0) {
  2834. processedData[i - 1].isTransitionGreen = 1;
  2835. } else if (processedData[i].type == 1) {
  2836. processedData[i - 1].isTransitionRed = 1;
  2837. } else if (processedData[i].type == 2) {
  2838. processedData[i - 1].isTransitionYellow = 1;
  2839. }
  2840. // // 创建过渡点,使用前一个点的值
  2841. // processedData[i - 1].isTransition = true
  2842. }
  2843. }
  2844. let greenData = [];
  2845. let redData = [];
  2846. let yellowData = [];
  2847. processedData.forEach((item, idx) => {
  2848. const point = [item.date, item.value];
  2849. if (item.type === 0) {
  2850. greenData.push(point);
  2851. redData.push([item.date, "-"]);
  2852. yellowData.push([item.date, "-"]);
  2853. // if (item.isTransition) {
  2854. // redData[redData.length - 1] = [processedData[idx].date, processedData[idx].value]
  2855. // yellowData[yellowData.length - 1] = [processedData[idx].date, processedData[idx].value]
  2856. // }
  2857. if (item.isTransitionGreen) {
  2858. greenData[greenData.length - 1] = [
  2859. processedData[idx].date,
  2860. processedData[idx].value,
  2861. ];
  2862. } else if (item.isTransitionRed) {
  2863. redData[redData.length - 1] = [
  2864. processedData[idx].date,
  2865. processedData[idx].value,
  2866. ];
  2867. } else if (item.isTransitionYellow) {
  2868. yellowData[yellowData.length - 1] = [
  2869. processedData[idx].date,
  2870. processedData[idx].value,
  2871. ];
  2872. }
  2873. } else if (item.type === 1) {
  2874. redData.push(point);
  2875. greenData.push([item.date, "-"]);
  2876. yellowData.push([item.date, "-"]);
  2877. // if (item.isTransition) {
  2878. // greenData[greenData.length - 1] = [processedData[idx].date, processedData[idx].value]
  2879. // yellowData[yellowData.length - 1] = [processedData[idx].date, processedData[idx].value]
  2880. // }
  2881. if (item.isTransitionGreen) {
  2882. greenData[greenData.length - 1] = [
  2883. processedData[idx].date,
  2884. processedData[idx].value,
  2885. ];
  2886. } else if (item.isTransitionRed) {
  2887. redData[redData.length - 1] = [
  2888. processedData[idx].date,
  2889. processedData[idx].value,
  2890. ];
  2891. } else if (item.isTransitionYellow) {
  2892. yellowData[yellowData.length - 1] = [
  2893. processedData[idx].date,
  2894. processedData[idx].value,
  2895. ];
  2896. }
  2897. } else if (item.type === 2) {
  2898. redData.push([item.date, "-"]);
  2899. greenData.push([item.date, "-"]);
  2900. yellowData.push(point);
  2901. // if (item.isTransition) {
  2902. // greenData[greenData.length - 1] = [processedData[idx].date, processedData[idx].value]
  2903. // redData[redData.length - 1] = [processedData[idx].date, processedData[idx].value]
  2904. // }
  2905. if (item.isTransitionGreen) {
  2906. greenData[greenData.length - 1] = [
  2907. processedData[idx].date,
  2908. processedData[idx].value,
  2909. ];
  2910. } else if (item.isTransitionRed) {
  2911. redData[redData.length - 1] = [
  2912. processedData[idx].date,
  2913. processedData[idx].value,
  2914. ];
  2915. } else if (item.isTransitionYellow) {
  2916. yellowData[yellowData.length - 1] = [
  2917. processedData[idx].date,
  2918. processedData[idx].value,
  2919. ];
  2920. }
  2921. }
  2922. });
  2923. return {
  2924. greenData: greenData,
  2925. redData: redData,
  2926. yellowData: yellowData,
  2927. singleTypeGreen: singleTypeGreen,
  2928. singleTypeRed: singleTypeRed,
  2929. singleTypeYellow: singleTypeYellow,
  2930. };
  2931. }
  2932. const maData = processMAData(data.FCX);
  2933. const maDuchiData = processMAData(data.DNC);
  2934. if (data.FCX[0][1] == "-1") {
  2935. maData.greenData = [];
  2936. maData.redData = [];
  2937. maData.yellowData = [];
  2938. }
  2939. const processBarData = (data) => {
  2940. const barData = [];
  2941. const markPointData = [];
  2942. data.forEach((item) => {
  2943. let color;
  2944. switch (item[4]) {
  2945. case 1:
  2946. color = "#13E113";
  2947. break;
  2948. case 2:
  2949. color = "#FF0E00";
  2950. break;
  2951. case 3:
  2952. color = "#0000FE";
  2953. break;
  2954. case 4:
  2955. color = "#1397FF";
  2956. break;
  2957. }
  2958. barData.push({
  2959. value: item[5],
  2960. itemStyle: {
  2961. normal: {
  2962. color: color,
  2963. },
  2964. },
  2965. });
  2966. if (item[1] === 1) {
  2967. markPointData.push({
  2968. coord: [item[0], item[5]],
  2969. symbol:
  2970. "image://https://d31zlh4on95l9h.cloudfront.net/images/5iujb101000d5si3v3hr7w2vg0h43z1u.png",
  2971. symbolSize: [30, 30],
  2972. label: {
  2973. normal: {
  2974. color: "rgba(0, 0, 0, 0)",
  2975. },
  2976. },
  2977. });
  2978. }
  2979. if (item[2] === 1) {
  2980. markPointData.push({
  2981. coord: [item[0], item[5] / 2],
  2982. symbol:
  2983. "image://https://d31zlh4on95l9h.cloudfront.net/images/5iujaz01000d5si016bxdf6vh0377d2h.png",
  2984. symbolSize: [30, 30],
  2985. label: {
  2986. normal: {
  2987. color: "rgba(0, 0, 0, 0)",
  2988. },
  2989. },
  2990. });
  2991. }
  2992. if (item[3] === 1) {
  2993. markPointData.push({
  2994. coord: [item[0], 0],
  2995. symbol:
  2996. "image://https://d31zlh4on95l9h.cloudfront.net/images/5iujb001000d5shzls0tmd4vs0e5tdrw.png",
  2997. symbolSize: [30, 30],
  2998. label: {
  2999. normal: {
  3000. color: "rgba(0, 0, 0, 0)",
  3001. },
  3002. },
  3003. });
  3004. }
  3005. });
  3006. return { barData, markPointData };
  3007. };
  3008. const { barData, markPointData } = processBarData(dealGnBullData);
  3009. KlineOption = {
  3010. legend: [
  3011. {
  3012. textStyle: {
  3013. color: "black",
  3014. fontSize: window.innerWidth > 768 ? 15 : vwToPx(1.8),
  3015. },
  3016. width: "100%",
  3017. top: window.innerWidth > 768 ? "0%" : "-1%",
  3018. left: "center",
  3019. itemGap: window.innerWidth > 768 ? 20 : 10,
  3020. itemWidth: 10,
  3021. itemHeight: 10,
  3022. data: [
  3023. {
  3024. name: "进攻K线",
  3025. itemStyle: {
  3026. color: "rgb(255,0,0)",
  3027. },
  3028. },
  3029. {
  3030. name: "防守K线",
  3031. itemStyle: {
  3032. color: "red",
  3033. },
  3034. },
  3035. {
  3036. name: "推进K线",
  3037. itemStyle: {
  3038. color: "orange",
  3039. },
  3040. },
  3041. {
  3042. name: "撤退K线",
  3043. itemStyle: {
  3044. color: "rgb(84,252,252)",
  3045. },
  3046. },
  3047. ],
  3048. },
  3049. {
  3050. textStyle: {
  3051. color: "black",
  3052. fontSize: window.innerWidth > 768 ? 15 : vwToPx(1.8),
  3053. },
  3054. orient: "horizontal",
  3055. top: window.innerWidth > 768 ? "3%" : "2%",
  3056. width: "100%",
  3057. left: "center",
  3058. itemGap: 15,
  3059. data: [
  3060. {
  3061. name: "{green|━}{red|━} " + "牵牛绳",
  3062. icon: "none",
  3063. textStyle: {
  3064. rich: {
  3065. green: {
  3066. color: "green",
  3067. fontSize: window.innerWidth > 768 ? 20 : 10,
  3068. },
  3069. red: {
  3070. color: "red",
  3071. fontSize: window.innerWidth > 768 ? 20 : 10,
  3072. },
  3073. },
  3074. },
  3075. },
  3076. {
  3077. name: "龙线",
  3078. },
  3079. {
  3080. name: "虫线",
  3081. },
  3082. ],
  3083. },
  3084. {
  3085. textStyle: {
  3086. color: "black",
  3087. fontSize: window.innerWidth > 768 ? 15 : vwToPx(1.8),
  3088. },
  3089. orient: "horizontal",
  3090. top: window.innerWidth > 768 ? "68%" : "64%",
  3091. width: "100%",
  3092. left: "center",
  3093. itemGap: 15,
  3094. data: [
  3095. {
  3096. name: "{green|━}{red|━} " + "度牛尺",
  3097. icon: "none",
  3098. textStyle: {
  3099. rich: {
  3100. green: {
  3101. color: "green",
  3102. fontSize: window.innerWidth > 768 ? 20 : 10,
  3103. },
  3104. red: {
  3105. color: "red",
  3106. fontSize: window.innerWidth > 768 ? 20 : 10,
  3107. },
  3108. },
  3109. },
  3110. },
  3111. ],
  3112. },
  3113. ],
  3114. tooltip: {
  3115. formatter: function (a, b, d) {
  3116. if (a[0].seriesIndex == 0) {
  3117. const KlineTag = ref([]);
  3118. const AIBullTag = ref([]);
  3119. KlineTag.value = a.find((item) => item.data[1])?.data || [];
  3120. AIBullTag.value =
  3121. a.slice(4).find((item) => item.data[1] !== "-")?.data || [];
  3122. // console.log(AIBullTag.value)
  3123. return (
  3124. a[0].name +
  3125. "<br/>" +
  3126. "开盘价" +
  3127. ":" +
  3128. KlineTag.value[1] +
  3129. "<br/>" +
  3130. "收盘价" +
  3131. ":" +
  3132. KlineTag.value[2] +
  3133. "<br/>" +
  3134. "最低价" +
  3135. ":" +
  3136. KlineTag.value[3] +
  3137. "<br/>" +
  3138. "最高价" +
  3139. ":" +
  3140. KlineTag.value[4] +
  3141. "<br/>" +
  3142. "牵牛绳" +
  3143. ":" +
  3144. AIBullTag.value[1]
  3145. );
  3146. }
  3147. if (a[0].seriesIndex == 4) {
  3148. let formattedVolume;
  3149. if (a[0].data.value >= 10000) {
  3150. formattedVolume = (a[0].data.value / 10000).toFixed(2) + "w";
  3151. } else {
  3152. formattedVolume = a[0].data.value;
  3153. }
  3154. return a[0].name + "<br/>" + "成交量" + ":" + formattedVolume;
  3155. }
  3156. if ([10, 11, 12].includes(a[0].seriesIndex)) {
  3157. const duchiData = a.find(
  3158. (item) => item.data && item.data[1] !== "-"
  3159. );
  3160. return duchiData
  3161. ? a[0].axisValue + "<br/>" + "度牛尺" + ":" + duchiData.data[1]
  3162. : null;
  3163. }
  3164. },
  3165. trigger: "axis",
  3166. axisPointer: {
  3167. type: "cross",
  3168. },
  3169. backgroundColor: "rgba(119, 120, 125, 0.6)",
  3170. borderWidth: 1,
  3171. borderColor: "#77787D",
  3172. padding: 10,
  3173. textStyle: {
  3174. color: "#fff",
  3175. },
  3176. },
  3177. axisPointer: {
  3178. link: [
  3179. {
  3180. xAxisIndex: "all",
  3181. },
  3182. ],
  3183. label: {
  3184. backgroundColor: "#77787D",
  3185. },
  3186. },
  3187. toolbox: {
  3188. show: false,
  3189. },
  3190. grid: [
  3191. {
  3192. left:
  3193. window.innerWidth > 1024
  3194. ? "70vw"
  3195. : window.innerWidth > 768
  3196. ? "65vw"
  3197. : "55vw",
  3198. right:
  3199. window.innerWidth > 1024
  3200. ? "40vw"
  3201. : window.innerWidth > 768
  3202. ? "30vw"
  3203. : "40vw",
  3204. top: window.innerWidth > 768 ? "8%" : "5%",
  3205. height: window.innerWidth > 768 ? "34%" : "34%",
  3206. containLabel: false,
  3207. },
  3208. {
  3209. left:
  3210. window.innerWidth > 1024
  3211. ? "70vw"
  3212. : window.innerWidth > 768
  3213. ? "65vw"
  3214. : "55vw",
  3215. right:
  3216. window.innerWidth > 1024
  3217. ? "40vw"
  3218. : window.innerWidth > 768
  3219. ? "30vw"
  3220. : "40vw",
  3221. top: window.innerWidth > 768 ? "45%" : "42%",
  3222. height: window.innerWidth > 768 ? "22%" : "22%",
  3223. containLabel: false,
  3224. },
  3225. {
  3226. left:
  3227. window.innerWidth > 1024
  3228. ? "70vw"
  3229. : window.innerWidth > 768
  3230. ? "65vw"
  3231. : "55vw",
  3232. right:
  3233. window.innerWidth > 1024
  3234. ? "40vw"
  3235. : window.innerWidth > 768
  3236. ? "30vw"
  3237. : "40vw",
  3238. top: window.innerWidth > 768 ? "73%" : "70%",
  3239. height: window.innerWidth > 768 ? "20%" : "22%",
  3240. containLabel: false,
  3241. },
  3242. ],
  3243. xAxis: [
  3244. {
  3245. type: "category",
  3246. data: dealData.categoryData,
  3247. boundaryGap: true,
  3248. axisLine: { onZero: false },
  3249. splitLine: { show: false },
  3250. min: "dataMin",
  3251. max: "dataMax",
  3252. axisPointer: {
  3253. z: 100,
  3254. },
  3255. axisLine: {
  3256. lineStyle: {
  3257. color: "black",
  3258. },
  3259. }, //
  3260. axisLabel: { show: false },
  3261. axisTick: { show: false },
  3262. },
  3263. {
  3264. type: "category",
  3265. gridIndex: 1,
  3266. data: dealData.categoryData,
  3267. boundaryGap: true,
  3268. axisLine: { lineStyle: { color: "black" } },
  3269. axisLabel: {
  3270. show: false,
  3271. interval: "auto",
  3272. },
  3273. },
  3274. {
  3275. type: "category",
  3276. gridIndex: 2,
  3277. data: dealData.categoryData,
  3278. boundaryGap: true,
  3279. axisLine: { lineStyle: { color: "black" } },
  3280. axisLabel: {
  3281. show: true,
  3282. interval: "auto",
  3283. },
  3284. },
  3285. ],
  3286. yAxis: [
  3287. {
  3288. scale: true,
  3289. gridIndex: 0,
  3290. position: "left",
  3291. axisLabel: {
  3292. inside: false,
  3293. align: "right",
  3294. fontSize: window.innerWidth > 768 ? 15 : 10,
  3295. },
  3296. axisLine: {
  3297. show: true,
  3298. lineStyle: {
  3299. fontSize: "",
  3300. color: "black",
  3301. },
  3302. },
  3303. axisTick: { show: false },
  3304. splitLine: { show: false },
  3305. },
  3306. {
  3307. scale: true,
  3308. gridIndex: 1,
  3309. splitNumber: 4,
  3310. min: 0,
  3311. minInterval: 1,
  3312. axisLabel: {
  3313. show: true,
  3314. fontSize: window.innerWidth > 768 ? 15 : 10,
  3315. margin: 8,
  3316. formatter: (value) => {
  3317. if (value >= 1000000000) {
  3318. return (value / 1000000000).toFixed(1) + "B";
  3319. } else if (value >= 1000000) {
  3320. return (value / 1000000).toFixed(1) + "M";
  3321. } else if (value >= 10000) {
  3322. return (value / 10000).toFixed(1) + "W";
  3323. }
  3324. return value.toFixed(0);
  3325. },
  3326. },
  3327. axisLine: { show: true, lineStyle: { color: "black" } },
  3328. axisTick: { show: false },
  3329. splitLine: { show: true, lineStyle: { type: "dashed" } },
  3330. boundaryGap: ["20%", "20%"],
  3331. },
  3332. {
  3333. type: "value",
  3334. gridIndex: 2,
  3335. min: 0,
  3336. max: 100,
  3337. axisLabel: {
  3338. show: true,
  3339. fontSize: window.innerWidth > 768 ? 15 : 10,
  3340. formatter: function (value) {
  3341. var customValues = [0, 20, 50, 80, 100];
  3342. return customValues.indexOf(value) > -1 ? value : "";
  3343. },
  3344. },
  3345. axisLine: {
  3346. show: true,
  3347. lineStyle: {
  3348. color: "black",
  3349. },
  3350. },
  3351. axisTick: {
  3352. show: false,
  3353. },
  3354. splitNumber: 10,
  3355. splitLine: {
  3356. show: true,
  3357. lineStyle: {
  3358. type: "dashed",
  3359. color: "#fff",
  3360. width: 1,
  3361. },
  3362. interval: function (index, value) {
  3363. return [20, 50, 80, 100].indexOf(value) > -1;
  3364. },
  3365. },
  3366. },
  3367. ],
  3368. dataZoom: [
  3369. {
  3370. type: "inside",
  3371. xAxisIndex: [0, 1, 2],
  3372. start: 55,
  3373. end: 100,
  3374. },
  3375. {
  3376. show: true,
  3377. xAxisIndex: [0, 1, 2],
  3378. type: "slider",
  3379. top: window.innerWidth > 768 ? "95%" : "96%",
  3380. // left: window.innerWidth > 768 ? "10%" : "8%",
  3381. // right: window.innerWidth > 768 ? "4%" : "8%",
  3382. start: 98,
  3383. end: 100,
  3384. },
  3385. ],
  3386. visualMap: [
  3387. {
  3388. type: "piecewise",
  3389. show: false,
  3390. pieces: maData.singleTypeGreen,
  3391. outOfRange: {
  3392. color: "green",
  3393. },
  3394. dimension: 0,
  3395. seriesIndex: 7,
  3396. },
  3397. {
  3398. type: "piecewise",
  3399. show: false,
  3400. pieces: maData.singleTypeRed,
  3401. outOfRange: {
  3402. color: "red",
  3403. },
  3404. dimension: 0,
  3405. seriesIndex: 8,
  3406. },
  3407. {
  3408. type: "piecewise",
  3409. show: false,
  3410. pieces: maData.singleTypeYellow,
  3411. outOfRange: {
  3412. color: "yellow",
  3413. },
  3414. dimension: 0,
  3415. seriesIndex: 9,
  3416. },
  3417. ],
  3418. series: [
  3419. {
  3420. name: "进攻K线",
  3421. type: "candlestick",
  3422. barWidth: "50%",
  3423. data: dealData1.values,
  3424. xAxisIndex: 0,
  3425. yAxisIndex: 0,
  3426. itemStyle: {
  3427. normal: {
  3428. color: "rgb(255,0,0)",
  3429. color0: "rgb(255,0,0)",
  3430. borderColor: "rgb(255,0,0)",
  3431. borderColor0: "rgb(255,0,0)",
  3432. },
  3433. },
  3434. gridIndex: 0,
  3435. },
  3436. //
  3437. {
  3438. name: "推进K线",
  3439. type: "candlestick",
  3440. barWidth: "50%",
  3441. data: dealData2.values,
  3442. xAxisIndex: 0,
  3443. yAxisIndex: 0,
  3444. itemStyle: {
  3445. normal: {
  3446. color: "rgb(0,0,252)",
  3447. color0: "rgb(0,0,252)",
  3448. borderColor: "rgb(0,0,252)",
  3449. borderColor0: "rgb(0,0,252)",
  3450. },
  3451. },
  3452. gridIndex: 0,
  3453. },
  3454. {
  3455. name: "防守K线",
  3456. type: "candlestick",
  3457. barWidth: "50%",
  3458. data: dealData3.values,
  3459. xAxisIndex: 0,
  3460. yAxisIndex: 0,
  3461. itemStyle: {
  3462. normal: {
  3463. color: "orange",
  3464. color0: "orange",
  3465. borderColor: "orange",
  3466. borderColor0: "orange",
  3467. },
  3468. },
  3469. gridIndex: 0,
  3470. },
  3471. {
  3472. name: "撤退K线",
  3473. type: "candlestick",
  3474. barWidth: "50%",
  3475. data: dealData4.values,
  3476. xAxisIndex: 0,
  3477. yAxisIndex: 0,
  3478. itemStyle: {
  3479. normal: {
  3480. color: "rgb(84,252,252)",
  3481. color0: "rgb(84,252,252)",
  3482. borderColor: "rgb(84,252,252)",
  3483. borderColor0: "rgb(84,252,252)",
  3484. },
  3485. },
  3486. gridIndex: 0,
  3487. },
  3488. {
  3489. name: "成交量",
  3490. type: "bar",
  3491. barWidth: "70%",
  3492. xAxisIndex: 1,
  3493. yAxisIndex: 1,
  3494. data: barData,
  3495. markPoint: {
  3496. data: markPointData,
  3497. label: {
  3498. show: false,
  3499. },
  3500. },
  3501. },
  3502. {
  3503. name: "{green|━}{red|━} " + "牵牛绳",
  3504. type: "line",
  3505. data: [],
  3506. smooth: true,
  3507. symbol: "none",
  3508. xAxisIndex: 0,
  3509. yAxisIndex: 0,
  3510. showSymbol: false,
  3511. lineStyle: {
  3512. opacity: 0,
  3513. },
  3514. itemStyle: {
  3515. normal: {
  3516. color: "green",
  3517. },
  3518. },
  3519. gridIndex: 0,
  3520. },
  3521. {
  3522. name: "{green|━}{red|━} " + "度牛尺",
  3523. type: "line",
  3524. data: [],
  3525. smooth: true,
  3526. symbol: "none",
  3527. xAxisIndex: 0,
  3528. yAxisIndex: 0,
  3529. showSymbol: false,
  3530. lineStyle: {
  3531. opacity: 0,
  3532. },
  3533. itemStyle: {
  3534. normal: {
  3535. color: "green",
  3536. },
  3537. },
  3538. gridIndex: 0,
  3539. },
  3540. {
  3541. name: "虫线",
  3542. type: "line",
  3543. data: maData.greenData,
  3544. smooth: true,
  3545. symbol: "none",
  3546. xAxisIndex: 0,
  3547. yAxisIndex: 0,
  3548. itemStyle: {
  3549. normal: {
  3550. color: "green",
  3551. lineStyle: {
  3552. width: 2,
  3553. type: "solid",
  3554. },
  3555. },
  3556. },
  3557. gridIndex: 0,
  3558. },
  3559. {
  3560. name: "龙线",
  3561. type: "line",
  3562. data: maData.redData,
  3563. smooth: true,
  3564. symbol: "none",
  3565. xAxisIndex: 0,
  3566. yAxisIndex: 0,
  3567. itemStyle: {
  3568. normal: {
  3569. color: "red",
  3570. lineStyle: {
  3571. width: 2,
  3572. type: "solid",
  3573. },
  3574. },
  3575. },
  3576. gridIndex: 0,
  3577. },
  3578. {
  3579. name: "黄色",
  3580. type: "line",
  3581. data: maData.yellowData,
  3582. smooth: true,
  3583. symbol: "none",
  3584. xAxisIndex: 0,
  3585. yAxisIndex: 0,
  3586. itemStyle: {
  3587. normal: {
  3588. color: "yellow",
  3589. lineStyle: {
  3590. width: 2,
  3591. type: "solid",
  3592. },
  3593. },
  3594. },
  3595. gridIndex: 0,
  3596. },
  3597. {
  3598. name: "背景区域",
  3599. type: "line",
  3600. data: [],
  3601. xAxisIndex: 2,
  3602. yAxisIndex: 2,
  3603. markArea: {
  3604. silent: true,
  3605. itemStyle: {
  3606. normal: {
  3607. opacity: 1,
  3608. },
  3609. },
  3610. label: {
  3611. normal: {
  3612. show: true,
  3613. position: "insideRight",
  3614. fontSize: window.innerWidth > 768 ? 16 : 12,
  3615. fontWeight: "bold",
  3616. color: "#13E113",
  3617. distance: 10,
  3618. },
  3619. },
  3620. data: [
  3621. [
  3622. {
  3623. yAxis: 0,
  3624. itemStyle: {
  3625. normal: {
  3626. color: "#CFFFCF",
  3627. },
  3628. },
  3629. label: {
  3630. normal: {
  3631. formatter: "度牛区",
  3632. },
  3633. },
  3634. },
  3635. {
  3636. yAxis: 20,
  3637. },
  3638. ],
  3639. [
  3640. {
  3641. yAxis: 20,
  3642. itemStyle: {
  3643. normal: {
  3644. color: "#A6FFFF",
  3645. },
  3646. },
  3647. },
  3648. {
  3649. yAxis: 40,
  3650. },
  3651. ],
  3652. [
  3653. {
  3654. yAxis: 40,
  3655. itemStyle: {
  3656. normal: {
  3657. color: "#FFF686",
  3658. },
  3659. },
  3660. },
  3661. {
  3662. yAxis: 60,
  3663. },
  3664. ],
  3665. [
  3666. {
  3667. yAxis: 60,
  3668. itemStyle: {
  3669. normal: { color: "#FFD2B3" },
  3670. },
  3671. },
  3672. {
  3673. yAxis: 80,
  3674. },
  3675. ],
  3676. [
  3677. {
  3678. yAxis: 80,
  3679. itemStyle: {
  3680. normal: { color: "#FFB8B8" },
  3681. },
  3682. label: {
  3683. normal: {
  3684. formatter: "度牛区",
  3685. color: "#FF0000",
  3686. position: "insideLeft",
  3687. distance: 10,
  3688. },
  3689. },
  3690. },
  3691. {
  3692. yAxis: 100,
  3693. },
  3694. ],
  3695. ],
  3696. },
  3697. },
  3698. {
  3699. name: "度牛尺",
  3700. type: "line",
  3701. data: maDuchiData.greenData,
  3702. symbol: "none",
  3703. xAxisIndex: 2,
  3704. yAxisIndex: 2,
  3705. itemStyle: {
  3706. normal: {
  3707. color: "green",
  3708. lineStyle: {
  3709. width: 2,
  3710. type: "solid",
  3711. },
  3712. },
  3713. },
  3714. gridIndex: 2,
  3715. markPoint: {
  3716. symbol: "rect",
  3717. symbolSize: (value, params) => {
  3718. const width = window.innerWidth;
  3719. const baseHeight = 22;
  3720. // if (width <= 375) {
  3721. // return [2, 16];
  3722. // } else if (width <= 768) {
  3723. // return [2, 22];
  3724. // }
  3725. return [2, baseHeight];
  3726. },
  3727. itemStyle: {
  3728. normal: {
  3729. label: {
  3730. show: false,
  3731. },
  3732. },
  3733. },
  3734. data: [
  3735. ...maDuchiData.greenData
  3736. .map((item) => {
  3737. if (item[1] === 0) {
  3738. return {
  3739. coord: [item[0], 20],
  3740. symbolOffset: [0, 10],
  3741. itemStyle: {
  3742. color: "#00ff00",
  3743. },
  3744. };
  3745. }
  3746. })
  3747. .filter(Boolean),
  3748. ],
  3749. },
  3750. },
  3751. {
  3752. type: "line",
  3753. data: maDuchiData.redData,
  3754. // smooth: true,
  3755. symbol: "none",
  3756. xAxisIndex: 2,
  3757. yAxisIndex: 2,
  3758. itemStyle: {
  3759. normal: {
  3760. color: "red",
  3761. lineStyle: {
  3762. width: 2,
  3763. type: "solid",
  3764. },
  3765. },
  3766. },
  3767. gridIndex: 2,
  3768. markPoint: {
  3769. symbol: "rect",
  3770. symbolSize: (value, params) => {
  3771. const width = window.innerWidth;
  3772. const baseHeight = 22;
  3773. // if (width <= 375) {
  3774. // return [2, 16];
  3775. // } else if (width <= 768) {
  3776. // return [2, 24];
  3777. // }
  3778. return [2, baseHeight];
  3779. },
  3780. itemStyle: {
  3781. normal: {
  3782. label: {
  3783. show: false,
  3784. },
  3785. },
  3786. },
  3787. data: [
  3788. ...maDuchiData.redData
  3789. .map((item) => {
  3790. if (item[1] === 100) {
  3791. return {
  3792. coord: [item[0], 80],
  3793. symbolOffset: [0, -10],
  3794. itemStyle: {
  3795. color: "#ff0000",
  3796. },
  3797. };
  3798. }
  3799. })
  3800. .filter(Boolean),
  3801. ],
  3802. },
  3803. },
  3804. {
  3805. name: "辅助线",
  3806. type: "line",
  3807. data: [],
  3808. xAxisIndex: 2,
  3809. yAxisIndex: 2,
  3810. markLine: {
  3811. silent: true,
  3812. symbol: "none",
  3813. lineStyle: {
  3814. color: "#000000",
  3815. width: 3,
  3816. type: "solid",
  3817. },
  3818. data: [{ yAxis: 20 }],
  3819. },
  3820. },
  3821. {
  3822. name: "辅助线",
  3823. type: "line",
  3824. data: [],
  3825. xAxisIndex: 2,
  3826. yAxisIndex: 2,
  3827. markLine: {
  3828. silent: true,
  3829. symbol: "none",
  3830. lineStyle: {
  3831. color: "#000000",
  3832. width: 3,
  3833. type: "solid",
  3834. },
  3835. data: [{ yAxis: 50 }],
  3836. },
  3837. },
  3838. {
  3839. name: "辅助线",
  3840. type: "line",
  3841. data: [],
  3842. xAxisIndex: 2,
  3843. yAxisIndex: 2,
  3844. markLine: {
  3845. silent: true,
  3846. symbol: "none",
  3847. lineStyle: {
  3848. color: "#000000",
  3849. width: 3,
  3850. type: "solid",
  3851. },
  3852. data: [{ yAxis: 80 }],
  3853. },
  3854. },
  3855. {
  3856. name: "辅助线",
  3857. type: "line",
  3858. data: [],
  3859. xAxisIndex: 2,
  3860. yAxisIndex: 2,
  3861. markLine: {
  3862. silent: true,
  3863. symbol: "none",
  3864. lineStyle: {
  3865. color: "#000000",
  3866. width: 3,
  3867. type: "solid",
  3868. },
  3869. data: [{ yAxis: 100 }],
  3870. },
  3871. },
  3872. ],
  3873. };
  3874. }
  3875. // console.log("KLine渲染: 图表配置完成");
  3876. try {
  3877. // 应用配置
  3878. // console.log("KLine渲染: 开始设置图表选项");
  3879. chartInstancesMap[containerId].setOption(KlineOption);
  3880. // console.log("KLine渲染: 图表选项设置成功");
  3881. // 窗口大小变化时重新渲染图表
  3882. const resizeFunc = _.throttle(
  3883. function () {
  3884. console.log("窗口大小改变,调整图表大小");
  3885. if (
  3886. chartInstancesMap[containerId] &&
  3887. !chartInstancesMap[containerId].isDisposed()
  3888. ) {
  3889. // 如果设备类型发生变化,重新渲染
  3890. const newIsMobile = window.innerWidth < 768;
  3891. const newIsTablet =
  3892. window.innerWidth >= 768 && window.innerWidth < 1024;
  3893. if (newIsMobile !== isMobile || newIsTablet !== isTablet) {
  3894. console.log("设备类型变化,重新渲染图表");
  3895. KlineCanvsEcharts(containerId);
  3896. return;
  3897. }
  3898. chartInstancesMap[containerId].resize();
  3899. }
  3900. },
  3901. 1000,
  3902. { trailing: false }
  3903. );
  3904. // 给resize事件绑定一个特定的函数名,便于后续移除
  3905. window[`resize_${containerId}`] = resizeFunc;
  3906. // 绑定resize事件
  3907. window.removeEventListener("resize", window[`resize_${containerId}`]);
  3908. window.addEventListener("resize", window[`resize_${containerId}`]);
  3909. // console.log("KLine渲染: 图表渲染完成");
  3910. } catch (error) {
  3911. // console.error("KLine渲染: 图表渲染出错", error);
  3912. }
  3913. }
  3914. watch(
  3915. () => audioStore.isVoiceEnabled,
  3916. (newVal) => {
  3917. // 添加状态锁定逻辑
  3918. if (newVal === audioStore.lastVoiceState) return;
  3919. audioStore.lastVoiceState = newVal;
  3920. if (newVal) {
  3921. console.log("开启语音播放");
  3922. // 添加重试机制
  3923. const tryPlay = () => {
  3924. if (!audioStore.ttsUrl) return; // 新增空值判断
  3925. if (audioStore.soundInstance?.playing()) return;
  3926. playAudio(audioStore.ttsUrl);
  3927. setTimeout(() => {
  3928. if (!audioStore.soundInstance?.playing()) {
  3929. Howler.unload();
  3930. }
  3931. }, 1000);
  3932. };
  3933. tryPlay();
  3934. } else {
  3935. console.log("关闭语音播放");
  3936. pauseAudio();
  3937. }
  3938. },
  3939. { immediate: true }
  3940. );
  3941. watch(
  3942. () => dataStore.activeTabIndex,
  3943. (newVal) => {
  3944. setTimeout(() => {
  3945. console.log("activeTabIndex变化:", newVal);
  3946. // 当标签页切换回来时,重新渲染所有图表
  3947. if (newVal === 0) {
  3948. console.log("切换到AI聊天页,重新渲染图表");
  3949. // 延迟执行以确保DOM已渲染
  3950. renderAllKlineCharts();
  3951. }
  3952. }, 1000);
  3953. },
  3954. { immediate: true } // 添加immediate属性,确保初始化时执行一次
  3955. );
  3956. // 添加渲染所有K线图的方法
  3957. function renderAllKlineCharts() {
  3958. console.log("重新渲染所有K线图");
  3959. // 查找所有K线消息
  3960. const messages = chatStore.messages;
  3961. for (let i = 0; i < messages.length; i++) {
  3962. if (messages[i].kline && messages[i].chartData) {
  3963. const containerId = `kline-container-${i}`;
  3964. console.log(`尝试渲染K线图: ${containerId}`);
  3965. // 确保DOM已经渲染
  3966. const container = document.getElementById(containerId);
  3967. if (container) {
  3968. // 渲染图表
  3969. KlineCanvsEcharts(containerId);
  3970. } else {
  3971. console.warn(`找不到容器: ${containerId}`);
  3972. }
  3973. }
  3974. }
  3975. }
  3976. // 初始化随机GIF
  3977. onMounted(() => {
  3978. // 初始化marked组件
  3979. marked.setOptions({
  3980. breaks: true, // 支持换行符转换为 <br>
  3981. gfm: true, // 启用 GitHub Flavored Markdown
  3982. sanitize: false, // 不清理 HTML(谨慎使用)
  3983. smartLists: true, // 智能列表
  3984. smartypants: true, // 智能标点符号
  3985. xhtml: false, // 不使用 XHTML 输出
  3986. renderer: renderer,
  3987. });
  3988. renderAllKlineCharts();
  3989. console.log("组件挂载完成");
  3990. chatStore.messages.forEach((item) => {
  3991. if (item.sender == "user") {
  3992. item.audioStatus = false;
  3993. }
  3994. });
  3995. // 添加页面可见性变化监听器
  3996. document.addEventListener("visibilitychange", handleVisibilityChange);
  3997. // 添加DOM变化监听器
  3998. const observer = new MutationObserver((mutations) => {
  3999. mutations.forEach((mutation) => {
  4000. if (mutation.type === "childList" && mutation.addedNodes.length) {
  4001. // 检查是否添加了图表容器
  4002. const containers = document.querySelectorAll(
  4003. '[id^="kline-container-"]'
  4004. );
  4005. if (containers.length) {
  4006. // console.log("DOM变化监听到K线容器:", Array.from(containers).map(el => el.id));
  4007. }
  4008. }
  4009. });
  4010. });
  4011. // 开始监听DOM变化
  4012. observer.observe(document.body, { childList: true, subtree: true });
  4013. });
  4014. // 页面可见性变化处理
  4015. let wasPlayingBeforeHidden = false;
  4016. const handleVisibilityChange = () => {
  4017. if (document.hidden) {
  4018. // 页面被隐藏时,如果音频正在播放,则暂停并记录状态
  4019. if (audioStore.isPlaying) {
  4020. wasPlayingBeforeHidden = true;
  4021. audioStore.pause();
  4022. console.log("页面切换离开,音频已暂停");
  4023. } else {
  4024. wasPlayingBeforeHidden = false;
  4025. }
  4026. } else {
  4027. // 页面重新可见时,如果之前在播放,则恢复播放
  4028. if (wasPlayingBeforeHidden && !audioStore.isPlaying) {
  4029. audioStore.play();
  4030. console.log("页面切换回来,音频已恢复播放");
  4031. wasPlayingBeforeHidden = false;
  4032. }
  4033. }
  4034. };
  4035. // 组件卸载时清理所有图表实例和事件监听器
  4036. onUnmounted(() => {
  4037. // 移除页面可见性变化监听器
  4038. document.removeEventListener("visibilitychange", handleVisibilityChange);
  4039. // 停止音频播放
  4040. if (audioStore.isPlaying) {
  4041. audioStore.stop();
  4042. console.log("组件卸载,音频已停止");
  4043. }
  4044. // 清理所有图表实例
  4045. Object.keys(chartInstancesMap).forEach((key) => {
  4046. if (chartInstancesMap[key]) {
  4047. // 移除resize事件监听
  4048. if (window[`resize_${key}`]) {
  4049. window.removeEventListener("resize", window[`resize_${key}`]);
  4050. delete window[`resize_${key}`];
  4051. }
  4052. // 销毁图表实例
  4053. chartInstancesMap[key].dispose();
  4054. delete chartInstancesMap[key];
  4055. }
  4056. });
  4057. });
  4058. </script>
  4059. <template>
  4060. <div class="chat-container">
  4061. <!-- GIF区域 -->
  4062. <div class="gif-area">
  4063. <img
  4064. src="https://d31zlh4on95l9h.cloudfront.net/images/03ddbbbee489f29dc6a1427f9ed4f389.png"
  4065. alt="夺宝奇兵大模型logo"
  4066. class="bgc"
  4067. />
  4068. <img :src="logo1" alt="夺宝奇兵大模型logo" class="logo1" />
  4069. <img :src="logo2" alt="夺宝奇兵大模型logo" class="logo2" />
  4070. </div>
  4071. <div v-for="(msg, index) in chatMsg" :key="index">
  4072. <!-- 用户消息容器包含喇叭按钮 -->
  4073. <div v-if="msg.sender === 'user'" class="user-message-container">
  4074. <img
  4075. :src="msg.audioStatus ? voice : voiceNoActive"
  4076. class="user-message-speaker"
  4077. :class="{
  4078. 'speaker-active': msg.audioStatus,
  4079. }"
  4080. @click="toggleVoiceForUser(index)"
  4081. alt="喇叭"
  4082. />
  4083. <div
  4084. :class="{
  4085. 'message-bubble': true,
  4086. [msg.sender]: msg.sender,
  4087. [msg.class]: msg.class,
  4088. }"
  4089. >
  4090. <div v-html="msg.content"></div>
  4091. </div>
  4092. </div>
  4093. <!-- AI消息和其他类型消息 -->
  4094. <div
  4095. v-else
  4096. :class="{
  4097. 'message-bubble': true,
  4098. [msg.sender]: msg.sender,
  4099. [msg.class]: msg.class,
  4100. }"
  4101. >
  4102. <div v-if="msg.type === 'kline'" class="kline-container">
  4103. <div :id="'kline-container-' + index" class="chart-mount-point">
  4104. <div v-if="!msg.hasValidData" class="no-data-message">
  4105. <p>暂无K线数据</p>
  4106. </div>
  4107. </div>
  4108. </div>
  4109. <div v-else-if="msg.type == 'ing'" class="ai-message-container">
  4110. <img
  4111. v-if="msg.gif"
  4112. :src="msg.gif"
  4113. alt="思考过程"
  4114. class="thinking-gif"
  4115. />
  4116. <div class="ai-message-content">
  4117. <div v-if="msg.flag">
  4118. <span>{{ msg.content }}</span>
  4119. <span class="loading-dots">
  4120. <span class="dot">.</span>
  4121. <span class="dot">.</span>
  4122. <span class="dot">.</span>
  4123. <span class="dot">.</span>
  4124. <span class="dot">.</span>
  4125. <span class="dot">.</span>
  4126. </span>
  4127. </div>
  4128. <div v-else v-html="msg.content"></div>
  4129. </div>
  4130. </div>
  4131. <div
  4132. v-else-if="msg.type == 'title1'"
  4133. style="display: flex; width: 100%"
  4134. >
  4135. <div class="mainTitle">
  4136. {{ msg.content }}
  4137. </div>
  4138. <div class="date">
  4139. {{ msg.date }}
  4140. </div>
  4141. </div>
  4142. <div v-else-if="msg.type == 'title2'" class="title2">
  4143. <img class="title1Img" :src="title1" alt="出错了" />
  4144. </div>
  4145. <div v-else-if="msg.type == 'title3'" class="title3">
  4146. <img class="title2Img" :src="msg.content" alt="出错了" />
  4147. </div>
  4148. <div v-else-if="msg.type == 'content1'" class="content1">
  4149. <div v-if="msg.kline" class="kline-container content1chart">
  4150. <div :id="'kline-container-' + index" class="chart-mount-point">
  4151. <div v-if="!msg.hasValidData" class="no-data-message">
  4152. <p>暂无数据</p>
  4153. </div>
  4154. </div>
  4155. </div>
  4156. <div v-else class="content1Text">
  4157. <div v-html="msg.content" class="text1"></div>
  4158. </div>
  4159. </div>
  4160. <div v-else-if="msg.type == 'content2'" class="content2">
  4161. <div class="kline-container content2chart">
  4162. <div :id="'kline-container-' + index" class="chart-mount-point">
  4163. <div v-if="!msg.hasValidData" class="no-data-message">
  4164. <p>暂无数据</p>
  4165. </div>
  4166. </div>
  4167. </div>
  4168. </div>
  4169. <div v-else-if="msg.type == 'content3'" class="content3">
  4170. <div class="content3Text">
  4171. <div v-html="msg.content" class="text3"></div>
  4172. </div>
  4173. </div>
  4174. <div v-else-if="msg.type == 'mianze'" class="mianze">
  4175. <div v-html="msg.content"></div>
  4176. </div>
  4177. <div v-else v-html="msg.content"></div>
  4178. </div>
  4179. </div>
  4180. </div>
  4181. </template>
  4182. <style scoped>
  4183. p {
  4184. font-size: 20px;
  4185. }
  4186. .bgc {
  4187. position: absolute;
  4188. z-index: -1;
  4189. max-width: 440px;
  4190. min-width: 300px;
  4191. top: -15px;
  4192. width: 40%;
  4193. height: auto;
  4194. /* 添加旋转动画 */
  4195. animation: rotate 10s linear infinite reverse;
  4196. }
  4197. /* 定义旋转动画 */
  4198. @keyframes rotate {
  4199. from {
  4200. transform: rotate(0deg);
  4201. }
  4202. to {
  4203. transform: rotate(360deg);
  4204. }
  4205. }
  4206. .logo1 {
  4207. max-width: 350px;
  4208. min-width: 200px;
  4209. width: 25%;
  4210. }
  4211. .logo2 {
  4212. margin-top: 20px;
  4213. max-width: 350px;
  4214. min-width: 200px;
  4215. width: 30%;
  4216. /* position: relative; */
  4217. }
  4218. .chat-container {
  4219. display: flex;
  4220. flex-direction: column;
  4221. overflow: hidden;
  4222. }
  4223. .gif-area {
  4224. padding: 70px 0px;
  4225. position: relative;
  4226. /* height: 30vh; */
  4227. display: flex;
  4228. flex-direction: column;
  4229. justify-content: center;
  4230. align-items: center;
  4231. flex-shrink: 0;
  4232. /* 防止GIF区域被压缩 */
  4233. }
  4234. .message-area {
  4235. margin-top: 2%;
  4236. flex: 1;
  4237. /* 消息区域占据剩余空间 */
  4238. overflow-y: auto;
  4239. padding: 20px;
  4240. display: flex;
  4241. flex-direction: column;
  4242. gap: 15px;
  4243. }
  4244. .marquee-container {
  4245. /* position: absolute; */
  4246. bottom: 0;
  4247. width: 100%;
  4248. /* ga */
  4249. }
  4250. .marquee-row {
  4251. white-space: nowrap;
  4252. overflow: visible;
  4253. padding: 8px 0;
  4254. width: 100%;
  4255. }
  4256. .marquee-item {
  4257. display: inline-block;
  4258. margin: 0 15px;
  4259. padding: 8px 20px;
  4260. background: rgba(255, 255, 255, 0.9);
  4261. /* 白色背景 */
  4262. border-radius: 10px;
  4263. /* 圆角矩形 */
  4264. color: #333;
  4265. /* 文字颜色改为深色 */
  4266. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  4267. /* 添加阴影 */
  4268. transition: all 0.3s;
  4269. transition: color 0.3s;
  4270. }
  4271. .top {
  4272. animation: marquee 25s linear infinite;
  4273. /* 默认动画是运行状态 */
  4274. animation-play-state: running;
  4275. }
  4276. .bottom {
  4277. animation: marquee 15s linear infinite reverse;
  4278. /* 默认动画是运行状态 */
  4279. animation-play-state: running;
  4280. }
  4281. /* 添加PC端专用速度 */
  4282. @media (min-width: 768px) {
  4283. .top {
  4284. animation-duration: 35s;
  4285. /* PC端改为35秒 */
  4286. }
  4287. .bottom {
  4288. animation-duration: 35s;
  4289. /* PC端改为35秒 */
  4290. }
  4291. }
  4292. @keyframes marquee {
  4293. 0% {
  4294. transform: translateX(100%);
  4295. }
  4296. 100% {
  4297. transform: translateX(-250%);
  4298. }
  4299. }
  4300. .loading-dots {
  4301. display: inline-block;
  4302. }
  4303. .dot {
  4304. opacity: 0.4;
  4305. animation: loading 1.4s infinite;
  4306. }
  4307. .dot:nth-child(1) {
  4308. animation-delay: 0s;
  4309. }
  4310. .dot:nth-child(2) {
  4311. animation-delay: 0.2s;
  4312. }
  4313. .dot:nth-child(3) {
  4314. animation-delay: 0.4s;
  4315. }
  4316. .dot:nth-child(4) {
  4317. animation-delay: 0.6s;
  4318. }
  4319. .dot:nth-child(5) {
  4320. animation-delay: 0.8s;
  4321. }
  4322. .dot:nth-child(6) {
  4323. animation-delay: 1s;
  4324. }
  4325. @keyframes loading {
  4326. 0%,
  4327. 60%,
  4328. 100% {
  4329. opacity: 0.4;
  4330. }
  4331. 30% {
  4332. opacity: 1;
  4333. }
  4334. }
  4335. .message-bubble {
  4336. max-width: 80%;
  4337. margin: 10px 0px;
  4338. padding: 15px 20px;
  4339. position: relative;
  4340. }
  4341. /* 用户消息容器样式 */
  4342. .user-message-container {
  4343. display: flex;
  4344. align-items: flex-start;
  4345. margin: 10px 0px;
  4346. justify-content: flex-end;
  4347. gap: 10px;
  4348. align-items: center;
  4349. }
  4350. .user-message-speaker {
  4351. width: 32px;
  4352. height: 32px;
  4353. object-fit: contain;
  4354. margin-top: 5px;
  4355. cursor: pointer;
  4356. transition: all 0.3s ease;
  4357. }
  4358. .user-message-speaker:hover {
  4359. transform: scale(1.1);
  4360. }
  4361. .user-message-speaker.speaker-active {
  4362. animation: pulse 1.5s infinite;
  4363. }
  4364. @keyframes pulse {
  4365. 0% {
  4366. transform: scale(1);
  4367. }
  4368. 50% {
  4369. transform: scale(1.1);
  4370. }
  4371. 100% {
  4372. transform: scale(1);
  4373. }
  4374. }
  4375. .message-bubble.user {
  4376. color: #6d22f8;
  4377. background: white;
  4378. font-weight: bold;
  4379. border-radius: 10px;
  4380. margin: 0;
  4381. width: fit-content;
  4382. display: flex;
  4383. align-items: center;
  4384. }
  4385. .message-bubble.ai {
  4386. background: #2b378d;
  4387. color: #ffffff;
  4388. margin: 0 auto;
  4389. /* border-bottom-left-radius: 5px; */
  4390. }
  4391. .message-bubble.ing {
  4392. background: #ffffff;
  4393. color: #000000;
  4394. font-weight: bold;
  4395. border-radius: 10px;
  4396. margin-left: 20px;
  4397. margin-right: auto;
  4398. display: flex;
  4399. align-items: center;
  4400. width: fit-content;
  4401. }
  4402. .message-bubble.ai.title1 {
  4403. width: 100%;
  4404. display: flex;
  4405. border-radius: 10px 10px 0px 0px;
  4406. /* border-bottom-left-radius: 5px; */
  4407. }
  4408. .mainTitle {
  4409. font-size: 16px;
  4410. font-weight: bold;
  4411. background-image: url("@/assets/img/AiEmotion/bk01.png");
  4412. background-repeat: no-repeat;
  4413. background-size: 100% 100%;
  4414. min-width: 200px;
  4415. width: 20vw;
  4416. max-width: 50%;
  4417. height: 50px;
  4418. padding: 15px 10px;
  4419. text-align: center;
  4420. line-height: 20px;
  4421. overflow: hidden;
  4422. text-overflow: ellipsis;
  4423. white-space: nowrap;
  4424. box-sizing: border-box;
  4425. }
  4426. .date {
  4427. font-size: 1.5rem;
  4428. font-weight: bold;
  4429. margin-left: auto;
  4430. /* width: 100px; */
  4431. display: flex;
  4432. justify-content: center;
  4433. align-items: center;
  4434. }
  4435. .message-bubble.ai.title2 {
  4436. width: 100%;
  4437. display: flex;
  4438. justify-content: center;
  4439. align-items: center;
  4440. }
  4441. .title1Img {
  4442. max-width: 500px;
  4443. width: 90vw;
  4444. }
  4445. .message-bubble.ai.title3 {
  4446. width: 100%;
  4447. display: flex;
  4448. justify-content: center;
  4449. align-items: center;
  4450. }
  4451. .title2Img {
  4452. max-width: 500px;
  4453. width: 90vw;
  4454. }
  4455. .message-bubble.ai.content1 {
  4456. width: 100%;
  4457. display: flex;
  4458. justify-content: center;
  4459. align-items: center;
  4460. }
  4461. .content1chart {
  4462. background-image: url("@/assets/img/AIchat/罗盘边框.png");
  4463. background-repeat: no-repeat;
  4464. background-size: 100% 100%;
  4465. width: 50vw;
  4466. min-width: 350px;
  4467. display: flex;
  4468. justify-content: center;
  4469. align-items: center;
  4470. }
  4471. .content1Text {
  4472. background-image: url("@/assets/img/AIchat/框.png");
  4473. background-repeat: no-repeat;
  4474. background-size: 100% 100%;
  4475. width: 50vw;
  4476. min-width: 350px;
  4477. /* height: 20vw; */
  4478. /* max-height: 400px; */
  4479. padding: 5% 0;
  4480. }
  4481. .text1 {
  4482. font-weight: bold;
  4483. /* margin-left: 6%; */
  4484. /* margin-bottom: 10px; */
  4485. margin: 0px 6% 10px 6%;
  4486. font-size: 20px;
  4487. }
  4488. .message-bubble.ai.content2 {
  4489. width: 100%;
  4490. display: flex;
  4491. justify-content: center;
  4492. align-items: center;
  4493. }
  4494. .content2chart {
  4495. background-image: url("@/assets/img/AIchat/PCbackPic.png");
  4496. background-repeat: no-repeat;
  4497. background-size: 100% 100%;
  4498. width: 50vw;
  4499. min-width: 350px;
  4500. display: flex;
  4501. justify-content: center;
  4502. align-items: center;
  4503. height: calc(500px + 10vw) !important;
  4504. }
  4505. .message-bubble.ai.content3 {
  4506. width: 100%;
  4507. display: flex;
  4508. justify-content: center;
  4509. align-items: center;
  4510. }
  4511. .content3Text {
  4512. background-image: url("@/assets/img/AIchat/边框.png");
  4513. background-repeat: no-repeat;
  4514. background-size: 100% 100%;
  4515. width: 50vw;
  4516. min-width: 350px;
  4517. /* height: 20vw; */
  4518. /* max-height: 400px; */
  4519. padding: 5% 0px;
  4520. }
  4521. .text3 {
  4522. /* font-weight: bold; */
  4523. /* margin-left: 6%; */
  4524. /* margin-bottom: 10px; */
  4525. margin: 0px 6% 10px 6%;
  4526. font-size: 20px;
  4527. }
  4528. .message-bubble.ai.mianze {
  4529. width: 100%;
  4530. text-align: center;
  4531. font-weight: bold;
  4532. font-size: 24px;
  4533. border-radius: 0px 0px 10px 10px;
  4534. }
  4535. .kline-container {
  4536. margin-top: 10px;
  4537. /* 最小高度 */
  4538. min-height: 320px;
  4539. /* 视口高度单位 */
  4540. height: 40vh;
  4541. width: 50vw;
  4542. }
  4543. @media (max-width: 768px) {
  4544. .kline-container {
  4545. min-width: 75vw;
  4546. }
  4547. .content1Text {
  4548. width: 77vw;
  4549. min-width: 0px;
  4550. /* height: 20vw; */
  4551. /* min-height: 150px; */
  4552. }
  4553. .date {
  4554. font-size: 14px;
  4555. }
  4556. .text1 {
  4557. font-size: 20px;
  4558. }
  4559. .content2chart {
  4560. background-image: url("@/assets/img/AIchat/new-app-bgc.png") !important;
  4561. height: 100vw;
  4562. }
  4563. .content3Text {
  4564. width: 77vw;
  4565. min-width: 0px;
  4566. /* height: 20vw; */
  4567. /* min-height: 150px; */
  4568. }
  4569. .text3 {
  4570. font-size: 20px;
  4571. }
  4572. .message-bubble.ai.mianze {
  4573. font-size: 18px;
  4574. }
  4575. }
  4576. .kline-container .chart-mount-point {
  4577. display: flex;
  4578. justify-content: center;
  4579. align-items: center;
  4580. height: 80%;
  4581. width: 90%;
  4582. }
  4583. /* AI消息容器样式 */
  4584. .ai-message-container {
  4585. display: flex;
  4586. align-items: flex-start;
  4587. gap: 10px;
  4588. margin-right: auto;
  4589. max-width: 80%;
  4590. }
  4591. /* 思考过程动图样式 */
  4592. .thinking-gif {
  4593. width: 40px;
  4594. height: 40px;
  4595. object-fit: contain;
  4596. margin-top: 5px;
  4597. border-radius: 8px;
  4598. animation: float 2s ease-in-out infinite;
  4599. }
  4600. @keyframes float {
  4601. 0%,
  4602. 100% {
  4603. transform: translateY(0px);
  4604. }
  4605. 50% {
  4606. transform: translateY(-5px);
  4607. }
  4608. }
  4609. /* AI消息内容样式 */
  4610. .ai-message-content {
  4611. display: flex;
  4612. align-items: center;
  4613. white-space: nowrap;
  4614. width: fit-content;
  4615. overflow: visible;
  4616. }
  4617. </style>