user.ts 3.03 KB
Newer Older
duanledexianxianxian's avatar
duanledexianxianxian committed
1
import { checkUserLoginStatus, getCurrentUserInfo } from '@/services/user';
2

3 4
import { Effect } from 'dva';
import { Reducer } from 'redux';
duanledexianxianxian's avatar
duanledexianxianxian committed
5
import config from '@/config';
6

duanledexianxianxian's avatar
duanledexianxianxian committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
const { fileServerUrl, defaultAvatar } = config;

export interface Role extends BaseModel {
  roleId?: number;
  roleName?: string;
  roleCode?: string;
  remark?: string;
  roleState?: number;
}

export interface Menu extends BaseModel {
  menuId?: number;
  pMenuId?: number;
  menuName?: string;
  menuType?: number;
  menuUrl?: string;
  menuIcon?: string;
  viewIndex?: number;
  menuState?: number;
}

export interface Component extends BaseModel {
  componentId?: number;
  componentName?: string;
  componentCode?: string;
  menuId?: number;
  componentType?: number;
  componentState?: number;
}

export interface CurrentUser extends BaseModel {
  userId?: string;
  loginId?: string;
  userPassword?: string;
  userName?: string;
  effDate?: string;
  expDate?: string;
  userTitle?: string;
  mobilePhone?: string;
  fixedPhone?: string;
  userEmail?: string;
  userState?: string;
  lockState?: string;
  userPortrait?: string;
何乐's avatar
何乐 committed
51
  avatar?: string;
duanledexianxianxian's avatar
duanledexianxianxian committed
52 53 54 55 56 57 58 59
  enabled?: string;
  accountNonExpired?: string;
  credentialsNonExpired?: string;
  accountNonLocked?: string;
  roleList?: Role[];
  menuList?: Menu[];
  componentList?: Component[];
  allCount?: number;
何乐's avatar
何乐 committed
60 61 62
  unreadCount?: number;
}

duanledexianxianxian's avatar
duanledexianxianxian committed
63 64 65 66 67 68 69 70 71 72 73 74 75
export interface UserSetting {
  enabled?: number;
  appTheme?: string;
  contentWidth?: number;
  fixedHead?: number;
  fixedSide?: number;
  hideHead?: number;
  navigatorStyle?: number;
  settingId?: number;
  userId?: number;
  userSetting?: UserSetting;
}

76
export interface UserModelState {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77
  currentUser?: CurrentUser;
78 79 80 81 82 83
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
duanledexianxianxian's avatar
duanledexianxianxian committed
84 85
    getCurrent: Effect;
    checkLoginStatus: Effect;
86 87
  };
  reducers: {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
88 89
    saveCurrentUser: Reducer<UserModelState>;
    changeNotifyCount: Reducer<UserModelState>;
90 91 92 93
  };
}

const UserModel: UserModelType = {
94 95 96 97 98 99 100
  namespace: 'user',

  state: {
    currentUser: {},
  },

  effects: {
duanledexianxianxian's avatar
duanledexianxianxian committed
101 102
    *checkLoginStatus({ payload }, { call }) {
      return yield call(checkUserLoginStatus, payload);
103
    },
duanledexianxianxian's avatar
duanledexianxianxian committed
104 105
    *getCurrent({ payload }, { call, put }) {
      const { data, code } = yield call(getCurrentUserInfo, payload);
106 107
      yield put({
        type: 'saveCurrentUser',
duanledexianxianxian's avatar
duanledexianxianxian committed
108
        payload: { ...data },
109
      });
duanledexianxianxian's avatar
duanledexianxianxian committed
110 111
      const result = code === 'sys.success';
      return result;
duanledexianxianxian's avatar
sync  
duanledexianxianxian committed
112
    },
113 114 115
  },

  reducers: {
duanledexianxianxian's avatar
duanledexianxianxian committed
116 117
    saveCurrentUser(state, { payload }) {
      const { userPortrait } = payload;
118 119
      return {
        ...state,
duanledexianxianxian's avatar
duanledexianxianxian committed
120 121 122 123
        currentUser: {
          avatar: userPortrait ? `${fileServerUrl}/${userPortrait}` : defaultAvatar,
          ...(payload || {}),
        },
124 125
      };
    },
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
126 127 128 129 130 131
    changeNotifyCount(
      state = {
        currentUser: {},
      },
      action,
    ) {
afc163's avatar
afc163 committed
132 133 134 135
      return {
        ...state,
        currentUser: {
          ...state.currentUser,
duanledexianxianxian's avatar
duanledexianxianxian committed
136
          allCount: action.payload.totalCount,
137
          unreadCount: action.payload.unreadCount,
afc163's avatar
afc163 committed
138 139 140
        },
      };
    },
141 142
  },
};
143 144

export default UserModel;