import { Store } from "antd/es/form/interface"; import "./org_chart.less"; import { inject, observer } from "mobx-react"; import { useEffect, useState } from "react"; import { orgData } from "./org_config"; const OrgChartSelf = (props: Store) => { const { depStore } = props; const [data, setOrgData] = useState([]); useEffect(() => { depStore.getOrg().then((res) => { // setOrgData(res.data.record) // setOrgData(orgData); }); }, []); const getArrayDepth = (arr) => { let maxDepth = 0; let maxUser = 0; function dfs(currentArr, currentDepth) { currentDepth++; for (let i = 0; i < currentArr.length; i++) { if (Array.isArray(currentArr[i].children)) { dfs(currentArr[i].children, currentDepth); } else { maxUser = Math.max(maxUser, currentArr[i].users.length); maxDepth = Math.max(maxDepth, currentDepth); } } } dfs(arr, 0); return maxUser; }; // 示例代码 // const arr = [[1, 2, [3, 4]], [[5], 6], 7]; // console.log(getArrayDepth(arr)); // 输出: 3 const dc = (v) => { let max = getArrayDepth(orgData); if (v.depId === 1) { return 70 * max - 40; } if (v.children.length > 0) { return v.children?.length * 70 - 40; } }; const lf = (v) => { let max = getArrayDepth(orgData); if (v.depId === 1) { return -30 * max + 20; } if (v.children?.length > 0) { return -30 * v.children?.length + 10; } }; const renderTree = (list: Array) => { return (
{list?.map((v, _) => { if (v.children) { return (
{v.depId !== 2 ? ( {v.dep_name} ) : null} {v.depId === 1 ? (
{v.users?.map((v1, _) => { return (
{v1.user_name}
); })}
) : ( v.users?.map((v1, _) => { return (
{v1.user_name}
); }) )} {v.depId !== 2 ? (
) : null} {renderTree(v.children)}
); } return (
{v.users?.map((v1, _) => { return (
{v1.user_name}
); })}
); })}
); }; return
{renderTree(orgData)}
; }; // export default OrgChartSelf; export default inject("depStore")(observer(OrgChartSelf));