index.js 1.46 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1
import { login } from '../services';
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
2 3 4
import store from '@/utils/store';
import { routerRedux } from 'dva/router';
import { getPageQuery } from '@/utils';
duanledexianxianxian's avatar
duanledexianxianxian committed
5 6 7

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