import { AnyAction , Reducer } from 'redux' ; import { parse , stringify } from 'qs' ; import { EffectsCommandMap } from 'dva' ; import { routerRedux } from 'dva/router' ; export function getPageQuery ( ) { return parse ( window . location . href . split ( '?' ) [ 1 ] ) ; } export type Effect = ( action : AnyAction , effects : EffectsCommandMap & { select : < T > ( func : ( state : { } ) => T ) => T ; } ) => void ; export interface ModelType { namespace : string ; state : { } ; effects : { logout : Effect ; } ; reducers : { changeLoginStatus : Reducer < { } > ; } ; } const Model = { namespace : 'login' , state : { status : undefined , } , effects : { * logout ( _ , { put } ) { const { redirect } = getPageQuery ( ) ; 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 ;