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