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

84 lines
2.3 KiB
JavaScript

import { request } from "@cqsjjb/jjb-common-lib/http.js";
import { useEffect, useState } from "react";
import BasicSelectTree from "../../Basic";
// 全局缓存
const cacheMap = new Map();
/**
* 部门下拉树组件(港务局版本)
*/
function DepartmentSelectTree(props) {
const {
params = {},
placeholder = "部门",
isNeedCorpInfoId = false,
isNeedParentId = false,
searchType = "current",
...restProps
} = props;
const [treeData, setTreeData] = useState([]);
const getData = async () => {
// 生成缓存键
const paramsStr = JSON.stringify(params);
const cacheKey = `${searchType}_${paramsStr}`;
// 检查缓存,如果存在直接返回缓存结果
if (cacheMap.has(cacheKey)) {
setTreeData(cacheMap.get(cacheKey));
return;
}
setTreeData([]);
if (searchType === "current") {
if (isNeedCorpInfoId && !params.eqCorpinfoId)
return;
if (isNeedParentId && !params.eqParentId)
return;
}
let requestUrl = "";
const actualParams = { ...params, time: new Date().getTime() };
if (searchType === "current") {
requestUrl = "/basicInfo/department/listTree";
actualParams.inType = undefined;
actualParams.enterpriseType = undefined;
}
if (searchType === "all") {
requestUrl = "/basicInfo/department/listAllTree";
actualParams.inType = undefined;
actualParams.enterpriseType = undefined;
}
if (searchType === "inType") {
requestUrl = "/basicInfo/department/listAllTreeByCorpType";
actualParams.eqCorpinfoId = undefined;
actualParams.eqParentId = undefined;
if (actualParams.enterpriseType && !Array.isArray(actualParams.enterpriseType)) {
actualParams.enterpriseType = [actualParams.enterpriseType];
}
}
const { data } = await request(requestUrl, "post", actualParams);
// 存入缓存
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;