feat: 产品菜单路由权限

This commit is contained in:
rd
2025-07-08 16:55:04 +08:00
parent ead209da4d
commit f87e5ff020
26 changed files with 263 additions and 102 deletions

View File

@ -1,5 +1,7 @@
import { fetchEnterpriseInfo } from '@/api/all/login';
import { useSidebarStore } from '@/stores/modules/side-bar';
import { useUserStore } from '@/stores/modules/user';
import { glsWithCatch, slsWithCatch, rlsWithCatch } from '@/utils/stroage';
interface EnterpriseInfo {
id: number;
@ -17,24 +19,14 @@ interface EnterpriseState {
export const useEnterpriseStore = defineStore('enterprise', {
state: (): EnterpriseState => ({
enterpriseInfo: (() => {
const stored = localStorage.getItem('enterpriseInfo');
if (stored) {
try {
return JSON.parse(stored) as EnterpriseInfo;
} catch {
return null;
}
}
return null;
})(),
enterpriseInfo: (glsWithCatch('enterpriseInfo') && JSON.parse(glsWithCatch('enterpriseInfo') as string)) || null,
}),
actions: {
setEnterpriseInfo(enterpriseInfo: EnterpriseInfo) {
this.enterpriseInfo = enterpriseInfo;
localStorage.setItem('enterpriseInfo', JSON.stringify(enterpriseInfo));
slsWithCatch('enterpriseInfo', JSON.stringify(enterpriseInfo));
},
clearEnterpriseInfo() {
clearUserEnterpriseInfo() {
this.enterpriseInfo = null;
localStorage.removeItem('enterpriseInfo');
},
@ -53,17 +45,12 @@ export const useEnterpriseStore = defineStore('enterprise', {
this.enterpriseInfo.used_sub_account_count++;
}
},
getEnterpriseInfo(): EnterpriseInfo | null {
return this.enterpriseInfo;
},
async updateEnterpriseInfo() {
const sidebarStore = useSidebarStore();
const res = await fetchEnterpriseInfo(this.enterpriseInfo!.id);
const { code, data } = res;
if (code === 200) {
this.setEnterpriseInfo(data);
sidebarStore.getNavbarMenuList();
async getUserEnterpriseInfo() {
if (this.enterpriseInfo) {
const { code, data } = await fetchEnterpriseInfo(this.enterpriseInfo!.id);
if (code === 200) {
this.setEnterpriseInfo(data);
}
}
},
},

View File

@ -1,15 +1,25 @@
/*
* @Author: rd 1344903914@qq.com
* @Date: 2025-07-08 11:22:05
* @LastEditors: rd 1344903914@qq.com
* @LastEditTime: 2025-07-08 14:06:41
* @FilePath: /lingji-work-fe/src/stores/modules/side-bar/constants.ts
* @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
*/
import { MENU_GROUP_IDS } from '@/router/constants';
export const MENU_LIST = [
{
id: MENU_GROUP_IDS.WORK_BENCH_ID,
name: '工作台',
pathName: 'Home',
requiresAuth: false,
permissionKey: '', // 权限key如果为空则表示该菜单不需要权限与后端约定
},
{
id: MENU_GROUP_IDS.DATA_ENGINE_ID,
name: '全域数据分析',
permissionKey: 'data_analysis',
requiresAuth: true,
children: [
{
name: '行业热门话题洞察',
@ -41,6 +51,7 @@ export const MENU_LIST = [
id: MENU_GROUP_IDS.PROPERTY_ID,
name: '营销资产中台',
permissionKey: 'marketing_asset',
requiresAuth: true,
children: [
{
name: '品牌资产管理',

View File

@ -11,12 +11,15 @@ import { useEnterpriseStore } from '@/stores/modules/enterprise';
interface sidebarState {
activeMenuId: number | null;
menuList: any[];
allowAccessRoutes: any[];
}
export const useSidebarStore = defineStore('sidebar', {
state: (): sidebarState => ({
activeMenuId: null,
menuList: [],
allowAccessRoutes: [], // 允许访问的路由列表
}),
actions: {
clearActiveMenuId() {
@ -25,12 +28,14 @@ export const useSidebarStore = defineStore('sidebar', {
setActiveMenuId(id: number) {
this.activeMenuId = id;
},
clearUserNavbarMenuList() {
this.menuList = [];
},
// navbar菜单列表由企业对应权限决定
getNavbarMenuList() {
getUserNavbarMenuList() {
const enterpriseStore = useEnterpriseStore();
const enterpriseInfo = enterpriseStore.getEnterpriseInfo();
this.menuList = MENU_LIST.filter(
(item) => !item.permissionKey || enterpriseInfo?.permissions?.includes(item.permissionKey),
(item) => !item.permissionKey || enterpriseStore.enterpriseInfo?.permissions?.includes(item.permissionKey),
);
},
// 根据当前路由自动设置 activeMenuId
@ -61,7 +66,7 @@ export const useSidebarStore = defineStore('sidebar', {
return null;
};
const menuId = findMenuGroup(appRoutes);
const menuId = findMenuGroup(appRoutes as any);
if (menuId !== null) {
this.activeMenuId = menuId;
}

View File

@ -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));
},
},
});