优化useUploadFile、useDeleteFile、useGetFile
parent
816bb2d071
commit
8845deefb2
|
|
@ -16,10 +16,16 @@ function useDeleteFile(returnType = "object") {
|
|||
setLoading(true);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const { files, single = true } = options;
|
||||
const { files = [], single = true } = options;
|
||||
|
||||
if (!files)
|
||||
throw new Error("请传入 files");
|
||||
if (!Array.isArray(files))
|
||||
throw new Error("请传入有效的 files");
|
||||
|
||||
// 如果没有文件则直接返回
|
||||
if (files.length === 0)
|
||||
resolve();
|
||||
|
||||
// 发送请求
|
||||
request(
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ export interface GetFileOptions {
|
|||
eqType: number;
|
||||
/** 外键ID */
|
||||
eqForeignKey: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface FileItem {
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ function useGetFile(returnType = "object") {
|
|||
if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(eqType))
|
||||
throw new Error("传入的 eqType 不在 UPLOAD_FILE_TYPE_ENUM 中");
|
||||
|
||||
if (!eqForeignKey)
|
||||
if (eqForeignKey === undefined || eqForeignKey === null)
|
||||
throw new Error("请传入 options.eqForeignKey");
|
||||
|
||||
// 发送请求
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@ export interface BaseParams {
|
|||
type: number;
|
||||
/** 企业id */
|
||||
corpinfoId?: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface SingleParams extends BaseParams {
|
||||
|
|
@ -44,16 +43,20 @@ export interface MultipleUploadFileOptions {
|
|||
params: MultipleParams;
|
||||
}
|
||||
|
||||
export interface SingleFileResponse {
|
||||
/** 文件路径 */
|
||||
filePath: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface MultipleFileResponse {
|
||||
/** 文件列表 */
|
||||
files: string[];
|
||||
/** 文件ID */
|
||||
id: string;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
export interface UploadFileFunction {
|
||||
(options: SingleUploadFileOptions): Promise<string>;
|
||||
(options: SingleUploadFileOptions): Promise<SingleFileResponse>;
|
||||
(options: MultipleUploadFileOptions): Promise<MultipleFileResponse>;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,10 +17,12 @@ function useUploadFile(returnType = "object") {
|
|||
setLoading(true);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const { files, single = true, params } = options;
|
||||
const { files = [], single = true, params } = options;
|
||||
|
||||
if (!files)
|
||||
throw new Error("请传入 files");
|
||||
if (!Array.isArray(files))
|
||||
throw new Error("请传入有效的 files");
|
||||
if (!params)
|
||||
throw new Error("请传入 options.params");
|
||||
if (!params.type)
|
||||
|
|
@ -39,6 +41,14 @@ function useUploadFile(returnType = "object") {
|
|||
if (!single && (params.foreignKey === undefined || params.foreignKey === null))
|
||||
throw new Error("请传入 options.params.foreignKey");
|
||||
|
||||
// 如果没有文件则直接返回
|
||||
if (files.length === 0)
|
||||
resolve(
|
||||
single
|
||||
? { filePath: '' }
|
||||
: { id: '' },
|
||||
);
|
||||
|
||||
const formData = new FormData();
|
||||
|
||||
// 将文件添加到formData中
|
||||
|
|
|
|||
Loading…
Reference in New Issue