知与行后台管理端

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { routerRedux } from 'dva/router';
  2. import { stringify } from 'querystring';
  3. // import { fakeAccountLogin, getFakeCaptcha } from '@/services/login';
  4. import { setAuthority } from '@/utils/authority';
  5. import { getPageQuery } from '@/utils/utils';
  6. import { fetch, apis } from '@/utils/request';
  7. const signin = fetch(apis.user.signin);
  8. const signout = fetch(apis.user.signout);
  9. const Model = {
  10. namespace: 'login',
  11. state: {
  12. status: undefined,
  13. },
  14. effects: {
  15. *login({ payload }, { call, put }) {
  16. const response = yield call(signin, { data: payload });
  17. yield put({
  18. type: 'changeLoginStatus',
  19. payload: response,
  20. }); // Login successfully
  21. const urlParams = new URL(window.location.href);
  22. const params = getPageQuery();
  23. let { redirect } = params;
  24. if (redirect) {
  25. const redirectUrlParams = new URL(redirect);
  26. if (redirectUrlParams.origin === urlParams.origin) {
  27. redirect = redirect.substr(urlParams.origin.length);
  28. if (redirect.match(/^\/.*#/)) {
  29. redirect = redirect.substr(redirect.indexOf('#') + 1);
  30. }
  31. } else {
  32. window.location.href = redirect;
  33. return;
  34. }
  35. }
  36. yield put(routerRedux.replace(redirect || '/'));
  37. },
  38. *getCaptcha({ payload }, { call }) {
  39. yield call(getFakeCaptcha, payload);
  40. },
  41. *logout(_, { put, call }) {
  42. const { redirect } = getPageQuery(); // redirect
  43. yield call(signout);
  44. if (window.location.pathname !== '/user/login' && !redirect) {
  45. yield put(
  46. routerRedux.replace({
  47. pathname: '/user/login',
  48. search: stringify({
  49. redirect: window.location.href,
  50. }),
  51. }),
  52. );
  53. }
  54. },
  55. },
  56. reducers: {
  57. changeLoginStatus(state, { payload }) {
  58. // setAuthority((payload.user.roles || []).map(x => x.roleId));
  59. return { ...state, status: 'ok', type: payload.type };
  60. },
  61. },
  62. };
  63. export default Model;