import config from '@/config' import { RequestCodeEnum } from '@/enums/requestEnums' 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 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 fileObj = file instanceof File ? file : new File([file], filename, { type: file.type || 'video/webm' }) return uploadMaterialFile(fileObj, 'video', 0) } export function fileCateAdd(params: Record) { return request.post({ url: '/file/addCate', params }) } export function fileCateEdit(params: Record) { return request.post({ url: '/file/editCate', params }) } // 文件分类删除 export function fileCateDelete(params: Record) { return request.post({ url: '/file/delCate', params }) } // 文件分类列表 export function fileCateLists(params: Record) { return request.get({ url: '/file/listCate', params }) } // 文件列表 export function fileList(params: Record) { return request.get( { url: '/file/lists', params }, { ignoreCancelToken: true, isOpenRetry: false } ) } // 文件删除 export function fileDelete(params: Record) { return request.post({ url: '/file/delete', params }) } // 文件移动 export function fileMove(params: Record) { return request.post({ url: '/file/move', params }) } // 文件重命名 export function fileRename(params: { id: number; name: string }) { return request.post({ url: '/file/rename', params }) } /** 浏览器直传 OSS - 凭证类型 */ export interface OssCredentialsResponse { provider: string fallback: boolean bucket?: string region?: string host?: string cdn_domain?: string key_prefix?: string max_size?: number duration?: number expired_time?: number start_time?: number credentials?: { tmpSecretId: string tmpSecretKey: string sessionToken: string } } /** 申请 STS 临时凭证 */ export function getOssCredentials(params: { type: 'video' }) { return request.post({ url: '/upload/ossCredentials', params }) as Promise } /** 直传完成回执:写 file 表 + HEAD 校验 */ export function confirmOssUpload(params: { type: 'video' key: string name: string size: number content_type: string cid?: number }) { return request.post({ url: '/upload/ossConfirm', params }) as Promise<{ id: number cid: number type: number name: string uri: string url: string }> }