fix: 创建组合时正确传递持仓币种
1. 新增 inferCurrencyFromCode 函数,根据股票代码推断币种 - .US/.NYSE/.NASDAQ → USD - .HK/.HKEX → HKD - .SZ/.SH 或 6 位数字 → CNY - 加密货币 → USD 2. 搜索股票时映射 priceCurrency 字段 3. 选择股票时保存 currency 到表单 4. 提交时同时传递 assetType 字段
This commit is contained in:
parent
eefde6da0e
commit
1702b88ebf
@ -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<void> =
|
||||
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<void> => {
|
||||
}
|
||||
}
|
||||
|
||||
// 根据股票代码推断币种
|
||||
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<void> => {
|
||||
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'
|
||||
}))
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user