index.js 1.55 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
duanledexianxianxian committed
6 7 8

const initData = {};
export default {
duanledexianxianxian's avatar
duanledexianxianxian committed
9
  namespace: 'userLogin',
duanledexianxianxian's avatar
duanledexianxianxian committed
10 11 12 13
  state: {
    ...initData,
  },
  effects: {
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
    *login({ payload }, { call, put }) {
      const { code, data } = yield call(login, payload);
      if (code === 'sys.success') {
        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
duanledexianxianxian committed
37 38
        } else {
          redirect = config.homePage;
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
39 40 41
        }
        yield put(routerRedux.replace(redirect || '/'));
      }
duanledexianxianxian's avatar
duanledexianxianxian committed
42 43 44 45
    },
  },
  reducers: {
    clearData: () => ({ ...initData }),
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
46 47 48 49 50 51 52 53
    changeLoginStatus(state, { payload }) {
      // setAuthority(payload.currentAuthority);
      // return {
      //   ...state,
      //   status: payload.status,
      //   type: payload.type,
      // };
    },
duanledexianxianxian's avatar
duanledexianxianxian committed
54 55
  },
};