Files
lingji-work-fe/src/stores/modules/user/index.ts

114 lines
3.1 KiB
TypeScript
Raw Normal View History

2025-07-25 11:52:23 +08:00
import { useRouter } from 'vue-router';
2025-06-20 06:10:15 -04:00
import { defineStore } from 'pinia';
import { fetchProfileInfo } from '@/api/all/login';
2025-07-08 16:55:04 +08:00
import { useSidebarStore } from '@/stores/modules/side-bar';
import { glsWithCatch, slsWithCatch, rlsWithCatch } from '@/utils/stroage';
2025-06-16 14:42:26 +08:00
2025-06-21 15:31:21 +08:00
interface UserInfo {
id: number;
name: string;
head_image: String;
current_enterprise_id: number;
mobile: string;
// 添加其他用户属性...
}
2025-06-16 14:42:26 +08:00
interface UserState {
2025-06-21 15:31:21 +08:00
token: string;
userInfo: UserInfo | null;
2025-07-09 16:37:25 +08:00
allowAccessRoutes: string[];
// isLogin: boolean;
}
interface UserInfo {
id: number;
mobile: string;
name: string;
2025-06-16 14:42:26 +08:00
}
export const useUserStore = defineStore('user', {
state: (): UserState => ({
2025-07-08 16:55:04 +08:00
token: glsWithCatch('accessToken') || '',
userInfo: (glsWithCatch('userInfo') && JSON.parse(glsWithCatch('userInfo') as string)) || null,
allowAccessRoutes:
(glsWithCatch('allowAccessRoutes') && JSON.parse(glsWithCatch('allowAccessRoutes') as string)) || [], // 允许访问的路由列表
2025-06-16 14:42:26 +08:00
}),
2025-06-23 03:16:55 -04:00
getters: {
isLogin(): boolean {
return !!this.token;
},
},
2025-06-16 14:42:26 +08:00
actions: {
// 设置 Token
setToken(token: string) {
this.token = `Bearer ${token}`;
2025-07-08 16:55:04 +08:00
slsWithCatch('accessToken', this.token);
2025-06-16 14:42:26 +08:00
},
2025-07-18 17:10:12 +08:00
clearToken() {
2025-06-22 22:28:47 -04:00
this.token = '';
2025-07-08 16:55:04 +08:00
rlsWithCatch('accessToken');
2025-06-22 22:28:47 -04:00
},
2025-06-21 15:31:21 +08:00
// 获取 Token
getToken() {
return this.token;
2025-06-16 14:42:26 +08:00
},
// 设置用户信息
2025-06-21 15:31:21 +08:00
setUserInfo(userInfo: UserInfo | null) {
this.userInfo = userInfo;
2025-07-08 16:55:04 +08:00
slsWithCatch('userInfo', JSON.stringify(userInfo));
},
clearUserInfo() {
this.userInfo = null;
rlsWithCatch('userInfo');
2025-06-21 15:31:21 +08:00
},
// 获取用户信息
2025-07-08 16:55:04 +08:00
async getUserInfo() {
const { code, data } = await fetchProfileInfo();
if (code === 200) {
this.setUserInfo(data);
}
2025-06-21 15:31:21 +08:00
},
2025-07-08 16:55:04 +08:00
clearUserAllowAccessRoutes() {
this.allowAccessRoutes = [];
rlsWithCatch('allowAccessRoutes');
},
getUserAllowAccessRoutes() {
const sidebarStore = useSidebarStore();
2025-07-25 11:52:23 +08:00
const router = useRouter();
2025-07-08 16:55:04 +08:00
const menuList = sidebarStore.menuList;
2025-07-25 11:52:23 +08:00
const appRoutes = router.options.routes;
2025-07-08 16:55:04 +08:00
appRoutes.forEach((route: any) => {
if (!route.meta?.requiresAuth) {
2025-07-09 16:37:25 +08:00
this.allowAccessRoutes.push(route.name);
2025-07-08 16:55:04 +08:00
}
});
2025-07-10 10:04:21 +08:00
const pushAllowAccessRoutes = (includeRouteNames: string[]) => {
2025-07-09 16:37:25 +08:00
const matchedRoute = appRoutes
2025-07-10 10:04:21 +08:00
.filter((route: any) => includeRouteNames.includes(route.name))
2025-07-09 16:37:25 +08:00
.map((route: any) => route.name);
2025-07-09 16:23:05 +08:00
this.allowAccessRoutes.push(...matchedRoute);
};
2025-07-08 16:55:04 +08:00
menuList.forEach((item) => {
if (item.children && item.children.length > 0) {
item.children.forEach((child: any) => {
2025-07-10 10:04:21 +08:00
pushAllowAccessRoutes(child.includeRouteNames);
2025-07-08 16:55:04 +08:00
});
} else {
2025-07-10 10:04:21 +08:00
pushAllowAccessRoutes(item.includeRouteNames);
2025-07-08 16:55:04 +08:00
}
});
2025-07-09 16:37:25 +08:00
this.allowAccessRoutes = uniq(this.allowAccessRoutes);
2025-07-08 16:55:04 +08:00
slsWithCatch('allowAccessRoutes', JSON.stringify(this.allowAccessRoutes));
},
2025-06-22 22:28:47 -04:00
},
2025-06-16 14:42:26 +08:00
});