57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
|
|
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;
|