forked from huge/frontEnd/hugeOA

liyj
2020-05-27 b419b2add64d70bf1bdb2f491c3eb090dda2cfa3
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
import React from 'react';
import { Link } from 'react-router-dom';
import { Card, Row, Col, Form, Input, Button, message, Spin, TreeSelect } from 'antd';
import MultiSelect from '../../components/common/multiSelect';
import { createHashHistory } from 'history';
import fetch from '../../api/request';
 
const history = createHashHistory();
const { TextArea } = Input;
const FormItem = Form.Item;
 
class RoleDetail extends React.Component {
  constructor(props) {
    super(props)
    this.id = props.match.params.id == 'new' ? '' : props.match.params.id;
    this.flag = props.match.params.flag == 'Modify' ? '修改' : '新增';
 
    this.state = {
      spinning: false,//页面loading
      groupList: [],
      ownGroups: [],
      title: '修改',
      dataSet: {},
    }
  }
 
  componentDidMount() {
    let p = [this.findGroups()];
    if (this.id) {
      p = p.concat(this.getRoleDetail())
    }
    this.setState({ spinning: true });
    Promise.all(p).then(res => {
      console.log('res', res);
      if (res.length == p.length) {
        this.setState({ spinning: false });
        this.setState({
          groupList: res[0], //所有组
        })
        if (this.id) {
          this.setState({
            dataSet: res[res.length - 1],//角色信息
            groupIds: res[res.length -1].groupIds || []//拥有组
          })
        }
      }
    })
  }
 
  //组
  findGroups = () => {
    return new Promise((resolve, reject) => {
      fetch({
        url: `api/group/finds`,
      }).then(res => {
        resolve(res)
      })
    })
  }
 
  //角色详情
  getRoleDetail = () => {
    return new Promise((resolve, reject) => {
      fetch({
        url: `api/role/getById`,
        params: { id: this.id }
      }).then(res => {
        resolve(res)
      })
    })
  }
 
  groupChange = checkedValues => {
    this.setState({ groupIds: checkedValues })
  }
 
  // 提交表单数据(新增 | 编辑)
  handleSubmit = (e) => {
    e.preventDefault();
    this.props.form.validateFields((err, values) => {
      if (err) return;
      this.setState({ spinning: true });
      fetch({
        url: `api/role/save`,
        method: 'POST',
        data: {
          role: { ...values, id: this.id },
          groupIds: this.state.groupIds
        }
      }).then(res => {
        this.setState({ spinning: false });
        if (res) {
          message.success('保存角色成功');
          history.goBack();
        }
      })
    });
  }
 
  //返回函数
  onBack = () => {
    history.goBack();
  }
 
  render() {
    const { getFieldDecorator } = this.props.form;
    const formItemLayout1 = {
      labelCol: { xs: { span: 24 }, sm: { span: 10 }, },
      wrapperCol: { xs: { span: 24 }, sm: { span: 14 }, },
    };
    const formItemLayout2 = {
      labelCol: { xs: { span: 24 }, sm: { span: 5 }, },
      wrapperCol: { xs: { span: 24 }, sm: { span: 19 }, },
    };
    const { groupList, dataSet, groupIds } = this.state;
 
    return (
      <div className="roledetail-main h-100 margin padding bg-white">
        <Spin spinning={this.state.spinning}>
          <div>
            <Form onSubmit={this.handleSubmit}>
              <Card title={'基础信息'} bordered={false}
                extra={
                  <Row type="flex" gutter={20}>
                    <Col>
                      <Button onClick={this.onBack}>返回</Button>
                    </Col>
                    <Col>
                      <Button htmlType="submit" type='primary'>确定</Button>
                    </Col>
                  </Row>
                }
              >
                <Row type="flex">
                  <Col span={8}>
                    <FormItem label={"标识符"} {...formItemLayout1}>
                      {getFieldDecorator('symbol', {
                        rules: [{ required: true, message: '标识符必填' }],
                        initialValue: dataSet.symbol || ''
                      })(
                        <Input placeholder="请输入标识符" />
                      )}</FormItem>
                  </Col>
                  <Col span={8}>
                    <FormItem label={"角色名称"} {...formItemLayout1}>
                      {getFieldDecorator('name', {
                        rules: [{ required: true, message: '角色名称必填' }],
                        initialValue: dataSet.name || ''
                      })(
                        <Input placeholder="请输入角色名称" />
                      )}</FormItem>
                  </Col>
                </Row>
                <Row>
                  <Col span={16}>
                    <FormItem label={"描述"} {...formItemLayout2}>
                      {getFieldDecorator('description', {
                        rules: [{ max: 200, message: '描述的内容不能超过200字' }],
                        initialValue: dataSet.description || ''
                      })(
                        <TextArea placeholder="请输入描述" rows={5} />
                      )}</FormItem>
                  </Col>
                </Row>
              </Card>
            </Form>
            <Card title={'拥有组'} bordered={false} extra={
              <Row>
                <Link to={{ pathname: "/baseManage/group/Detail/new/Add", state: { id: '' } }}>
                  没有组?点击新建
                </Link>
              </Row>
            }>
              <Row>
                <MultiSelect key={groupIds ? groupIds.toString() : 'init'} all={groupList} own={groupIds} onChange={this.groupChange} />
              </Row>
            </Card>
          </div>
        </Spin>
      </div>
    )
  }
}
 
const SHOW_PARENT = TreeSelect.SHOW_PARENT;
const RoleDetailForm = Form.create()(RoleDetail);
export default RoleDetailForm;