fix: 修复涨跌幅获取

This commit is contained in:
Cassianvale
2025-03-10 15:13:57 +08:00
parent 992f4e2c68
commit 43b1fdd00b
4 changed files with 25 additions and 27 deletions

View File

@@ -469,21 +469,12 @@ function handleStreamUpdate(data: StreamAnalysisUpdate) {
// 确保所有数值类型的字段都有默认值
stock.price = data.price ?? stock.price ?? undefined;
stock.price_change = data.price_change ?? stock.price_change ?? undefined;
// 使用change_percent作为涨跌幅
stock.price_change = data.price_change_value ?? data.price_change ?? stock.price_change ?? undefined;
stock.changePercent = data.change_percent ?? stock.changePercent ?? undefined;
stock.marketValue = data.market_value ?? stock.marketValue ?? undefined;
stock.score = data.score ?? stock.score ?? undefined;
stock.rsi = data.rsi ?? stock.rsi ?? undefined;
// 如果没有change_percent但有price_change和price尝试计算changePercent
if (stock.changePercent === undefined && stock.price_change !== undefined && stock.price !== undefined) {
const basePrice = stock.price - stock.price_change;
if (basePrice !== 0) {
stock.changePercent = (stock.price_change / basePrice) * 100;
}
}
// 更新分析状态
if (data.status) {
stock.analysisStatus = data.status;

View File

@@ -211,23 +211,11 @@ function highlightKeywords(html: string): string {
return html;
}
// 计算涨跌幅
// 获取涨跌幅
const calculatedChangePercent = computed(() => {
// 如果已有changePercent则直接使用
if (props.stock.changePercent !== undefined) {
return props.stock.changePercent;
}
// 如果有price_change和price则计算涨跌幅
if (props.stock.price_change !== undefined && props.stock.price !== undefined) {
// 计算涨跌幅百分比 = (价格变动 / (当前价格 - 价格变动)) * 100
const basePrice = props.stock.price - props.stock.price_change;
if (basePrice !== 0) {
return (props.stock.price_change / basePrice) * 100;
}
}
// 无法计算则返回undefined
return undefined;
});

View File

@@ -96,6 +96,7 @@ export interface StreamAnalysisUpdate {
name?: string;
price?: number;
change_percent?: number;
price_change_value?: number;
market_value?: number;
score?: number;
recommendation?: string;