完善useUploadFile、useDeleteFile、useGetFile

master
LiuJiaNan 2025-11-01 17:58:18 +08:00
parent 72ae11aec3
commit 9d64fd33ea
9 changed files with 309 additions and 33 deletions

131
enum/uploadFile/index.js Normal file
View File

@ -0,0 +1,131 @@
/**
* 文件上传类型枚举
*/
export const UPLOAD_FILE_TYPE_ENUM = {
10: 10,
11: 11,
12: 12,
13: 13,
14: 14,
15: 15,
16: 16,
19: 19,
601: 601,
602: 602,
603: 603,
604: 604,
605: 605,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
17: 17,
18: 18,
20: 20,
21: 21,
22: 22,
23: 23,
50: 50,
101: 101,
102: 102,
105: 105,
106: 106,
107: 107,
108: 108,
109: 109,
110: 110,
111: 111,
112: 112,
113: 113,
114: 114,
115: 115,
116: 116,
117: 117,
118: 118,
119: 119,
120: 120,
121: 121,
122: 122,
123: 123,
124: 124,
125: 125,
126: 126,
127: 127,
128: 128,
129: 129,
130: 130,
131: 131,
132: 132,
133: 133,
134: 134,
135: 135,
};
/**
* 文件上传类型对应的 path 枚举
*/
export const UPLOAD_FILE_PATH_ENUM = {
10: "labor_contract_image",
11: "commercial_insurance_image",
12: "certificate_information",
13: "user_avatar",
14: "id_card_photo",
15: "social_security_card_photo",
16: "work_related_injury_insurance_certificate",
19: "three_level_safety_education_training",
601: "gate_access_vehicle_license_photo",
602: "gate_access_vehicle_photo",
603: "gate_access_vehicle_attachment",
604: "emission_standard_certificate",
605: "motor_vehicle_registration_certificate_green_book",
3: "hidden_danger_photo",
4: "hidden_danger_rectification_photo",
5: "hidden_danger_verification_photo",
6: "certificate_photo",
7: "confined_space_plan",
8: "hidden_danger_rectification_plan",
9: "confined_space_confirmation_signature",
17: "special_equipment_inspection_photo",
18: "personnel_certificate",
20: "major_hazard_source_alarm_before_handling_photo",
21: "major_hazard_source_alarm_after_handling_photo",
22: "smart_access_control_external_vehicle_driver_license_photo",
23: "smart_access_control_external_vehicle_registration_photo",
50: "safety_and_environmental_inspection_final_acceptance_image",
101: "hidden_danger_extension_temporary_measures_attachment",
102: "hidden_danger_video",
105: "blind_plate_position_map",
106: "temporary_disposal_information",
107: "rectification_suggestions_and_plan",
108: "major_hidden_danger_investigation_report",
109: "major_hidden_danger_safety_committee_or_party_committee_resolution_record",
110: "significant_hidden_danger_rectification_temporary_disposal_measures",
111: "significant_hidden_danger_rectification_hidden_danger_rectification_process_record",
112: "supplement_major_hidden_danger_information",
113: "safety_committee_office_meeting_record",
114: "alarm_photo",
115: "fire_equipment_type_qualification_photo",
116: "hot_work_personnel_photo",
117: "safety_pledge_signature",
118: "hot_work_unit_responsible_person_confirmation_signature",
119: "on_site_jurisdiction_unit_responsible_person_signature",
120: "hot_work_permit_issuing_unit_signature",
121: "hot_work_permit_signature",
122: "pre_hot_work_jurisdiction_unit_confirmation_signature",
123: "on_site_responsible_person_confirmation_signature",
124: "post_hot_work_site_jurisdiction_unit_confirmation",
125: "delayed_fire_monitoring_pictures",
126: "safety_and_environmental_inspection_initiation_signature",
127: "inspector_confirmation_signature",
128: "inspected_person_confirmation_signature",
129: "safety_and_environmental_inspection_appeal_signature",
130: "hk_safety_committee_office_director_safety_director_issuance",
131: "hot_work_contracting_unit_signature",
132: "safety_director_approval",
133: "branch_safety_director_approval",
134: "project_authority_review_signature",
135: "branch_manager_approval_signature",
};

