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.

147 lines
4.9 KiB

  1. // @/store/emotion.js
  2. import { defineStore } from 'pinia';
  3. export const useEmotionStore = defineStore('emotion', {
  4. state: () => ({
  5. history: [] as HistoryItem[], // 历史记录数组
  6. stockList: [] as StockData[], // 当前显示的股票列表
  7. activeStockIndex: 0, // 当前激活的股票索引
  8. maxStocks: 10, // 最大股票数量限制
  9. }),
  10. persist: {
  11. key: 'emotion-store',
  12. storage: sessionStorage,
  13. paths: ['history', 'stockList', 'activeStockIndex']
  14. },
  15. getters: {
  16. // 获取当前激活的股票
  17. activeStock: (state) => {
  18. return state.stockList[state.activeStockIndex] || null;
  19. },
  20. // 获取股票数量
  21. stockCount: (state) => state.stockList.length,
  22. // 是否达到最大股票数量
  23. isMaxStocks: (state) => state.stockList.length >= state.maxStocks,
  24. },
  25. actions: {
  26. // 添加历史记录
  27. addHistory(item: HistoryItem) {
  28. this.history.unshift(item); // 新记录添加到数组顶部,保持顺序
  29. },
  30. // 清空历史记录
  31. clearHistory() {
  32. this.history = [];
  33. },
  34. // 添加新股票
  35. addStock(stockData: StockData) {
  36. // 检查是否已存在相同的股票
  37. const existingIndex = this.stockList.findIndex(
  38. stock => stock.stockInfo.code === stockData.stockInfo.code &&
  39. stock.stockInfo.market === stockData.stockInfo.market
  40. );
  41. if (existingIndex !== -1) {
  42. // 如果股票已存在,更新数据并切换到该股票
  43. this.stockList[existingIndex] = stockData;
  44. this.activeStockIndex = existingIndex;
  45. } else {
  46. // 如果达到最大数量,移除最旧的股票
  47. if (this.stockList.length >= this.maxStocks) {
  48. this.stockList.shift();
  49. if (this.activeStockIndex > 0) {
  50. this.activeStockIndex--;
  51. }
  52. }
  53. // 添加新股票并设为当前激活
  54. this.stockList.push(stockData);
  55. this.activeStockIndex = this.stockList.length - 1;
  56. }
  57. // 同时添加到历史记录
  58. this.addHistory({
  59. queryText: stockData.queryText,
  60. stockInfo: stockData.stockInfo,
  61. apiData: stockData.apiData,
  62. conclusionData: stockData.conclusionData,
  63. timestamp: stockData.timestamp
  64. });
  65. },
  66. // 切换股票
  67. switchStock(index: number) {
  68. if (index >= 0 && index < this.stockList.length) {
  69. this.activeStockIndex = index;
  70. }
  71. },
  72. // 移除股票
  73. removeStock(index: number) {
  74. if (index >= 0 && index < this.stockList.length) {
  75. this.stockList.splice(index, 1);
  76. // 调整激活索引
  77. if (this.activeStockIndex >= this.stockList.length) {
  78. this.activeStockIndex = Math.max(0, this.stockList.length - 1);
  79. } else if (this.activeStockIndex > index) {
  80. this.activeStockIndex--;
  81. }
  82. }
  83. },
  84. // 更新股票数据
  85. updateStockData(index: number, apiData: any) {
  86. if (index >= 0 && index < this.stockList.length) {
  87. this.stockList[index].apiData = apiData;
  88. this.stockList[index].timestamp = new Date().toISOString();
  89. }
  90. },
  91. // 清空所有股票
  92. clearAllStocks() {
  93. this.stockList = [];
  94. this.activeStockIndex = 0;
  95. },
  96. // 从历史记录恢复股票
  97. restoreFromHistory(historyItem: HistoryItem) {
  98. const stockData: StockData = {
  99. queryText: historyItem.queryText,
  100. stockInfo: historyItem.stockInfo,
  101. apiData: historyItem.apiData,
  102. conclusionData: historyItem.conclusionData,
  103. timestamp: historyItem.timestamp
  104. };
  105. this.addStock(stockData);
  106. },
  107. // 更新股票的结论数据
  108. updateStockConclusion(index: number, conclusionData: string) {
  109. if (index >= 0 && index < this.stockList.length) {
  110. this.stockList[index].conclusionData = conclusionData;
  111. this.stockList[index].timestamp = new Date().toISOString();
  112. }
  113. },
  114. // 更新当前激活股票的结论数据
  115. updateActiveStockConclusion(conclusionData: string) {
  116. this.updateStockConclusion(this.activeStockIndex, conclusionData);
  117. },
  118. },
  119. });
  120. // 定义历史记录项的类型
  121. interface HistoryItem {
  122. queryText: string; // 用户输入的查询文本(如股票名称/代码)
  123. stockInfo: {
  124. name: string; // 股票名称
  125. code: string; // 股票代码
  126. market: string; // 市场(如 usa/cn/hk)
  127. };
  128. apiData: any; // 接口返回的原始数据(包含图表数据)
  129. conclusionData?: string; // 第二个工作流接口返回的结论数据
  130. timestamp: string; // 记录时间
  131. }
  132. // 定义股票数据的类型
  133. interface StockData {
  134. queryText: string; // 用户输入的查询文本
  135. stockInfo: {
  136. name: string; // 股票名称
  137. code: string; // 股票代码
  138. market: string; // 市场
  139. };
  140. apiData: any; // API返回的完整数据
  141. conclusionData?: string; // 第二个工作流接口返回的结论数据
  142. timestamp: string; // 数据获取时间
  143. }