#
luxiaotao1123
2024-02-22 6bc2d74ca9a45d3061b607e690548eacda74833a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import React, { useState, useRef, useEffect } from 'react';
import {
    ProForm,
    ProFormDigit,
} from '@ant-design/pro-components';
import { Col, Form, Modal, Row, Checkbox, Skeleton, Tree } from 'antd';
import Http from '@/utils/http';
import { transformTreeData, getTreeAllKeys } from '@/utils/tree-util'
 
const Scope = (props) => {
    const [form] = Form.useForm();
    const { originMenuIds } = props;
    const [menuTreeLoading, setMenuTreeLoading] = useState(false);
    const [menuTreeData, setMenuTreeData] = useState([]);
    const [menuExpandedKeys, setMenuExpandedKeys] = useState([]);
    const [menuIds, setMenuIds] = useState([]);
 
    const [menuTreeAllKeys, setMenuTreeAllKeys] = useState([]);
 
    const loadMenuTree = (param) => {
        setMenuTreeLoading(true);
        Http.doPostPromise('/api/menu/tree', param, (res) => {
            setMenuTreeLoading(false);
            const treeData = transformTreeData(res.data);
            setMenuTreeData(treeData);
            const treeAllKeys = getTreeAllKeys(treeData);
            setMenuTreeAllKeys(treeAllKeys);
        }).catch((err) => {
            console.error(err);
            setMenuTreeLoading(false);
        })
    }
 
    useEffect(() => {
        setMenuIds(originMenuIds);
        form.resetFields();
        form.setFieldsValue({
            ...props.values
        })
 
        loadMenuTree();
    }, [form, props])
 
    const handleCancel = () => {
        props.onCancel();
    };
 
    const handleOk = () => {
        form.submit();
    }
 
    const handleFinish = async (values) => {
        console.log({ ...values, menuIds }); return
        props.onSubmit({ ...values, menuIds });
    }
 
    return (
        <>
            <Modal
                title="Scope"
                width={640}
                forceRender
                destroyOnClose
                open={props.open}
                onCancel={handleCancel}
                onOk={handleOk}
            >
                <ProForm
                    form={form}
                    submitter={false}
                    onFinish={handleFinish}
                    layout="horizontal"
                    grid={true}
                >
                    <ProFormDigit
                        name="id"
                        disabled
                        hidden={true}
                    />
 
                    <ProForm.Item
                        name="deptIds"
                        label="菜单权限"
                        colProps={{ md: 24, xl: 24 }}
                    >
 
                        <Row gutter={[16, 16]}>
                            <Col md={24}>
                                <Checkbox.Group
                                    options={[
                                        { label: '展开/折叠', value: 'expandAll' },
                                        { label: '全选/全不选', value: 'checkAll' },
                                    ]}
                                    onChange={(values) => {
                                        if (values.includes('expandAll')) {
                                            setMenuExpandedKeys(menuTreeAllKeys);
                                        } else {
                                            setMenuExpandedKeys([]);
                                        }
                                        if (values.includes('checkAll')) {
 
                                        } else {
 
                                        }
                                    }} />
                            </Col>
                            <Col md={24}>
                                {menuTreeLoading ? (
                                    <Skeleton active />
                                ) : (
                                    <Tree
                                        checkable
                                        treeData={menuTreeData}
                                        expandedKeys={menuExpandedKeys}
                                        onExpand={(expandedKeys) => {
                                            setMenuExpandedKeys(expandedKeys)
                                        }}
                                        checkedKeys={menuIds}
                                        onCheck={(checkedKeys, checkInfo) => {
                                            return setMenuIds({
                                                checked: checkedKeys,
                                                halfChecked: checkInfo.halfCheckedKeys  // 父节点
                                            });
                                        }}
                                    // defaultCheckedKeys={onSelect}
                                    />
                                )}
                            </Col>
                        </Row>
 
                    </ProForm.Item>
                </ProForm>
            </Modal>
        </>
    )
}
 
export default Scope;