diff --git a/pages/config/config.vue b/pages/config/config.vue index cfc3441..f920e51 100755 --- a/pages/config/config.vue +++ b/pages/config/config.vue @@ -187,6 +187,7 @@ interface SearchResult { name?: string; exchange?: string; assetType?: string; + currency?: string; stockIndex: number; } @@ -230,7 +231,11 @@ const searchStock = async (keyword: string, stockIndex: number): Promise = try { const res = await api.ticker.search(keyword); if (res.code === 200) { - searchResults.value = res.data.map((item: any) => ({ ...item, stockIndex })); + searchResults.value = res.data.map((item: any) => ({ + ...item, + stockIndex, + currency: item.priceCurrency || item.currency // 映射币种字段 + })); } } catch { searchResults.value = []; @@ -244,6 +249,7 @@ const selectStock = (result: SearchResult): void => { if (stock) { stock.name = result.ticker; stock.assetType = result.assetType; + stock.currency = result.currency; // 保存搜索结果中的币种 } searchResults.value = []; activeSearchIndex.value = -1; @@ -353,6 +359,30 @@ const submitForm = async (): Promise => { } } +// 根据股票代码推断币种 +const inferCurrencyFromCode = (code: string): string => { + if (!code) return selectedCurrency; + const upperCode = code.toUpperCase(); + // 美股 + if (upperCode.endsWith('.US') || upperCode.endsWith('.NYSE') || upperCode.endsWith('.NASDAQ')) { + return 'USD'; + } + // 港股 + if (upperCode.endsWith('.HK') || upperCode.endsWith('.HKEX')) { + return 'HKD'; + } + // A股 + if (upperCode.endsWith('.SZ') || upperCode.endsWith('.SH') || /^[0-9]{6}$/.test(upperCode)) { + return 'CNY'; + } + // 加密货币默认 USD + if (upperCode.includes('BTC') || upperCode.includes('ETH') || upperCode.includes('USDT')) { + return 'USD'; + } + // 默认使用组合本位币 + return selectedCurrency; +}; + const selectedCurrency = currencyList.value[currencyIndex.value].code; const requestData = { name: form.value.name, @@ -364,7 +394,8 @@ const submitForm = async (): Promise => { price: parseFloat(stock.price) || 0, amount: parseFloat(stock.amount) || 0, date: stock.date, - currency: selectedCurrency + currency: stock.currency || inferCurrencyFromCode(stock.name), + assetType: stock.assetType || 'Stock' })) };