知与行后台管理端

login.js 2.1KB

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. const response = yield call(signin, { data: payload });
  17. yield put({
  18. type: 'changeLoginStatus',
  19. payload: response,
  20. }); // Login successfully
  21. window.localStorage.setItem('showSwiperIndex', 1)
  22. const urlParams = new URL(window.location.href);
  23. const params = getPageQuery();
  24. let { redirect } = params;
  25. if (redirect) {
  26. const redirectUrlParams = new URL(redirect);
  27. if (redirectUrlParams.origin === urlParams.origin) {
  28. redirect = redirect.substr(urlParams.origin.length);
  29. if (redirect.match(/^\/.*#/)) {
  30. redirect = redirect.substr(redirect.indexOf('#') + 1);
  31. }
  32. } else {
  33. window.location.href = redirect;
  34. return;
  35. }
  36. }
  37. // yield put(routerRedux.replace(redirect || '/'));
  38. yield put(routerRedux.replace('/'));
  39. },
  40. *getCaptcha({ payload }, { call }) {
  41. yield call(getFakeCaptcha, payload);
  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;