login.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { routerRedux } from 'dva/router';
  2. import { accountLogin, accountLogout } from '../services/user';
  3. import { setAuthority, setLocalUser } from '../utils/authority';
  4. import { reloadAuthorized } from '../utils/Authorized';
  5. export default {
  6. namespace: 'login',
  7. state: {
  8. status: undefined,
  9. },
  10. effects: {
  11. *login({ payload }, { call, put }) {
  12. const response = yield call(accountLogin, payload);
  13. yield put({
  14. type: 'changeLoginStatus',
  15. payload: response,
  16. });
  17. // Login successfully
  18. if (response.success) {
  19. reloadAuthorized();
  20. yield put(routerRedux.push('/dashboard/sold'));
  21. }
  22. },
  23. *logout(_, { call, put }) {
  24. const response = yield call(accountLogout);
  25. if (response.success) {
  26. reloadAuthorized();
  27. yield put(routerRedux.push('/user/login'));
  28. }
  29. },
  30. },
  31. reducers: {
  32. changeLoginStatus(state, { payload }) {
  33. if (payload.data) {
  34. // 将用户信息存储到localStorage
  35. setLocalUser(payload.data);
  36. // 分析登录用户的角色
  37. const { id, cp, merchant, platForm } = payload.data;
  38. let userType = '';
  39. if (platForm && id === '1') {
  40. userType = 'admin';
  41. } else if (platForm && id !== '1') {
  42. userType = 'platform';
  43. } else if (cp) {
  44. userType = 'cp';
  45. } else if (merchant) {
  46. userType = 'channel';
  47. }
  48. // 设置用户权限
  49. setAuthority(userType);
  50. }
  51. return {
  52. ...state,
  53. status: payload.success,
  54. repMsg: payload.message,
  55. };
  56. },
  57. },
  58. };