From f2f3f99e8f9336b2d6b1110a5ab45b2138a27d47 Mon Sep 17 00:00:00 2001 From: zhangjianjun Date: Thu, 16 Apr 2026 18:32:55 +0800 Subject: [PATCH] update:qiniu --- api/common.ts | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/api/common.ts b/api/common.ts index 23c97ca..037776a 100644 --- a/api/common.ts +++ b/api/common.ts @@ -1,5 +1,23 @@ +import { qiniuUpload } from "lib/utils/qiniu"; import Request from "lib/utils/requests"; +const LARGE_FILE_SIZE = 20 * 1024 * 1024; + +const getFileFromFormData = (formData: FormData) => { + const directFile = formData.get("file"); + if (directFile instanceof File) { + return directFile; + } + + for (const [, value] of formData.entries()) { + if (value instanceof File) { + return value; + } + } + + return null; +}; + // 翻译 export const translateApi = { translate(params: { text: string, from?: string, to?: string }) { @@ -44,9 +62,25 @@ export const categoryApi = { } + // 上传 export const uploadApi = { - upload(params: any) { + getUploadToken() { + return Request({ + url: "yt/upload/token", + method: "get", + }) + }, + async upload(params: any) { + const file = params instanceof FormData ? getFileFromFormData(params) : null; + if (file && file.size > LARGE_FILE_SIZE) { + const res = await uploadApi.getUploadToken() + const token = res.data?.token || "" + return qiniuUpload(file, token).then(res => { + return { data: { url: res.url } }; + }) + } + return Request({ url: 'yt/upload', method: 'post', @@ -61,5 +95,6 @@ export const uploadApi = { return {data: {url: res.data}} }) }, + }