index.js 3.99 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
zhanghaozhe committed
26

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

zhanghaozhe committed
62 63

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

    componentDidUpdate() {
        this.routeWhenUserLoggedIn()
    }

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


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


120 121 122 123 124 125 126
export default compose(
    connect(
        state => ({user: state.user}),
        null
    ),
    WithFullSize
)(Passport)