Files
filecodebox/src/utils/config-form 2.ts
T
orion 8badb80600 ✨ feat(core): 实现管理端后台界面及核心功能模块
* 初始化后台管理布局与路由框架
* 新增文件管理、仪表盘、系统设置及日志视图
* 实现文件上传预签名、提取流程及发送记录管理
* 构建常用组件库如模态框、表格、分页及进度条
* 引入多语言支持(中英文)及主题切换功能
* 封装后台业务逻辑 Hooks 和数据持久化存储功能
2026-09-11 16:51:50 +08:00

108 lines
2.2 KiB
TypeScript

import type { ConfigState } from '@/types'
export type FileSizeUnit = 'KB' | 'MB' | 'GB'
export type SaveTimeUnit = '秒' | '分' | '时' | '天'
export type FileSizeForm = {
value: number
unit: FileSizeUnit
}
export type SaveTimeForm = {
value: number
unit: SaveTimeUnit
}
const FILE_SIZE_UNITS: Record<FileSizeUnit, number> = {
KB: 1024,
MB: 1024 * 1024,
GB: 1024 * 1024 * 1024
}
const SAVE_TIME_UNITS: Record<SaveTimeUnit, number> = {
秒: 1,
分: 60,
时: 3600,
天: 86400
}
export function bytesToFileSizeForm(bytes: number): FileSizeForm {
if (bytes >= FILE_SIZE_UNITS.GB) {
return {
value: Math.round(bytes / FILE_SIZE_UNITS.GB),
unit: 'GB'
}
}
if (bytes >= FILE_SIZE_UNITS.MB) {
return {
value: Math.round(bytes / FILE_SIZE_UNITS.MB),
unit: 'MB'
}
}
return {
value: Math.round(bytes / FILE_SIZE_UNITS.KB),
unit: 'KB'
}
}
export function fileSizeFormToBytes(value: number, unit: FileSizeUnit): number {
return value * FILE_SIZE_UNITS[unit]
}
export function secondsToSaveTimeForm(seconds: number): SaveTimeForm {
if (seconds === 0) {
return {
value: 7,
unit: '天'
}
}
if (seconds % SAVE_TIME_UNITS.天 === 0 && seconds >= SAVE_TIME_UNITS.天) {
return {
value: seconds / SAVE_TIME_UNITS.天,
unit: '天'
}
}
if (seconds % SAVE_TIME_UNITS.时 === 0 && seconds >= SAVE_TIME_UNITS.时) {
return {
value: seconds / SAVE_TIME_UNITS.时,
unit: '时'
}
}
if (seconds % SAVE_TIME_UNITS.分 === 0 && seconds >= SAVE_TIME_UNITS.分) {
return {
value: seconds / SAVE_TIME_UNITS.分,
unit: '分'
}
}
return {
value: seconds,
unit: '秒'
}
}
export function saveTimeFormToSeconds(value: number, unit: SaveTimeUnit): number {
if (value === 0) {
return 7 * SAVE_TIME_UNITS.天
}
return value * SAVE_TIME_UNITS[unit]
}
export function buildConfigSubmitPayload(
config: ConfigState,
fileSize: FileSizeForm,
saveTime: SaveTimeForm
): ConfigState {
return {
...config,
uploadSize: fileSizeFormToBytes(fileSize.value, fileSize.unit),
max_save_seconds: saveTimeFormToSeconds(saveTime.value, saveTime.unit)
}
}