mirror of
https://github.com/YMFE/yapi.git
synced 2025-01-06 12:45:22 +08:00
feat: 获取登录状态并进行相应的跳转
This commit is contained in:
parent
8aa3e6b0b0
commit
5522cadac8
@ -1,12 +1,21 @@
|
||||
import {
|
||||
LOGIN,
|
||||
LOGIN_OUT,
|
||||
LOGIN_TYPE
|
||||
LOGIN_TYPE,
|
||||
GET_LOGIN_STATE
|
||||
} from '../constants/action-types.js';
|
||||
import axios from 'axios';
|
||||
import Cookies from 'universal-cookie';
|
||||
const cookies = new Cookies();
|
||||
|
||||
const checkLoginState = () => {
|
||||
return {
|
||||
type: GET_LOGIN_STATE,
|
||||
// payload 可以返回 Promise,异步请求使用 axios 即可
|
||||
payload: axios.get('/user/status')
|
||||
};
|
||||
}
|
||||
|
||||
const loginActions = (data) => {
|
||||
return (dispatch) => {
|
||||
axios.post('/user/login', data).then((res) => {
|
||||
@ -69,5 +78,6 @@ export default {
|
||||
loginActions,
|
||||
regActions,
|
||||
logoutActions,
|
||||
loginTypeAction
|
||||
loginTypeAction,
|
||||
checkLoginState
|
||||
}
|
||||
|
@ -4,7 +4,8 @@ import PropTypes from 'prop-types'
|
||||
import { connect } from 'react-redux'
|
||||
import { Link } from 'react-router-dom'
|
||||
import { Icon, Layout, Menu} from 'antd'
|
||||
import { logoutActions, loginTypeAction} from '../../actions/login'
|
||||
import { checkLoginState, logoutActions, loginTypeAction} from '../../actions/login'
|
||||
import { withRouter } from 'react-router';
|
||||
|
||||
const { Header } = Layout;
|
||||
const ToolUser = (props)=> (
|
||||
@ -32,6 +33,7 @@ ToolGuest.propTypes={
|
||||
onReg:PropTypes.func
|
||||
}
|
||||
|
||||
@withRouter
|
||||
class HeaderCom extends Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
@ -40,12 +42,16 @@ class HeaderCom extends Component {
|
||||
}
|
||||
}
|
||||
static propTypes ={
|
||||
router: PropTypes.object,
|
||||
user: PropTypes.string,
|
||||
msg: PropTypes.string,
|
||||
login:PropTypes.bool,
|
||||
relieveLink:PropTypes.func,
|
||||
logoutActions:PropTypes.func,
|
||||
loginTypeAction:PropTypes.func
|
||||
checkLoginState:PropTypes.func,
|
||||
loginTypeAction:PropTypes.func,
|
||||
history: PropTypes.object,
|
||||
location: PropTypes.object
|
||||
}
|
||||
linkTo = (e) =>{
|
||||
this.setState({
|
||||
@ -69,7 +75,17 @@ class HeaderCom extends Component {
|
||||
e.preventDefault();
|
||||
this.props.loginTypeAction("2");
|
||||
}
|
||||
checkLoginState = () => {
|
||||
this.props.checkLoginState().then((res) => {
|
||||
if (res.payload.data.errcode !== 0) {
|
||||
this.props.history.push('/');
|
||||
}
|
||||
}).catch((err) => {
|
||||
console.log(err);
|
||||
})
|
||||
}
|
||||
render () {
|
||||
this.checkLoginState();
|
||||
const { login, user, msg } = this.props;
|
||||
return (
|
||||
<acticle className="header-box">
|
||||
@ -118,7 +134,7 @@ export default connect(
|
||||
},
|
||||
{
|
||||
loginTypeAction,
|
||||
logoutActions
|
||||
logoutActions,
|
||||
checkLoginState
|
||||
}
|
||||
)(HeaderCom)
|
||||
|
||||
|
@ -14,6 +14,7 @@ export const PROJECT_DEL = 'PROJECT_DEL'
|
||||
|
||||
// login
|
||||
export const LOGIN = 'LOGIN';
|
||||
export const GET_LOGIN_STATE = 'GET_LOGIN_STATE';
|
||||
export const REGISTER = 'REGISTER';
|
||||
|
||||
//header
|
||||
|
@ -1,7 +1,8 @@
|
||||
import {
|
||||
LOGIN,
|
||||
LOGIN_OUT,
|
||||
LOGIN_TYPE
|
||||
LOGIN_TYPE,
|
||||
GET_LOGIN_STATE
|
||||
} from '../../constants/action-types';
|
||||
|
||||
const initialState = {
|
||||
@ -13,8 +14,14 @@ const initialState = {
|
||||
|
||||
export default (state = initialState, action) => {
|
||||
switch (action.type) {
|
||||
case GET_LOGIN_STATE: {
|
||||
return {
|
||||
...state,
|
||||
isLogin: (action.payload.data.errcode == 0),
|
||||
userName: action.payload.data.data ? action.payload.data.data.username : null
|
||||
};
|
||||
}
|
||||
case LOGIN: {
|
||||
console.log(action);
|
||||
return {
|
||||
...state,
|
||||
isLogin: true,
|
||||
|
Loading…
Reference in New Issue
Block a user