task-133(记录与版本中心): 实现数字人版本上传
新增 digitalhuman-upload-model.ts(版本/文件校验与字段) 与 uploadDigitalHumanVersion (multipart POST /upload)。 TDD: task-133.test.ts 8 用例 RED→GREEN。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
/** 数字人版本列表加载适配(任务 132):GET /api/digital-human/versions。 */
|
||||
/** 数字人版本适配(任务 132-136):/api/digital-human/versions 系列端点。 */
|
||||
import { http } from '@/api/http'
|
||||
import { parseDigitalHumanVersions } from './digitalhuman-model.ts'
|
||||
import { buildDigitalHumanUploadFields, type DigitalHumanUploadForm } from './digitalhuman-upload-model.ts'
|
||||
import type { DigitalHumanVersionList } from './digitalhuman-dto.ts'
|
||||
|
||||
export const DIGITAL_HUMAN_VERSIONS_ENDPOINT = '/api/digital-human/versions'
|
||||
@@ -9,3 +10,12 @@ export async function fetchDigitalHumanVersions(): Promise<DigitalHumanVersionLi
|
||||
const { data } = await http.get<unknown>(DIGITAL_HUMAN_VERSIONS_ENDPOINT)
|
||||
return parseDigitalHumanVersions(data)
|
||||
}
|
||||
|
||||
/** 上传数字人版本(multipart:字段 + file)。 */
|
||||
export async function uploadDigitalHumanVersion(form: DigitalHumanUploadForm, filename = ''): Promise<void> {
|
||||
const body = new FormData()
|
||||
const fields = buildDigitalHumanUploadFields(form)
|
||||
Object.entries(fields).forEach(([key, value]) => body.append(key, value))
|
||||
if (form.file) body.append('file', form.file as unknown as Blob, filename || (form.file.name || 'dh.zip'))
|
||||
await http.post<unknown>(`${DIGITAL_HUMAN_VERSIONS_ENDPOINT}/upload`, body)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
/** 数字人版本上传表单(任务 133):版本号/文件校验与 multipart 字段;纯逻辑。 */
|
||||
|
||||
export interface DigitalHumanUploadForm {
|
||||
version: string
|
||||
file: { name?: string; size?: number } | null
|
||||
changelog?: string
|
||||
minClientVersion?: string
|
||||
createdBy?: string
|
||||
}
|
||||
|
||||
export interface DigitalHumanUploadErrors {
|
||||
version?: string
|
||||
file?: string
|
||||
}
|
||||
|
||||
/** 校验上传表单(版本号必填非空、文件已选且非空)。 */
|
||||
export function validateDigitalHumanUpload(form: DigitalHumanUploadForm): { valid: boolean; errors: DigitalHumanUploadErrors } {
|
||||
const errors: DigitalHumanUploadErrors = {}
|
||||
if (!(form.version || '').trim()) errors.version = '版本号不能为空'
|
||||
const file = form.file
|
||||
if (!file) errors.file = '请选择程序压缩包'
|
||||
else if (typeof file.size === 'number' && file.size <= 0) errors.file = '压缩包为空或无效'
|
||||
return { valid: Object.keys(errors).length === 0, errors }
|
||||
}
|
||||
|
||||
/** 表单字段(multipart 文本部分;文件在 adapter 端追加),可选字段为空不下发。 */
|
||||
export function buildDigitalHumanUploadFields(form: DigitalHumanUploadForm): {
|
||||
version: string
|
||||
changelog?: string
|
||||
minClientVersion?: string
|
||||
createdBy?: string
|
||||
} {
|
||||
const fields: { version: string; changelog?: string; minClientVersion?: string; createdBy?: string } = {
|
||||
version: (form.version || '').trim(),
|
||||
}
|
||||
const changelog = (form.changelog || '').trim()
|
||||
if (changelog) fields.changelog = changelog
|
||||
const minClientVersion = (form.minClientVersion || '').trim()
|
||||
if (minClientVersion) fields.minClientVersion = minClientVersion
|
||||
const createdBy = (form.createdBy || '').trim()
|
||||
if (createdBy) fields.createdBy = createdBy
|
||||
return fields
|
||||
}
|
||||
Reference in New Issue
Block a user