Files
yinghuoapi/frontend/src/utils/format.ts
shaw 752882a022 fix: Token 统计支持 M 单位并修复 lint 错误
- 用户仪表盘 Token 统计卡片支持 K/M 单位自动切换
- 更新 formatTokensK 工具函数支持百万级显示
- 修复 setup.go 中未检查返回值的 errcheck 错误
2026-01-06 10:13:12 +08:00

186 lines
5.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 格式化工具函数
* 参考 CRS 项目的 format.js 实现
*/
import { i18n, getLocale } from '@/i18n'
/**
* 格式化相对时间
* @param date 日期字符串或 Date 对象
* @returns 相对时间字符串,如 "5m ago", "2h ago", "3d ago"
*/
export function formatRelativeTime(date: string | Date | null | undefined): string {
if (!date) return i18n.global.t('common.time.never')
const now = new Date()
const past = new Date(date)
const diffMs = now.getTime() - past.getTime()
// 处理未来时间或无效日期
if (diffMs < 0 || isNaN(diffMs)) return i18n.global.t('common.time.never')
const diffSecs = Math.floor(diffMs / 1000)
const diffMins = Math.floor(diffSecs / 60)
const diffHours = Math.floor(diffMins / 60)
const diffDays = Math.floor(diffHours / 24)
if (diffDays > 0) return i18n.global.t('common.time.daysAgo', { n: diffDays })
if (diffHours > 0) return i18n.global.t('common.time.hoursAgo', { n: diffHours })
if (diffMins > 0) return i18n.global.t('common.time.minutesAgo', { n: diffMins })
return i18n.global.t('common.time.justNow')
}
/**
* 格式化数字(支持 K/M/B 单位)
* @param num 数字
* @returns 格式化后的字符串,如 "1.2K", "3.5M"
*/
export function formatNumber(num: number | null | undefined): string {
if (num === null || num === undefined) return '0'
const locale = getLocale()
const absNum = Math.abs(num)
// Use Intl.NumberFormat for compact notation if supported and needed
// Note: Compact notation in 'zh' uses '万/亿', which is appropriate for Chinese
const formatter = new Intl.NumberFormat(locale, {
notation: absNum >= 10000 ? 'compact' : 'standard',
maximumFractionDigits: 1
})
return formatter.format(num)
}
/**
* 格式化货币金额
* @param amount 金额
* @param currency 货币代码,默认 USD
* @returns 格式化后的字符串,如 "$1.25"
*/
export function formatCurrency(amount: number | null | undefined, currency: string = 'USD'): string {
if (amount === null || amount === undefined) return '$0.00'
const locale = getLocale()
// For very small amounts, show more decimals
const fractionDigits = amount > 0 && amount < 0.01 ? 6 : 2
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: currency,
minimumFractionDigits: fractionDigits,
maximumFractionDigits: fractionDigits
}).format(amount)
}
/**
* 格式化字节大小
* @param bytes 字节数
* @param decimals 小数位数
* @returns 格式化后的字符串,如 "1.5 MB"
*/
export function formatBytes(bytes: number, decimals: number = 2): string {
if (bytes === 0) return '0 Bytes'
const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
const i = Math.floor(Math.log(bytes) / Math.log(k))
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]
}
/**
* 格式化日期
* @param date 日期字符串或 Date 对象
* @param options Intl.DateTimeFormatOptions
* @returns 格式化后的日期字符串
*/
export function formatDate(
date: string | Date | null | undefined,
options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
}
): string {
if (!date) return ''
const d = new Date(date)
if (isNaN(d.getTime())) return ''
const locale = getLocale()
return new Intl.DateTimeFormat(locale, options).format(d)
}
/**
* 格式化日期(只显示日期部分)
* @param date 日期字符串或 Date 对象
* @returns 格式化后的日期字符串
*/
export function formatDateOnly(date: string | Date | null | undefined): string {
return formatDate(date, {
year: 'numeric',
month: '2-digit',
day: '2-digit'
})
}
/**
* 格式化日期时间(完整格式)
* @param date 日期字符串或 Date 对象
* @returns 格式化后的日期时间字符串
*/
export function formatDateTime(date: string | Date | null | undefined): string {
return formatDate(date)
}
/**
* 格式化时间(只显示时分)
* @param date 日期字符串或 Date 对象
* @returns 格式化后的时间字符串
*/
export function formatTime(date: string | Date | null | undefined): string {
return formatDate(date, {
hour: '2-digit',
minute: '2-digit',
hour12: false
})
}
/**
* 格式化数字(千分位分隔,不使用紧凑单位)
* @param num 数字
* @returns 格式化后的字符串,如 "12,345"
*/
export function formatNumberLocaleString(num: number): string {
return num.toLocaleString()
}
/**
* 格式化金额(固定小数位,不带货币符号)
* @param amount 金额
* @param fractionDigits 小数位数,默认 4
* @returns 格式化后的字符串,如 "1.2345"
*/
export function formatCostFixed(amount: number, fractionDigits: number = 4): string {
return amount.toFixed(fractionDigits)
}
/**
* 格式化 token 数量(>=1M 显示为 M>=1K 显示为 K保留 1 位小数)
* @param tokens token 数量
* @returns 格式化后的字符串,如 "950", "1.2K", "3.5M"
*/
export function formatTokensK(tokens: number): string {
if (tokens >= 1_000_000) return `${(tokens / 1_000_000).toFixed(1)}M`
if (tokens >= 1000) return `${(tokens / 1000).toFixed(1)}K`
return tokens.toString()
}