import React from 'react' import { Card, Col, Row, Tree, Space } from 'antd'; import './index.css' import HCserviceV3 from "../tmpl/hcservice-v3"; import { BlockMenu, Level2Menu, ProgramAuth } from "../tmpl/interface"; import { LocaleContext } from "../locale/LocaleProvider"; import { DeleteOutlined, KeyOutlined, LockOutlined, PlusOutlined, RedoOutlined, UnlockOutlined } from '@ant-design/icons' interface BlockMenuAuthTreeProps { checks: string[], //checkAuthCodes: string[], onChange: (checkeds: string[]) => void, // onCheck: (keys, info: object) => void, } interface BlockMenuAuthTreeState { treeData?: Object[], loading?: boolean, // treekeyAuthId?:{}, // authIdtreekeys?:{} } class BlockMenuAuthTree extends React.PureComponent { static defaultProps = { checkeds: [] } treekeyAuthId = {}; authIdtreekeys = {}; static contextType = LocaleContext; context: React.ContextType; state = { treeData: undefined, loading: false, // treekeyAuthId:{}, // authIdtreekeys:{} } async componentDidMount() { await this.refresh(); } async componentDidUpdate(prevProps, prevState) { // const {checks:prechecks,} = prevProps; // const {checks} = this.props; // if(checks!=prechecks){ // await this.refresh(); // } } async componentWillUnmount() { } initData = async () => { this.setState({ loading: true }) let data: BlockMenu[] = await HCserviceV3.requestBlockMenuAuths(); //转换成Treenode let treeData = []; if (data) { treeData = this.toTreeNode(data); console.log("treeData", treeData) } //已经选了那些 this.setState({ treeData, loading: false }) } toTreeNode = (blockMenus: (BlockMenu | Level2Menu)[]): Object[] => { let treeData = []; // let authIdtreekeys={}; // let treekeyAuthId={}; if (blockMenus) { blockMenus.forEach((block) => { let blockNode = { key: block.id, title: block.title } treeData.push(blockNode); let nextBlockMenus = []; blockNode["children"] = nextBlockMenus; nextBlockMenus.push(...this.toTreeNode(block.items)); //处理权限 if (block["programAuths"]) { block["programAuths"].forEach((auth) => { let key = blockNode.key + auth.id; this.treekeyAuthId[key] = auth.id; if (!this.authIdtreekeys[auth.id]) { this.authIdtreekeys[auth.id] = []; } this.authIdtreekeys[auth.id].push(key); let authNode = { key, title: auth.title, isLeaf: true } authNode["code"] = auth.id; nextBlockMenus.push(authNode); }); } else { } }); } return treeData; } onCheck = (checkedKeys, e) => { const { checked, node } = e; let checks = []; if (checkedKeys) { checkedKeys.forEach((k) => { const temCode = this.treekeyAuthId[k]; if (temCode && (checked || (!checked && temCode !== node.code))) { checks.push(temCode); } }); } this.props.onChange(checks); } async refresh() { this.setState({ loading: true }) await this.initData(); this.setState({ loading: false, }) } renderTitle = (nodeData) => { const { translate } = this.context; return {nodeData["isLeaf"] ? : null} {translate("${" + nodeData.title + "}")} } render() { const { checks, onChange } = this.props; const { loading, treeData } = this.state; let checkedKeys = []; checks.forEach((id) => { if (this.authIdtreekeys[id]) { checkedKeys.push(...this.authIdtreekeys[id]); } }) // console.log("treeData", treeData); // console.log("checkedKeys", checkedKeys); return { return {this.renderTitle(nodeData)} }} treeData={treeData} /> } } export default BlockMenuAuthTree