2025-11-01 17:58:18 +08:00
|
|
|
import { request } from "@cqsjjb/jjb-common-lib/http";
|
|
|
|
|
import { useState } from "react";
|
2025-11-03 16:50:34 +08:00
|
|
|
import { UPLOAD_FILE_TYPE_ENUM } from "../../enum/uploadFile/gwj";
|
2025-11-01 17:58:18 +08:00
|
|
|
import { addingPrefixToFile } from "../../utils";
|
|
|
|
|
|
|
|
|
|
/**
|
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);
|
|
|
|
|
|
|
|
|
|
// 获取文件
|
|
|
|
|
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 中");
|
|
|
|
|
|
2025-11-04 10:07:18 +08:00
|
|
|
if (eqForeignKey === undefined || eqForeignKey === null)
|
2025-11-01 17:58:18 +08:00
|
|
|
throw new Error("请传入 options.eqForeignKey");
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
request(
|
|
|
|
|
"/basic-info/imgFiles/listAll",
|
|
|
|
|
"get",
|
|
|
|
|
{ eqType, eqForeignKey },
|
|
|
|
|
)
|
|
|
|
|
.then((res) => {
|
2025-11-06 17:50:04 +08:00
|
|
|
resolve(addingPrefixToFile(res.data).map(item => ({ ...item, type: item.type.toString() })));
|
2025-11-01 17:58:18 +08:00
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
reject(err);
|
|
|
|
|
})
|
|
|
|
|
.finally(() => {
|
|
|
|
|
setLoading(false);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (returnType === "array")
|
|
|
|
|
return [loading, getFile];
|
|
|
|
|
return { loading, getFile };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default useGetFile;
|