2024-08-23 13:19:20 +08:00
|
|
|
|
import { defineStore } from "pinia";
|
|
|
|
|
|
import { AuthState } from "@/stores/interface";
|
2024-10-15 15:37:50 +08:00
|
|
|
|
import { getAuthButtonListApi, getAuthMenuListApi } from "@/api/user/login";
|
2024-08-23 13:19:20 +08:00
|
|
|
|
import {
|
|
|
|
|
|
getFlatMenuList,
|
|
|
|
|
|
getShowMenuList,
|
|
|
|
|
|
getAllBreadcrumbList,
|
|
|
|
|
|
} from "@/utils";
|
2024-10-10 17:47:55 +08:00
|
|
|
|
import { useRouter } from "vue-router";
|
2024-08-23 13:19:20 +08:00
|
|
|
|
import { AUTH_STORE_KEY } from "@/stores/constant";
|
2024-08-22 11:27:06 +08:00
|
|
|
|
export const useAuthStore = defineStore({
|
|
|
|
|
|
id: AUTH_STORE_KEY,
|
|
|
|
|
|
state: (): AuthState => ({
|
|
|
|
|
|
// 按钮权限列表
|
|
|
|
|
|
authButtonList: {},
|
|
|
|
|
|
// 菜单权限列表
|
|
|
|
|
|
authMenuList: [],
|
|
|
|
|
|
// 当前页面的 router name,用来做按钮权限筛选
|
2024-08-23 13:19:20 +08:00
|
|
|
|
routeName: "",
|
|
|
|
|
|
//登录不显示菜单栏和导航栏,点击进入测试的时候显示
|
|
|
|
|
|
showMenuFlag: JSON.parse(localStorage.getItem("showMenuFlag")),
|
2024-10-10 17:47:55 +08:00
|
|
|
|
router: useRouter(),
|
2024-08-22 11:27:06 +08:00
|
|
|
|
}),
|
|
|
|
|
|
getters: {
|
|
|
|
|
|
// 按钮权限列表
|
2024-08-23 13:19:20 +08:00
|
|
|
|
authButtonListGet: (state) => state.authButtonList,
|
2024-08-22 11:27:06 +08:00
|
|
|
|
// 菜单权限列表 ==> 这里的菜单没有经过任何处理
|
2024-08-23 13:19:20 +08:00
|
|
|
|
authMenuListGet: (state) => state.authMenuList,
|
2024-08-22 11:27:06 +08:00
|
|
|
|
// 菜单权限列表 ==> 左侧菜单栏渲染,需要剔除 isHide == true
|
2024-08-23 13:19:20 +08:00
|
|
|
|
showMenuListGet: (state) => getShowMenuList(state.authMenuList),
|
2024-08-22 11:27:06 +08:00
|
|
|
|
// 菜单权限列表 ==> 扁平化之后的一维数组菜单,主要用来添加动态路由
|
2024-08-23 13:19:20 +08:00
|
|
|
|
flatMenuListGet: (state) => getFlatMenuList(state.authMenuList),
|
2024-08-22 11:27:06 +08:00
|
|
|
|
// 递归处理后的所有面包屑导航列表
|
2024-08-23 13:19:20 +08:00
|
|
|
|
breadcrumbListGet: (state) => getAllBreadcrumbList(state.authMenuList),
|
|
|
|
|
|
//是否显示菜单和导航栏
|
|
|
|
|
|
showMenuFlagGet: (state) => state.showMenuFlag,
|
2024-08-22 11:27:06 +08:00
|
|
|
|
},
|
|
|
|
|
|
actions: {
|
|
|
|
|
|
// Get AuthButtonList
|
|
|
|
|
|
async getAuthButtonList() {
|
2024-08-23 13:19:20 +08:00
|
|
|
|
const { data } = await getAuthButtonListApi();
|
2024-08-22 11:27:06 +08:00
|
|
|
|
this.authButtonList = data;
|
|
|
|
|
|
},
|
|
|
|
|
|
// Get AuthMenuList
|
|
|
|
|
|
async getAuthMenuList() {
|
2024-08-23 13:19:20 +08:00
|
|
|
|
const { data } = await getAuthMenuListApi();
|
2024-08-22 11:27:06 +08:00
|
|
|
|
this.authMenuList = data;
|
2024-11-04 18:43:24 +08:00
|
|
|
|
|
2024-08-22 11:27:06 +08:00
|
|
|
|
},
|
|
|
|
|
|
// Set RouteName
|
|
|
|
|
|
async setRouteName(name: string) {
|
|
|
|
|
|
this.routeName = name;
|
2024-08-23 13:19:20 +08:00
|
|
|
|
},
|
|
|
|
|
|
//重置权限
|
|
|
|
|
|
async resetAuthStore() {
|
2024-10-10 17:47:55 +08:00
|
|
|
|
this.showMenuFlag = false;
|
2024-08-23 13:19:20 +08:00
|
|
|
|
localStorage.removeItem("showMenuFlag");
|
|
|
|
|
|
},
|
|
|
|
|
|
//修改判断菜单栏/导航栏显示条件
|
|
|
|
|
|
async setShowMenu() {
|
2024-10-10 17:47:55 +08:00
|
|
|
|
this.showMenuFlag = true;
|
|
|
|
|
|
localStorage.setItem("showMenuFlag", true);
|
2024-08-23 13:19:20 +08:00
|
|
|
|
},
|
|
|
|
|
|
//更改模式
|
2024-10-10 17:47:55 +08:00
|
|
|
|
async changeModel() {
|
|
|
|
|
|
this.showMenuFlag = !this.showMenuFlag;
|
|
|
|
|
|
if (this.showMenuFlag) {
|
2024-08-23 13:19:20 +08:00
|
|
|
|
localStorage.setItem("showMenuFlag", true);
|
2024-10-10 17:47:55 +08:00
|
|
|
|
} else {
|
2024-08-23 13:19:20 +08:00
|
|
|
|
localStorage.removeItem("showMenuFlag");
|
|
|
|
|
|
}
|
2024-10-10 17:47:55 +08:00
|
|
|
|
this.router.push({ path: "/home/index" });
|
|
|
|
|
|
},
|
2024-08-23 13:19:20 +08:00
|
|
|
|
},
|
2024-08-22 11:27:06 +08:00
|
|
|
|
});
|