View File

@ -1,14 +1,16 @@
export interface DeleteFileItem { export interface DeleteFileItem {
/** 文件URL */
filePath?: string;
/** 文件ID */ /** 文件ID */
id: string; id?: string;
[key: string]: any; [key: string]: any;
} }
export interface DeleteFileOptions { export interface DeleteFileOptions {
/** 要删除的文件列表 */ /** 要删除的文件列表 */
files: DeleteFileItem[]; files: DeleteFileItem[];
/** 额外的参数 */ /** 是否单文件删除 */
params?: Record<string, any>; single?: boolean;
} }
export type DeleteFileFunction = (options: DeleteFileOptions) => Promise<any>; export type DeleteFileFunction = (options: DeleteFileOptions) => Promise<any>;
@ -16,4 +18,5 @@ export type DeleteFileFunction = (options: DeleteFileOptions) => Promise<any>;
/** /**
* *
*/ */
export default function useDeleteFile(): [boolean, DeleteFileFunction]; export default function useDeleteFile(returnType: "array"): [boolean, DeleteFileFunction];
export default function useDeleteFile(returnType?: "object"): { loading: boolean; deleteFile: DeleteFileFunction };

View File

@ -4,25 +4,28 @@ import { useState } from "react";
/** /**
* 删除文件 TODO * 删除文件 TODO
*/ */
function useDeleteFile() { function useDeleteFile(returnType = "object") {
// loading状态 // loading状态
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
// 删除文件 // 删除文件
const deleteFile = (options) => { const deleteFile = (options) => {
if (!options)
throw new Error("请传入 options");
setLoading(true); setLoading(true);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const { files = [], params = {} } = options; const { files, single = true } = options;
// 构建参数 if (!files)
const actualParams = { throw new Error("请传入 files");
id: files.map(file => file.id),
...params,
};
// 发送请求 // 发送请求
request("", "post", actualParams) request(
single ? `/basic-info/imgFiles/${files[0].filePath}` : `/basic-info/imgFiles/ids?ids=${files.map(f => f.id)}`,
"delete",
)
.then((res) => { .then((res) => {
resolve(res.data); resolve(res.data);
}) })
@ -35,7 +38,9 @@ function useDeleteFile() {
}); });
}; };
return [loading, deleteFile]; if (returnType === "array")
return [loading, deleteFile];
return { loading, deleteFile };
} }
export default useDeleteFile; export default useDeleteFile;

29
todo/hooks/useGetFile/index.d.ts vendored Normal file
View File

@ -0,0 +1,29 @@
export interface GetFileOptions {
/** 文件类型 */
eqType: number;
/** 外键ID */
eqForeignKey: string;
[key: string]: any;
}
export interface FileItem {
/** 文件路径 */
filePath: string;
/** 文件名称 */
fileName: string;
/** 文件ID */
id: string;
/** 文件URL(由addingPrefixToFile函数添加) */
url: string;
/** 文件名称(由addingPrefixToFile函数添加) */
name: string;
[key: string]: any;
}
export type GetFileFunction = (options: GetFileOptions) => Promise<FileItem[]>;
/**
*
*/
export default function useGetFile(returnType: "array"): [boolean, GetFileFunction];
export default function useGetFile(returnType?: "object"): { loading: boolean; getFile: GetFileFunction };

View File

