From ab732c5cf6a6922ada923be6b317d48d63c2f76e Mon Sep 17 00:00:00 2001 From: "yhui.yang" Date: Fri, 21 Jul 2017 12:27:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9action=EF=BC=8C?= =?UTF-8?q?=E5=8E=BB=E6=8E=89=E7=99=BB=E5=BD=95=E9=80=80=E5=87=BA=E7=9A=84?= =?UTF-8?q?=E5=BC=BA=E5=88=B6=E5=88=B7=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/App.js | 27 ++++++--------------------- client/actions/login.js | 19 ++++++++++++------- client/app.js | 27 ++++++--------------------- client/components/Header/Header.js | 1 - client/reducer/Login/login.js | 8 ++++++++ client/reducer/login/login.js | 8 ++++++++ 6 files changed, 40 insertions(+), 50 deletions(-) diff --git a/client/App.js b/client/App.js index 9d354d9f..b207ccc5 100644 --- a/client/App.js +++ b/client/App.js @@ -9,7 +9,7 @@ import { checkLoginState } from './actions/login' const LOADING_STATUS = 0; const GUEST_STATUS = 1; -const MEMBER_STATUS = 2; +// const MEMBER_STATUS = 2; class App extends Component { @@ -21,7 +21,8 @@ class App extends Component { } } static propTypes = { - checkLoginState:PropTypes.func + checkLoginState:PropTypes.func, + loginState:PropTypes.number } route = (status) => { let r; @@ -61,34 +62,18 @@ class App extends Component { } componentDidMount() { - this.props.checkLoginState().then((res) => { - 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) - }); + this.props.checkLoginState(); } render() { - return this.route(this.state.login) + return this.route(this.props.loginState) } } export default connect( state => { return{ - login:state.login.isLogin + loginState:state.login.loginState } }, { diff --git a/client/actions/login.js b/client/actions/login.js index ce7f9d45..431318da 100644 --- a/client/actions/login.js +++ b/client/actions/login.js @@ -6,12 +6,19 @@ import { } from '../constants/action-types.js'; import axios from 'axios'; + const checkLoginState = () => { - return { - type: GET_LOGIN_STATE, - // payload 可以返回 Promise,异步请求使用 axios 即可 - payload: axios.get('/user/status') - }; + return(dispatch)=> { + axios.get('/user/status').then((res) => { + console.log(res); + dispatch({ + type: GET_LOGIN_STATE, + payload: res + }); + }).catch((err) => { + console.log(err); + }) + } } const loginActions = (data) => { @@ -24,7 +31,6 @@ const loginActions = (data) => { data: res } }); - location.reload(); } else { console.log('登录失败,errcode不为0'); } @@ -58,7 +64,6 @@ const logoutActions = () => { dispatch({ type: LOGIN_OUT }) - location.reload(); } }).catch((err) => { console.log(err); diff --git a/client/app.js b/client/app.js index 9d354d9f..b207ccc5 100644 --- a/client/app.js +++ b/client/app.js @@ -9,7 +9,7 @@ import { checkLoginState } from './actions/login' const LOADING_STATUS = 0; const GUEST_STATUS = 1; -const MEMBER_STATUS = 2; +// const MEMBER_STATUS = 2; class App extends Component { @@ -21,7 +21,8 @@ class App extends Component { } } static propTypes = { - checkLoginState:PropTypes.func + checkLoginState:PropTypes.func, + loginState:PropTypes.number } route = (status) => { let r; @@ -61,34 +62,18 @@ class App extends Component { } componentDidMount() { - this.props.checkLoginState().then((res) => { - 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) - }); + this.props.checkLoginState(); } render() { - return this.route(this.state.login) + return this.route(this.props.loginState) } } export default connect( state => { return{ - login:state.login.isLogin + loginState:state.login.loginState } }, { diff --git a/client/components/Header/Header.js b/client/components/Header/Header.js index abb85e4d..8d746cba 100644 --- a/client/components/Header/Header.js +++ b/client/components/Header/Header.js @@ -124,7 +124,6 @@ class HeaderCom extends Component { } render () { const { login, user, msg, uid, curKey } = this.props; - console.log(curKey); return ( diff --git a/client/reducer/Login/login.js b/client/reducer/Login/login.js index 72c4ce46..6db36794 100644 --- a/client/reducer/Login/login.js +++ b/client/reducer/Login/login.js @@ -5,10 +5,15 @@ import { GET_LOGIN_STATE } from '../../constants/action-types'; +const LOADING_STATUS = 0; +const GUEST_STATUS = 1; +const MEMBER_STATUS = 2; + const initialState = { isLogin: false, userName: null, uid: null, + loginState:LOADING_STATUS, loginWrapActiveKey:"1" }; @@ -18,6 +23,7 @@ export default (state = initialState, action) => { return { ...state, 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 }; } @@ -25,6 +31,7 @@ export default (state = initialState, action) => { return { ...state, isLogin: true, + loginState: MEMBER_STATUS, uid: action.payload.data.uid, userName: action.payload.data.userName }; @@ -33,6 +40,7 @@ export default (state = initialState, action) => { return{ ...state, isLogin: false, + loginState: GUEST_STATUS, userName: null, uid: null } diff --git a/client/reducer/login/login.js b/client/reducer/login/login.js index 72c4ce46..6db36794 100644 --- a/client/reducer/login/login.js +++ b/client/reducer/login/login.js @@ -5,10 +5,15 @@ import { GET_LOGIN_STATE } from '../../constants/action-types'; +const LOADING_STATUS = 0; +const GUEST_STATUS = 1; +const MEMBER_STATUS = 2; + const initialState = { isLogin: false, userName: null, uid: null, + loginState:LOADING_STATUS, loginWrapActiveKey:"1" }; @@ -18,6 +23,7 @@ export default (state = initialState, action) => { return { ...state, 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 }; } @@ -25,6 +31,7 @@ export default (state = initialState, action) => { return { ...state, isLogin: true, + loginState: MEMBER_STATUS, uid: action.payload.data.uid, userName: action.payload.data.userName }; @@ -33,6 +40,7 @@ export default (state = initialState, action) => { return{ ...state, isLogin: false, + loginState: GUEST_STATUS, userName: null, uid: null }