user.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import type { Effect, Reducer } from 'umi';
  2. import { queryCurrent, query as queryUsers } from '@/services/user';
  3. export type CurrentUser = {
  4. avatar?: string;
  5. name?: string;
  6. title?: string;
  7. group?: string;
  8. signature?: string;
  9. tags?: {
  10. key: string;
  11. label: string;
  12. }[];
  13. userid?: string;
  14. unreadCount?: number;
  15. };
  16. export type UserModelState = {
  17. currentUser?: CurrentUser;
  18. };
  19. export type UserModelType = {
  20. namespace: 'user';
  21. state: UserModelState;
  22. effects: {
  23. fetch: Effect;
  24. fetchCurrent: Effect;
  25. };
  26. reducers: {
  27. saveCurrentUser: Reducer<UserModelState>;
  28. changeNotifyCount: Reducer<UserModelState>;
  29. };
  30. };
  31. const UserModel: UserModelType = {
  32. namespace: 'user',
  33. state: {
  34. currentUser: {},
  35. },
  36. effects: {
  37. *fetch(_, { call, put }) {
  38. const response = yield call(queryUsers);
  39. yield put({
  40. type: 'save',
  41. payload: response,
  42. });
  43. },
  44. *fetchCurrent(_, { call, put }) {
  45. const response = yield call(queryCurrent);
  46. yield put({
  47. type: 'saveCurrentUser',
  48. payload: response,
  49. });
  50. },
  51. },
  52. reducers: {
  53. saveCurrentUser(state, action) {
  54. return {
  55. ...state,
  56. currentUser: action.payload || {},
  57. };
  58. },
  59. changeNotifyCount(
  60. state = {
  61. currentUser: {},
  62. },
  63. action,
  64. ) {
  65. return {
  66. ...state,
  67. currentUser: {
  68. ...state.currentUser,
  69. notifyCount: action.payload.totalCount,
  70. unreadCount: action.payload.unreadCount,
  71. },
  72. };
  73. },
  74. },
  75. };
  76. export default UserModel;