vue-vben-admin/src/store/modules/user.ts

161 lines
4.1 KiB
TypeScript
Raw Normal View History

2020-09-28 20:19:10 +08:00
import type {
LoginParams,
GetUserInfoByUserIdModel,
GetUserInfoByUserIdParams,
} from '/@/api/sys/model/userModel';
import store from '/@/store/index';
import { VuexModule, Module, getModule, Mutation, Action } from 'vuex-module-decorators';
import { hotModuleUnregisterModule } from '/@/utils/helper/vuexHelper';
import { PageEnum } from '/@/enums/pageEnum';
import { RoleEnum } from '/@/enums/roleEnum';
2020-11-25 21:26:10 +08:00
import { CacheTypeEnum, ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
2020-09-28 20:19:10 +08:00
import { useMessage } from '/@/hooks/web/useMessage';
2020-11-23 00:35:15 +08:00
import router from '/@/router';
2020-09-28 20:19:10 +08:00
import { loginApi, getUserInfoById } from '/@/api/sys/user';
2020-11-25 21:26:10 +08:00
import { setLocal, getLocal, getSession, setSession } from '/@/utils/helper/persistent';
import { useProjectSetting } from '/@/hooks/setting';
2020-11-26 21:10:21 +08:00
import { useI18n } from '/@/hooks/web/useI18n';
import { ErrorMessageMode } from '/@/utils/http/axios/types';
2020-09-28 20:19:10 +08:00
export type UserInfo = Omit<GetUserInfoByUserIdModel, 'roles'>;
const NAME = 'user';
hotModuleUnregisterModule(NAME);
2020-11-25 21:26:10 +08:00
const { permissionCacheType } = useProjectSetting();
function getCache<T>(key: string) {
const fn = permissionCacheType === CacheTypeEnum.LOCAL ? getLocal : getSession;
return fn(key) as T;
}
function setCache(USER_INFO_KEY: string, info: any) {
if (!info) return;
// const fn = permissionCacheType === CacheTypeEnum.LOCAL ? setLocal : setSession;
setLocal(USER_INFO_KEY, info, true);
// TODO
setSession(USER_INFO_KEY, info, true);
}
2020-09-28 20:19:10 +08:00
@Module({ namespaced: true, name: NAME, dynamic: true, store })
class User extends VuexModule {
// user info
private userInfoState: UserInfo | null = null;
// token
private tokenState = '';
// roleList
private roleListState: RoleEnum[] = [];
get getUserInfoState(): UserInfo {
2020-11-25 21:26:10 +08:00
return this.userInfoState || getCache<UserInfo>(USER_INFO_KEY) || {};
2020-09-28 20:19:10 +08:00
}
get getTokenState(): string {
2020-11-25 21:26:10 +08:00
return this.tokenState || getCache<string>(TOKEN_KEY);
2020-09-28 20:19:10 +08:00
}
get getRoleListState(): RoleEnum[] {
2020-11-25 21:26:10 +08:00
return this.roleListState.length > 0 ? this.roleListState : getCache<RoleEnum[]>(ROLES_KEY);
2020-09-28 20:19:10 +08:00
}
@Mutation
2020-11-23 00:35:15 +08:00
commitResetState(): void {
2020-09-28 20:19:10 +08:00
this.userInfoState = null;
this.tokenState = '';
this.roleListState = [];
}
@Mutation
commitUserInfoState(info: UserInfo): void {
this.userInfoState = info;
2020-11-25 21:26:10 +08:00
setCache(USER_INFO_KEY, info);
2020-09-28 20:19:10 +08:00
}
@Mutation
commitRoleListState(roleList: RoleEnum[]): void {
this.roleListState = roleList;
2020-11-25 21:26:10 +08:00
setCache(ROLES_KEY, roleList);
2020-09-28 20:19:10 +08:00
}
@Mutation
commitTokenState(info: string): void {
this.tokenState = info;
2020-11-25 21:26:10 +08:00
setCache(TOKEN_KEY, info);
2020-09-28 20:19:10 +08:00
}
/**
* @description: login
*/
@Action
async login(
params: LoginParams & {
goHome?: boolean;
mode?: ErrorMessageMode;
}
): Promise<GetUserInfoByUserIdModel | null> {
2020-09-28 20:19:10 +08:00
try {
const { goHome = true, mode, ...loginParams } = params;
const data = await loginApi(loginParams, mode);
2020-09-28 20:19:10 +08:00
const { token, userId } = data;
// save token
this.commitTokenState(token);
2020-12-29 23:37:40 +08:00
// get user info
const userInfo = await this.getUserInfoAction({ userId });
2020-09-28 20:19:10 +08:00
// const name = FULL_PAGE_NOT_FOUND_ROUTE.name;
// name && router.removeRoute(name);
goHome && (await router.replace(PageEnum.BASE_HOME));
2020-09-28 20:19:10 +08:00
return userInfo;
} catch (error) {
return null;
}
}
@Action
async getUserInfoAction({ userId }: GetUserInfoByUserIdParams) {
const userInfo = await getUserInfoById({ userId });
const { role } = userInfo;
const roleList = [role.value] as RoleEnum[];
this.commitUserInfoState(userInfo);
this.commitRoleListState(roleList);
return userInfo;
}
/**
* @description: login out
*/
@Action
async loginOut(goLogin = false) {
goLogin && router.push(PageEnum.BASE_LOGIN);
}
/**
* @description: Confirm before logging out
*/
@Action
async confirmLoginOut() {
const { createConfirm } = useMessage();
2020-12-01 23:51:39 +08:00
const { t } = useI18n();
2020-09-28 20:19:10 +08:00
createConfirm({
iconType: 'warning',
2020-12-01 23:51:39 +08:00
title: t('sys.app.loginOutTip'),
content: t('sys.app.loginOutMessage'),
2020-09-28 20:19:10 +08:00
onOk: async () => {
await this.loginOut(true);
},
});
}
}
export const userStore = getModule<User>(User);