import React, { Component } from 'react'; import './index.less'; import { Modal, Icon, Button, Tooltip } from 'antd'; import Assets from '@src/components/Assets'; import { asyncSMessage } from '@src/services/AsyncTools'; import { Icon as GIcon } from '../Icon'; import { Button as GButton } from '../Button'; import { User } from '../../stores/user'; import { My } from '../../stores/my'; import { Common } from '../../stores/common'; import { MobileArea } from '../../../Constant'; const LOGIN_PHONE = 'LOGIN_PHONE'; const LOGIN_WX = 'LOGIN_WX'; const BIND_PHONE = 'BIND_PHONE'; const BIND_WX = 'BIND_WX'; const BIND_WX_ERROR = 'BIND_WX_ERROR'; export default class Login extends Component { constructor(props) { super(props); this.state = { type: LOGIN_PHONE }; } close() { User.closeLogin(); } login() { const { data, needEmail, mobileError, validError } = this.state; const { area, mobile, mobileVerifyCode, email } = data; if (mobileError !== '' || validError !== '') return; if (area === '' || mobile === '' || mobileVerifyCode === '') return; if (needEmail && email === '') return; User.login(area, mobile, mobileVerifyCode).then(() => { let handler = null; if (needEmail) { handler = My.bindEmail(email); } else { handler = Promise.resolve(); } handler.then((result) => { if (result.bindWechat) { this.close(); } else { this.setState({ type: BIND_WX }); } }); }) .catch(err => { if (err.message.indexOf('验证码') >= 0) { this.setState({ validError: err.message }); } else { this.setState({ mobileError: err.message }); } }); } bind() { const { data, needEmail, mobileError, validError } = this.state; const { area, mobile, mobileVerifyCode, email } = data; if (mobileError !== '' || validError !== '') return; if (area === '' || mobile === '' || mobileVerifyCode === '') return; if (needEmail && email === '') return; User.bind(area, mobile, mobileVerifyCode).then(() => { let handler = null; if (needEmail) { handler = My.bindEmail(email); } else { handler = Promise.resolve(); } handler.then(() => { this.close(); }); }) .catch(err => { if (err.message.indexOf('验证码') >= 0) { this.setState({ validError: err.message }); } else { this.setState({ mobileError: err.message }); } }); } scanLogin() { User.loginWechat('').then((result) => { if (result.id) { this.close(); } else { this.setState({ type: BIND_PHONE }); } }); } scanBind() { User.loginWechat('').then(() => { this.close(); }).catch(err => { this.setState({ type: BIND_WX_ERROR, wechatError: err.message }); }); } changeData(field, value) { let { data } = this.state; data = data || {}; data[field] = value; this.setState({ data }); } validMobile() { const { data } = this.state; const { area, mobile } = data; if (area === '' || mobile === '') return; User.validWechat(area, mobile) .then(result => { if (result) { this.setState({ mobileError: '' }); return User.validMobile(area, mobile) .then(r => { this.setState({ needEmail: r }); }); } this.setState({ needEmail: false }); return Promise.reject(new Error('该手机已绑定其他账号,请更换手机号码')); }) .catch(err => { this.setState({ mobileError: err.message }); }); } sendValid() { const { data, mobileError } = this.state; const { area, mobile } = data; if (area === '' || mobile === '' || mobileError !== '') return Promise.reject(); return Common.sendSms(area, mobile) .then((result) => { if (result) { asyncSMessage('发送成功'); this.setState({ mobileError: '', validError: '' }); } else { throw new Error('发送失败'); } }) .catch(err => { this.setState({ mobileError: err.message }); throw err; }); } render() { const { type } = this.state; const { user } = this.props; return ( {this.renderBody(type)} { this.close(); }} /> ); } renderBody(type) { switch (type) { case LOGIN_WX: return this.renderLoginWx(); case BIND_PHONE: return this.renderBindPhone(); case BIND_WX: return this.renderBindWx(); case BIND_WX_ERROR: return this.renderBindWxError(); case LOGIN_PHONE: default: return this.renderLoginPhone(); } } renderLoginPhone() { const { needEmail } = this.state; return (
手机号登录
{ this.changeData('area', value); }} onChange={(e) => { this.changeData('mobile', e.target.value); this.validMobile(e.target.value); }} /> { return this.sendValid(); }} /> {needEmail && { this.changeData('email', e.target.value); }} />} { this.setState({ type: LOGIN_WX }); }}>
); } renderLoginWx() { return (
微信扫码登录
请使用微信扫描二维码登录
{ this.setState({ type: LOGIN_PHONE }); }}>
); } renderBindPhone() { const { needEmail } = this.state; return (
绑定手机号
微信登录成功!为更好的使用服务,请您绑定手机号和邮箱。
{ this.changeData('area', value); }} onChange={(e) => { this.changeData('mobile', e.target.value); this.validMobile(e.target.value); }} /> { return this.sendValid(); }} /> {needEmail && { this.changeData('email', e.target.value); }} />}
); } renderBindWx() { return (
绑定微信号
手机号注册成功!为更好的使用服务,建议您绑定微信号。
请使用微信扫描二维码登录
{ this.close(); }}>跳过
); } renderBindWxError() { return (
绑定失败
该微信账户已绑定其他手机号,您可直接使用微信登入。
{ this.close(); }}>Ok
); } } class Input extends Component { render() { const { className = '', onChange, placeholder, value, error, left, right } = this.props; return (
{left &&
{left}
} onChange && onChange(data)} /> {right &&
{right}
}
); } } class SelectInput extends Component { constructor(props) { super(props); this.state = { showSelect: false }; } render() { const { showSelect } = this.state; const { className = '', onChange, placeholder, value, error, selectValue, select, onSelect } = this.props; return ( this.setState({ showSelect: !showSelect })}> {selectValue} {showSelect && } } value={value} placeholder={placeholder} onChange={data => onChange && onChange(data)} error={error} /> ); } } export class VerificationInput extends Component { constructor(props) { super(props); this.timeKey = null; this.state = { loading: 0 }; } componentWillUnmount() { if (this.timeKey) clearTimeout(this.timeKey); } onSend() { const { onSend, time = 60 } = this.props; if (onSend) { onSend() .then(() => { this.setTime(time); }); } } setTime(time) { this.setState({ loading: time }); this.timeKey = setTimeout(() => { this.setTime(time - 1); }, 1000); } render() { const { loading } = this.state; const { className = '', onChange, placeholder, value } = this.props; return ( this.onSend()}> 获取验证码 ) : ( 等待{loading}秒 ) } value={value} placeholder={placeholder} onChange={data => onChange && onChange(data)} /> ); } }