用户登录,退出,用户信息

  1. import { doLogin, doLogup, doLogout } from '../services/user';
  2. import { getCurrentUser, fetchIsAuth, redirect } from '../utils/webSession';
  3. export default {
  4. namespace: 'user',
  5. state: {
  6. username: '',
  7. isLogin: false,
  8. modalVisible: false,
  9. authToken: '',
  10. pathname: '/',
  11. },
  12. subscriptions: {
  13. setup({ dispatch, history }) {
  14. history.listen(location => {
  15. if (location.pathname === '/' && sessionStorage.getItem('userInfo')) {
  16. //权限验证通过
  17. dispatch({
  18. type: 'loginSuccess',
  19. payload: JSON.parse(sessionStorage.getItem('userInfo')) || {}
  20. });
  21. }
  22. });
  23. },
  24. },
  25. effects: {
  26. *doLogin({ payload }, { call, put }) {
  27. let {
  28. userData,
  29. resolve,
  30. reject
  31. } = payload;
  32. yield put({ type: 'showLoading' });
  33. const { data } = yield call(doLogin, userData);
  34. if (data && data.success) {
  35. let userInfo = data.userInfo;
  36. yield sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
  37. //登录成功
  38. yield put({
  39. type: 'loginSuccess',
  40. payload: userInfo
  41. });
  42. resolve();
  43. } else {
  44. reject(data);
  45. }
  46. },
  47. *doLogup({ payload }, { call, put }) {
  48. let {
  49. userData,
  50. resolve,
  51. reject
  52. } = payload;
  53. yield put({ type: 'showLoading' });
  54. const { data } = yield call(doLogup, userData);
  55. if (data && data.success) {
  56. let userInfo = data.userInfo;
  57. yield sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
  58. //注册成功
  59. yield put({
  60. type: 'logupSuccess',
  61. payload: userInfo
  62. });
  63. resolve();
  64. } else {
  65. reject(data);
  66. }
  67. },
  68. *doLogout({ payload }, { call, put }) {
  69. const { data } = yield call(doLogout);
  70. if (data && data.success) {
  71. yield sessionStorage.removeItem('userInfo');
  72. //退出登录成功
  73. yield put({
  74. type: 'logoutSuccess',
  75. payload: data.userInfo
  76. });
  77. }
  78. }
  79. },
  80. reducers: {
  81. login(state, action) {
  82. return { ...state, modalVisible: true };
  83. },
  84. loginSuccess(state, action) {
  85. let userInfo = action.payload;
  86. return { ...state, ...userInfo, isLogin: true, modalVisible: false };
  87. },
  88. logupSuccess(state, action) {
  89. let userInfo = action.payload;
  90. return { ...state, ...userInfo, isLogin: true };
  91. },
  92. logoutSuccess(state, action) {
  93. return { ...state, username: '', authToken: '', isLogin: false };
  94. },
  95. hideModal(state) {
  96. return { ...state, modalVisible: false };
  97. },
  98. },
  99. };

/services/user.js

  1. import request from '../utils/request';
  2. const LOGIN_API = '/system';
  3. export async function doLogin(params) {
  4. console.log(params);
  5. return request( `${LOGIN_API}/login`,{
  6. method: 'POST',
  7. body: JSON.stringify(params)
  8. });
  9. }
  10. export async function doLogup(params) {
  11. return request( `${LOGIN_API}/logup`,{
  12. method: 'POST',
  13. body: JSON.stringify(params)
  14. });
  15. }
  16. export async function doLogout() {
  17. return request( `${LOGIN_API}/logout`,{
  18. method: 'POST'
  19. });
  20. }

/utils/webSession.js

  1. import request from '../utils/request';
  2. import { message } from 'antd';
  3. import { browserHistory } from 'dva/router';
  4. const AUTH_API = `/api/auth`;
  5. export function getCurrentUser() {
  6. let sessionStorage = window.sessionStorage;
  7. if (sessionStorage['userInfo']) {
  8. return JSON.parse(sessionStorage['userInfo']);
  9. }
  10. return {};
  11. }
  12. export async function fetchIsAuth(callback) {
  13. return request(`${AUTH_API}?authToken=${getCurrentUser()['authToken']}`)
  14. .then(data => callback(data.data.isAuth))
  15. .catch(e => console.log(e));
  16. }
  17. export function redirect() {
  18. message.error('请登录!');
  19. //browserHistory.push 可能会导致路由丢失,推荐使用 reduxRouter
  20. browserHistory.push('/');
  21. return null;
  22. }
  23. /*授权验证*/
  24. export function requireAuth(nextState, replace) {
  25. const userInfo = sessionStorage.getItem('userInfo');
  26. if (!userInfo || userInfo == 'null') {
  27. message.error('请登录!');
  28. replace('/');
  29. }
  30. }