@ -0,0 +1,56 @@
import { request } from "@cqsjjb/jjb-common-lib/http";
import { useState } from "react";
import { UPLOAD_FILE_TYPE_ENUM } from "../../enum/uploadFile";
import { addingPrefixToFile } from "../../utils";
/**
* 获取文件 TODO
*/
function useGetFile(returnType = "object") {
// loading状态
const [loading, setLoading] = useState(false);
// 获取文件
const getFile = (options) => {
if (!options)
throw new Error("请传入 options");
setLoading(true);
return new Promise((resolve, reject) => {
const { eqType, eqForeignKey } = options;
if (!eqType)
throw new Error("请传入 options.eqType");
// 检查eqType是否在UPLOAD_FILE_TYPE_ENUM中
if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(eqType))
throw new Error("传入的 eqType 不在 UPLOAD_FILE_TYPE_ENUM 中");
if (!eqForeignKey)
throw new Error("请传入 options.eqForeignKey");
// 发送请求
request(
"/basic-info/imgFiles/listAll",
"get",
{ eqType, eqForeignKey },
)
.then((res) => {
resolve(addingPrefixToFile(res.data));
})
.catch((err) => {
reject(err);
})
.finally(() => {
setLoading(false);
});
});
};
if (returnType === "array")
return [loading, getFile];
return { loading, getFile };
}
export default useGetFile;

View File

@ -1,6 +1,22 @@
export interface UploadFile { export interface UploadFile {
/** 原始文件对象 */ /** 原始文件对象 */
originFileObj?: File; originFileObj?: File;
/** 文件URL */
filePath?: string;
/** 文件ID */
id?: string;
[key: string]: any;
}
export interface Params {
/** 文件类型 */
type: number;
/** 所属端 */
path: string;
/** 企业id */
corpinfoId: string;
/** 外键id */
foreignKey: string;
[key: string]: any; [key: string]: any;
} }
@ -10,7 +26,7 @@ export interface SingleUploadFileOptions {
/** 是否单文件上传 */ /** 是否单文件上传 */
single?: true; single?: true;
/** 上传的参数 */ /** 上传的参数 */
params?: Record<string, string | number>; params: Params;
} }
export interface MultipleUploadFileOptions { export interface MultipleUploadFileOptions {
@ -19,7 +35,7 @@ export interface MultipleUploadFileOptions {
/** 是否单文件上传 */ /** 是否单文件上传 */
single: false; single: false;
/** 上传的参数 */ /** 上传的参数 */
params?: Record<string, string | number>; params: Params;
} }
export interface MultipleFileResponse { export interface MultipleFileResponse {
@ -38,4 +54,5 @@ export interface UploadFileFunction {
/** /**
* *
*/ */
export default function useUploadFile(): [boolean, UploadFileFunction]; export default function useUploadFile(returnType: "array"): [boolean, UploadFileFunction];
export default function useUploadFile(returnType?: "object"): { loading: boolean; uploadFile: UploadFileFunction };

View File

@ -1,19 +1,47 @@
import { request } from "@cqsjjb/jjb-common-lib/http"; import { request } from "@cqsjjb/jjb-common-lib/http";
import { useState } from "react"; import { useState } from "react";
import { UPLOAD_FILE_PATH_ENUM, UPLOAD_FILE_TYPE_ENUM } from "../../enum/uploadFile";
/** /**
* 上传文件 TODO * 上传文件 TODO
*/ */
function useUploadFile() { function useUploadFile(returnType = "object") {
// loading状态 // loading状态
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
// 上传文件 // 上传文件
const uploadFile = (options) => { const uploadFile = (options) => {
if (!options)
throw new Error("请传入 options");
setLoading(true); setLoading(true);
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const { files = [], single = true, params = {} } = options; const { files, single = true, params } = options;
if (!files)
throw new Error("请传入 files");
if (!params)
throw new Error("请传入 options.params");
if (!params.type)
throw new Error("请传入 options.params.type");
// 检查type是否在UPLOAD_FILE_TYPE_ENUM中
if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(params.type))
throw new Error("传入的 type 不在 UPLOAD_FILE_TYPE_ENUM 中");
// 根据type获取对应的path
const path = UPLOAD_FILE_PATH_ENUM[params.type];
if (!path)
throw new Error(`未找到 type ${params.type} 对应的 path `);
// if (!params.path)
// throw new Error("请传入 options.params.path");
if (params.corpinfoId === undefined || params.corpinfoId === null)
throw new Error("请传入 options.params.corpinfoId");
if (params.foreignKey === undefined || params.foreignKey === null)
throw new Error("请传入 options.params.foreignKey");
const formData = new FormData(); const formData = new FormData();
// 将文件添加到formData中 // 将文件添加到formData中
@ -26,6 +54,9 @@ function useUploadFile() {
formData.append(key, params[key]); formData.append(key, params[key]);
}); });
// 添加path参数
formData.append("path", path);
// 获取待上传的文件 // 获取待上传的文件
const filesLength = formData.getAll("files").length; const filesLength = formData.getAll("files").length;
@ -34,22 +65,24 @@ function useUploadFile() {
setLoading(false); setLoading(false);
resolve( resolve(
single single
? files?.[0]?.url || "" ? { filePath: files[0].filePath }
: { files: files.map(f => f.url), id: "" }, : { id: params.foreignKey },
); );
return; return;
} }
// 发送请求 // 发送请求
request(single ? "单文件上传的接口" : "多文件上传的接口", "post", formData, { "Content-Type": "multipart/form-data" }) request(
single ? "/basic-info/imgFiles/save" : "/basic-info/imgFiles/batchSave",
"post",
formData,
{ "Content-Type": "multipart/form-data" },
)
.then((res) => { .then((res) => {
resolve( resolve(
single single
? res.data.url ? { filePath: res.data.filePath }
: { : { id: res.data.foreignKey },
files: [...res.data.files.map(f => f.url), ...files.filter(f => !f.originFileObj).map(f => f.url)],
id: res.data.id,
},
); );
}) })
.catch((err) => { .catch((err) => {
@ -61,7 +94,9 @@ function useUploadFile() {
}); });
}; };
return [loading, uploadFile]; if (returnType === "array")
return [loading, uploadFile];
return { loading, uploadFile };
} }
export default useUploadFile; export default useUploadFile;

