yintai-company-home-am/api/common.ts

101 lines
2.3 KiB
TypeScript

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 }) {
return Request({
url: "yt/translate",
method: "get",
params,
})
},
}
// 分类
export const categoryApi = {
getDataList(params: any) {
return Request({
url: 'yt/category',
method: 'get',
params
})
},
updateData(params: any) {
return Request({
url: 'yt/category',
method: 'put',
data: params
})
},
deleteData(params: any) {
return Request({
url: 'yt/category',
method: 'delete',
params: params
})
},
addData(params: any) {
return Request({
url: 'yt/category',
method: 'post',
data: params
})
},
}
// 上传
export const uploadApi = {
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',
data: params,
params: {
expire: 86400 * 24 * 30 * 12 * 10,
},
headers: {
"Content-Type": "multipart/form-data",
},
}).then(res => {
return {data: {url: res.data}}
})
},
}