fix: 修改action,去掉登录退出的强制刷新

This commit is contained in:
yhui.yang 2017-07-21 12:27:24 +08:00
parent 63f18dad65
commit ab732c5cf6
6 changed files with 40 additions and 50 deletions

View File

@ -9,7 +9,7 @@ import { checkLoginState } from './actions/login'
const LOADING_STATUS = 0; const LOADING_STATUS = 0;
const GUEST_STATUS = 1; const GUEST_STATUS = 1;
const MEMBER_STATUS = 2; // const MEMBER_STATUS = 2;
class App extends Component { class App extends Component {
@ -21,7 +21,8 @@ class App extends Component {
} }
} }
static propTypes = { static propTypes = {
checkLoginState:PropTypes.func checkLoginState:PropTypes.func,
loginState:PropTypes.number
} }
route = (status) => { route = (status) => {
let r; let r;
@ -61,34 +62,18 @@ class App extends Component {
} }
componentDidMount() { componentDidMount() {
this.props.checkLoginState().then((res) => { this.props.checkLoginState();
console.log(res);
if (res.payload.data.errcode === 0 && res.payload.data.data._id > 0) {
this.setState({
login: MEMBER_STATUS
})
} else {
this.setState({
login: GUEST_STATUS
})
}
}).catch((err) => {
this.setState({
login: GUEST_STATUS
});
console.log(err)
});
} }
render() { render() {
return this.route(this.state.login) return this.route(this.props.loginState)
} }
} }
export default connect( export default connect(
state => { state => {
return{ return{
login:state.login.isLogin loginState:state.login.loginState
} }
}, },
{ {

View File

@ -6,12 +6,19 @@ import {
} from '../constants/action-types.js'; } from '../constants/action-types.js';
import axios from 'axios'; import axios from 'axios';
const checkLoginState = () => { const checkLoginState = () => {
return { return(dispatch)=> {
axios.get('/user/status').then((res) => {
console.log(res);
dispatch({
type: GET_LOGIN_STATE, type: GET_LOGIN_STATE,
// payload 可以返回 Promise异步请求使用 axios 即可 payload: res
payload: axios.get('/user/status') });
}; }).catch((err) => {
console.log(err);
})
}
} }
const loginActions = (data) => { const loginActions = (data) => {
@ -24,7 +31,6 @@ const loginActions = (data) => {
data: res data: res
} }
}); });
location.reload();
} else { } else {
console.log('登录失败,errcode不为0'); console.log('登录失败,errcode不为0');
} }
@ -58,7 +64,6 @@ const logoutActions = () => {
dispatch({ dispatch({
type: LOGIN_OUT type: LOGIN_OUT
}) })
location.reload();
} }
}).catch((err) => { }).catch((err) => {
console.log(err); console.log(err);

View File

@ -9,7 +9,7 @@ import { checkLoginState } from './actions/login'
const LOADING_STATUS = 0; const LOADING_STATUS = 0;
const GUEST_STATUS = 1; const GUEST_STATUS = 1;
const MEMBER_STATUS = 2; // const MEMBER_STATUS = 2;
class App extends Component { class App extends Component {
@ -21,7 +21,8 @@ class App extends Component {
} }
} }
static propTypes = { static propTypes = {
checkLoginState:PropTypes.func checkLoginState:PropTypes.func,
loginState:PropTypes.number
} }
route = (status) => { route = (status) => {
let r; let r;
@ -61,34 +62,18 @@ class App extends Component {
} }
componentDidMount() { componentDidMount() {
this.props.checkLoginState().then((res) => { this.props.checkLoginState();
console.log(res);
if (res.payload.data.errcode === 0 && res.payload.data.data._id > 0) {
this.setState({
login: MEMBER_STATUS
})
} else {
this.setState({
login: GUEST_STATUS
})
}
}).catch((err) => {
this.setState({
login: GUEST_STATUS
});
console.log(err)
});
} }
render() { render() {
return this.route(this.state.login) return this.route(this.props.loginState)
} }
} }
export default connect( export default connect(
state => { state => {
return{ return{
login:state.login.isLogin loginState:state.login.loginState
} }
}, },
{ {

View File

@ -124,7 +124,6 @@ class HeaderCom extends Component {
} }
render () { render () {
const { login, user, msg, uid, curKey } = this.props; const { login, user, msg, uid, curKey } = this.props;
console.log(curKey);
return ( return (
<acticle className="header-box"> <acticle className="header-box">
<Layout className="'layout"> <Layout className="'layout">

View File

@ -5,10 +5,15 @@ import {
GET_LOGIN_STATE GET_LOGIN_STATE
} from '../../constants/action-types'; } from '../../constants/action-types';
const LOADING_STATUS = 0;
const GUEST_STATUS = 1;
const MEMBER_STATUS = 2;
const initialState = { const initialState = {
isLogin: false, isLogin: false,
userName: null, userName: null,
uid: null, uid: null,
loginState:LOADING_STATUS,
loginWrapActiveKey:"1" loginWrapActiveKey:"1"
}; };
@ -18,6 +23,7 @@ export default (state = initialState, action) => {
return { return {
...state, ...state,
isLogin: (action.payload.data.errcode == 0), isLogin: (action.payload.data.errcode == 0),
loginState: (action.payload.data.errcode == 0)?MEMBER_STATUS:GUEST_STATUS,
userName: action.payload.data.data ? action.payload.data.data.username : null userName: action.payload.data.data ? action.payload.data.data.username : null
}; };
} }
@ -25,6 +31,7 @@ export default (state = initialState, action) => {
return { return {
...state, ...state,
isLogin: true, isLogin: true,
loginState: MEMBER_STATUS,
uid: action.payload.data.uid, uid: action.payload.data.uid,
userName: action.payload.data.userName userName: action.payload.data.userName
}; };
@ -33,6 +40,7 @@ export default (state = initialState, action) => {
return{ return{
...state, ...state,
isLogin: false, isLogin: false,
loginState: GUEST_STATUS,
userName: null, userName: null,
uid: null uid: null
} }

View File

@ -5,10 +5,15 @@ import {
GET_LOGIN_STATE GET_LOGIN_STATE
} from '../../constants/action-types'; } from '../../constants/action-types';
const LOADING_STATUS = 0;
const GUEST_STATUS = 1;
const MEMBER_STATUS = 2;
const initialState = { const initialState = {
isLogin: false, isLogin: false,
userName: null, userName: null,
uid: null, uid: null,
loginState:LOADING_STATUS,
loginWrapActiveKey:"1" loginWrapActiveKey:"1"
}; };
@ -18,6 +23,7 @@ export default (state = initialState, action) => {
return { return {
...state, ...state,
isLogin: (action.payload.data.errcode == 0), isLogin: (action.payload.data.errcode == 0),
loginState: (action.payload.data.errcode == 0)?MEMBER_STATUS:GUEST_STATUS,
userName: action.payload.data.data ? action.payload.data.data.username : null userName: action.payload.data.data ? action.payload.data.data.username : null
}; };
} }
@ -25,6 +31,7 @@ export default (state = initialState, action) => {
return { return {
...state, ...state,
isLogin: true, isLogin: true,
loginState: MEMBER_STATUS,
uid: action.payload.data.uid, uid: action.payload.data.uid,
userName: action.payload.data.userName userName: action.payload.data.userName
}; };
@ -33,6 +40,7 @@ export default (state = initialState, action) => {
return{ return{
...state, ...state,
isLogin: false, isLogin: false,
loginState: GUEST_STATUS,
userName: null, userName: null,
uid: null uid: null
} }