zy-react-library/src/hooks/useGetFile/index.js

126 lines
3.7 KiB
JavaScript
Raw Normal View History

2026-01-06 15:24:18 +08:00
import { request } from "@cqsjjb/jjb-common-lib/http.js";
import { useState } from "react";
import { UPLOAD_FILE_TYPE_ENUM } from "../../enum/uploadFile/index.d.ts";
import { addingPrefixToFile, getBaseGateway } from "../../utils";
/**
* 获取文件
*/
function useGetFile(returnType = "object") {
// loading状态
const [loading, setLoading] = useState(false);
// 用于跟踪进行中的请求数量(不暴露给外部)
let requestCount = 0;
// 获取文件
const getFile = (options) => {
if (!options) {
console.error("【getFile】 缺少 options 参数");
return;
}
// 增加请求数量并设置loading状态
requestCount++;
if (requestCount > 0) {
setLoading(true);
}
return new Promise((resolve, reject) => {
2026-01-21 08:49:31 +08:00
const { eqType, eqForeignKey, single = true, foreignKey, dataSource } = options;
if (!eqType) {
console.error("【getFile】 缺少 options.eqType 参数");
return;
}
// 检查eqType是否在UPLOAD_FILE_TYPE_ENUM中
const isValidEqType = Object.values(UPLOAD_FILE_TYPE_ENUM).includes(eqType);
if (!isValidEqType) {
console.error(`【getFile】 传入的 eqType 不在 UPLOAD_FILE_TYPE_ENUM 中,当前传入的 eqType 是 ${eqType}`);
return;
}
2026-01-21 08:49:31 +08:00
if (single) {
if (eqForeignKey === undefined || eqForeignKey === null || eqForeignKey === "") {
console.error("【getFile】 缺少 options.eqForeignKey 参数");
return;
}
}
else {
if (foreignKey === undefined || foreignKey === null || foreignKey === "") {
console.error("【getFile】 缺少 options.foreignKey 参数");
return;
}
if (dataSource === undefined || dataSource === null || dataSource === "") {
console.error("【getFile】 缺少 options.dataSource 参数");
return;
}
if (dataSource.length === 0) {
2026-02-03 09:33:52 +08:00
setLoading(false);
2026-01-21 08:49:31 +08:00
resolve(dataSource);
return;
}
}
2026-01-21 08:49:31 +08:00
const params = single ? { eqType, eqForeignKey } : { eqType, inForeignKey: dataSource.map(item => item[foreignKey]).join(",") };
// 发送请求
request(
`/${getBaseGateway()}/imgFiles/listAll`,
"get",
2026-01-21 08:49:31 +08:00
params,
)
.then((res) => {
2026-01-21 08:49:31 +08:00
if (single) {
resolve(addingPrefixToFile(res.data).map(item => ({ ...item, type: undefined })));
}
else {
// 创建一个 Map 来提高查找效率
const fileMap = new Map();
// 将 res.data 按照 foreigKey 分组
res.data.forEach((file) => {
if (!fileMap.has(file.foreignKey)) {
fileMap.set(file.foreignKey, []);
}
fileMap.get(file.foreignKey).push({ ...file, type: undefined });
});
// 为 dataSource 中的每一项添加对应的文件列表
const result = dataSource.map((item) => {
const itemForeignKey = item[foreignKey];
const matchedFiles = fileMap.has(itemForeignKey)
? addingPrefixToFile(fileMap.get(itemForeignKey))
: [];
return {
...item,
files: matchedFiles,
};
});
resolve(result);
}
})
.catch((err) => {
reject(err);
})
.finally(() => {
// 减少请求数量并在没有进行中的请求时设置loading为false
requestCount--;
if (requestCount <= 0) {
setLoading(false);
}
});
});
};
if (returnType === "array")
return [loading, getFile];
return { loading, getFile };
}
export default useGetFile;