|
|
// @/store/emotion.js
import { defineStore } from 'pinia';
export const useEmotionStore = defineStore('emotion', { state: () => ({ history: [] as HistoryItem[], // 历史记录数组
stockList: [] as StockData[], // 当前显示的股票列表
activeStockIndex: 0, // 当前激活的股票索引
maxStocks: 10, // 最大股票数量限制
}), persist: { key: 'emotion-store', storage: sessionStorage, paths: ['history', 'stockList', 'activeStockIndex'] }, getters: { // 获取当前激活的股票
activeStock: (state) => { return state.stockList[state.activeStockIndex] || null; }, // 获取股票数量
stockCount: (state) => state.stockList.length, // 是否达到最大股票数量
isMaxStocks: (state) => state.stockList.length >= state.maxStocks, }, actions: { // 添加历史记录
addHistory(item: HistoryItem) { this.history.unshift(item); // 新记录添加到数组顶部,保持顺序
}, // 清空历史记录
clearHistory() { this.history = []; }, // 添加新股票
addStock(stockData: StockData) { // 检查是否已存在相同的股票
const existingIndex = this.stockList.findIndex( stock => stock.stockInfo.code === stockData.stockInfo.code && stock.stockInfo.market === stockData.stockInfo.market ); if (existingIndex !== -1) { // 如果股票已存在,更新数据并切换到该股票
this.stockList[existingIndex] = stockData; this.activeStockIndex = existingIndex; } else { // 如果达到最大数量,移除最旧的股票
if (this.stockList.length >= this.maxStocks) { this.stockList.shift(); if (this.activeStockIndex > 0) { this.activeStockIndex--; } } // 添加新股票并设为当前激活
this.stockList.push(stockData); this.activeStockIndex = this.stockList.length - 1; } // 同时添加到历史记录
this.addHistory({ queryText: stockData.queryText, stockInfo: stockData.stockInfo, apiData: stockData.apiData, conclusionData: stockData.conclusionData, timestamp: stockData.timestamp }); }, // 切换股票
switchStock(index: number) { if (index >= 0 && index < this.stockList.length) { this.activeStockIndex = index; } }, // 移除股票
removeStock(index: number) { if (index >= 0 && index < this.stockList.length) { this.stockList.splice(index, 1); // 调整激活索引
if (this.activeStockIndex >= this.stockList.length) { this.activeStockIndex = Math.max(0, this.stockList.length - 1); } else if (this.activeStockIndex > index) { this.activeStockIndex--; } } }, // 更新股票数据
updateStockData(index: number, apiData: any) { if (index >= 0 && index < this.stockList.length) { this.stockList[index].apiData = apiData; this.stockList[index].timestamp = new Date().toISOString(); } }, // 清空所有股票
clearAllStocks() { this.stockList = []; this.activeStockIndex = 0; }, // 从历史记录恢复股票
restoreFromHistory(historyItem: HistoryItem) { const stockData: StockData = { queryText: historyItem.queryText, stockInfo: historyItem.stockInfo, apiData: historyItem.apiData, conclusionData: historyItem.conclusionData, timestamp: historyItem.timestamp }; this.addStock(stockData); }, // 更新股票的结论数据
updateStockConclusion(index: number, conclusionData: string) { if (index >= 0 && index < this.stockList.length) { this.stockList[index].conclusionData = conclusionData; this.stockList[index].timestamp = new Date().toISOString(); } }, // 更新当前激活股票的结论数据
updateActiveStockConclusion(conclusionData: string) { this.updateStockConclusion(this.activeStockIndex, conclusionData); }, }, });
// 定义历史记录项的类型
interface HistoryItem { queryText: string; // 用户输入的查询文本(如股票名称/代码)
stockInfo: { name: string; // 股票名称
code: string; // 股票代码
market: string; // 市场(如 usa/cn/hk)
}; apiData: any; // 接口返回的原始数据(包含图表数据)
conclusionData?: string; // 第二个工作流接口返回的结论数据
timestamp: string; // 记录时间
}
// 定义股票数据的类型
interface StockData { queryText: string; // 用户输入的查询文本
stockInfo: { name: string; // 股票名称
code: string; // 股票代码
market: string; // 市场
}; apiData: any; // API返回的完整数据
conclusionData?: string; // 第二个工作流接口返回的结论数据
timestamp: string; // 数据获取时间
}
|