98 lines
3.2 KiB
TypeScript
98 lines
3.2 KiB
TypeScript
import { defineStore } from "pinia";
|
||
import { AuthState } from "@/stores/interface";
|
||
import { getAuthButtonListApi, getAuthMenuListApi } from "@/api/user/login";
|
||
import {
|
||
getFlatMenuList,
|
||
getShowMenuList,
|
||
getAllBreadcrumbList,
|
||
} from "@/utils";
|
||
import { useRouter } from "vue-router";
|
||
import { AUTH_STORE_KEY } from "@/stores/constant";
|
||
import {useModeStore} from '@/stores/modules/mode'
|
||
|
||
|
||
export const useAuthStore = defineStore({
|
||
id: AUTH_STORE_KEY,
|
||
state: (): AuthState => ({
|
||
// 按钮权限列表
|
||
authButtonList: {},
|
||
// 菜单权限列表
|
||
authMenuList: [],
|
||
// 当前页面的 router name,用来做按钮权限筛选
|
||
routeName: "",
|
||
//登录不显示菜单栏和导航栏,点击进入测试的时候显示
|
||
showMenuFlag: JSON.parse(localStorage.getItem("showMenuFlag")),
|
||
router: useRouter(),
|
||
}),
|
||
getters: {
|
||
// 按钮权限列表
|
||
authButtonListGet: (state) => state.authButtonList,
|
||
// 菜单权限列表 ==> 这里的菜单没有经过任何处理
|
||
authMenuListGet: (state) => state.authMenuList,
|
||
// 菜单权限列表 ==> 左侧菜单栏渲染,需要剔除 isHide == true
|
||
showMenuListGet: (state) => getShowMenuList(state.authMenuList),
|
||
// 菜单权限列表 ==> 扁平化之后的一维数组菜单,主要用来添加动态路由
|
||
flatMenuListGet: (state) => getFlatMenuList(state.authMenuList),
|
||
// 递归处理后的所有面包屑导航列表
|
||
breadcrumbListGet: (state) => getAllBreadcrumbList(state.authMenuList),
|
||
//是否显示菜单和导航栏
|
||
showMenuFlagGet: (state) => state.showMenuFlag,
|
||
},
|
||
actions: {
|
||
// Get AuthButtonList
|
||
async getAuthButtonList() {
|
||
const { data } = await getAuthButtonListApi();
|
||
this.authButtonList = data;
|
||
},
|
||
// Get AuthMenuList
|
||
async getAuthMenuList() {
|
||
const modeStore = useModeStore()
|
||
|
||
const { data: menuData } = await getAuthMenuListApi();
|
||
let data = menuData; // 新增变量接收并操作
|
||
if(modeStore.currentMode === '比对式'){
|
||
data = filterMenuTree(data);
|
||
}
|
||
this.authMenuList = data;
|
||
|
||
},
|
||
// Set RouteName
|
||
async setRouteName(name: string) {
|
||
this.routeName = name;
|
||
},
|
||
//重置权限
|
||
async resetAuthStore() {
|
||
this.showMenuFlag = false;
|
||
localStorage.removeItem("showMenuFlag");
|
||
},
|
||
//修改判断菜单栏/导航栏显示条件
|
||
async setShowMenu() {
|
||
this.showMenuFlag = true;
|
||
localStorage.setItem("showMenuFlag", true);
|
||
},
|
||
//更改模式
|
||
async changeModel() {
|
||
this.showMenuFlag = !this.showMenuFlag;
|
||
if (this.showMenuFlag) {
|
||
localStorage.setItem("showMenuFlag", true);
|
||
} else {
|
||
localStorage.removeItem("showMenuFlag");
|
||
}
|
||
this.router.push({ path: "/home/index" });
|
||
},
|
||
},
|
||
});
|
||
|
||
|
||
// 工具函数:递归过滤掉 name == 'test' 的菜单项
|
||
function filterMenuTree(menuList: any[]) {
|
||
return menuList.filter(menu => {
|
||
// 如果当前项有 children,递归处理子项
|
||
if (menu.children && menu.children.length > 0) {
|
||
menu.children = filterMenuTree(menu.children);
|
||
}
|
||
// 过滤掉 name 是 testSource、testScript 或 controlSource 的菜单项
|
||
return !['testSource', 'testScript', 'controlSource'].includes(menu.name);
|
||
});
|
||
}
|