import { routerRedux } from 'dva/router'; import { Reducer } from 'redux'; import { EffectsCommandMap } from 'dva'; import { AnyAction } from 'redux'; import { stringify, parse } from 'qs'; export function getPageQuery() { return parse(window.location.href.split('?')[1]); } export interface IStateType {} export type Effect = ( action: AnyAction, effects: EffectsCommandMap & { select: (func: (state: IStateType) => T) => T }, ) => void; export interface ModelType { namespace: string; state: IStateType; effects: { logout: Effect; }; reducers: { changeLoginStatus: Reducer; }; } const Model: ModelType = { namespace: 'login', state: { status: undefined, }, effects: { *logout(_, { put }) { const { redirect } = getPageQuery(); // redirect if (window.location.pathname !== '/user/login' && !redirect) { yield put( routerRedux.replace({ pathname: '/user/login', search: stringify({ redirect: window.location.href, }), }), ); } }, }, reducers: { changeLoginStatus(state, { payload }) { return { ...state, status: payload.status, type: payload.type, }; }, }, }; export default Model;