index.js 3.55 KB
Newer Older
zhanghaozhe committed
1
import React, { Component } from 'react'
zhanghaozhe committed
2
import { Route, Switch, Redirect } from 'react-router-dom'
zhanghaozhe committed
3 4

import './passport.scss'
zhanghaozhe committed
5
import { WithFullSize } from '@/HOCs'
zhanghaozhe committed
6
import Login from './login'
zhanghaozhe committed
7
import AccountLogin from './accountLogin'
zhanghaozhe committed
8 9 10
import ForgotPassword from './forgotPassword'
import SetPassword from './setPassword'
import BindingTel from './bindingTel'
zhanghaozhe committed
11 12
import { connect } from "react-redux";
import { compose } from "redux";
zhanghaozhe committed
13 14 15 16 17

import account from './account.png'
import qq from './qq.png'
import sina from './sina.png'
import wechat from './wechat.png'
zhanghaozhe committed
18
import { getParam } from "@/utils";
zhanghaozhe committed
19 20 21


class Passport extends Component {
zhanghaozhe committed
22

zhanghaozhe committed
23
    redirectURL = '/'
zhanghaozhe committed
24

zhanghaozhe committed
25 26
    constructor(props) {
        super(props);
zhanghaozhe committed
27 28 29
        const {location} = props
        const {pathname, search, hash} = location
        const {from} = location.state || {from: {pathname: pathname, search: search, hash: hash}}
zhanghaozhe committed
30
        this.redirectURL = window.location.origin + from.pathname + from.search + from.hash
zhanghaozhe committed
31 32 33 34 35 36 37 38
        this.state = {
            loginWays: [
                {
                    logo: account,
                    text: '账号登录'
                },
                {
                    logo: wechat,
zhanghaozhe committed
39
                    text: '微信',
zhanghaozhe committed
40
                    url: ''
zhanghaozhe committed
41 42 43
                },
                {
                    logo: qq,
zhanghaozhe committed
44
                    text: 'QQ',
zhanghaozhe committed
45
                    url: `${API["passport-api"]}/mob/qqlogin?redirect_url=${this.redirectURL}`
zhanghaozhe committed
46 47 48
                },
                {
                    logo: sina,
zhanghaozhe committed
49
                    text: '新浪',
zhanghaozhe committed
50
                    url: `${API['passport-api']}/mob/sinalogin?redirect_url=${this.redirectURL}`
zhanghaozhe committed
51 52 53 54 55
                },
            ]
        }
    }

zhanghaozhe committed
56 57 58 59 60 61 62 63 64 65 66

    componentDidMount() {
        this.routeWhenUserLoggedIn()
    }

    componentDidUpdate() {
        this.routeWhenUserLoggedIn()
    }


    routeWhenUserLoggedIn = () => {
FE committed
67
        let {history, location} = this.props
68 69
        let {data} = this.props.user
        if (data && Object.values(data).filter(item => !!item).length) {
zhanghaozhe committed
70
            if (history.action === 'POP' && history.length <= 3) {
FE committed
71 72
                history.push('/')
            } else {
zhanghaozhe committed
73 74 75 76 77
                if (!(this.props.user && this.props.user.stage)) {
                    const from = location.state && location.state.from || {pathname: '/', search: ''};
                    const redirectURI = getParam('redirect')
                    redirectURI ? (window.location.href = redirectURI) : history.replace(from);
                }
FE committed
78
            }
zhanghaozhe committed
79 80 81 82
        }
    }


zhanghaozhe committed
83
    render() {
zhanghaozhe committed
84
        let {match, location} = this.props
zhanghaozhe committed
85 86 87
        return (
            <div className="passport">
                <Switch>
zhanghaozhe committed
88 89
                    <Redirect exact
                              from={'/passport'}
zhanghaozhe committed
90
                              to={{...location, ...{pathname: '/passport/login'}}}
zhanghaozhe committed
91
                    />
zhanghaozhe committed
92
                    <Route path={match.url + '/login'}
93 94 95
                           render={props => {
                               return <Login {...props} loginWays={this.state.loginWays}/>
                           }}/>
zhanghaozhe committed
96
                    <Route path={match.url + '/account-login'} component={AccountLogin}/>
zhanghaozhe committed
97 98 99
                    <Route path={match.url + '/forgot-password'} component={ForgotPassword}/>
                    <Route path={match.url + '/set-password'} component={SetPassword}/>
                    <Route path={match.url + '/binding-tel'} component={BindingTel}/>
zhanghaozhe committed
100 101 102 103 104 105 106
                </Switch>
            </div>
        )
    }
}


107 108 109 110 111 112 113
export default compose(
    connect(
        state => ({user: state.user}),
        null
    ),
    WithFullSize
)(Passport)