index.js 4.7 KB
Newer Older
xuzhenghua committed
1
import React, { Component } from 'react'
zhanghaozhe committed
2
import './set-password.scss'
xuzhenghua 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'
xuzhenghua committed
8 9 10 11 12 13 14 15
import { HeaderBar } from "@/common"
import { http } from "@/utils"
import { Toast } from "antd-mobile"
import { encrypt } from "@/components/passport/encryption"
import { Link } from "react-router-dom"
import { isEmpty } from "lodash"
import { connect } from "react-redux"
import { setCurrentUser } from '@/store/userAction'
zhanghaozhe committed
16

17
class SetPassword extends Component {
xuzhenghua committed
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
  render() {
    let {values, errors, location} = this.props
    let {from} = location.state || {from: {pathname: '/'}}
    return (
      <>
        <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}
                  />
                )
              }}
            />
            <Button className={'btn-active'}
                    active={values.password && values.agreement && isEmpty(errors)}>完成</Button>
            <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>
          <div className="skip"
               style={{display: from && from.pathname.includes('forgot-password') ? 'none' : 'block'}}>
            <Link replace to='/passport/account-login'>跳过</Link>
          </div>
        </div>
      </>
    )
  }
zhanghaozhe committed
61 62
}

zhanghaozhe committed
63
const formikConfig = {
xuzhenghua committed
64 65 66 67 68 69 70
  mapPropsToValues() {
    return {
      password: '',
      agreement: true
    }
  },
  handleSubmit: (values, {props}) => {
71

FE committed
72 73 74
    const {location} = props

    let from = location.state && location.state.records && location.state.records[location.state.records.length - 2] || {pathname: '/'}
xuzhenghua committed
75 76 77 78 79
    if (from.pathname.includes('forgot-password')) {
      forgotPasswordReset(values, props)
    } else {
      bindMobileSetPassword(values, props)
    }
zhanghaozhe committed
80

xuzhenghua committed
81 82 83 84 85 86 87 88 89 90 91 92
  },
  validateOnChange: false,
  validate: values => {
    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)
zhanghaozhe committed
93
    }
xuzhenghua committed
94 95
    return errors
  }
zhanghaozhe committed
96 97
}

zhanghaozhe committed
98 99

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

xuzhenghua committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
  let requestKey = key === 'email' ? 'email' : 'phone'
  http.post(`${API['passport-api']}/account/up_pass_by_${requestKey}`, {
    [requestKey]: sessionStorage.getItem(key),
    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)
      }
zhanghaozhe committed
116 117 118 119 120
    })
}

function bindMobileSetPassword(values, props) {

xuzhenghua committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  http.post(`${API['passport-api']}/bind_mobile/set_pwd_new`, {
    uid: props.user.data.uid,
    password: encrypt(values.password)
  })
    .then(res => {
      if (res.data.errno == 200) {
        const {location, history} = props
        Toast.info('密码设置成功')
        let from = location.state && location.state.from || {pathname: '/'}
        let local_redirect_url = JSON.parse(window.localStorage.getItem('binding_redirect'))
        setTimeout(function () {
          if (local_redirect_url) {
            const {pathname, search, hash} = local_redirect_url
            history.replace(pathname + search + hash)
          } else {
            history.replace(from.pathname)
          }
        }, 1000)
      } else {
        Toast.info(res.data.msg, 2, null, false)
      }
zhanghaozhe committed
142 143 144
    })
}

zhanghaozhe committed
145
export default compose(
xuzhenghua committed
146 147 148 149 150 151
  connect(
    state => ({user: state.user}),
    {setCurrentUser}
  ),
  withFormik(formikConfig)
)(SetPassword)