| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- import { localST_UserInfo, localST_RoutesList } from '../base/storage';
- /**
- * 根据 系统资源标识符 判断当前用户对某个资源是否拥有权限
- * 所有可用的资源标识符,请查看下方链接中的定义
- * 大模型2.0 -> 权限管理 -> 资源管理
- * @see http://smartai.com:8293/#/permission/resource
- * @param {String} str - 资源标识符,例如:system:menu:list
- * @returns {Boolean}
- */
- export const hasPermission = (str) => {
- try {
- const user_nfo = localST_UserInfo.getItem();
- if (!user_nfo) return false;
- const parsed_user_info = JSON.parse(user_nfo);
- if (!parsed_user_info || !Array.isArray(parsed_user_info.permissions)) return false;
- if (parsed_user_info.permissions.includes('*:*:*')) return true;
- return parsed_user_info.permissions.includes(str);
- } catch (error) {
- // 捕获任何异常,比如 JSON 解析错误,并返回 false
- console.error('错误信息:', error);
- return false;
- }
- };
- // 格式化路由数据
- export const formatRoutes = (data) => {
- const routes = [];
- function convertRoute(item) {
- // 如果没有地址或者没有组件,则直接过滤
- if (item.path === '-' || item.component === '-') {
- return null;
- }
- const route = {
- path: item.path,
- // 判断是layout还是view
- component: item.component.split('layout/')[1]
- ? () => import(`../views/layout/${item.component.split('layout/')[1]}.vue`)
- : () => import(`../views/${item.component}.vue`)
- };
- if (item.children && item.children.length > 0) {
- route.children = item.children.map((child) => convertRoute(child)).filter((childRoute) => childRoute !== null);
- }
- // 只返回非null的route对象
- return route;
- }
- function addRoutes(item) {
- const formattedRoute = convertRoute(item);
- if (formattedRoute !== null) {
- routes.push(formattedRoute);
- }
- }
- if (data && Array.isArray(data)) {
- data.forEach((item) => {
- addRoutes(item);
- });
- }
- return routes;
- };
- // 扁平化路由数据
- export const flattenRoutes = (routes) => {
- let flatRoutes = [];
- function recurse(routeList) {
- routeList.forEach((route) => {
- flatRoutes.push(route);
- if (route.children) {
- recurse(route.children);
- }
- });
- }
- recurse(routes);
- return flatRoutes;
- };
- // 获取有权限的路由
- let routesList = [];
- let accessRoutes = [];
- let accessFlatRoutes = [];
- export const initializeRoutes = () => {
- try {
- routesList = JSON.parse(localST_RoutesList.getItem());
- } catch (error) {
- console.error('路由解析错误:', error);
- }
- accessRoutes = formatRoutes(routesList);
- accessFlatRoutes = flattenRoutes(accessRoutes);
- return { accessRoutes, accessFlatRoutes };
- };
- // 递归过滤未启用的节点
- export const filterNotEnabledTree = (nodes) => {
- return nodes
- .filter((node) => node.status === '1')
- .map((node) => ({
- ...node,
- children: node.children ? filterNotEnabledTree(node.children) : []
- }));
- };
|