123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 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';
- import { CacheTypeEnum, ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
- import { useMessage } from '/@/hooks/web/useMessage';
- import router from '/@/router';
- import { loginApi, getUserInfoById } from '/@/api/sys/user';
- import { setLocal, getLocal, getSession, setSession } from '/@/utils/helper/persistent';
- import { useProjectSetting } from '/@/hooks/setting';
- import { useI18n } from '/@/hooks/web/useI18n';
- import { ErrorMessageMode } from '/@/utils/http/axios/types';
- export type UserInfo = Omit<GetUserInfoByUserIdModel, 'roles'>;
- const NAME = 'user';
- hotModuleUnregisterModule(NAME);
- 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);
- }
- @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 {
- return this.userInfoState || getCache<UserInfo>(USER_INFO_KEY) || {};
- }
- get getTokenState(): string {
- return this.tokenState || getCache<string>(TOKEN_KEY);
- }
- get getRoleListState(): RoleEnum[] {
- return this.roleListState.length > 0 ? this.roleListState : getCache<RoleEnum[]>(ROLES_KEY);
- }
- @Mutation
- commitResetState(): void {
- this.userInfoState = null;
- this.tokenState = '';
- this.roleListState = [];
- }
- @Mutation
- commitUserInfoState(info: UserInfo): void {
- this.userInfoState = info;
- setCache(USER_INFO_KEY, info);
- }
- @Mutation
- commitRoleListState(roleList: RoleEnum[]): void {
- this.roleListState = roleList;
- setCache(ROLES_KEY, roleList);
- }
- @Mutation
- commitTokenState(info: string): void {
- this.tokenState = info;
- setCache(TOKEN_KEY, info);
- }
- /**
- * @description: login
- */
- @Action
- async login(
- params: LoginParams & {
- goHome?: boolean;
- mode?: ErrorMessageMode;
- }
- ): Promise<GetUserInfoByUserIdModel | null> {
- try {
- const { goHome = true, mode, ...loginParams } = params;
- const data = await loginApi(loginParams, mode);
- const { token, userId } = data;
- // save token
- this.commitTokenState(token);
- // get user info
- const userInfo = await this.getUserInfoAction({ userId });
- goHome && (await router.replace(PageEnum.BASE_HOME));
- return userInfo;
- } catch (error) {
- return null;
- }
- }
- @Action
- async getUserInfoAction({ userId }: GetUserInfoByUserIdParams) {
- const userInfo = await getUserInfoById({ userId });
- const { roles } = userInfo;
- const roleList = roles.map((item) => item.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();
- const { t } = useI18n();
- createConfirm({
- iconType: 'warning',
- title: t('sys.app.loginOutTip'),
- content: t('sys.app.loginOutMessage'),
- onOk: async () => {
- await this.loginOut(true);
- },
- });
- }
- }
- export const userStore = getModule<User>(User);
|