zy-react-library/src/components/SelectTree/Department/Gwj/index.js

84 lines
2.3 KiB
JavaScript
Raw Normal View History

2026-01-06 15:24:18 +08:00
import { request } from "@cqsjjb/jjb-common-lib/http.js";
import { useEffect, useState } from "react";
import BasicSelectTree from "../../Basic";
2026-01-19 11:04:24 +08:00
// 全局缓存
const cacheMap = new Map();
/**
* 部门下拉树组件港务局版本
*/
function DepartmentSelectTree(props) {
const {
params = {},
2025-11-06 09:38:23 +08:00
placeholder = "部门",
isNeedCorpInfoId = false,
isNeedParentId = false,
searchType = "current",
...restProps
} = props;
2025-11-04 17:41:15 +08:00
const [treeData, setTreeData] = useState([]);
const getData = async () => {
2026-01-19 11:04:24 +08:00
// 生成缓存键
const paramsStr = JSON.stringify(params);
const cacheKey = `${searchType}_${paramsStr}`;
// 检查缓存,如果存在直接返回缓存结果
if (cacheMap.has(cacheKey)) {
setTreeData(cacheMap.get(cacheKey));
return;
}
2025-11-06 09:38:23 +08:00
setTreeData([]);
2025-11-19 17:40:49 +08:00
if (searchType === "current") {
if (isNeedCorpInfoId && !params.eqCorpinfoId)
return;
if (isNeedParentId && !params.eqParentId)
return;
}
2025-11-06 09:38:23 +08:00
2026-01-07 15:35:29 +08:00
let requestUrl = "";
2026-01-19 11:04:24 +08:00
const actualParams = { ...params, time: new Date().getTime() };
2026-01-16 10:16:27 +08:00
if (searchType === "current") {
2026-01-07 15:35:29 +08:00
requestUrl = "/basicInfo/department/listTree";
2026-01-16 10:16:27 +08:00
actualParams.inType = undefined;
actualParams.enterpriseType = undefined;
}
if (searchType === "all") {
2026-01-07 15:35:29 +08:00
requestUrl = "/basicInfo/department/listAllTree";
2026-01-16 10:16:27 +08:00
actualParams.inType = undefined;
actualParams.enterpriseType = undefined;
}
if (searchType === "inType") {
2026-01-07 15:35:29 +08:00
requestUrl = "/basicInfo/department/listAllTreeByCorpType";
2026-01-16 10:16:27 +08:00
actualParams.eqCorpinfoId = undefined;
actualParams.eqParentId = undefined;
2026-01-22 16:24:50 +08:00
if (actualParams.enterpriseType && !Array.isArray(actualParams.enterpriseType)) {
actualParams.enterpriseType = [actualParams.enterpriseType];
}
2026-01-16 10:16:27 +08:00
}
2026-01-07 15:35:29 +08:00
2026-01-16 10:16:27 +08:00
const { data } = await request(requestUrl, "post", actualParams);
2026-01-19 11:04:24 +08:00
// 存入缓存
cacheMap.set(cacheKey, data);
setTreeData(data);
};
useEffect(() => {
getData();
}, [JSON.stringify(params), isNeedCorpInfoId, isNeedParentId, searchType]);
return (
<BasicSelectTree treeData={treeData} placeholder={placeholder} childrenKey="childrenList" {...restProps} />
);
}
DepartmentSelectTree.displayName = "DepartmentSelectTree";
export default DepartmentSelectTree;