Commit da9aeaf6 by zhanghaozhe

登录相关逻辑

parent 9765af18
...@@ -7,7 +7,6 @@ import { withRouter } from 'react-router-dom' ...@@ -7,7 +7,6 @@ import { withRouter } from 'react-router-dom'
import { compose } from 'redux' import { compose } from 'redux'
import { getParam, http } from "@/utils"; import { getParam, http } from "@/utils";
import { Toast } from "antd-mobile"; import { Toast } from "antd-mobile";
import jsCookie from 'js-cookie'
import { addDays } from 'date-fns' import { addDays } from 'date-fns'
...@@ -27,7 +26,8 @@ class App extends Component { ...@@ -27,7 +26,8 @@ class App extends Component {
componentDidMount() { componentDidMount() {
//平台信息 //平台信息
cookie.set('plat', '5') cookie.set('plat', '5', {domain: '.julyedu.com'})
this.props.startFetchUser() this.props.startFetchUser()
http.get(`${API.home}/m/user_info`).then(res => { http.get(`${API.home}/m/user_info`).then(res => {
...@@ -35,6 +35,9 @@ class App extends Component { ...@@ -35,6 +35,9 @@ class App extends Component {
}) })
//微信
let code = getParam('code') let code = getParam('code')
if (code) { if (code) {
...@@ -42,7 +45,6 @@ class App extends Component { ...@@ -42,7 +45,6 @@ class App extends Component {
http.get(`${api['home']}/m/wx_loginInfo/code/${code}`) http.get(`${api['home']}/m/wx_loginInfo/code/${code}`)
.then(res => { .then(res => {
let data = res.data let data = res.data
console.log(res)
if (data.errno == 200) { if (data.errno == 200) {
if (data.data['is_bind_mobile']) { if (data.data['is_bind_mobile']) {
window.location.assign(data.data.url) window.location.assign(data.data.url)
...@@ -51,9 +53,9 @@ class App extends Component { ...@@ -51,9 +53,9 @@ class App extends Component {
let user = this.transformWxUser(res) let user = this.transformWxUser(res)
let {role, uid, token} = data.data let {role, uid, token} = data.data
let expires = {expires: addDays(new Date(), 90)} let expires = {expires: addDays(new Date(), 90)}
jsCookie.set('role', role, expires) cookie.set('role', role, expires)
jsCookie.set('uid', uid, expires) cookie.set('uid', uid, expires)
jsCookie.set('token', token, expires) cookie.set('token', token, expires)
this.props.setCurrentUser(user) this.props.setCurrentUser(user)
} }
} else { } else {
......
...@@ -64,7 +64,7 @@ class Passport extends Component { ...@@ -64,7 +64,7 @@ class Passport extends Component {
let {history} = this.props let {history} = this.props
let {data} = this.props.user let {data} = this.props.user
if (data && Object.values(data).filter(item => !!item).length) { if (data && Object.values(data).filter(item => !!item).length) {
history.action === 'POP' && history.go(-1) history.action === 'POP' && history.length <= 3 ? history.push('/') : history.go(-1)
} }
} }
......
import React from 'react'; import React, { useState, useEffect } from 'react';
import { Route } from "react-router-dom"; import { Route } from "react-router-dom";
import { connect } from "react-redux"; import { connect } from "react-redux";
import jsCookie from 'js-cookie'
import RouteMiddlePage from './route-middle-page' import RouteMiddlePage from './route-middle-page'
import Loading from '@/common/Loading'
const PrivateRoute = ({component: Component, path, user, ...rest}) => { const PrivateRoute = ({component: Component, path, user, ...rest}) => {
let authenticated = jsCookie.get('token') && jsCookie.get('uid') const [isLoading, setLoadingState] = useState(true)
const [authenticated, setAuthorization] = useState(false)
useEffect(() => {
let _auth = !user.hasError && user.code != 4040
typeof _auth !== 'undefined' && (setAuthorization(_auth) , setLoadingState(false))
})
return ( return (
<Route {...rest} render={props => { <Route {...rest} render={props => {
return authenticated return <Loading isLoading={isLoading}>
{authenticated
? <Component {...props}/> ? <Component {...props}/>
: <RouteMiddlePage state={{from: props.location}}/> : <RouteMiddlePage state={{from: props.location}}/>}
</Loading>
}}/> }}/>
); )
}; };
export default connect( export default connect(
......
...@@ -13,7 +13,7 @@ function RouteMiddlePage(props) { ...@@ -13,7 +13,7 @@ function RouteMiddlePage(props) {
let {user, location, history} = props let {user, location, history} = props
if (history.action === 'POP') { if (history.action === 'POP' && history.length > 2) {
history.goBack(); history.goBack();
return return
} }
...@@ -27,7 +27,7 @@ function RouteMiddlePage(props) { ...@@ -27,7 +27,7 @@ function RouteMiddlePage(props) {
if (data && Object.values(data).every(item => !!item)) { if (data && Object.values(data).every(item => !!item)) {
history.replace(location.pathname) history.replace(location.pathname)
setLoadingState(false) setLoadingState(false)
}else{ } else {
history.replace('/passport', {from: location}) history.replace('/passport', {from: location})
} }
} }
...@@ -37,7 +37,7 @@ function RouteMiddlePage(props) { ...@@ -37,7 +37,7 @@ function RouteMiddlePage(props) {
return ( return (
<div className={'loading-route'}> <div className={'loading-route'}>
<LoadingComponent isLoading={isLoading}> <LoadingComponent isLoading={isLoading}>
<div></div> <div/>
</LoadingComponent> </LoadingComponent>
</div> </div>
); );
......
...@@ -54,7 +54,7 @@ const LOGOUT = 'LOGOUT' ...@@ -54,7 +54,7 @@ const LOGOUT = 'LOGOUT'
const logout = () => dispatch => { const logout = () => dispatch => {
jsCookie.remove('token', {path: '/', domain: '.julyedu.com'}) jsCookie.remove('token', {path: '/', domain: '.julyedu.com'})
jsCookie.remove('uid', {path: '/', domain: '.julyedu.com'}) jsCookie.remove('uid', {path: '/', domain: '.julyedu.com'})
dispatch(setCurrentUser({})) dispatch(setCurrentUser({hasError: true}))
} }
......
...@@ -4,6 +4,7 @@ import { merge } from 'lodash' ...@@ -4,6 +4,7 @@ import { merge } from 'lodash'
const initialState = { const initialState = {
hasError: false, hasError: false,
code: 0,
msg: '', msg: '',
data: { data: {
username: '', username: '',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment