动态菜单和动态路由的 antd pro

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { routerRedux } from 'dva/router';
  2. import { fakeAccountLogin } from '../services/api';
  3. import { setAuthority } 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(fakeAccountLogin, payload);
  13. yield put({
  14. type: 'changeLoginStatus',
  15. payload: response,
  16. });
  17. // Login successfully
  18. if (response.status === 'ok') {
  19. reloadAuthorized();
  20. yield put(routerRedux.push('/'));
  21. }
  22. },
  23. *logout(_, { put, select }) {
  24. try {
  25. // get location pathname
  26. const urlParams = new URL(window.location.href);
  27. const pathname = yield select(state => state.routing.location.pathname);
  28. // add the parameters in the url
  29. urlParams.searchParams.set('redirect', pathname);
  30. window.history.replaceState(null, 'login', urlParams.href);
  31. } finally {
  32. yield put({
  33. type: 'changeLoginStatus',
  34. payload: {
  35. status: false,
  36. currentAuthority: 'guest',
  37. },
  38. });
  39. reloadAuthorized();
  40. yield put(routerRedux.push('/user/login'));
  41. }
  42. },
  43. },
  44. reducers: {
  45. changeLoginStatus(state, { payload }) {
  46. setAuthority(payload.currentAuthority);
  47. return {
  48. ...state,
  49. status: payload.status,
  50. type: payload.type,
  51. };
  52. },
  53. },
  54. };