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

131 lines
3.4 KiB
TypeScript
Raw Normal View History

2021-04-10 19:25:49 +08:00
import type { UserInfo } from '/#/store';
import type { ErrorMessageMode } from '/@/utils/http/axios/types';
2020-09-28 20:19:10 +08:00
2021-04-10 19:25:49 +08:00
import { defineStore } from 'pinia';
import { store } from '/@/store';
2020-09-28 20:19:10 +08:00
import { RoleEnum } from '/@/enums/roleEnum';
2021-04-10 19:25:49 +08:00
import { PageEnum } from '/@/enums/pageEnum';
import { ROLES_KEY, TOKEN_KEY, USER_INFO_KEY } from '/@/enums/cacheEnum';
2020-09-28 20:19:10 +08:00
2021-04-10 19:25:49 +08:00
import { getAuthCache, setAuthCache } from '/@/utils/auth';
import {
GetUserInfoByUserIdModel,
GetUserInfoByUserIdParams,
LoginParams,
} from '/@/api/sys/model/userModel';
2020-09-28 20:19:10 +08:00
2021-04-10 19:25:49 +08:00
import { getUserInfoById, loginApi } from '/@/api/sys/user';
2020-09-28 20:19:10 +08:00
2020-11-26 21:10:21 +08:00
import { useI18n } from '/@/hooks/web/useI18n';
2021-04-10 19:25:49 +08:00
import { useMessage } from '/@/hooks/web/useMessage';
import router from '/@/router';
2020-09-28 20:19:10 +08:00
2021-04-10 19:25:49 +08:00
interface UserState {
userInfo: Nullable<UserInfo>;
token?: string;
roleList: RoleEnum[];
}
2020-12-29 23:37:40 +08:00
2021-04-10 19:25:49 +08:00
export const useUserStore = defineStore({
id: 'app-user',
state: (): UserState => ({
// user info
userInfo: null,
// token
token: undefined,
// roleList
roleList: [],
}),
getters: {
2021-05-23 23:45:21 +08:00
getUserInfo(_): UserInfo {
2021-04-10 19:25:49 +08:00
return this.userInfo || getAuthCache<UserInfo>(USER_INFO_KEY) || {};
},
2021-05-23 23:45:21 +08:00
getToken(_): string {
2021-04-10 19:25:49 +08:00
return this.token || getAuthCache<string>(TOKEN_KEY);
},
2021-05-23 23:45:21 +08:00
getRoleList(_): RoleEnum[] {
2021-04-10 19:25:49 +08:00
return this.roleList.length > 0 ? this.roleList : getAuthCache<RoleEnum[]>(ROLES_KEY);
},
},
actions: {
setToken(info: string) {
this.token = info;
setAuthCache(TOKEN_KEY, info);
},
setRoleList(roleList: RoleEnum[]) {
this.roleList = roleList;
setAuthCache(ROLES_KEY, roleList);
},
setUserInfo(info: UserInfo) {
this.userInfo = info;
setAuthCache(USER_INFO_KEY, info);
},
resetState() {
this.userInfo = null;
this.token = '';
this.roleList = [];
},
/**
* @description: login
*/
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.setToken(token);
// get user info
const userInfo = await this.getUserInfoAction({ userId });
goHome && (await router.replace(PageEnum.BASE_HOME));
return userInfo;
} catch (error) {
return null;
}
},
async getUserInfoAction({ userId }: GetUserInfoByUserIdParams) {
const userInfo = await getUserInfoById({ userId });
const { roles } = userInfo;
const roleList = roles.map((item) => item.value) as RoleEnum[];
this.setUserInfo(userInfo);
this.setRoleList(roleList);
2020-09-28 20:19:10 +08:00
return userInfo;
2021-04-10 19:25:49 +08:00
},
/**
* @description: logout
*/
logout(goLogin = false) {
goLogin && router.push(PageEnum.BASE_LOGIN);
},
/**
* @description: Confirm before logging out
*/
confirmLoginOut() {
const { createConfirm } = useMessage();
const { t } = useI18n();
createConfirm({
iconType: 'warning',
title: t('sys.app.logoutTip'),
content: t('sys.app.logoutMessage'),
onOk: async () => {
await this.logout(true);
},
});
},
},
});
// Need to be used outside the setup
export function useUserStoreWidthOut() {
return useUserStore(store);
2020-09-28 20:19:10 +08:00
}