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