From 9d64fd33ea2feff99c38c1900a212677a006a08a Mon Sep 17 00:00:00 2001 From: LiuJiaNan <15703339975@163.com> Date: Sat, 1 Nov 2025 17:58:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84useUploadFile=E3=80=81useDele?= =?UTF-8?q?teFile=E3=80=81useGetFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- enum/uploadFile/index.js | 131 ++++++++++++++++++++++++++++ todo/hooks/useDeleteFile/index.d.ts | 11 ++- todo/hooks/useDeleteFile/index.js | 23 +++-- todo/hooks/useGetFile/index.d.ts | 29 ++++++ todo/hooks/useGetFile/index.js | 56 ++++++++++++ todo/hooks/useUploadFile/index.d.ts | 23 ++++- todo/hooks/useUploadFile/index.js | 57 +++++++++--- utils/index.d.ts | 8 +- utils/index.js | 4 +- 9 files changed, 309 insertions(+), 33 deletions(-) create mode 100644 enum/uploadFile/index.js create mode 100644 todo/hooks/useGetFile/index.d.ts create mode 100644 todo/hooks/useGetFile/index.js diff --git a/enum/uploadFile/index.js b/enum/uploadFile/index.js new file mode 100644 index 0000000..e950dce --- /dev/null +++ b/enum/uploadFile/index.js @@ -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", +}; diff --git a/todo/hooks/useDeleteFile/index.d.ts b/todo/hooks/useDeleteFile/index.d.ts index f5854f2..92d6eb1 100644 --- a/todo/hooks/useDeleteFile/index.d.ts +++ b/todo/hooks/useDeleteFile/index.d.ts @@ -1,14 +1,16 @@ export interface DeleteFileItem { + /** 文件URL */ + filePath?: string; /** 文件ID */ - id: string; + id?: string; [key: string]: any; } export interface DeleteFileOptions { /** 要删除的文件列表 */ files: DeleteFileItem[]; - /** 额外的参数 */ - params?: Record; + /** 是否单文件删除 */ + single?: boolean; } export type DeleteFileFunction = (options: DeleteFileOptions) => Promise; @@ -16,4 +18,5 @@ export type DeleteFileFunction = (options: DeleteFileOptions) => Promise; /** * 删除文件 */ -export default function useDeleteFile(): [boolean, DeleteFileFunction]; +export default function useDeleteFile(returnType: "array"): [boolean, DeleteFileFunction]; +export default function useDeleteFile(returnType?: "object"): { loading: boolean; deleteFile: DeleteFileFunction }; diff --git a/todo/hooks/useDeleteFile/index.js b/todo/hooks/useDeleteFile/index.js index 6d4ba8a..344ce62 100644 --- a/todo/hooks/useDeleteFile/index.js +++ b/todo/hooks/useDeleteFile/index.js @@ -4,25 +4,28 @@ import { useState } from "react"; /** * 删除文件 TODO */ -function useDeleteFile() { +function useDeleteFile(returnType = "object") { // loading状态 const [loading, setLoading] = useState(false); // 删除文件 const deleteFile = (options) => { + if (!options) + throw new Error("请传入 options"); + setLoading(true); return new Promise((resolve, reject) => { - const { files = [], params = {} } = options; + const { files, single = true } = options; - // 构建参数 - const actualParams = { - id: files.map(file => file.id), - ...params, - }; + if (!files) + throw new Error("请传入 files"); // 发送请求 - request("", "post", actualParams) + request( + single ? `/basic-info/imgFiles/${files[0].filePath}` : `/basic-info/imgFiles/ids?ids=${files.map(f => f.id)}`, + "delete", + ) .then((res) => { resolve(res.data); }) @@ -35,7 +38,9 @@ function useDeleteFile() { }); }; - return [loading, deleteFile]; + if (returnType === "array") + return [loading, deleteFile]; + return { loading, deleteFile }; } export default useDeleteFile; diff --git a/todo/hooks/useGetFile/index.d.ts b/todo/hooks/useGetFile/index.d.ts new file mode 100644 index 0000000..770c5d3 --- /dev/null +++ b/todo/hooks/useGetFile/index.d.ts @@ -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; + +/** + * 获取文件 + */ +export default function useGetFile(returnType: "array"): [boolean, GetFileFunction]; +export default function useGetFile(returnType?: "object"): { loading: boolean; getFile: GetFileFunction }; diff --git a/todo/hooks/useGetFile/index.js b/todo/hooks/useGetFile/index.js new file mode 100644 index 0000000..587472c --- /dev/null +++ b/todo/hooks/useGetFile/index.js @@ -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; diff --git a/todo/hooks/useUploadFile/index.d.ts b/todo/hooks/useUploadFile/index.d.ts index 6537158..80b0500 100644 --- a/todo/hooks/useUploadFile/index.d.ts +++ b/todo/hooks/useUploadFile/index.d.ts @@ -1,6 +1,22 @@ export interface UploadFile { /** 原始文件对象 */ 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; } @@ -10,7 +26,7 @@ export interface SingleUploadFileOptions { /** 是否单文件上传 */ single?: true; /** 上传的参数 */ - params?: Record; + params: Params; } export interface MultipleUploadFileOptions { @@ -19,7 +35,7 @@ export interface MultipleUploadFileOptions { /** 是否单文件上传 */ single: false; /** 上传的参数 */ - params?: Record; + params: Params; } 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 }; diff --git a/todo/hooks/useUploadFile/index.js b/todo/hooks/useUploadFile/index.js index 4294065..41c0d7c 100644 --- a/todo/hooks/useUploadFile/index.js +++ b/todo/hooks/useUploadFile/index.js @@ -1,19 +1,47 @@ import { request } from "@cqsjjb/jjb-common-lib/http"; import { useState } from "react"; +import { UPLOAD_FILE_PATH_ENUM, UPLOAD_FILE_TYPE_ENUM } from "../../enum/uploadFile"; /** * 上传文件 TODO */ -function useUploadFile() { +function useUploadFile(returnType = "object") { // loading状态 const [loading, setLoading] = useState(false); // 上传文件 const uploadFile = (options) => { + if (!options) + throw new Error("请传入 options"); + 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 (!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(); // 将文件添加到formData中 @@ -26,6 +54,9 @@ function useUploadFile() { formData.append(key, params[key]); }); + // 添加path参数 + formData.append("path", path); + // 获取待上传的文件 const filesLength = formData.getAll("files").length; @@ -34,22 +65,24 @@ function useUploadFile() { setLoading(false); resolve( single - ? files?.[0]?.url || "" - : { files: files.map(f => f.url), id: "" }, + ? { filePath: files[0].filePath } + : { id: params.foreignKey }, ); 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) => { resolve( single - ? res.data.url - : { - files: [...res.data.files.map(f => f.url), ...files.filter(f => !f.originFileObj).map(f => f.url)], - id: res.data.id, - }, + ? { filePath: res.data.filePath } + : { id: res.data.foreignKey }, ); }) .catch((err) => { @@ -61,7 +94,9 @@ function useUploadFile() { }); }; - return [loading, uploadFile]; + if (returnType === "array") + return [loading, uploadFile]; + return { loading, uploadFile }; } export default useUploadFile; diff --git a/utils/index.d.ts b/utils/index.d.ts index 7de4653..fc2a7cc 100644 --- a/utils/index.d.ts +++ b/utils/index.d.ts @@ -128,7 +128,7 @@ interface IsEmptyToWhetherOptions { */ export function serialNumber( pagination: BasePaginationConfig, - index: number + index: number, ): number; /** @@ -226,8 +226,8 @@ export function secondConversion(second: string | number): string; */ export function addingPrefixToFile>( list: T[], - options?: AddingPrefixToFileOptions -): (T & { url: string; name: string; imgFilesId: any })[]; + options?: AddingPrefixToFileOptions, +): (T & { url: string; name: string; id: any })[]; /** * 翻译状态 @@ -259,7 +259,7 @@ export function listTransTree(options: ListTransTreeOptions): T[]; */ export function isEmptyToWhether( value: any, - options?: IsEmptyToWhetherOptions + options?: IsEmptyToWhetherOptions, ): string; /** diff --git a/utils/index.js b/utils/index.js index 91fe2a7..aef6506 100644 --- a/utils/index.js +++ b/utils/index.js @@ -258,13 +258,13 @@ export function addingPrefixToFile(list, options = {}) { const { pathKey = "filePath", nameKey = "fileName", - idKey = "imgFilesId", + idKey = "id", } = options; const FILE_URL = getFileUrl(); for (let i = 0; i < list.length; i++) { list[i].url = FILE_URL + 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; }