2026-01-06 15:24:18 +08:00
|
|
|
import { request } from "@cqsjjb/jjb-common-lib/http.js";
|
2025-11-01 17:58:18 +08:00
|
|
|
import { useState } from "react";
|
2026-01-21 09:44:32 +08:00
|
|
|
import { UPLOAD_FILE_TYPE_ENUM } from "../../enum/uploadFile/gwj";
|
2026-06-16 10:56:04 +08:00
|
|
|
import { addingPrefixToFile, getBaseGateway } from "../../utils";
|
2025-11-01 17:58:18 +08:00
|
|
|
|
|
|
|
|
/**
|
2025-11-03 16:50:34 +08:00
|
|
|
* 获取文件
|
2025-11-01 17:58:18 +08:00
|
|
|
*/
|
|
|
|
|
function useGetFile(returnType = "object") {
|
|
|
|
|
// loading状态
|
|
|
|
|
const [loading, setLoading] = useState(false);
|
2025-11-12 15:11:36 +08:00
|
|
|
// 用于跟踪进行中的请求数量(不暴露给外部)
|
|
|
|
|
let requestCount = 0;
|
2025-11-01 17:58:18 +08:00
|
|
|
|
|
|
|
|
// 获取文件
|
|
|
|
|
const getFile = (options) => {
|
2025-12-20 16:42:46 +08:00
|
|
|
if (!options) {
|
2026-01-13 09:05:08 +08:00
|
|
|
console.error("【getFile】 缺少 options 参数");
|
2025-12-20 16:42:46 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2025-11-01 17:58:18 +08:00
|
|
|
|
2025-11-12 15:11:36 +08:00
|
|
|
// 增加请求数量并设置loading状态
|
|
|
|
|
requestCount++;
|
|
|
|
|
if (requestCount > 0) {
|
|
|
|
|
setLoading(true);
|
|
|
|
|
}
|
2025-11-01 17:58:18 +08:00
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
2026-01-21 08:49:31 +08:00
|
|
|
const { eqType, eqForeignKey, single = true, foreignKey, dataSource } = options;
|
2025-11-01 17:58:18 +08:00
|
|
|
|
2025-12-20 16:42:46 +08:00
|
|
|
if (!eqType) {
|
2026-01-13 09:05:08 +08:00
|
|
|
console.error("【getFile】 缺少 options.eqType 参数");
|
2025-12-20 16:42:46 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2025-11-01 17:58:18 +08:00
|
|
|
|
|
|
|
|
// 检查eqType是否在UPLOAD_FILE_TYPE_ENUM中
|
2025-12-20 16:42:46 +08:00
|
|
|
if (!Object.values(UPLOAD_FILE_TYPE_ENUM).includes(eqType)) {
|
2026-01-13 09:05:08 +08:00
|
|
|
console.error(`【getFile】 传入的 eqType 不在 UPLOAD_FILE_TYPE_ENUM 中,当前传入的 eqType 是 ${eqType}`);
|
2025-12-20 16:42:46 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2025-11-01 17:58:18 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2025-12-20 16:42:46 +08:00
|
|
|
}
|
2025-11-01 17:58:18 +08:00
|
|
|
|
2026-01-21 08:49:31 +08:00
|
|
|
const params = single ? { eqType, eqForeignKey } : { eqType, inForeignKey: dataSource.map(item => item[foreignKey]).join(",") };
|
|
|
|
|
|
2025-11-01 17:58:18 +08:00
|
|
|
// 发送请求
|
|
|
|
|
request(
|
2026-06-16 10:56:04 +08:00
|
|
|
`/${getBaseGateway()}/imgFiles/listAll`,
|
2025-11-01 17:58:18 +08:00
|
|
|
"get",
|
2026-01-21 08:49:31 +08:00
|
|
|
params,
|
2025-11-01 17:58:18 +08:00
|
|
|
)
|
|
|
|
|
.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);
|
|
|
|
|
}
|
2025-11-01 17:58:18 +08:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
reject(err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
2025-11-12 15:11:36 +08:00
|
|
|
// 减少请求数量并在没有进行中的请求时设置loading为false
|
|
|
|
|
requestCount--;
|
|
|
|
|
if (requestCount <= 0) {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
}
|
2025-11-01 17:58:18 +08:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (returnType === "array")
|
|
|
|
|
return [loading, getFile];
|
|
|
|
|
return { loading, getFile };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default useGetFile;
|