diff --git a/app/pages/home/components/ItemDetailSheet/index.vue b/app/pages/home/components/ItemDetailSheet/index.vue new file mode 100644 index 0000000..2818556 --- /dev/null +++ b/app/pages/home/components/ItemDetailSheet/index.vue @@ -0,0 +1,96 @@ + + + + + \ No newline at end of file diff --git a/app/utils/format.js b/app/utils/format.js new file mode 100644 index 0000000..c4ee7b2 --- /dev/null +++ b/app/utils/format.js @@ -0,0 +1,44 @@ +/** + * 格式化价格 + * @param {number} price - 价格数值 + * @param {string} currency - 货币符号,默认为 ¥ + * @returns {string} 格式化后的价格字符串 + */ +export const formatPrice = (price, currency = '¥') => { + if (price == null || isNaN(price)) return `${currency}0` + + // 将价格转换为数字 + const numPrice = Number(price) + + // 处理小数点,保留两位小数 + const formattedPrice = numPrice.toFixed(2) + + // 添加千位分隔符 + const parts = formattedPrice.toString().split('.') + parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',') + + return `${currency}${parts.join('.')}` +} + +/** + * 格式化数字 + * @param {number} num - 需要格式化的数字 + * @returns {string} 格式化后的数字字符串 + */ +export const formatNumber = (num) => { + if (num == null || isNaN(num)) return '0' + + return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',') +} + +/** + * 格式化百分比 + * @param {number} value - 需要格式化的值 + * @param {number} decimals - 小数位数,默认为 2 + * @returns {string} 格式化后的百分比字符串 + */ +export const formatPercent = (value, decimals = 2) => { + if (value == null || isNaN(value)) return '0%' + + return `${(Number(value) * 100).toFixed(decimals)}%` +} \ No newline at end of file diff --git a/nuxt.config.js b/nuxt.config.js index 7a4fad8..a7012c8 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -20,7 +20,7 @@ export default defineNuxtConfig({ pages.push({ name: 'home', path: '/', - file: '~/pages/home/index.vue' + file: '@/pages/home/index.vue' }) } },