forked from huge/frontEnd/hugeOA

...
liyj
2020-06-24 7af09e42b49cd18f160c19297f47c4622b1eedc3
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import React from 'react';
import { Card, Row, Col, Icon, Form, Input, Button, Table, DatePicker, message, Breadcrumb, Tree, Modal, Layout, Popconfirm, List, Spin, Empty } from 'antd';
import SearchTree from '../../components/common/Tree';
import fetch from '../../api/request';
 
const Search = Input.Search;
const confirm = Modal.confirm;
 
class ResourceManage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      spinning: false,
      resourceList: [],
      treeKey: 'init',
      expandedKeys: [],
      treeData: [],
      edit: 'false',
      rightShow: false,
      dataSet: {},
      currentNode: [],
 
      roleList: [],  //角色列表
      resourceIds: [],  //资源集合
      treeCheckArr: [],
      roleId: '',
      checkedKeys: [],
 
      leftRoleLoading: false,//左侧角色列表loading
      rightTreeRightLoading: false,//右侧角色对应菜单权限loading
 
    };
  }
 
  componentDidMount() {
    this.findRoles();
    this.getAllFunctionsByTree();
  }
 
  //取消按钮
  cancelFun = () => {
    this.setState({
      edit: 'false',
      rightShow: false,
      roleId: ''
    })
  }
 
  //所有角色列表
  findRoles = () => {
    this.setState({ leftRoleLoading: true });
    fetch({
      url: `api/role/finds`
    }).then(res => {
      this.setState({ leftRoleLoading: false });
      if (res) {
        this.setState({ roleList: res });
      }
    })
  }
 
  //加载所有应用菜单树形结构
  getAllFunctionsByTree = () => {
    this.setState({ rightTreeRightLoading: true });
    fetch({
      url: `api/function/getAllFunctionsByTree`,
    }).then(res => {
      this.setState({ rightTreeRightLoading: false });
      if (res) {
        for (let i = 0; i < res.length; i++) {
          res[i].resourceType = 1;
        }
        res = this.handleTreeData(res);
        this.setState({ treeData: res })
      }
    })
  }
 
  //通过角色id查询对应角色的菜单权限
  getResourcesByRoleId = (id) => {
    this.setState({ rightTreeRightLoading: true });
    fetch({
      url: `api/role/getResourcesByRoleId`,
      params: { roleId: id }
    }).then(res => {
      this.setState({ rightTreeRightLoading: false });
      console.log('res', res);
      if (res) {
        this.setState({
          checkedKeys: res.map(({ id }) => id),
          treeCheckArr: res
        })
      }
    })
  }
 
  //处理树形数据结构
  handleTreeData = data => {
    for (let i = 0; i < data.length; i++) {
      const node = data[i];
      node.key = node.id;
      node.title = node.name;
      if (data[i].modules) {
        data[i].children = data[i].modules;
        this.handleTreeData(node.children);
      }
      if (data[i].children) {
        this.handleTreeData(node.children);
      }
      if (data[i].functions) {
        data[i].children = data[i].functions;
        this.handleTreeData(node.children);
      }
    }
    return data;
  };
 
 
  treeSelect = (tableData, currentNode) => {
    console.log(tableData, currentNode);
    // 点击任意节点都可以查看、编辑、添加
    this.setState({
      rightShow: true
    })
    this.setState({
      leafNodes: tableData,
      currentNode
    })
  };
 
  currentTreeCheck = (treeCheckArr) => {
    this.setState({
      treeCheckArr
    })
  }
 
  onExpand = (expandedKeys) => {
    this.setState({
      expandedKeys,
    });
  }
 
  //选中角色
  selectRole = (e) => {
    let roleId = e.target.getAttribute("data-roleid");
    console.log(roleId)
    this.setState({
      rightShow: true,
      roleId,
      spinning: true
    }, () => {
      this.getResourcesByRoleId(this.state.roleId);//查询角色权限
    })
  }
 
  onCheck = (checkedKeys, checkedNodes) => {
    console.log('checkedNodes', checkedNodes);
    this.setState({
      checkedKeys
    })
  }
 
  saveFun = () => {
    const { treeCheckArr, roleId, funs } = this.state;
    let resourceIds = [];
 
    console.log(resourceIds, treeCheckArr);
    this.setState({ rightTreeRightLoading: true });
    fetch({
      url: `api/role/auth`,
      method: 'POST',
      data: {
        roleId,
        resourceIds: treeCheckArr.map(({ id, resourceType }) => ({ resourceId: id, resourceType }))
      }
    }).then(res => {
      this.setState({ rightTreeRightLoading: false });
      if (res) {
        message.success('配置保存成功');
      }
    })
  }
 
  render() {
    const { rightShow, treeData, roleList, roleId, leftRoleLoading, rightTreeRightLoading } = this.state;
 
    return (
      <div className="authormanage-main h-100 margin padding bg-white">
 
        <div className="padding h-100" >
          <Row style={{ height: '100%' }} >
            <Col span={8} style={{ border: "1px solid #e8e8e8", padding: 10 }} className="h-100">
              <Spin spinning={leftRoleLoading}>
                <Row style={{ marginTop: 10 }} >
 
                  {
                    roleList.map((item, idx) => {
                      return (
                        roleId == item.id ?
                          <div key={item.id} data-roleid={item.id} onClick={this.selectRole} style={{ border: "1px solid #e8e8e8", padding: 8, textAlign: 'center', cursor: 'pointer' }} className="ant-btn-primary ant-menu-submenu-selected">
                            {item.name}
                          </div> :
                          <div key={item.id} data-roleid={item.id} onClick={this.selectRole} style={{ border: "1px solid #e8e8e8", padding: 8, textAlign: 'center', cursor: 'pointer' }}>
                            {item.name}
                          </div>
                      )
                    })
                  }
                </Row>
              </Spin>
            </Col>
            <Col span={1}></Col>
            <Col span={15} style={{ border: "1px solid #e8e8e8" }} className="h-100">
              {rightShow &&
                <div className="h-100">
                  <Spin spinning={rightTreeRightLoading} height={'100%'}>
                    <div>
                      <Form>
                        <Card title={'模块权限配置'} bordered={false} extra={
                          <Row type="flex" gutter={20}>
                            <Col>
                              <Button onClick={this.cancelFun}>取消</Button>
                            </Col>
                            <Col>
                              <Button className="button-do" htmlType="submit" onClick={this.saveFun} type="primary">保存</Button>
                            </Col>
                          </Row>
                        }>
                          {/* 此处的key用于控制滚动条的位置 */}
                          <div style={{ height: '65vh', overflow: 'auto' }} key={roleId}>
                            <SearchTree
                              data={treeData}
                              onExpand={this.onExpand}
                              expandedKeys={this.state.expandedKeys}
                              treeSelect={this.treeSelect}
                              showSearch={false}
                              checkable={true}
                              defaultExpandAll={true}
                              currentTreeCheck={this.currentTreeCheck}
                              checkedKeys={this.state.checkedKeys}
                              onCheck={this.onCheck}
                            />
                          </div>
                        </Card>
                      </Form>
                    </div>
                  </Spin>
                </div>
              }
              {
                !rightShow &&
                <Empty description={'暂无数据,请点击左侧树形结构'} className="h-100 flex-box-column align-center justify-content" />
              }
            </Col>
          </Row>
          <Row>
          </Row>
        </div>
      </div>
    )
  }
}
 
 
 
const ResourceManageList = Form.create()(ResourceManage);
export default ResourceManageList;