|
|
@ -132,9 +132,8 @@ const fetchCashData = async () => { |
|
|
value: resMap.get(m.name) ?? 0 |
|
|
value: resMap.get(m.name) ?? 0 |
|
|
})) |
|
|
})) |
|
|
} else { |
|
|
} else { |
|
|
// 1. 定义币种中英文对照表 |
|
|
|
|
|
|
|
|
// 1. 定义币种中英文对照表 |
|
|
const currencyMap = { |
|
|
const currencyMap = { |
|
|
// usd: '美元', |
|
|
|
|
|
sgd: '新币', |
|
|
sgd: '新币', |
|
|
myr: '马币', |
|
|
myr: '马币', |
|
|
hkd: '港币', |
|
|
hkd: '港币', |
|
|
@ -143,26 +142,31 @@ const fetchCashData = async () => { |
|
|
vdn: '越南盾', |
|
|
vdn: '越南盾', |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// 2. 取出所有币种字段(排除 market 与 totalSGD) |
|
|
|
|
|
// 当res为空时,直接使用currencyMap的key作为基础币种 |
|
|
|
|
|
const currencyKeys = res.length > 0 |
|
|
|
|
|
? Object.keys(res[0]).filter(key => key !== 'market' && key !== 'totalSGD') |
|
|
|
|
|
: Object.keys(currencyMap); |
|
|
|
|
|
|
|
|
|
|
|
// 3. 累加每个币种的总额并替换中文名(res为空时默认值为0) |
|
|
|
|
|
|
|
|
// 2. 取出所有币种字段(排除 market 与 totalSGD),只保留 currencyMap 中定义的币种 |
|
|
|
|
|
const currencyKeys = |
|
|
|
|
|
res.length > 0 |
|
|
|
|
|
? Object.keys(res[0]).filter( |
|
|
|
|
|
key => |
|
|
|
|
|
key !== 'market' && |
|
|
|
|
|
key !== 'totalSGD' && |
|
|
|
|
|
Object.keys(currencyMap).includes(key.toLowerCase()) |
|
|
|
|
|
) |
|
|
|
|
|
: Object.keys(currencyMap); |
|
|
|
|
|
|
|
|
|
|
|
// 3. 累加每个币种的总额并替换中文名 |
|
|
markets.value = currencyKeys.map(currency => { |
|
|
markets.value = currencyKeys.map(currency => { |
|
|
// 处理币种key的大小写统一(转为小写匹配currencyMap) |
|
|
|
|
|
const lowerCurrency = currency.toLowerCase(); |
|
|
const lowerCurrency = currency.toLowerCase(); |
|
|
// 当res为空时直接返回0,否则累加计算 |
|
|
|
|
|
const total = res.length > 0 |
|
|
|
|
|
? res.reduce((sum, item) => sum + (Number(item[currency]) || 0), 0) |
|
|
|
|
|
: 0; |
|
|
|
|
|
|
|
|
const total = |
|
|
|
|
|
res.length > 0 |
|
|
|
|
|
? res.reduce((sum, item) => sum + (Number(item[currency]) || 0), 0) |
|
|
|
|
|
: 0; |
|
|
|
|
|
|
|
|
return { |
|
|
return { |
|
|
// 优先使用对照表的中文名称,确保res为空时也能正确显示中文 |
|
|
|
|
|
name: currencyMap[lowerCurrency] || currency.toUpperCase(), |
|
|
|
|
|
value: total |
|
|
|
|
|
|
|
|
name: currencyMap[lowerCurrency], |
|
|
|
|
|
value: total, |
|
|
}; |
|
|
}; |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|