index.js 4.41 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
import ForgotPasswordEmail from './forgotPasswordEmail'
zhanghaozhe committed
12 13 14
import { connect } from "react-redux"
import { compose } from "redux"
import { getParam } from "@/utils"
FE committed
15
import StudentRoot from './studentRoot';
zhanghaozhe committed
16

zhanghaozhe committed
17 18 19 20
import account from './icons/account.png'
import qq from './icons/qq.png'
import sina from './icons/sina.png'
import wechat from './icons/wechat.png'
zhanghaozhe committed
21 22 23


class Passport extends Component {
zhanghaozhe committed
24

zhanghaozhe committed
25
    redirectURL = '/'
zhanghaozhe committed
26
    count = 1
xuzhenghua committed
27
    blackList = new Set(['/passport/binding-tel', '/passport/set-password'])
zhanghaozhe committed
28

zhanghaozhe committed
29
    constructor(props) {
zhanghaozhe committed
30
        super(props)
zhanghaozhe committed
31 32
        const {location} = props
        const {pathname, search, hash} = location
zhanghaozhe committed
33
        const from = location.state && location.state.from || {pathname: pathname, search: search, hash: hash}
xuzhenghua committed
34 35
        const referrer = document.referrer
        this.redirectURL = referrer ? referrer : window.location.origin + from.pathname + from.search + from.hash
zhanghaozhe committed
36 37 38 39
        this.state = {
            loginWays: [
                {
                    logo: account,
zhanghaozhe committed
40 41
                    text: '账号登录',
                    id: 'account'
zhanghaozhe committed
42 43 44
                },
                {
                    logo: wechat,
zhanghaozhe committed
45
                    text: '微信',
zhanghaozhe committed
46 47
                    url: '',
                    id: 'wechat'
zhanghaozhe committed
48 49 50
                },
                {
                    logo: qq,
zhanghaozhe committed
51
                    text: 'QQ',
FE committed
52
                    url: `${API["passport-api"]}/m/login/qqLogin?redirect_url=${encodeURIComponent(this.redirectURL)}`,
zhanghaozhe committed
53
                    id: 'qq'
zhanghaozhe committed
54 55 56
                },
                {
                    logo: sina,
zhanghaozhe committed
57
                    text: '新浪',
FE committed
58
                    url: `${API['passport-api']}/m/login/sinaLogin?redirect_url=${encodeURIComponent(this.redirectURL)}`,
zhanghaozhe committed
59 60
                    id: 'sina'
                }
zhanghaozhe committed
61 62 63 64
            ]
        }
    }

zhanghaozhe committed
65 66

    componentDidMount() {
zhanghaozhe committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
        const {history} = this.props
        if (!window.passportHistoryListener) {
            this.unlisten = history.listen((location, action) => {

                window.passportHistoryListener = this.unlisten

                location.pathname !== '/country' && !location.pathname.startsWith('/passport') && this.unlisten()

                if (action === 'PUSH') {
                    this.count++
                } else if (action === 'POP') {
                    this.count--
                }
            })

        }
zhanghaozhe committed
83 84 85 86 87 88 89
    }

    componentDidUpdate() {
        this.routeWhenUserLoggedIn()
    }

    routeWhenUserLoggedIn = () => {
zhanghaozhe committed
90
        let {hasError} = this.props.user
xuzhenghua committed
91
        if (!hasError && !this.blackList.has(this.props.location.pathname)) {
zhanghaozhe committed
92 93
            const redirectURI = getParam('redirect')
            redirectURI ? (window.location.href = redirectURI) : this.props.history.go(-this.count)
zhanghaozhe committed
94 95 96 97
        }
    }


zhanghaozhe committed
98
    render() {
zhanghaozhe committed
99
        let {match, location} = this.props
zhanghaozhe committed
100 101 102
        return (
            <div className="passport">
                <Switch>
zhanghaozhe committed
103 104
                    <Redirect exact
                              from={'/passport'}
zhanghaozhe committed
105
                              to={{...location, ...{pathname: '/passport/login'}}}
zhanghaozhe committed
106
                    />
FE committed
107
                    <Route path={match.url + '/student-login'} component={StudentRoot}/>
zhanghaozhe committed
108
                    <Route path={match.url + '/login'}
109 110 111
                           render={props => {
                               return <Login {...props} loginWays={this.state.loginWays}/>
                           }}/>
zhanghaozhe committed
112
                    <Route path={match.url + '/account-login'} component={AccountLogin}/>
zhanghaozhe committed
113
                    <Route path={match.url + '/forgot-password'} component={ForgotPassword}/>
zhanghaozhe committed
114
                    <Route path={match.url + '/forgot-password-email'} component={ForgotPasswordEmail}/>
xuzhenghua committed
115 116 117
                    <Route path={match.url + '/set-password'} render={props => {
                        return <SetPassword {...props} count={this.count}/>
                    }}/>
zhanghaozhe committed
118
                    <Route path={match.url + '/binding-tel'} component={BindingTel}/>
zhanghaozhe committed
119 120 121 122 123 124 125
                </Switch>
            </div>
        )
    }
}


126 127 128 129 130 131
export default compose(
    connect(
        state => ({user: state.user}),
        null
    ),
    WithFullSize
xuzhenghua committed
132
)(Passport)