diff --git a/pages/detail/detail.vue b/pages/detail/detail.vue index 7ec2bc1..e008f27 100755 --- a/pages/detail/detail.vue +++ b/pages/detail/detail.vue @@ -50,7 +50,7 @@ 当日盈亏 - {{ (portfolioData.todayProfit || 0) >= 0 ? '+' : '' }}{{ getCurrencySymbol(portfolioData.currency) }}{{ (portfolioData.todayProfit || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }} + {{ (portfolioData.todayProfit || 0) >= 0 ? '+' : '' }}{{ getCurrencySymbol(portfolioData.todayProfitCurrency || portfolioData.currency) }}{{ (portfolioData.todayProfit || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }} diff --git a/pages/index/index.vue b/pages/index/index.vue index d57ace6..b051b92 100755 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -41,7 +41,7 @@ 今日账面变动 - {{ (assetData.todayProfit || 0) >= 0 ? '+' : '' }}¥{{ (assetData.todayProfit || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }} + {{ (assetData.todayProfit || 0) >= 0 ? '+' : '' }}{{ getCurrencySymbol(assetData.todayProfitCurrency || 'CNY') }}{{ (assetData.todayProfit || 0).toLocaleString('zh-CN', { minimumFractionDigits: 2, maximumFractionDigits: 2 }) }} 历史总变动 @@ -132,6 +132,7 @@ import { ref } from 'vue'; import { onShow } from '@dcloudio/uni-app'; import { api } from '../../utils/api'; +import { getCurrencySymbol } from '../../utils/currency'; // 加载状态 const loading = ref(true); diff --git a/utils/currency.js b/utils/currency.js new file mode 100644 index 0000000..6a885b3 --- /dev/null +++ b/utils/currency.js @@ -0,0 +1,57 @@ +/** + * 货币工具函数 + */ + +/** + * 获取货币符号 + * @param {string} currency - 货币代码 (CNY, USD, HKD 等) + * @returns {string} 货币符号 + */ +export const getCurrencySymbol = (currency) => { + const symbols = { + 'CNY': '¥', + 'USD': '$', + 'HKD': 'HK$', + 'EUR': '€', + 'GBP': '£', + 'JPY': '¥', + 'SGD': 'S$', + 'AUD': 'A$' + }; + return symbols[currency?.toUpperCase()] || '¥'; +}; + +/** + * 格式化金额 + * @param {number} amount - 金额 + * @param {string} currency - 货币代码 + * @param {number} decimals - 小数位数 + * @returns {string} 格式化后的金额字符串 + */ +export const formatAmount = (amount, currency = 'CNY', decimals = 2) => { + const symbol = getCurrencySymbol(currency); + const formatted = Math.abs(amount || 0).toLocaleString('zh-CN', { + minimumFractionDigits: decimals, + maximumFractionDigits: decimals + }); + const sign = amount >= 0 ? '' : '-'; + return `${sign}${symbol}${formatted}`; +}; + +/** + * 格式化金额(带正负号) + * @param {number} amount - 金额 + * @param {string} currency - 货币代码 + * @param {number} decimals - 小数位数 + * @returns {string} 格式化后的金额字符串 + */ +export const formatAmountWithSign = (amount, currency = 'CNY', decimals = 2) => { + const symbol = getCurrencySymbol(currency); + const formatted = Math.abs(amount || 0).toLocaleString('zh-CN', { + minimumFractionDigits: decimals, + maximumFractionDigits: decimals + }); + if (amount > 0) return `+${symbol}${formatted}`; + if (amount < 0) return `-${symbol}${formatted}`; + return `${symbol}${formatted}`; +};