forked from huge/frontEnd/hugeOA

Mr Ke
2020-05-06 5519a3fbaf3e32244641fe9c7d6fe35333fb951e
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
/* eslint-disable */
/**kelq
 * 4/29/2020, 4:28:00 PM
 * doc comment for the file goes here
 */
 
/** 个人信息 */
import React, { ReactNode, ReactEventHandler, Component } from 'react';
import { Card, Form, Row, Col, Input, Button, Select, message, Spin } from 'antd';
import ChangePswView from '../../../common/ChangePswView';
 
const FormItem = Form.Item;
const Option = Select.Option;
 
import './index.scss';
import fetch from '../../../../api/request';
import creatHistory from 'history/createHashHistory';
const history = creatHistory();//返回上一页这段代码
 
class Information extends Component {
  constructor(props) {
    super(props);
    this.config = {
    };
    this.state = {
      spinning: true,
      btnLoading: false,//修改密码弹窗loading
      submitLoading: false,//提交按钮loading
      account: {},
      user: {}
    };
  }
 
  componentDidMount() {
    this.initPersonInfo();
  }
 
  initPersonInfo = () => {
    this.setState({ spinning: true })
    fetch({
      url: `api/user/findUserInfo`,
    }).then(res => {
      this.setState({ spinning: false });
      if (res) {
        console.log('res', res);
        this.setState({
          ...res
        })
      }
    })
  }
 
  // 验证手机格式
  isMobile = (rule, value, callback) => {
    const regMobile = /^0?1[3|4|5|8][0-9]\d{8}$/
    if (!regMobile.test(value)) {
      if (value == "") {
        callback();
        return;
      } else {
        callback('手机号码格式不正确');
        return;
      }
    }
    callback()
  }
 
  // 验证idcard格式
  isIdCard = (rule, value, callback) => {
    const regMobile = /^[1-9]\d{7}((0[1-9])|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0[1-9])|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
    if (!regMobile.test(value)) {
      if (value == "") {
        callback();
        return;
      } else {
        callback('身份证号码格式不正确');
        return;
      }
    }
    callback()
  }
 
  //修改密码
  onOK = (data) => {
    console.log('data', data);
    let _this = this;
    let formData = {
      accountId: _this.state.account.id,
      newPassword: data.newPassWord
    }
    _this.setState({
      btnLoading: true
    })
    fetch({
      url: `api/account/changePassword`,
      params: formData
    }).then(res => {
      _this.setState({
        btnLoading: false,
        changePswVisible: false
      })
      if (res) {
        message.success("密码修改成功");
      }
    })
  }
 
  //修改个人信息
  handleSubmit = (e) => {
    e.preventDefault();
    let _this = this;
    let { account, user } = _this.state;
    _this.props.form.validateFields((err, values) => {
      if (err) return;
      console.log(values);
      _this.setState({ submitLoading: true })
      fetch({
        url: `api/user/save`,
        method: 'POST',
        data: {
          user: {
            ..._this.state.user,
            trueName: values.trueName,
            sex: values.sex,
            idcard: values.idcard,
            mobile: values.mobile,
            email: values.email,
          }
        }
      }).then(res => {
        _this.setState({ submitLoading: false });
        if (res) {
          message.success('保存成功')
          this.props.setData({
            key: Date.now()
          })
        }
      })
    })
  }
 
  render() {
    const { getFieldDecorator } = this.props.form;
    const { account, user, changePswVisible, btnLoading, spinning, submitLoading } = this.state;
    const formItemLayout = {
      labelCol: { xs: { span: 24 }, sm: { span: 10 }, },
      wrapperCol: { xs: { span: 24 }, sm: { span: 14 }, },
    };
 
    return (
      <div className="information-main">
        {
          spinning && <Spin spinning={this.state.spinning} style={{ height: 200, display: 'flex', justifyContent: 'center', alignItems: 'center' }}></Spin>
        }
        {
          !spinning && <React.Fragment>
            <Card title="账号信息" bordered={false} >
              <Row>
                <Col span={16}>
                  <Row>
                    <Col span={12}>
                      <FormItem label={"账号"} {...formItemLayout}>
                        {account.account || ''}
                      </FormItem>
                    </Col>
                    <Col span={12}>
                      <FormItem label={"密码"} {...formItemLayout}>
                        <div>
                          ********<Button type="primary" onClick={() => {
                            this.setState({
                              changePswVisible: true
                            })
                          }} style={{ marginLeft: 20 }}>修改密码</Button>
                        </div>
                      </FormItem>
                    </Col>
                  </Row>
                </Col>
              </Row>
            </Card>
            <Card title="基础信息" bordered={false} >
              <Row>
                <Col span={16}>
                  <Row>
                    <Col span={12}>
                      <FormItem label={"姓名"} {...formItemLayout}>
                        {getFieldDecorator('trueName', {
                          rules: [{ required: true, message: '姓名必填' }],
                          initialValue: user.trueName || ''
                        })(
                          <Input placeholder="请输入姓名" />
                        )}</FormItem>
                    </Col>
                    <Col span={12}>
                      <FormItem label={"性别"} {...formItemLayout}>
                        {getFieldDecorator('sex', {
                          rules: [{ required: true, message: '微信用户名称必填' }],
                          initialValue: user.sex || undefined
                        })(
                          // <Input placeholder="请输入" />
                          <Select placeholder="请选择" style={{ width: '100%' }}>
                            <Option value={1}>男</Option>
                            <Option value={2}>女</Option>
                          </Select>
                        )}</FormItem>
                    </Col>
                  </Row>
                  <Row>
                    <Col span={12}>
                      <FormItem label={"手机号码"} {...formItemLayout}>
                        {getFieldDecorator('mobile', {
                          rules: [{
                            required: true, message: '手机号码必填'
                          }, {
                            validator: this.isMobile
                          }],
                          initialValue: user.mobile || ''
                        })(
                          <Input placeholder="请输入手机号码" />
                        )}</FormItem>
                    </Col>
                    <Col span={12}>
                      <FormItem label={"电子邮箱"} {...formItemLayout}>
                        {getFieldDecorator('email', {
                          rules: [{
                            type: 'email', message: '电子邮箱格式不正确',
                          }],
                          initialValue: user.email || ''
                        })(
                          <Input placeholder="请输入电子邮箱" />
                        )}</FormItem>
                    </Col>
                  </Row>
                  <Row>
                    <Col span={12}>
                      <FormItem label={"身份证号码"} {...formItemLayout}>
                        {getFieldDecorator('idcard', {
                          rules: [{
                            required: true, message: '身份证号码必填'
                          }, {
                            validator: this.isIdCard
                          }],
                          initialValue: user.idcard || ''
                        })(
                          <Input placeholder="请输入身份证号码" />
                        )}</FormItem>
                    </Col>
                  </Row>
                </Col>
              </Row>
            </Card>
 
            <Row type="flex" gutter={20} style={{ marginTop: '12px' }}>
              {/* <Col className="gutter-row" ><Button onClick={() => { history.goBack() }}>返回</Button></Col> */}
              <Col className="gutter-row" ><Button type="primary" loading={submitLoading} onClick={this.handleSubmit}>确定</Button></Col>
            </Row>
            {
              changePswVisible && <ChangePswView onCancel={() => { this.setState({ changePswVisible: false }) }} onOK={this.onOK} btnLoading={btnLoading} />
            }
          </React.Fragment>
        }
 
      </div>
 
    )
  }
}
 
const InformationForm = Form.create()(Information);
export default InformationForm;