index.js 1.69 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1
import { routerRedux } from 'dva/router';
duanledexianxianxian's avatar
duanledexianxianxian committed
2
import { login } from '../services';
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
3 4
import store from '@/utils/store';
import { getPageQuery } from '@/utils';
duanledexianxianxian's avatar
duanledexianxianxian committed
5
import config from '@/config';
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
6 7
import { setAuthority } from '@/utils/authority';
import { reloadAuthorized } from '@/utils/Authorized';
duanledexianxianxian's avatar
duanledexianxianxian committed
8 9 10

const initData = {};
export default {
duanledexianxianxian's avatar
duanledexianxianxian committed
11
  namespace: 'userLogin',
duanledexianxianxian's avatar
duanledexianxianxian committed
12 13 14 15
  state: {
    ...initData,
  },
  effects: {
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
16 17 18
    *login({ payload }, { call, put }) {
      const { code, data } = yield call(login, payload);
      if (code === 'sys.success') {
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
19 20 21 22 23
        yield put({
          type: 'changeLoginStatus',
          payload: {},
        });
        reloadAuthorized();
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        const { token, userId } = data;
        store.set('token', token);
        store.set('userId', userId);

        const urlParams = new URL(window.location.href);
        const params = getPageQuery();
        // 是否需要重定向
        let { redirect } = params;
        if (redirect) {
          const redirectUrlParams = new URL(redirect);
          // origin相同
          if (redirectUrlParams.origin === urlParams.origin) {
            redirect = redirect.substr(urlParams.origin.length);
            if (redirect.match(/^\/.*#/)) {
              redirect = redirect.substr(redirect.indexOf('#') + 1);
            }
          } else {
            window.location.href = redirect;
            return;
          }
        }
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
45 46 47
        // else {
        //   redirect = config.homePage;
        // }
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
48 49
        yield put(routerRedux.replace(redirect || '/'));
      }
duanledexianxianxian's avatar
duanledexianxianxian committed
50 51 52 53
    },
  },
  reducers: {
    clearData: () => ({ ...initData }),
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
54
    changeLoginStatus(state, { payload }) {
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
55 56 57 58
      setAuthority('user');
      return {
        ...state,
      };
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
59
    },
duanledexianxianxian's avatar
duanledexianxianxian committed
60 61
  },
};