8
utils/index.d.ts vendored
View File

@ -128,7 +128,7 @@ interface IsEmptyToWhetherOptions {
*/ */
export function serialNumber( export function serialNumber(
pagination: BasePaginationConfig, pagination: BasePaginationConfig,
index: number index: number,
): number; ): number;
/** /**
@ -226,8 +226,8 @@ export function secondConversion(second: string | number): string;
*/ */
export function addingPrefixToFile<T extends Record<string, any>>( export function addingPrefixToFile<T extends Record<string, any>>(
list: T[], list: T[],
options?: AddingPrefixToFileOptions options?: AddingPrefixToFileOptions,
): (T & { url: string; name: string; imgFilesId: any })[]; ): (T & { url: string; name: string; id: any })[];
/** /**
* *
@ -259,7 +259,7 @@ export function listTransTree<T>(options: ListTransTreeOptions): T[];
*/ */
export function isEmptyToWhether( export function isEmptyToWhether(
value: any, value: any,
options?: IsEmptyToWhetherOptions options?: IsEmptyToWhetherOptions,
): string; ): string;
/** /**

View File

@ -258,13 +258,13 @@ export function addingPrefixToFile(list, options = {}) {
const { const {
pathKey = "filePath", pathKey = "filePath",
nameKey = "fileName", nameKey = "fileName",
idKey = "imgFilesId", idKey = "id",
} = options; } = options;
const FILE_URL = getFileUrl(); const FILE_URL = getFileUrl();
for (let i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
list[i].url = FILE_URL + list[i][pathKey]; list[i].url = FILE_URL + list[i][pathKey];
list[i].name = list[i][nameKey] || getFileName(list[i][pathKey]); list[i].name = list[i][nameKey] || getFileName(list[i][pathKey]);
list[i].imgFilesId = list[i][idKey]; list[i].id = list[i][idKey];
} }
return list; return list;
} }