login.js 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. try {
  17. const response = yield call(signin, { data: payload });
  18. } catch (e) {
  19. return e;
  20. }
  21. yield put({
  22. type: 'changeLoginStatus',
  23. payload: { type: '' },
  24. }); // Login successfully
  25. window.localStorage.setItem('showSwiperIndex', 1)
  26. const urlParams = new URL(window.location.href);
  27. const params = getPageQuery();
  28. let { redirect } = params;
  29. if (redirect) {
  30. const redirectUrlParams = new URL(redirect);
  31. if (redirectUrlParams.origin === urlParams.origin) {
  32. redirect = redirect.substr(urlParams.origin.length);
  33. if (redirect.match(/^\/.*#/)) {
  34. redirect = redirect.substr(redirect.indexOf('#') + 1);
  35. }
  36. } else {
  37. window.location.href = redirect;
  38. return;
  39. }
  40. }
  41. yield put(routerRedux.replace(redirect || '/'));
  42. },
  43. *logout(_, { put, call }) {
  44. const { redirect } = getPageQuery(); // redirect
  45. yield call(signout);
  46. if (window.location.pathname !== '/user/login' && !redirect) {
  47. yield put(
  48. routerRedux.replace({
  49. pathname: '/user/login',
  50. search: stringify({
  51. redirect: window.location.href,
  52. }),
  53. }),
  54. );
  55. }
  56. },
  57. },
  58. reducers: {
  59. changeLoginStatus(state, { payload }) {
  60. // setAuthority((payload.user.roles || []).map(x => x.roleId));
  61. return { ...state, status: 'ok', type: payload.type };
  62. },
  63. },
  64. };
  65. export default Model;