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

@@ -81,6 +81,12 @@ const local: any = {
},
tablePaginationTotal: 'Total {total} items',
inputPlease: 'Please input',
time:{
second: 's',
minute: 'Min',
hour: 'Hour',
day: 'Day'
}
},
// 组件
components: {

View File

@@ -81,6 +81,12 @@ const local:any = {
},
tablePaginationTotal: '总共 {total} 条',
inputPlease: '请输入',
time:{
second: '秒',
minute: '分钟',
hour: '小时',
day: '天'
}
},
// 组件
components: {

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}`)}`
};
};
};

View File

@@ -127,7 +127,9 @@
/>
<ASelect
v-model:value="formState.durationUnit"
:options="timeUnits.map(unit => ({ label: unit, value: unit }))"
:options="timeUnits.map(item => ({
label: item.label,
value: item.key }))"
style="width: 100px"
/>
</div>
@@ -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<HTMLElement | null>(null);
const { height: wrapperElHeight } = useElementSize(wrapperEl);
@@ -355,7 +358,7 @@ const formState = ref<PackageForm>({
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