Files

137 lines
4.7 KiB
JavaScript
Raw Normal View History

2022-08-30 10:36:30 +08:00
import auth from "@/plugins/auth";
2023-06-09 17:31:39 +08:00
import {agentRoutes, constantRoutes, enterpriseRoutes, expertRoutes, laboratoryRoutes, researchRoutes} from "@/router";
2022-08-30 10:36:30 +08:00
import Layout from "@/layout/index";
import ParentView from "@/components/ParentView";
import InnerLink from "@/layout/components/InnerLink";
2021-11-30 09:55:37 +08:00
// 匹配views里面所有的.vue文件
2022-08-30 10:36:30 +08:00
const modules = import.meta.glob("./../../views/**/*.vue");
2021-11-30 09:55:37 +08:00
2022-08-30 10:36:30 +08:00
const usePermissionStore = defineStore("permission", {
2023-06-07 10:44:31 +08:00
state: () => ({
2023-06-09 17:31:39 +08:00
routes: [], addRoutes: [], defaultRoutes: [], topbarRouters: [], sidebarRouters: [],
}), actions: {
2023-06-07 10:44:31 +08:00
setRoutes(routes) {
this.addRoutes = routes;
this.routes = constantRoutes.concat(routes);
2023-06-09 17:31:39 +08:00
}, setDefaultRoutes(routes) {
2023-06-07 10:44:31 +08:00
this.defaultRoutes = constantRoutes.concat(routes);
2023-06-09 17:31:39 +08:00
}, setTopbarRoutes(routes) {
2023-06-07 10:44:31 +08:00
this.topbarRouters = routes;
2023-06-09 17:31:39 +08:00
}, setSidebarRouters(routes) {
2023-06-07 10:44:31 +08:00
this.sidebarRouters = routes;
2023-06-09 17:31:39 +08:00
}, generateRoutes(roles) {
2023-06-07 10:44:31 +08:00
return new Promise((resolve) => {
console.log(roles)
let routesList = [];
2023-06-09 17:31:39 +08:00
switch (roles) {
case "1":
routesList = enterpriseRoutes
break
case "2":
routesList = expertRoutes
break
case "3":
routesList = laboratoryRoutes
break
case '4':
routesList = researchRoutes
break
case '5':
routesList = agentRoutes
break
default:
routesList = []
2023-06-07 10:44:31 +08:00
}
this.setRoutes(routesList);
this.setSidebarRouters(constantRoutes.concat(routesList));
this.setDefaultRoutes(routesList);
this.setTopbarRoutes(routesList);
resolve(routesList);
});
},
2022-08-30 10:36:30 +08:00
},
});
2021-11-30 09:55:37 +08:00
// 遍历后台传来的路由字符串,转换为组件对象
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) {
2023-06-07 10:44:31 +08:00
return asyncRouterMap.filter((route) => {
if (type && route.children) {
route.children = filterChildren(route.children);
}
if (route.component) {
// Layout ParentView 组件特殊处理
if (route.component === "Layout") {
route.component = Layout;
} else if (route.component === "ParentView") {
route.component = ParentView;
} else if (route.component === "InnerLink") {
route.component = InnerLink;
} else {
route.component = loadView(route.component);
}
}
if (route.children != null && route.children && route.children.length) {
route.children = filterAsyncRouter(route.children, route, type);
} else {
delete route["children"];
delete route["redirect"];
}
return true;
});
2021-11-30 09:55:37 +08:00
}
function filterChildren(childrenMap, lastRouter = false) {
2023-06-07 10:44:31 +08:00
var children = [];
childrenMap.forEach((el, index) => {
if (el.children && el.children.length) {
if (el.component === "ParentView" && !lastRouter) {
el.children.forEach((c) => {
c.path = el.path + "/" + c.path;
if (c.children && c.children.length) {
children = children.concat(filterChildren(c.children, c));
return;
}
children.push(c);
});
return;
}
}
if (lastRouter) {
el.path = lastRouter.path + "/" + el.path;
}
children = children.concat(el);
});
return children;
2021-11-30 09:55:37 +08:00
}
// 动态路由遍历,验证是否具备权限
export function filterDynamicRoutes(routes) {
2023-06-07 10:44:31 +08:00
const res = [];
routes.forEach((route) => {
if (route.permissions) {
if (auth.hasPermiOr(route.permissions)) {
res.push(route);
}
} else if (route.roles) {
if (auth.hasRoleOr(route.roles)) {
res.push(route);
}
}
});
return res;
}
2021-11-30 09:55:37 +08:00
export const loadView = (view) => {
2023-06-07 10:44:31 +08:00
let res;
for (const path in modules) {
const dir = path.split("views/")[1].split(".vue")[0];
if (dir === view) {
res = () => modules[path]();
}
2021-11-30 09:55:37 +08:00
}
2023-06-07 10:44:31 +08:00
return res;
2022-08-30 10:36:30 +08:00
};
2021-11-30 09:55:37 +08:00
2022-08-30 10:36:30 +08:00
export default usePermissionStore;