feat: 产品菜单路由权限
This commit is contained in:
@ -1,5 +1,10 @@
|
||||
import type { RouteRecordNormalized } from 'vue-router';
|
||||
|
||||
import { defineStore } from 'pinia';
|
||||
import { fetchProfileInfo } from '@/api/all/login';
|
||||
import { useSidebarStore } from '@/stores/modules/side-bar';
|
||||
import router from '@/router';
|
||||
import { glsWithCatch, slsWithCatch, rlsWithCatch } from '@/utils/stroage';
|
||||
|
||||
interface UserInfo {
|
||||
id: number;
|
||||
@ -10,16 +15,10 @@ interface UserInfo {
|
||||
// 添加其他用户属性...
|
||||
}
|
||||
|
||||
interface CompanyInfo {
|
||||
id: number;
|
||||
name: string;
|
||||
// 添加其他公司属性...
|
||||
}
|
||||
|
||||
interface UserState {
|
||||
token: string;
|
||||
userInfo: UserInfo | null;
|
||||
companyInfo: CompanyInfo | null;
|
||||
allowAccessRoutes: RouteRecordNormalized[];
|
||||
// isLogin: boolean;
|
||||
}
|
||||
|
||||
@ -31,9 +30,10 @@ interface UserInfo {
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
state: (): UserState => ({
|
||||
token: localStorage.getItem('accessToken') || '',
|
||||
userInfo: null,
|
||||
companyInfo: null,
|
||||
token: glsWithCatch('accessToken') || '',
|
||||
userInfo: (glsWithCatch('userInfo') && JSON.parse(glsWithCatch('userInfo') as string)) || null,
|
||||
allowAccessRoutes:
|
||||
(glsWithCatch('allowAccessRoutes') && JSON.parse(glsWithCatch('allowAccessRoutes') as string)) || [], // 允许访问的路由列表
|
||||
}),
|
||||
getters: {
|
||||
isLogin(): boolean {
|
||||
@ -44,12 +44,12 @@ export const useUserStore = defineStore('user', {
|
||||
// 设置 Token
|
||||
setToken(token: string) {
|
||||
this.token = `Bearer ${token}`;
|
||||
localStorage.setItem('accessToken', this.token);
|
||||
slsWithCatch('accessToken', this.token);
|
||||
},
|
||||
|
||||
deleteToken() {
|
||||
this.token = '';
|
||||
localStorage.removeItem('accessToken');
|
||||
rlsWithCatch('accessToken');
|
||||
},
|
||||
|
||||
// 获取 Token
|
||||
@ -60,14 +60,54 @@ export const useUserStore = defineStore('user', {
|
||||
// 设置用户信息
|
||||
setUserInfo(userInfo: UserInfo | null) {
|
||||
this.userInfo = userInfo;
|
||||
slsWithCatch('userInfo', JSON.stringify(userInfo));
|
||||
},
|
||||
clearUserInfo() {
|
||||
this.userInfo = null;
|
||||
rlsWithCatch('userInfo');
|
||||
},
|
||||
|
||||
// 获取用户信息
|
||||
async fetchUserInfo() {
|
||||
async getUserInfo() {
|
||||
const { code, data } = await fetchProfileInfo();
|
||||
if (code === 200) {
|
||||
this.setUserInfo(data);
|
||||
}
|
||||
},
|
||||
clearUserAllowAccessRoutes() {
|
||||
this.allowAccessRoutes = [];
|
||||
rlsWithCatch('allowAccessRoutes');
|
||||
},
|
||||
getUserAllowAccessRoutes() {
|
||||
const sidebarStore = useSidebarStore();
|
||||
const menuList = sidebarStore.menuList;
|
||||
|
||||
const appRoutes = router.getRoutes();
|
||||
|
||||
appRoutes.forEach((route: any) => {
|
||||
if (!route.meta?.requiresAuth) {
|
||||
this.allowAccessRoutes.push(route);
|
||||
}
|
||||
});
|
||||
|
||||
menuList.forEach((item) => {
|
||||
if (item.children && item.children.length > 0) {
|
||||
item.children.forEach((child: any) => {
|
||||
const matchedRoute = appRoutes.find((route: any) => route.name === child.pathName);
|
||||
if (matchedRoute) {
|
||||
this.allowAccessRoutes.push(matchedRoute);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
const matchedRoute = appRoutes.find((route: any) => route.name === item.pathName);
|
||||
if (matchedRoute) {
|
||||
this.allowAccessRoutes.push(matchedRoute);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
this.allowAccessRoutes = uniqBy(this.allowAccessRoutes, 'name');
|
||||
slsWithCatch('allowAccessRoutes', JSON.stringify(this.allowAccessRoutes));
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user