136 lines
3.1 KiB
TypeScript
136 lines
3.1 KiB
TypeScript
import { Effect, Reducer } from '@umijs/max';
|
|
|
|
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 PathKeyMap {
|
|
name: string;
|
|
icon?: string;
|
|
routeName?: string;
|
|
}
|
|
|
|
export interface UserModelState {
|
|
currentUser?: CurrentUser;
|
|
menuData?: any[];
|
|
pathKeyMap?: Map<string, PathKeyMap>;
|
|
}
|
|
|
|
export interface UserModelType {
|
|
namespace: 'user';
|
|
state: UserModelState;
|
|
effects: {
|
|
fetch: Effect;
|
|
fetchCurrent: Effect;
|
|
};
|
|
reducers: {
|
|
saveCurrentUser: Reducer<UserModelState>;
|
|
changeNotifyCount: Reducer<UserModelState>;
|
|
saveMenuData: Reducer<UserModelState>;
|
|
};
|
|
}
|
|
|
|
const UserModel: UserModelType = {
|
|
namespace: 'user',
|
|
|
|
state: {
|
|
currentUser: {},
|
|
menuData: [],
|
|
pathKeyMap: new Map<string, PathKeyMap>(),
|
|
},
|
|
|
|
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 || {},
|
|
};
|
|
},
|
|
saveMenuData(state, action) {
|
|
const menuData = action.payload.menuData || [];
|
|
const routeData = action.payload.routeData || [];
|
|
const pathKeyMap = new Map<string, PathKeyMap>();
|
|
// 将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);
|
|
let i = 1;
|
|
while (routeData[i]) {
|
|
const item = routeData[i];
|
|
if (item.name) {
|
|
const pathname = item.path.toLowerCase();
|
|
if (pathKeyMap.has(pathname)) {
|
|
const target = pathKeyMap.get(pathname)!;
|
|
const newItem = {
|
|
...target,
|
|
routeName: item.name,
|
|
}
|
|
pathKeyMap.set(pathname, newItem);
|
|
} else {
|
|
pathKeyMap.set(pathname, { name: item.name, icon: item.icon });
|
|
}
|
|
}
|
|
i++;
|
|
}
|
|
return {
|
|
...state,
|
|
menuData: action.payload || [],
|
|
pathKeyMap,
|
|
};
|
|
},
|
|
changeNotifyCount(
|
|
state = {
|
|
currentUser: {},
|
|
},
|
|
action,
|
|
) {
|
|
return {
|
|
...state,
|
|
currentUser: {
|
|
...state.currentUser,
|
|
notifyCount: action.payload.totalCount,
|
|
unreadCount: action.payload.unreadCount,
|
|
},
|
|
};
|
|
},
|
|
},
|
|
};
|
|
|
|
export default UserModel;
|