forked from huge/frontEnd/hugeOA

Mr Ke
2020-09-04 2e80539e531aea46e8e57fa5d1b8debca9f3bfbe
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import React from "react";
import { Link } from "react-router-dom";
import { Card, Row, Col, Icon, Form, message, Tree, Modal, Layout, Popconfirm, Divider, Spin, Button, Empty } from "antd";
import moment from "moment";
import SearchTree from '../../components/common/Tree';
import SearchFormView from '../../components/common/SearchFormView';
import TableView from '../../components/common/TableView';
import FunctionDetail from './FunctionDetail';//功能详情
import fetch from '../../api/request';
 
const FormItem = Form.Item;
const confirm = Modal.confirm;
const TreeNode = Tree.TreeNode;
 
class FunctionManage extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      resourceList: [],
      treeKey: Date.now(),
      expandedKeys: [],
      tableData: [],
      totalElements: 1,
      pageSize: 10,
      page: 1,
 
      treeData: [],
      leafNodes: [],
      currentNode: [],
      rightShow: false,
      modify: false,
      parentId: "",
      id: "",
      dataSet: {},
      selectedKeys: [],
      treeLoading: false,//左侧树结构loading
      loading: false,//部门详情的loading
      formData: {
        __key: Date.now(),
        page: 1,
        size: 10,
      }
    };
  }
  componentDidMount() {
    this.loadData();
  }
 
  //加载左侧树形数据
  loadData = () => {
    this.setState({ treeLoading: true });
    fetch({
      url: `api/module/getAllModulesByTree`
    }).then(res => {
      this.setState({ treeLoading: false });
      if (res) {
        res = this.handleTreeData(res);
        this.setState({ treeData: res, treeKey: Date.now() })
      }
    })
  };
 
  //处理树形数据结构
  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);
      }
      // else{
      //   this.handleTreeData([node])
      // }
    }
    return data;
  };
 
  //点击树形节点触发函数
  treeSelect = (tableData, currentNode) => {
    console.log(tableData, currentNode);
    if (currentNode[0].children && currentNode[0].children.length > 0) {
      message.error("请选择下级的节点");
      this.setState({
        rightShow: false
      });
      return;
    } else {
      message.success("已选择:" + currentNode[0].name);
    }
    this.setState(
      {
        leafNodes: tableData,
        currentNode,
        unitLocation: currentNode[0].name,
        parentId: currentNode[0].parentId,
        selectedKeys: [currentNode[0].id]
      },
      () => {
        this.loadTableData();
      }
    );
  };
 
  //加载右侧table表格数据
  loadTableData = () => {
    const { currentNode, formData } = this.state;
    if (currentNode[0].length < 0) {
      message.error("请选择左边节点后进行查询操作");
      return;
    }
    this.setState({
      rightShow: true,
      formData: {
        ...formData,
        __key: Date.now(),
        moduleId: currentNode[0].id
      }
    })
  };
 
  //功能(新增 | 编辑),详情接口
  modifyFun = id => {
    this.setState({
      modify: true,
      id,
      dataSet: {}
    });
    if (id) {
      this.setState({ loading: true });
      fetch({
        url: `api/function/getById`,
        params: { id }
      }).then(res => {
        this.setState({ loading: false });
        if (res) {
          this.setState({ dataSet: res })
        }
      })
    }
  };
 
  clearKeys = () => {
    let _this = this;
    _this.setState({
      currentNode: [],
      rightShow: false
    });
  };
 
  cancelFun = () => {
    this.setState({
      modify: false
    });
  };
 
  //保存表单(确定按钮)
  saveFun = data => {
    this.setState({ loading: true });
    fetch({
      url: `api/function/save`,
      method: 'POST',
      data
    }).then(res => {
      this.setState({ loading: false });
      if (res) {
        message.success("保存功能成功");
        this.setState({ modify: false });
        this.loadTableData();//重新加载表格table数据
      }
    })
  };
 
  onExpand = expandedKeys => {
    this.setState({
      expandedKeys
    });
  };
 
  onSelect = selectedKeys => {
    this.setState({
      selectedKeys
    });
  };
 
  //删除功能
  delete = id => {
    let _this = this;
    confirm({
      title: <span style={{ fontSize: 19 }}>确定要删除该功能吗?</span>,
      onOk() {
        fetch({
          url: `api/function/delete`,
          params: { ids: id }
        }).then(res => {
          if (res) {
            message.success("删除成功");
            _this.loadTableData();
          }
        })
      },
      onCancel() { }
    });
  };
 
  setFormData = data => {
    console.log('form', data);
    this.setState({
      formData: data,
    });
  }
 
  renderColumns = () => {
    return [
      { title: "功能名称", dataIndex: "name" },
      { title: "标识符", dataIndex: "symbol", ellipsis: true },
      { title: "功能描述", dataIndex: "description", ellipsis: true },
      { title: "创建时间", dataIndex: "updateTime", render: (text, record) => (text && moment(text).format("YYYY-MM-DD HH:mm")) },
      {
        title: "操作", key: "operation", render: (text, record) => {
          return (
            <div>
              <a onClick={() => this.modifyFun(record.id)}>修改</a>
              <Divider type="vertical" />
              <a onClick={() => this.delete(record.id)}>删除</a>
            </div>
          );
        }
      }
    ];
  };
 
  render() {
    const { treeKey, treeData, rightShow, treeLoading, formData, currentNode } = this.state;
 
    const params = {
      id: this.state.id,
      currentNode
    };
 
    let tableParams = {
      url: `api/function/query`,
      formData,
      key: formData.__key,
      columns: this.renderColumns(),
      extraFromData: {
        moduleId: currentNode.length ? currentNode[0].id : ''
      },
      setFormData: this.setFormData
    }
 
    return (
      <div className="functionmanage-main h-100 padding margin bg-white">
        {!this.state.modify && (
          <React.Fragment>
            <div className="padding h-100">
              <Row style={{ height: "100%" }}>
                <Col
                  span={8}
                  style={{ border: "1px solid #e8e8e8", padding: 10 }}
                  className="h-100"
                >
                  <Spin spinning={treeLoading}>
                    <SearchTree
                      key={treeKey}
                      data={treeData}
                      onExpand={this.onExpand}
                      expandedKeys={this.state.expandedKeys}
                      treeSelect={this.treeSelect}
                      clearKeys={this.clearKeys}
                      onSelect={this.onSelect}
                      selectedKeys={this.state.selectedKeys}
                    />
                  </Spin>
                </Col>
                <Col span={1} />
                <Col
                  span={15}
                  style={{ border: "1px solid #e8e8e8", padding: 10 }}
                  className="h-100"
                >
                  {
                    rightShow && (
                      <div>
                        <SearchFormView data={[{ type: 'input', name: '功能名称', label: '功能名称', key: 'name' }]} width={'50%'} formData={formData} setFormData={this.setFormData} width={'50%'}/>
                        <Row className="margin-bottom">
                          <Col>
                            <Button onClick={() => this.modifyFun('')} type="primary" className="button-do">
                              新增
                          </Button>
                          </Col>
                        </Row>
                        <TableView {...tableParams} />
                      </div>
                    )}
                  {
                    !rightShow && (
                      <Empty description={'暂无数据,请点击左侧树形结构'} className="h-100 flex-box-column align-center justify-content" />
                    )}
                </Col>
              </Row>
            </div>
          </React.Fragment>
        )}
 
        {this.state.modify && (
          <FunctionDetail
            cancelFun={this.cancelFun}
            params={params}
            dataSet={this.state.dataSet}
            saveFun={this.saveFun}
            loading={this.state.loading}
          />
        )}
      </div>
    );
  }
 
 
}
 
const FunctionManageList = Form.create()(FunctionManage);
export default FunctionManageList;