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.

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