275 lines
7.9 KiB
TypeScript
275 lines
7.9 KiB
TypeScript
/**
|
||
* 资产信息格式化工具函数
|
||
*/
|
||
|
||
type TagType = 'primary' | 'success' | 'warning' | 'info' | 'danger'
|
||
|
||
export function useAssetFormatters() {
|
||
// 格式化数据大小(MB 转 GB/MB)
|
||
const formatDataSize = (mb: number) => {
|
||
if (mb >= 1024) {
|
||
return `${(mb / 1024).toFixed(2)}GB`
|
||
}
|
||
return `${mb.toFixed(2)}MB`
|
||
}
|
||
|
||
// 格式化金额(分转元)
|
||
const formatAmount = (amount: number | undefined) => {
|
||
if (amount === undefined || amount === null) return '¥0.00'
|
||
const yuan = amount / 100
|
||
return `¥${yuan.toFixed(2)}`
|
||
}
|
||
|
||
// 格式化时长(秒转时分秒)
|
||
const formatDuration = (seconds?: string) => {
|
||
if (!seconds) return '-'
|
||
const sec = Number(seconds)
|
||
if (Number.isNaN(sec)) return '-'
|
||
|
||
const hours = Math.floor(sec / 3600)
|
||
const minutes = Math.floor((sec % 3600) / 60)
|
||
const secs = sec % 60
|
||
|
||
if (hours > 0) {
|
||
return `${hours}小时${minutes}分${secs}秒`
|
||
}
|
||
if (minutes > 0) {
|
||
return `${minutes}分${secs}秒`
|
||
}
|
||
return `${secs}秒`
|
||
}
|
||
|
||
// 格式化 ICCID,每 4 位一组,用横杠分隔
|
||
const formatIccidWithDashes = (iccid: string) => {
|
||
if (!iccid) return ''
|
||
return iccid.replace(/(\d{4})(?=\d)/g, '$1-')
|
||
}
|
||
|
||
// 获取实名状态名称
|
||
const getRealNameStatusName = (status?: number) => {
|
||
if (status === undefined || status === null) return '未知'
|
||
const statusMap: Record<number, string> = {
|
||
0: '未实名',
|
||
1: '已实名'
|
||
}
|
||
return statusMap[status] || '未知'
|
||
}
|
||
|
||
// 获取实名状态标签类型
|
||
const getRealNameStatusType = (status?: number): 'info' | 'success' | 'warning' | 'danger' => {
|
||
if (status === undefined || status === null) return 'info'
|
||
const map: Record<number, 'info' | 'success' | 'warning' | 'danger'> = {
|
||
0: 'info',
|
||
1: 'success'
|
||
}
|
||
return map[status] || 'info'
|
||
}
|
||
|
||
// 获取实名认证策略名称
|
||
const getRealnamePolicyName = (policy?: string) => {
|
||
if (!policy || policy === '') return '未知'
|
||
const policyMap: Record<string, string> = {
|
||
none: '无需实名',
|
||
before_order: '先实名后充值/购买',
|
||
after_order: '先充值/购买后实名'
|
||
}
|
||
return policyMap[policy] || '未知'
|
||
}
|
||
|
||
// 获取资产状态名称
|
||
const getAssetStatusName = (status?: number) => {
|
||
if (status === undefined || status === null) return '未知'
|
||
const map: Record<number, string> = {
|
||
1: '在库',
|
||
2: '已分销',
|
||
3: '已激活',
|
||
4: '已停用'
|
||
}
|
||
return map[status] || '未知'
|
||
}
|
||
|
||
// 获取资产状态标签类型
|
||
const getAssetStatusType = (status?: number): TagType => {
|
||
if (status === undefined || status === null) return 'info'
|
||
const map: Record<number, TagType> = {
|
||
1: 'info',
|
||
2: 'success',
|
||
3: 'success',
|
||
4: 'danger'
|
||
}
|
||
return map[status] || 'info'
|
||
}
|
||
|
||
// 获取在线状态名称
|
||
const getOnlineStatusName = (status?: number) => {
|
||
const map: Record<number, string> = {
|
||
0: '未知',
|
||
1: '在线',
|
||
2: '离线'
|
||
}
|
||
return map[status ?? 0] || '未知'
|
||
}
|
||
|
||
// 获取在线状态标签类型
|
||
const getOnlineStatusType = (status?: number): TagType => {
|
||
const map: Record<number, TagType> = {
|
||
0: 'info',
|
||
1: 'success',
|
||
2: 'danger'
|
||
}
|
||
return map[status ?? 0] || 'info'
|
||
}
|
||
|
||
// 获取钱包状态标签类型
|
||
const getWalletStatusType = (status?: number): TagType => {
|
||
const map: Record<number, TagType> = {
|
||
1: 'success',
|
||
2: 'warning',
|
||
3: 'danger'
|
||
}
|
||
return map[status || 1] || 'info'
|
||
}
|
||
|
||
// 获取套餐状态标签类型
|
||
const getPackageStatusTagType = (status?: number): TagType => {
|
||
if (status === undefined || status === null) return 'info'
|
||
const map: Record<number, TagType> = {
|
||
0: 'info',
|
||
1: 'success',
|
||
2: 'warning',
|
||
3: 'danger',
|
||
4: 'info'
|
||
}
|
||
return map[status] || 'info'
|
||
}
|
||
|
||
// 获取设备信号综合判断文案
|
||
const getSignalQualityText = (quality?: string | null) => {
|
||
return quality?.trim() || '暂无数据'
|
||
}
|
||
|
||
// 获取设备信号综合判断标签类型
|
||
const getSignalQualityType = (quality?: string | null): TagType => {
|
||
const normalizedQuality = getSignalQualityText(quality)
|
||
const map: Record<string, TagType> = {
|
||
信号很好: 'success',
|
||
信号正常: 'success',
|
||
信号较弱: 'warning',
|
||
信号很差: 'danger',
|
||
暂无数据: 'info'
|
||
}
|
||
return map[normalizedQuality] || 'info'
|
||
}
|
||
|
||
// 获取设备弱信号原因文案
|
||
const getSignalBadReasonText = (reason?: string | null) => {
|
||
return reason?.trim() || '暂时无法判断'
|
||
}
|
||
|
||
// 获取切卡模式名称
|
||
const getSwitchModeName = (mode?: string) => {
|
||
const map: Record<string, string> = {
|
||
'0': '自动',
|
||
'1': '手动'
|
||
}
|
||
return map[mode || ''] || '-'
|
||
}
|
||
|
||
// 获取交易类型标签颜色
|
||
const getTransactionTypeTag = (type: string | undefined): TagType => {
|
||
const map: Record<string, TagType> = {
|
||
recharge: 'success',
|
||
deduct: 'danger',
|
||
refund: 'warning'
|
||
}
|
||
return map[type || ''] || 'info'
|
||
}
|
||
|
||
// 获取支付状态标签类型
|
||
const getPaymentStatusType = (status: number): 'success' | 'warning' | 'danger' | 'info' => {
|
||
const statusMap: Record<number, 'success' | 'warning' | 'danger' | 'info'> = {
|
||
1: 'warning',
|
||
2: 'success',
|
||
3: 'danger',
|
||
4: 'info'
|
||
}
|
||
return statusMap[status] || 'info'
|
||
}
|
||
|
||
// 获取到期计时基准文案
|
||
const getExpiryBaseLabel = (expiryBase?: string | null) => {
|
||
const map: Record<string, string> = {
|
||
from_activation: '实名激活时起算',
|
||
from_purchase: '购买时起算'
|
||
}
|
||
return map[expiryBase || ''] || '--'
|
||
}
|
||
|
||
// 计算使用进度百分比
|
||
const getUsageProgress = (cumulative: number, total: number): number => {
|
||
if (!total || total <= 0) return 0
|
||
const percentage = (cumulative / total) * 100
|
||
return Math.min(Math.round(percentage * 100) / 100, 100)
|
||
}
|
||
|
||
// 根据百分比获取进度条颜色
|
||
const getProgressColorByPercentage = (percentage: number): string => {
|
||
if (percentage < 50) return '#67c23a'
|
||
if (percentage < 80) return '#e6a23c'
|
||
return '#f56c6c'
|
||
}
|
||
|
||
// 格式化进度百分比文本
|
||
const formatProgressPercentage = (percentage: number): string => {
|
||
if (!Number.isFinite(percentage)) return '0%'
|
||
const normalizedPercentage = Math.min(Math.max(percentage, 0), 100)
|
||
if (Number.isInteger(normalizedPercentage)) return `${normalizedPercentage}%`
|
||
return `${normalizedPercentage.toFixed(2).replace(/\.?0+$/, '')}%`
|
||
}
|
||
|
||
// 计算断点位置百分比(用于虚流量停机阈值标记)
|
||
const getBreakpointPercentage = (breakpoint: number, total: number): number => {
|
||
if (!total || total <= 0) return 0
|
||
const percentage = (breakpoint / total) * 100
|
||
return Math.min(Math.max(Math.round(percentage * 100) / 100, 0), 100)
|
||
}
|
||
|
||
// 生成断点标记样式,避免圆点在进度条两端溢出
|
||
const getBreakpointMarkerStyle = (breakpoint: number, total: number, markerSize: number = 8) => {
|
||
const percentage = getBreakpointPercentage(breakpoint, total)
|
||
const halfMarkerSize = markerSize / 2
|
||
|
||
return {
|
||
left: `clamp(${halfMarkerSize}px, ${percentage}%, calc(100% - ${halfMarkerSize}px))`
|
||
}
|
||
}
|
||
|
||
return {
|
||
formatDataSize,
|
||
formatAmount,
|
||
formatDuration,
|
||
formatIccidWithDashes,
|
||
getRealNameStatusName,
|
||
getRealNameStatusType,
|
||
getRealnamePolicyName,
|
||
getAssetStatusName,
|
||
getAssetStatusType,
|
||
getOnlineStatusName,
|
||
getOnlineStatusType,
|
||
getWalletStatusType,
|
||
getPackageStatusTagType,
|
||
getSignalQualityText,
|
||
getSignalQualityType,
|
||
getSignalBadReasonText,
|
||
getSwitchModeName,
|
||
getTransactionTypeTag,
|
||
getPaymentStatusType,
|
||
getExpiryBaseLabel,
|
||
getUsageProgress,
|
||
getProgressColorByPercentage,
|
||
formatProgressPercentage,
|
||
getBreakpointPercentage,
|
||
getBreakpointMarkerStyle
|
||
}
|
||
}
|