index.js 5.1 KB
Newer Older
zhanghaozhe committed
1 2
import React, { Component } from 'react';
import './set-password.scss'
zhanghaozhe committed
3
import { withFormik, Form, Field } from "formik";
zhanghaozhe committed
4 5 6
import PasswordInput from '../common/passwordInput'
import Button from '../common/Button'
import classnames from 'classnames'
zhanghaozhe committed
7
import { compose } from 'redux'
8 9 10 11 12
import { HeaderBar } from "@/common";
import { http, api } from "@/utils";
import { Toast } from "antd-mobile";
import { encrypt } from "@/components/passport/encryption";
import { Link } from "react-router-dom";
zhanghaozhe committed
13
import { isEmpty } from "lodash";
zhanghaozhe committed
14
import { connect } from "react-redux";
zhanghaozhe committed
15 16


17
class SetPassword extends Component {
zhanghaozhe committed
18

zhanghaozhe committed
19
    componentDidMount() {
20
        // console.log(this.props.location);
zhanghaozhe committed
21 22 23
    }


zhanghaozhe committed
24
    render() {
zhanghaozhe committed
25 26
        let {values, errors, location} = this.props
        let {from} = location.state || {from: {pathname: '/'}}
zhanghaozhe committed
27
        return (
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
            <>
                <HeaderBar arrow={true} title={'设置密码'}/>
                <div className={'set-password'}>
                    <p className='title'>密码需要包含6-16位字母及数字</p>
                    <Form>
                        <Field
                            name='password'
                            render={({field}) => {
                                return (
                                    <PasswordInput
                                        autoComplete={'on'}
                                        placeholder={'设置密码'}
                                        onChange={this.handleChange}
                                        {...field}
                                    />
                                )
                            }}
                        />
zhanghaozhe committed
46 47
                        <Button className={'btn-active'}
                                active={values.password && values.agreement && isEmpty(errors)}>完成</Button>
48 49 50 51 52 53 54 55 56 57 58
                        <label htmlFor="agreement" className='user-agreement'>
                            <Field type='checkbox'
                                   name='agreement'
                                   id='agreement'
                                   className={classnames([
                                       this.props.values.agreement ? 'iconfont iconiconfront-3' : 'disagree'
                                   ])}
                            />
                            同意<span>《七月在线用户使用协议》</span>
                        </label>
                    </Form>
zhanghaozhe committed
59 60
                    <div className="skip"
                         style={{display: from && from.pathname.includes('forgot-password') ? 'none' : 'block'}}>
61 62 63 64
                        <Link replace to='/passport/account-login'>跳过</Link>
                    </div>
                </div>
            </>
zhanghaozhe committed
65 66 67 68
        );
    }
}

zhanghaozhe committed
69
const formikConfig = {
70 71 72 73 74 75 76 77
    mapPropsToValues() {
        return {
            password: '',
            agreement: true
        }
    },
    handleSubmit: (values, {props}) => {

zhanghaozhe committed
78 79 80 81 82 83
        let {from} = props.location.state || {from: {pathname: '/'}}
        if (from.pathname.includes('forgot-password')) {
            forgotPasswordReset(values, props);
        } else {
            bindMobileSetPassword(values, props)
        }
zhanghaozhe committed
84

85 86
    },
    validateOnChange: false,
zhanghaozhe committed
87
    validate: values => {
88 89 90 91 92 93 94 95 96 97 98
        let errors = {}
        const re = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
        if (!re.test(values.password)) {
            errors.password = '密码需要包含6-16位字母及数字'
            Toast.info(errors.password, 2, null, false)
        }
        if (!values.agreement) {
            errors.agreement = '您须同意《七月在线用户使用协议》'
            Toast.info(errors.agreement, 2, null, false)
        }
        return errors
zhanghaozhe committed
99 100 101
    }
}

zhanghaozhe committed
102 103 104 105

function forgotPasswordReset(values, props) {
    let key = sessionStorage.getItem('r_type') === 'email' ? 'email' : 'tel'

xuzhenghua committed
106 107 108
    let requestKey = key === 'email' ? 'email' : 'phone'
    http.post(`${API['passport-api']}/account/up_pass_by_${requestKey}`, {
        [requestKey]: sessionStorage.getItem(key),
zhanghaozhe committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        password: encrypt(values.password)
    })
        .then(res => {
            if (res.data.errno == 200) {
                Toast.info('密码设置成功')
                setTimeout(function () {
                    props.history.replace('/passport/account-login')
                }, 1000)
            } else {
                Toast.info(res.data.msg, 2, null, false)
            }
        })
}

function bindMobileSetPassword(values, props) {

zhanghaozhe committed
125
    http.post(`${API['passport-api']}/bind_mobile/set_pwd_new`, {
zhanghaozhe committed
126 127 128 129 130 131
        uid: props.user.data.uid,
        password: encrypt(values.password)
    })
        .then(res => {
            if (res.data.errno == 200) {
                Toast.info('密码设置成功')
zhanghaozhe committed
132
                let {from} = location.state || {from: {pathname: '/'}}
zhanghaozhe committed
133 134 135 136 137 138 139 140 141
                setTimeout(function () {
                    props.history.replace(from.pathname)
                }, 1000)
            } else {
                Toast.info(res.data.msg, 2, null, false)
            }
        })
}

zhanghaozhe committed
142
export default compose(
zhanghaozhe committed
143 144 145 146
    connect(
        state => ({user: state.user}),
        null
    ),
zhanghaozhe committed
147 148
    withFormik(formikConfig)
)(SetPassword);