用户登录,退出,用户信息
import { doLogin, doLogup, doLogout } from '../services/user';
import { getCurrentUser, fetchIsAuth, redirect } from '../utils/webSession';
export default {
namespace: 'user',
state: {
username: '',
isLogin: false,
modalVisible: false,
authToken: '',
pathname: '/',
},
subscriptions: {
setup({ dispatch, history }) {
history.listen(location => {
if (location.pathname === '/' && sessionStorage.getItem('userInfo')) {
//权限验证通过
dispatch({
type: 'loginSuccess',
payload: JSON.parse(sessionStorage.getItem('userInfo')) || {}
});
}
});
},
},
effects: {
*doLogin({ payload }, { call, put }) {
let {
userData,
resolve,
reject
} = payload;
yield put({ type: 'showLoading' });
const { data } = yield call(doLogin, userData);
if (data && data.success) {
let userInfo = data.userInfo;
yield sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
//登录成功
yield put({
type: 'loginSuccess',
payload: userInfo
});
resolve();
} else {
reject(data);
}
},
*doLogup({ payload }, { call, put }) {
let {
userData,
resolve,
reject
} = payload;
yield put({ type: 'showLoading' });
const { data } = yield call(doLogup, userData);
if (data && data.success) {
let userInfo = data.userInfo;
yield sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
//注册成功
yield put({
type: 'logupSuccess',
payload: userInfo
});
resolve();
} else {
reject(data);
}
},
*doLogout({ payload }, { call, put }) {
const { data } = yield call(doLogout);
if (data && data.success) {
yield sessionStorage.removeItem('userInfo');
//退出登录成功
yield put({
type: 'logoutSuccess',
payload: data.userInfo
});
}
}
},
reducers: {
login(state, action) {
return { ...state, modalVisible: true };
},
loginSuccess(state, action) {
let userInfo = action.payload;
return { ...state, ...userInfo, isLogin: true, modalVisible: false };
},
logupSuccess(state, action) {
let userInfo = action.payload;
return { ...state, ...userInfo, isLogin: true };
},
logoutSuccess(state, action) {
return { ...state, username: '', authToken: '', isLogin: false };
},
hideModal(state) {
return { ...state, modalVisible: false };
},
},
};
/services/user.js
import request from '../utils/request';
const LOGIN_API = '/system';
export async function doLogin(params) {
console.log(params);
return request( `${LOGIN_API}/login`,{
method: 'POST',
body: JSON.stringify(params)
});
}
export async function doLogup(params) {
return request( `${LOGIN_API}/logup`,{
method: 'POST',
body: JSON.stringify(params)
});
}
export async function doLogout() {
return request( `${LOGIN_API}/logout`,{
method: 'POST'
});
}
/utils/webSession.js
import request from '../utils/request';
import { message } from 'antd';
import { browserHistory } from 'dva/router';
const AUTH_API = `/api/auth`;
export function getCurrentUser() {
let sessionStorage = window.sessionStorage;
if (sessionStorage['userInfo']) {
return JSON.parse(sessionStorage['userInfo']);
}
return {};
}
export async function fetchIsAuth(callback) {
return request(`${AUTH_API}?authToken=${getCurrentUser()['authToken']}`)
.then(data => callback(data.data.isAuth))
.catch(e => console.log(e));
}
export function redirect() {
message.error('请登录!');
//browserHistory.push 可能会导致路由丢失,推荐使用 reduxRouter
browserHistory.push('/');
return null;
}
/*授权验证*/
export function requireAuth(nextState, replace) {
const userInfo = sessionStorage.getItem('userInfo');
if (!userInfo || userInfo == 'null') {
message.error('请登录!');
replace('/');
}
}