Commit da9aeaf6 by zhanghaozhe

登录相关逻辑

parent 9765af18
......@@ -7,7 +7,6 @@ import { withRouter } from 'react-router-dom'
import { compose } from 'redux'
import { getParam, http } from "@/utils";
import { Toast } from "antd-mobile";
import jsCookie from 'js-cookie'
import { addDays } from 'date-fns'
......@@ -27,7 +26,8 @@ class App extends Component {
componentDidMount() {
//平台信息
cookie.set('plat', '5')
cookie.set('plat', '5', {domain: '.julyedu.com'})
this.props.startFetchUser()
http.get(`${API.home}/m/user_info`).then(res => {
......@@ -35,6 +35,9 @@ class App extends Component {
})
//微信
let code = getParam('code')
if (code) {
......@@ -42,7 +45,6 @@ class App extends Component {
http.get(`${api['home']}/m/wx_loginInfo/code/${code}`)
.then(res => {
let data = res.data
console.log(res)
if (data.errno == 200) {
if (data.data['is_bind_mobile']) {
window.location.assign(data.data.url)
......@@ -51,9 +53,9 @@ class App extends Component {
let user = this.transformWxUser(res)
let {role, uid, token} = data.data
let expires = {expires: addDays(new Date(), 90)}
jsCookie.set('role', role, expires)
jsCookie.set('uid', uid, expires)
jsCookie.set('token', token, expires)
cookie.set('role', role, expires)
cookie.set('uid', uid, expires)
cookie.set('token', token, expires)
this.props.setCurrentUser(user)
}
} else {
......
......@@ -64,7 +64,7 @@ class Passport extends Component {
let {history} = this.props
let {data} = this.props.user
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 { connect } from "react-redux";
import jsCookie from 'js-cookie'
import RouteMiddlePage from './route-middle-page'
import Loading from '@/common/Loading'
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 (
<Route {...rest} render={props => {
return authenticated
? <Component {...props}/>
: <RouteMiddlePage state={{from: props.location}}/>
return <Loading isLoading={isLoading}>
{authenticated
? <Component {...props}/>
: <RouteMiddlePage state={{from: props.location}}/>}
</Loading>
}}/>
);
)
};
export default connect(
......
......@@ -13,7 +13,7 @@ function RouteMiddlePage(props) {
let {user, location, history} = props
if (history.action === 'POP') {
if (history.action === 'POP' && history.length > 2) {
history.goBack();
return
}
......@@ -27,7 +27,7 @@ function RouteMiddlePage(props) {
if (data && Object.values(data).every(item => !!item)) {
history.replace(location.pathname)
setLoadingState(false)
}else{
} else {
history.replace('/passport', {from: location})
}
}
......@@ -37,7 +37,7 @@ function RouteMiddlePage(props) {
return (
<div className={'loading-route'}>
<LoadingComponent isLoading={isLoading}>
<div></div>
<div/>
</LoadingComponent>
</div>
);
......
......@@ -54,7 +54,7 @@ const LOGOUT = 'LOGOUT'
const logout = () => dispatch => {
jsCookie.remove('token', {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'
const initialState = {
hasError: false,
code: 0,
msg: '',
data: {
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