import { checkUserLoginStatus, getCurrentUserInfo } from '@/services/user'; import { Effect } from 'dva'; import { Reducer } from 'redux'; import config from '@/config'; const { fileServerUrl, defaultAvatar } = config; export interface Role extends BaseModel { roleId?: number; roleName?: string; roleCode?: string; remark?: string; roleState?: number; } export interface Menu extends BaseModel { menuId?: number; pMenuId?: number; menuName?: string; menuType?: number; menuUrl?: string; menuIcon?: string; viewIndex?: number; menuState?: number; } export interface Component extends BaseModel { componentId?: number; componentName?: string; componentCode?: string; menuId?: number; componentType?: number; componentState?: number; } export interface CurrentUser extends BaseModel { userId?: string; loginId?: string; userPassword?: string; userName?: string; effDate?: string; expDate?: string; userTitle?: string; mobilePhone?: string; fixedPhone?: string; userEmail?: string; userState?: string; lockState?: string; userPortrait?: string; avatar?: string; enabled?: string; accountNonExpired?: string; credentialsNonExpired?: string; accountNonLocked?: string; roleList?: Role[]; menuList?: Menu[]; componentList?: Component[]; allCount?: number; unreadCount?: number; } export interface UserSetting { enabled?: number; appTheme?: string; contentWidth?: number; fixedHead?: number; fixedSide?: number; hideHead?: number; navigatorStyle?: number; settingId?: number; userId?: number; userSetting?: UserSetting; } export interface UserModelState { currentUser?: CurrentUser; } export interface UserModelType { namespace: 'user'; state: UserModelState; effects: { getCurrent: Effect; checkLoginStatus: Effect; }; reducers: { saveCurrentUser: Reducer; changeNotifyCount: Reducer; }; } const UserModel: UserModelType = { namespace: 'user', state: { currentUser: {}, }, effects: { *checkLoginStatus({ payload }, { call }) { return yield call(checkUserLoginStatus, payload); }, *getCurrent({ payload }, { call, put }) { const { data, code } = yield call(getCurrentUserInfo, payload); yield put({ type: 'saveCurrentUser', payload: { ...data }, }); const result = code === 'sys.success'; return result; }, }, reducers: { saveCurrentUser(state, { payload }) { const { userPortrait } = payload; return { ...state, currentUser: { avatar: userPortrait ? `${fileServerUrl}/${userPortrait}` : defaultAvatar, ...(payload || {}), }, }; }, changeNotifyCount( state = { currentUser: {}, }, action, ) { return { ...state, currentUser: { ...state.currentUser, allCount: action.payload.totalCount, unreadCount: action.payload.unreadCount, }, }; }, }, }; export default UserModel;