index.js 5.82 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 14
import { Link } from "react-router-dom"
import { isEmpty } from "lodash"
import { connect } from "react-redux"
zhanghaozhe committed
15
import { setCurrentUser } from 'src/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

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


  //
  // componentDidMount() {
  //   const {location} = this.props
  //   const {action} = this.props.history
  //   let pathname = location.state && location.state.from && location.state.from.pathname
  //   let search = location.state && location.state.from && location.state.from.search
  //
  //   console.log(location);
  //   console.log(location.state);
  //   console.log(action,pathname,search);
  //
  //   // if(action !== 'PUSH'){
  //   //   let historyUrl = `${pathname}${search}`
  //   //   window.localStorage.setItem('HistoryUrl',historyUrl)
  //   // }
  //
  // }


xuzhenghua committed
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
  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'}}>
xuzhenghua committed
84
            <span onClick={this.toFrom}>跳过</span>
xuzhenghua committed
85 86 87 88 89
          </div>
        </div>
      </>
    )
  }
zhanghaozhe committed
90 91
}

zhanghaozhe committed
92
const formikConfig = {
xuzhenghua committed
93 94 95 96 97 98 99
  mapPropsToValues() {
    return {
      password: '',
      agreement: true
    }
  },
  handleSubmit: (values, {props}) => {
FE committed
100 101 102
    const {location} = props

    let from = location.state && location.state.records && location.state.records[location.state.records.length - 2] || {pathname: '/'}
xuzhenghua committed
103 104 105 106 107
    if (from.pathname.includes('forgot-password')) {
      forgotPasswordReset(values, props)
    } else {
      bindMobileSetPassword(values, props)
    }
zhanghaozhe committed
108

xuzhenghua committed
109 110 111 112 113 114 115 116 117 118 119 120
  },
  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
121
    }
xuzhenghua committed
122 123
    return errors
  }
zhanghaozhe committed
124 125
}

zhanghaozhe committed
126 127

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

xuzhenghua committed
130 131 132 133 134 135 136 137 138
  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
139
           props.history.replace('/passport/account-login')
xuzhenghua committed
140 141 142 143
        }, 1000)
      } else {
        Toast.info(res.data.msg, 2, null, false)
      }
zhanghaozhe committed
144 145 146 147
    })
}

function bindMobileSetPassword(values, props) {
xuzhenghua committed
148 149 150 151 152 153 154 155 156 157
  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
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

        // 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
173 174 175 176 177 178 179
          } else {
            history.replace(from.pathname)
          }
        }, 1000)
      } else {
        Toast.info(res.data.msg, 2, null, false)
      }
zhanghaozhe committed
180 181 182
    })
}

zhanghaozhe committed
183
export default compose(
xuzhenghua committed
184 185 186 187 188 189
  connect(
    state => ({user: state.user}),
    {setCurrentUser}
  ),
  withFormik(formikConfig)
)(SetPassword)