user.ts 1.76 KB
Newer Older
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
1
import { queryCurrent, query as queryUsers, checkUserLoginStatus } from '@/services/user';
2

3 4
import { Effect } from 'dva';
import { Reducer } from 'redux';
5

何乐's avatar
何乐 committed
6 7 8 9 10 11
export interface CurrentUser {
  avatar?: string;
  name?: string;
  title?: string;
  group?: string;
  signature?: string;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12 13 14 15
  tags?: {
    key: string;
    label: string;
  }[];
何乐's avatar
何乐 committed
16 17 18
  unreadCount?: number;
}

19
export interface UserModelState {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
20
  currentUser?: CurrentUser;
21 22 23 24 25 26 27 28
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetch: Effect;
    fetchCurrent: Effect;
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
29
    checkUserLoginStatus: Effect;
30 31
  };
  reducers: {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32 33
    saveCurrentUser: Reducer<UserModelState>;
    changeNotifyCount: Reducer<UserModelState>;
34 35 36 37
  };
}

const UserModel: UserModelType = {
38 39 40 41 42 43 44
  namespace: 'user',

  state: {
    currentUser: {},
  },

  effects: {
afc163's avatar
afc163 committed
45
    *fetch(_, { call, put }) {
46 47 48 49 50 51
      const response = yield call(queryUsers);
      yield put({
        type: 'save',
        payload: response,
      });
    },
afc163's avatar
afc163 committed
52
    *fetchCurrent(_, { call, put }) {
53 54 55 56 57 58
      const response = yield call(queryCurrent);
      yield put({
        type: 'saveCurrentUser',
        payload: response,
      });
    },
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
59 60 61 62
    *checkUserLoginStatus({ payload }, { call }) {
      const res = yield call(checkUserLoginStatus, payload);
      return res;
    },
63 64 65 66 67 68
  },

  reducers: {
    saveCurrentUser(state, action) {
      return {
        ...state,
afc163's avatar
afc163 committed
69
        currentUser: action.payload || {},
70 71
      };
    },
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
72 73 74 75 76 77
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
afc163's avatar
afc163 committed
78 79 80 81
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
82 83
          notifyCount: action.payload.totalCount,
          unreadCount: action.payload.unreadCount,
afc163's avatar
afc163 committed
84 85 86
        },
      };
    },
87 88
  },
};
89 90

export default UserModel;