index.js 5.15 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'
zhanghaozhe committed
8 9
import { HeaderBar } from "src/common"
import { http } from "src/utils"
xuzhenghua committed
10
import { Toast } from "antd-mobile"
zhanghaozhe committed
11
import { encrypt } from "src/components/passport/encryption"
xuzhenghua committed
12 13
import { isEmpty } from "lodash"
import { connect } from "react-redux"
zhanghaozhe committed
14
import { setCurrentUser } from 'src/store/userAction'
zhanghaozhe committed
15

16
class SetPassword extends Component {
xuzhenghua committed
17 18 19 20 21 22 23 24

  toFrom = () => {
    let historyUrl = window.localStorage.getItem('HistoryUrl')
    const {history} = this.props
    history.push(historyUrl)
  }


xuzhenghua committed
25 26 27 28 29 30 31 32 33 34 35
  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'
zhanghaozhe committed
36 37
            >
              {({field}) => {
xuzhenghua committed
38 39 40 41 42 43 44 45 46
                return (
                  <PasswordInput
                    autoComplete={'on'}
                    placeholder={'设置密码'}
                    onChange={this.handleChange}
                    {...field}
                  />
                )
              }}
zhanghaozhe committed
47
            </Field>
xuzhenghua committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
            <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'}}>
xuzhenghua committed
63
            <span onClick={this.toFrom}>跳过</span>
xuzhenghua committed
64 65 66 67 68
          </div>
        </div>
      </>
    )
  }
zhanghaozhe committed
69 70
}

zhanghaozhe committed
71
const formikConfig = {
xuzhenghua committed
72 73 74 75 76 77 78
  mapPropsToValues() {
    return {
      password: '',
      agreement: true
    }
  },
  handleSubmit: (values, {props}) => {
FE committed
79 80 81
    const {location} = props

    let from = location.state && location.state.records && location.state.records[location.state.records.length - 2] || {pathname: '/'}
xuzhenghua committed
82 83 84 85 86
    if (from.pathname.includes('forgot-password')) {
      forgotPasswordReset(values, props)
    } else {
      bindMobileSetPassword(values, props)
    }
zhanghaozhe committed
87

xuzhenghua committed
88 89 90 91 92 93 94 95 96 97 98 99
  },
  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
100
    }
xuzhenghua committed
101 102
    return errors
  }
zhanghaozhe committed
103 104
}

zhanghaozhe committed
105 106

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

xuzhenghua committed
109 110 111 112 113 114 115 116 117
  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 () {
xuzhenghua committed
118
           props.history.replace('/passport/account-login')
xuzhenghua committed
119 120 121 122
        }, 1000)
      } else {
        Toast.info(res.data.msg, 2, null, false)
      }
zhanghaozhe committed
123 124 125 126
    })
}

function bindMobileSetPassword(values, props) {
xuzhenghua committed
127 128 129 130 131 132 133 134 135 136
  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'))
xuzhenghua committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151

        // setTimeout(function () {
        //   if (local_redirect_url) {
        //     const {pathname, search, hash} = local_redirect_url
        //     history.replace(pathname + search + hash)
        //   } else {
        //     history.replace(from.pathname)
        //   }
        // }, 1000)

        let HistoryUrl = window.localStorage.getItem('HistoryUrl')
        setTimeout(()=> {
          if (HistoryUrl) {
            let historyUrl = window.localStorage.getItem('HistoryUrl')
            props.history.push(historyUrl)
xuzhenghua committed
152 153 154 155 156 157 158
          } else {
            history.replace(from.pathname)
          }
        }, 1000)
      } else {
        Toast.info(res.data.msg, 2, null, false)
      }
zhanghaozhe committed
159 160 161
    })
}

zhanghaozhe committed
162
export default compose(
xuzhenghua committed
163 164 165 166 167 168
  connect(
    state => ({user: state.user}),
    {setCurrentUser}
  ),
  withFormik(formikConfig)
)(SetPassword)