123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import type { Effect, Reducer } from 'umi';
- import { queryCurrent, query as queryUsers } from '@/services/user';
- import { getRoleList, getRoleListNew } from '@/services/role';
- export type CurrentUser = {
- avatar?: string;
- name?: string;
- title?: string;
- group?: string;
- signature?: string;
- tags?: {
- key: string;
- label: string;
- }[];
- userid?: string;
- unreadCount?: number;
- };
- export type Role = {
- createTime: number | string | null;
- id: string;
- label: string;
- };
- export type UserModelState = {
- currentUser?: CurrentUser;
- roleList?: Role[];
- };
- export type UserModelType = {
- namespace: 'user';
- state: UserModelState;
- effects: {
- fetch: Effect;
- fetchCurrent: Effect;
- getRoleList: Effect;
- };
- reducers: {
- saveRoleList: Reducer<UserModelState>;
- saveCurrentUser: Reducer<UserModelState>;
- changeNotifyCount: Reducer<UserModelState>;
- };
- };
- const UserModel: UserModelType = {
- namespace: 'user',
- state: {
- currentUser: {},
- roleList: [],
- },
- 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,
- });
- },
- *getRoleList(_, { call, put }) {
- const { payload, callback } = _;
- const r = yield call(getRoleList, payload);
- yield put({
- type: 'saveRoleList',
- payload: r.data.records,
- });
- callback();
- },
- *getRoleListNew(_, { call, put }) {
- const { payload, callback } = _;
- const r = yield call(getRoleListNew, payload);
- yield put({
- type: 'saveRoleListNew',
- payload: r.data,
- });
- if (callback) callback();
- },
- },
- reducers: {
- saveRoleListNew(state, action) {
- console.log(action, 'action');
- let newState = { ...state, roleList: action.payload || [] };
- return newState;
- },
- /**
- * @description 保存权限列表
- */
- saveRoleList(state, action) {
- console.log(action, 'action');
- let newState = { ...state, roleList: action.payload.slice(1) || [] };
- return newState;
- },
- 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;
|