forked from huge/frontEnd/hugeOA

Mr Ke
2020-04-06 bcbc6eea0677eb0179b828238a38be0ba67ecc95
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
/* eslint-disable */
/**柯礼钦
 * 4/3/2020, 10:19:45 AM
 * doc comment for the file goes here
 */
 
/** 登录页面 */
import React, { ReactNode, ReactEventHandler, Component } from 'react';
import { Form, Icon, Input, Button, Checkbox, message } from 'antd';
import './login.scss';
import fetch from '../../api/request';
 
class NormalLoginForm extends React.Component {
    constructor(props) {
        super(props);
        this.config = {
            navigationBarTitleText: '',
        };
        this.state = {
            btnLoading: false
        };
    }
    componentWillMount() {
        document.title = '用户登录'
    }
 
    handleSubmit = e => {
        e.preventDefault();
        this.props.form.validateFields((err, values) => {
            if (!err) {
                console.log('Received values of form: ', values);
                this.setState({
                    btnLoading: true
                });
                fetch({
                    url: `hik-common/api/opus/user/loginTest`,
                    params: {
                        ...values
                    }
                }).then(res => {
                    this.setState({
                        btnLoading: false
                    })
                    if (res) {
                        this.handleRes(res);
                    }
                })
            }
        });
    };
 
    handleRes = (res) => {
        let loginUser = {};
        let curPosIndex = window.localStorage.getItem('curPosIndex');
        curPosIndex = (curPosIndex || curPosIndex == {}) || 0;
        loginUser = res.user[curPosIndex];
 
        window.localStorage.setItem('user', res);
        window.localStorage.setItem('token', res.token);
        window.localStorage.setItem('curPosIndex', curPosIndex);
        window.localStorage.setItem('loginUser', JSON.stringify(loginUser));
        message.success('登录成功', () => {
            this.props.history.push({ pathname: "/index" });
        })
 
    }
 
    render() {
        const { getFieldDecorator } = this.props.form;
        const { btnLoading } = this.state;
        return (
            <div className="login-main">
                <Form onSubmit={this.handleSubmit} className="login-form">
                    <Form.Item>
                        {getFieldDecorator('loginName', {
                            rules: [{ required: true, message: '请输入账号!' }],
                        })(
                            <Input
                                prefix={<Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} />}
                                placeholder="请输入账号"
                            />,
                        )}
                    </Form.Item>
                    <Form.Item>
                        {getFieldDecorator('passWord', {
                            rules: [{ required: true, message: '请输入密码!' }],
                            initialValue: 'psw'
                        })(
                            <Input
                                prefix={<Icon type="lock" style={{ color: 'rgba(0,0,0,.25)' }} />}
                                type="password"
                                placeholder="请输入密码"
                            />,
                        )}
                    </Form.Item>
                    <Form.Item>
                        {/* {getFieldDecorator('remember', {
                            valuePropName: 'checked',
                            initialValue: true,
                        })(<Checkbox>记住</Checkbox>)}
                        <a className="login-form-forgot" href="">
                            忘记密码
                        </a> */}
                        <Button type="primary" htmlType="submit" className="login-form-button" loading={btnLoading}>
                            登录
                        </Button>
                    </Form.Item>
                </Form>
            </div>
        );
    }
}
 
const WrappedNormalLoginForm = Form.create({ name: 'normal_login' })(NormalLoginForm);
export default WrappedNormalLoginForm;