Files
fe_service_ebtp_frontend/src/models/user.ts

112 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-07-07 16:40:14 +08:00
import { Effect, Reducer } from '@umijs/max';
2020-12-23 11:14:35 +08:00
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;
2025-07-07 16:40:14 +08:00
menuData?: any[];
pathKeyMap?: Map<string, {name: string, icon?: string}>;
2020-12-23 11:14:35 +08:00
}
export interface UserModelType {
namespace: 'user';
state: UserModelState;
effects: {
fetch: Effect;
fetchCurrent: Effect;
};
reducers: {
saveCurrentUser: Reducer<UserModelState>;
changeNotifyCount: Reducer<UserModelState>;
2025-07-07 16:40:14 +08:00
saveMenuData: Reducer<UserModelState>;
2020-12-23 11:14:35 +08:00
};
}
const UserModel: UserModelType = {
namespace: 'user',
state: {
currentUser: {},
2025-07-07 16:40:14 +08:00
menuData: [],
pathKeyMap: new Map<string, {name: string, icon?: string}>(),
2020-12-23 11:14:35 +08:00
},
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: {
saveCurrentUser(state, action) {
return {
...state,
currentUser: action.payload || {},
};
},
2025-07-07 16:40:14 +08:00
saveMenuData(state, action) {
const menuData = action.payload || [];
const pathKeyMap = new Map<string, {name: string, icon?: string}>();
// 将menuData转换为pathKeyMap 递归处理
const convertToPathKeyMap = (menuData: any[]) => {
menuData.forEach((item: any) => {
const path = item.path ? item.path.toLowerCase() : '';
pathKeyMap.set(path, {name: item.name, icon: item.icon});
if (item.children) {
convertToPathKeyMap(item.children);
}
});
};
convertToPathKeyMap(menuData);
pathKeyMap.set('/dashboard', { name: '首页' });
return {
...state,
menuData: action.payload || [],
pathKeyMap,
};
},
2020-12-23 11:14:35 +08:00
changeNotifyCount(
state = {
currentUser: {},
},
action,
) {
return {
...state,
currentUser: {
...state.currentUser,
notifyCount: action.payload.totalCount,
unreadCount: action.payload.unreadCount,
},
};
},
},
};
export default UserModel;