import { Toast } from "antd-mobile" ; import { CustomError } from "common/Errors" ; import { ProjectConfig , StartupStep } from "entity/global" ; import { CurUser } from "entity/session" ; import { RootState } from "modules" ; import { ModuleNames } from "modules/names" ; import { Actions , BaseModuleHandlers , BaseModuleState , effect , ERROR , exportModel , LoadingState , reducer } from "react-coat" ; import * as sessionService from "./api/session" ; import * as settingsService from "./api/settings" ; export interface State extends BaseModuleState { showLoginPop ? : boolean ; showNotFoundPop ? : boolean ; showSearch ? : boolean ; projectConfig : ProjectConfig | null ; curUser : CurUser | null ; startupStep : StartupStep ; loading : { global : LoadingState ; login : LoadingState ; } ; } const initState = { projectConfig : null , curUser : null , startupStep : StartupStep . init , loading : { global : LoadingState . Stop , login : LoadingState . Stop , } , } ; class ModuleHandlers extends BaseModuleHandlers < State , RootState , ModuleNames > { @ reducer public putStartup ( startupStep ) { return { ... this . state , startupStep } ; } @ reducer protected putCurUser ( curUser ) { return { ... this . state , curUser } ; } @ reducer public putShowLoginPop ( showLoginPop ) { return { ... this . state , showLoginPop } ; } @ reducer public putShowNotFoundPop ( showNotFoundPop ) { return { ... this . state , showNotFoundPop } ; } @ reducer public putShowSearch ( showSearch ) { return { ... this . state , showSearch } ; } @ effect ( "login" ) public async login ( payload ) { const loginResult = await sessionService . api . login ( payload ) ; if ( ! loginResult . error ) { this . updateState ( { curUser : loginResult . data } ) ; Toast . success ( "欢迎您回来！" ) ; } else { alert ( loginResult . error . message ) ; } } @ effect ( null ) protected async [ ERROR ] ( error ) { if ( error . code === "401" ) { this . dispatch ( this . actions . putShowLoginPop ( true ) ) ; } else if ( error . code === "404" ) { this . dispatch ( this . actions . putShowNotFoundPop ( true ) ) ; } else if ( error . code === "301" || error . code === "302" ) { this . dispatch ( this . routerActions . replace ( error . detail ) ) ; } else { Toast . fail ( error . message ) ; await settingsService . api . reportError ( error ) ; } } @ effect ( ) protected async [ ModuleNames . app + "/INIT" ] ( ) { const [ projectConfig , curUser ] = await Promise . all ( [ settingsService . api . getSettings ( ) , sessionService . api . getCurUser ( ) ] ) ; this . updateState ( { projectConfig , curUser , startupStep : StartupStep . configLoaded , } ) ; } } export type ModuleActions = Actions < ModuleHandlers > ; export default exportModel ( ModuleNames . app , ModuleHandlers , initState ) ;