user.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import type { Effect, Reducer } from 'umi';
  2. import { queryCurrent, query as queryUsers } from '@/services/user';
  3. import { getRoleList, getRoleListNew } from '@/services/role';
  4. export type CurrentUser = {
  5. avatar?: string;
  6. name?: string;
  7. title?: string;
  8. group?: string;
  9. signature?: string;
  10. tags?: {
  11. key: string;
  12. label: string;
  13. }[];
  14. userid?: string;
  15. unreadCount?: number;
  16. };
  17. export type Role = {
  18. createTime: number | string | null;
  19. id: string;
  20. label: string;
  21. };
  22. export type UserModelState = {
  23. currentUser?: CurrentUser;
  24. roleList?: Role[];
  25. };
  26. export type UserModelType = {
  27. namespace: 'user';
  28. state: UserModelState;
  29. effects: {
  30. fetch: Effect;
  31. fetchCurrent: Effect;
  32. getRoleList: Effect;
  33. };
  34. reducers: {
  35. saveRoleList: Reducer<UserModelState>;
  36. saveCurrentUser: Reducer<UserModelState>;
  37. changeNotifyCount: Reducer<UserModelState>;
  38. };
  39. };
  40. const UserModel: UserModelType = {
  41. namespace: 'user',
  42. state: {
  43. currentUser: {},
  44. roleList: [],
  45. },
  46. effects: {
  47. *fetch(_, { call, put }) {
  48. const response = yield call(queryUsers);
  49. yield put({
  50. type: 'save',
  51. payload: response,
  52. });
  53. },
  54. *fetchCurrent(_, { call, put }) {
  55. const response = yield call(queryCurrent);
  56. yield put({
  57. type: 'saveCurrentUser',
  58. payload: response,
  59. });
  60. },
  61. *getRoleList(_, { call, put }) {
  62. const { payload, callback } = _;
  63. const r = yield call(getRoleList, payload);
  64. yield put({
  65. type: 'saveRoleList',
  66. payload: r.data.records,
  67. });
  68. callback();
  69. },
  70. *getRoleListNew(_, { call, put }) {
  71. const { payload, callback } = _;
  72. const r = yield call(getRoleListNew, payload);
  73. yield put({
  74. type: 'saveRoleListNew',
  75. payload: r.data,
  76. });
  77. if (callback) callback();
  78. },
  79. },
  80. reducers: {
  81. saveRoleListNew(state, action) {
  82. console.log(action, 'action');
  83. let newState = { ...state, roleList: action.payload || [] };
  84. return newState;
  85. },
  86. /**
  87. * @description 保存权限列表
  88. */
  89. saveRoleList(state, action) {
  90. console.log(action, 'action');
  91. let newState = { ...state, roleList: action.payload.slice(1) || [] };
  92. return newState;
  93. },
  94. saveCurrentUser(state, action) {
  95. return {
  96. ...state,
  97. currentUser: action.payload || {},
  98. };
  99. },
  100. changeNotifyCount(
  101. state = {
  102. currentUser: {},
  103. },
  104. action,
  105. ) {
  106. return {
  107. ...state,
  108. currentUser: {
  109. ...state.currentUser,
  110. notifyCount: action.payload.totalCount,
  111. unreadCount: action.payload.unreadCount,
  112. },
  113. };
  114. },
  115. },
  116. };
  117. export default UserModel;