import { routerRedux } from 'dva/router'; import { login } from '../services'; import store from '@/utils/store'; import { getPageQuery } from '@/utils'; import config from '@/config'; const initData = {}; export default { namespace: 'userLogin', state: { ...initData, }, effects: { *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; } } else { redirect = config.homePage; } yield put(routerRedux.replace(redirect || '/')); } }, }, reducers: { clearData: () => ({ ...initData }), changeLoginStatus(state, { payload }) { // setAuthority(payload.currentAuthority); // return { // ...state, // status: payload.status, // type: payload.type, // }; }, }, };