diff --git a/admin/src/api/file.ts b/admin/src/api/file.ts index 27e5ba5e..565b25d9 100644 --- a/admin/src/api/file.ts +++ b/admin/src/api/file.ts @@ -4,48 +4,50 @@ import useAppStore from '@/stores/modules/app' import { getToken } from '@/utils/auth' import request from '@/utils/request' +export type MaterialUploadType = 'image' | 'video' | 'file' + +/** 本地上传素材(与 `components/upload` 相同:POST /upload/{type},字段 file + cid) */ +export async function uploadMaterialFile( + file: File, + type: MaterialUploadType = 'image', + cid: string | number = 0 +) { + const appStore = useAppStore() + const formData = new FormData() + formData.append('file', file) + formData.append('cid', String(cid)) + const url = `${config.baseUrl}${config.urlPrefix}/upload/${type}` + const res = await fetch(url, { + method: 'POST', + headers: { + token: getToken() || '', + version: appStore.config.version || '' + }, + body: formData + }) + const json = await res.json() + if (json.code !== RequestCodeEnum.SUCCESS) { + throw new Error(json.msg || '上传失败') + } + return json.data as { id: number; uri: string; url: string } +} + /** 本地上传图片(与素材库「本地上传」同一接口),返回 data 含 url 相对路径 */ export async function uploadImageBlob(file: Blob, filename = 'screenshot.jpg') { - const appStore = useAppStore() - const formData = new FormData() - formData.append('file', file, filename) - formData.append('cid', '0') - const url = `${config.baseUrl}${config.urlPrefix}/upload/image` - const res = await fetch(url, { - method: 'POST', - headers: { - token: getToken() || '', - version: appStore.config.version || '' - }, - body: formData - }) - const json = await res.json() - if (json.code !== RequestCodeEnum.SUCCESS) { - throw new Error(json.msg || '上传失败') - } - return json.data as { id: number; uri: string; url: string } + const fileObj = + file instanceof File + ? file + : new File([file], filename, { type: file.type || 'image/jpeg' }) + return uploadMaterialFile(fileObj, 'image', 0) } /** 本地上传视频(管理端 /upload/video),返回 data 含 uri 完整访问地址、url 相对路径 */ export async function uploadVideoBlob(file: Blob, filename = 'recording.webm') { - const appStore = useAppStore() - const formData = new FormData() - formData.append('file', file, filename) - formData.append('cid', '0') - const url = `${config.baseUrl}${config.urlPrefix}/upload/video` - const res = await fetch(url, { - method: 'POST', - headers: { - token: getToken() || '', - version: appStore.config.version || '' - }, - body: formData - }) - const json = await res.json() - if (json.code !== RequestCodeEnum.SUCCESS) { - throw new Error(json.msg || '上传失败') - } - return json.data as { id: number; uri: string; url: string } + const fileObj = + file instanceof File + ? file + : new File([file], filename, { type: file.type || 'video/webm' }) + return uploadMaterialFile(fileObj, 'video', 0) } export function fileCateAdd(params: Record) { diff --git a/admin/src/components/material/picker.vue b/admin/src/components/material/picker.vue index 728cdfb4..2b328bd3 100644 --- a/admin/src/components/material/picker.vue +++ b/admin/src/components/material/picker.vue @@ -9,7 +9,17 @@ @close="handleClose" >