2017-08-07 19:34:34 +08:00
|
|
|
import axios from 'axios';
|
2017-08-07 15:40:21 +08:00
|
|
|
|
2017-08-07 19:34:34 +08:00
|
|
|
// Actions
|
2017-08-10 21:20:57 +08:00
|
|
|
const LOGIN = 'yapi/user/LOGIN';
|
|
|
|
const LOGIN_OUT = 'yapi/user/LOGIN_OUT';
|
|
|
|
const LOGIN_TYPE = 'yapi/user/LOGIN_TYPE';
|
|
|
|
const GET_LOGIN_STATE = 'yapi/user/GET_LOGIN_STATE';
|
|
|
|
const REGISTER = 'yapi/user/REGISTER';
|
2017-08-28 17:24:17 +08:00
|
|
|
const SET_BREADCRUMB = 'yapi/user/SET_BREADCRUMB';
|
2017-09-28 19:41:10 +08:00
|
|
|
const CHANGE_STUDY_TIP = 'yapi/user/CHANGE_STUDY_TIP';
|
|
|
|
const FINISH_STUDY = 'yapi/user/FINISH_STUDY';
|
2017-08-07 19:34:34 +08:00
|
|
|
|
|
|
|
// Reducer
|
2017-08-07 15:40:21 +08:00
|
|
|
const LOADING_STATUS = 0;
|
|
|
|
const GUEST_STATUS = 1;
|
|
|
|
const MEMBER_STATUS = 2;
|
2017-08-10 21:20:57 +08:00
|
|
|
// Reducer user
|
2017-08-07 15:40:21 +08:00
|
|
|
const initialState = {
|
|
|
|
isLogin: false,
|
|
|
|
userName: null,
|
|
|
|
uid: null,
|
2017-08-10 21:20:57 +08:00
|
|
|
email: '',
|
2017-08-07 15:40:21 +08:00
|
|
|
loginState: LOADING_STATUS,
|
2017-08-28 17:24:17 +08:00
|
|
|
loginWrapActiveKey: '1',
|
|
|
|
role: '',
|
|
|
|
type: '',
|
|
|
|
// breadcrumb: [{
|
|
|
|
// name: 'name',
|
|
|
|
// href: 'group'
|
|
|
|
// }, {
|
|
|
|
// name: '当前页面'
|
|
|
|
// }]
|
2017-09-28 19:41:10 +08:00
|
|
|
breadcrumb: [],
|
2017-09-29 15:22:39 +08:00
|
|
|
studyTip: 0,
|
|
|
|
study: false
|
2017-08-07 15:40:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default (state = initialState, action) => {
|
|
|
|
switch (action.type) {
|
|
|
|
case GET_LOGIN_STATE: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isLogin: (action.payload.data.errcode == 0),
|
|
|
|
role: action.payload.data.data ? action.payload.data.data.role:null,
|
|
|
|
loginState: (action.payload.data.errcode == 0)?MEMBER_STATUS:GUEST_STATUS,
|
|
|
|
userName: action.payload.data.data ? action.payload.data.data.username : null,
|
2017-08-17 16:55:52 +08:00
|
|
|
uid: action.payload.data.data ? action.payload.data.data._id : null,
|
2017-09-28 16:16:55 +08:00
|
|
|
type: action.payload.data.data ? action.payload.data.data.type : null,
|
|
|
|
study: action.payload.data.data ? action.payload.data.data.study : false
|
2017-08-07 15:40:21 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
case LOGIN: {
|
|
|
|
if (action.payload.data.errcode === 0) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isLogin: true,
|
|
|
|
loginState: MEMBER_STATUS,
|
|
|
|
uid: action.payload.data.data.uid,
|
2017-08-15 16:55:46 +08:00
|
|
|
userName: action.payload.data.data.username,
|
2017-08-17 20:08:10 +08:00
|
|
|
role: action.payload.data.data.role,
|
2017-09-28 16:16:55 +08:00
|
|
|
type: action.payload.data.data.type,
|
|
|
|
study: action.payload.data.data.study
|
2017-08-07 15:40:21 +08:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case LOGIN_OUT: {
|
|
|
|
return{
|
|
|
|
...state,
|
|
|
|
isLogin: false,
|
|
|
|
loginState: GUEST_STATUS,
|
|
|
|
userName: null,
|
2017-08-15 16:55:46 +08:00
|
|
|
uid: null,
|
2017-08-28 17:24:17 +08:00
|
|
|
role: '',
|
|
|
|
type: ''
|
2017-08-07 15:40:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case LOGIN_TYPE: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
loginWrapActiveKey: action.index
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case REGISTER: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isLogin: true,
|
|
|
|
loginState: MEMBER_STATUS,
|
|
|
|
uid: action.payload.data.data.uid,
|
2017-08-17 20:08:10 +08:00
|
|
|
userName: action.payload.data.data.username,
|
2017-09-29 15:22:39 +08:00
|
|
|
type: action.payload.data.data.type,
|
|
|
|
study: action.payload.data.data ? action.payload.data.data.study : false
|
2017-08-07 15:40:21 +08:00
|
|
|
};
|
|
|
|
}
|
2017-08-28 17:24:17 +08:00
|
|
|
case SET_BREADCRUMB: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
breadcrumb: action.data
|
|
|
|
};
|
|
|
|
}
|
2017-09-28 19:41:10 +08:00
|
|
|
case CHANGE_STUDY_TIP: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
studyTip: state.studyTip + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case FINISH_STUDY: {
|
|
|
|
return {
|
|
|
|
...state,
|
2017-09-29 15:22:39 +08:00
|
|
|
study: true,
|
|
|
|
studyTip: 0
|
2017-09-28 19:41:10 +08:00
|
|
|
};
|
|
|
|
}
|
2017-08-07 15:40:21 +08:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
2017-08-07 19:34:34 +08:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
export function checkLoginState() {
|
|
|
|
return(dispatch)=> {
|
2017-08-11 10:25:19 +08:00
|
|
|
axios.get('/api/user/status').then((res) => {
|
2017-08-07 19:34:34 +08:00
|
|
|
dispatch({
|
|
|
|
type: GET_LOGIN_STATE,
|
|
|
|
payload: res
|
|
|
|
});
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loginActions(data) {
|
|
|
|
return {
|
|
|
|
type: LOGIN,
|
2017-08-11 10:25:19 +08:00
|
|
|
payload: axios.post('/api/user/login', data)
|
2017-08-07 19:34:34 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function regActions(data) {
|
|
|
|
const { email, password, userName } = data;
|
|
|
|
const param = {
|
|
|
|
email,
|
|
|
|
password,
|
|
|
|
username: userName
|
|
|
|
};
|
|
|
|
return {
|
|
|
|
type: REGISTER,
|
2017-08-11 10:25:19 +08:00
|
|
|
payload: axios.post('/api/user/reg', param)
|
2017-08-07 19:34:34 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function logoutActions() {
|
|
|
|
return {
|
|
|
|
type: LOGIN_OUT,
|
2017-08-11 10:25:19 +08:00
|
|
|
payload: axios.get('/api/user/logout')
|
2017-08-07 19:34:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function loginTypeAction(index) {
|
|
|
|
return{
|
|
|
|
type: LOGIN_TYPE,
|
|
|
|
index
|
|
|
|
}
|
|
|
|
}
|
2017-08-28 17:24:17 +08:00
|
|
|
|
|
|
|
export function setBreadcrumb(data) {
|
|
|
|
return{
|
|
|
|
type: SET_BREADCRUMB,
|
|
|
|
data
|
|
|
|
}
|
|
|
|
}
|
2017-09-28 19:41:10 +08:00
|
|
|
|
|
|
|
export function changeStudyTip() {
|
|
|
|
return {
|
|
|
|
type: CHANGE_STUDY_TIP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function finishStudy() {
|
|
|
|
return {
|
|
|
|
type: FINISH_STUDY,
|
|
|
|
payload: axios.get('/api/user/up_study')
|
|
|
|
}
|
|
|
|
}
|