From 06dcb2f36b5793ef155c04e4361ee3a5d4faab35 Mon Sep 17 00:00:00 2001 From: zhongzm Date: Tue, 11 Feb 2025 16:32:13 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=A5=97?= =?UTF-8?q?=E9=A4=90=E7=AE=A1=E7=90=86=E5=92=8C=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E5=B7=A5=E5=85=B7=E7=9A=84=E4=B8=AD=E8=8B=B1=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/locales/langs/en-us.ts | 6 ++++ src/locales/langs/zh-cn.ts | 6 ++++ src/utils/units.ts | 54 ++++++++++++++++++++--------- src/views/billing/package/index.vue | 15 ++++---- 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/locales/langs/en-us.ts b/src/locales/langs/en-us.ts index 40f8ce8..ed5b43d 100644 --- a/src/locales/langs/en-us.ts +++ b/src/locales/langs/en-us.ts @@ -81,6 +81,12 @@ const local: any = { }, tablePaginationTotal: 'Total {total} items', inputPlease: 'Please input', + time:{ + second: 's', + minute: 'Min', + hour: 'Hour', + day: 'Day' + } }, // 组件 components: { diff --git a/src/locales/langs/zh-cn.ts b/src/locales/langs/zh-cn.ts index 39ba04f..6be19f4 100644 --- a/src/locales/langs/zh-cn.ts +++ b/src/locales/langs/zh-cn.ts @@ -81,6 +81,12 @@ const local:any = { }, tablePaginationTotal: '总共 {total} 条', inputPlease: '请输入', + time:{ + second: '秒', + minute: '分钟', + hour: '小时', + day: '天' + } }, // 组件 components: { diff --git a/src/utils/units.ts b/src/utils/units.ts index d9e78a0..b7456fe 100644 --- a/src/utils/units.ts +++ b/src/utils/units.ts @@ -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 = { - '秒': 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}`)}` + }; + }; +}; diff --git a/src/views/billing/package/index.vue b/src/views/billing/package/index.vue index 0b8ad8c..19c99b1 100644 --- a/src/views/billing/package/index.vue +++ b/src/views/billing/package/index.vue @@ -127,7 +127,9 @@ /> @@ -168,17 +170,18 @@ import type { Rule } from 'ant-design-vue/es/form'; import { formatBandwidth, formatStorage, - formatTime, + useFormatTime, convertStorage, convertTime, storageUnits, - timeUnits, + useTimeUnits, type StorageUnit, type TimeUnit } from '@/utils/units'; import { useI18n } from 'vue-i18n'; const { t } = useI18n(); - +const timeUnits = useTimeUnits(); +const formatTime = useFormatTime(); const wrapperEl = shallowRef(null); const { height: wrapperElHeight } = useElementSize(wrapperEl); @@ -355,7 +358,7 @@ const formState = ref({ rateLimitId: undefined, durationEnable: false, duration: 0, - durationUnit: t('page.package.hour'), + durationUnit: 'hour', clientNumEnable: false, clientNum: 0, packageEnable: true @@ -468,7 +471,7 @@ const handleCancel = () => { rateLimitId: undefined, durationEnable: false, duration: 0, - durationUnit:`${t('page.package.hour')}`, + durationUnit:'hour', clientNumEnable: false, clientNum: 0, packageEnable: true