index.js 4.27 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"
zhanghaozhe committed
15

zhanghaozhe committed
16 17 18 19
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
20 21 22


class Passport extends Component {
zhanghaozhe committed
23

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

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

zhanghaozhe committed
64 65

    componentDidMount() {
zhanghaozhe committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        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
82 83 84 85 86 87 88
    }

    componentDidUpdate() {
        this.routeWhenUserLoggedIn()
    }

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


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


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