import { Effect } from 'dva' ; import { Reducer } from 'redux' ; import { queryCurrent , query as queryUsers } from '@/services/user' ; export interface CurrentUser { avatar ? : string ; name ? : string ; title ? : string ; group ? : string ; signature ? : string ; tags ? : { key : string ; label : string ; } [ ] ; userid ? : string ; unreadCount ? : number ; } export interface UserModelState { currentUser ? : CurrentUser ; } export interface UserModelType { namespace : 'user' ; state : UserModelState ; effects : { fetch : Effect ; fetchCurrent : Effect ; } ; reducers : { saveCurrentUser : Reducer < UserModelState > ; changeNotifyCount : Reducer < UserModelState > ; } ; } const UserModel = { namespace : 'user' , state : { currentUser : { } , } , effects : { * fetch ( { callback } , { call , put } ) { const response = yield call ( queryUsers ) ; yield put ( { type : 'save' , payload : response , } ) ; if ( callback && typeof callback === 'function' ) { callback ( ) ; } } , * fetchCurrent ( { callback } , { call , put } ) { const response = yield call ( queryCurrent ) ; yield put ( { type : 'saveCurrentUser' , payload : response , } ) ; if ( callback && typeof callback === 'function' ) { callback ( ) ; } } , } , reducers : { saveCurrentUser ( state , action ) { return { ... state , currentUser : action . payload || { } , } ; } , changeNotifyCount ( state = { currentUser : { } , } , action ) { return { ... state , currentUser : { ... state . currentUser , notifyCount : action . payload . totalCount , unreadCount : action . payload . unreadCount , } , } ; } , } , } ; export default UserModel ;