2
0

fix:修复套餐管理和格式化工具的中英适配

This commit is contained in:
zhongzm
2025-02-11 16:32:13 +08:00
parent ada516208f
commit 06dcb2f36b
4 changed files with 59 additions and 22 deletions

View File

@@ -1,3 +1,4 @@
import {useI18n} from "vue-i18n";
// 带宽单位转换
export type BandwidthUnit = 'Kbps' | 'Mbps' | 'Gbps';
@@ -57,15 +58,24 @@ export function formatStorage(byteValue: number): { value: number; unit: Storage
}
// 时间单位转换
export type TimeUnit = '' | '分钟' | '小时' | '';
export type TimeUnit = 'second' | 'minute' | 'hour' | 'day';
export const timeUnits: TimeUnit[] = ['秒', '分钟', '小时', '天'];
export const useTimeUnits = () => {
const { t } = useI18n();
return computed(() => ([
{ key: 'second', label: t('common.time.second') },
{ key: 'minute', label: t('common.time.minute') },
{ key: 'hour', label: t('common.time.hour') },
{ key: 'day', label: t('common.time.day') }
]));
};
export const timeFactors: Record<TimeUnit, number> = {
'': 1,
'分钟': 60,
'小时': 3600,
'': 86400
'second': 1,
'minute': 60,
'hour': 3600,
'day': 86400
};
export function convertTime(value: number, fromUnit: TimeUnit, toUnit: TimeUnit): number {
@@ -74,13 +84,25 @@ export function convertTime(value: number, fromUnit: TimeUnit, toUnit: TimeUnit)
return (value * fromFactor) / toFactor;
}
export function formatTime(secondsValue: number): { value: number; unit: TimeUnit } {
if (secondsValue >= 86400) {
return { value: secondsValue / 86400, unit: '天' };
} else if (secondsValue >= 3600) {
return { value: secondsValue / 3600, unit: '小时' };
} else if (secondsValue >= 60) {
return { value: secondsValue / 60, unit: '分钟' };
}
return { value: secondsValue, unit: '秒' };
}
export const useFormatTime = () => {
const { t } = useI18n();
return (secondsValue: number): { value: number; unit: TimeUnit; display: string } => {
let result: { value: number; unit: TimeUnit };
if (secondsValue >= 86400) {
result = { value: secondsValue / 86400, unit: 'day' };
} else if (secondsValue >= 3600) {
result = { value: secondsValue / 3600, unit: 'hour' };
} else if (secondsValue >= 60) {
result = { value: secondsValue / 60, unit: 'minute' };
} else {
result = { value: secondsValue, unit: 'second' };
}
return {
...result,
display: `${result.value} ${t(`common.time.${result.unit}`)}`
};
};
};