permission.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { localST_UserInfo, localST_RoutesList } from '../base/storage';
  2. /**
  3. * 根据 系统资源标识符 判断当前用户对某个资源是否拥有权限
  4. * 所有可用的资源标识符,请查看下方链接中的定义
  5. * 大模型2.0 -> 权限管理 -> 资源管理
  6. * @see http://smartai.com:8293/#/permission/resource
  7. * @param {String} str - 资源标识符,例如:system:menu:list
  8. * @returns {Boolean}
  9. */
  10. export const hasPermission = (str) => {
  11. try {
  12. const user_nfo = localST_UserInfo.getItem();
  13. if (!user_nfo) return false;
  14. const parsed_user_info = JSON.parse(user_nfo);
  15. if (!parsed_user_info || !Array.isArray(parsed_user_info.permissions)) return false;
  16. if (parsed_user_info.permissions.includes('*:*:*')) return true;
  17. return parsed_user_info.permissions.includes(str);
  18. } catch (error) {
  19. // 捕获任何异常,比如 JSON 解析错误,并返回 false
  20. console.error('错误信息:', error);
  21. return false;
  22. }
  23. };
  24. // 格式化路由数据
  25. export const formatRoutes = (data) => {
  26. const routes = [];
  27. function convertRoute(item) {
  28. // 如果没有地址或者没有组件,则直接过滤
  29. if (item.path === '-' || item.component === '-') {
  30. return null;
  31. }
  32. const route = {
  33. path: item.path,
  34. // 判断是layout还是view
  35. component: item.component.split('layout/')[1]
  36. ? () => import(`../views/layout/${item.component.split('layout/')[1]}.vue`)
  37. : () => import(`../views/${item.component}.vue`)
  38. };
  39. if (item.children && item.children.length > 0) {
  40. route.children = item.children.map((child) => convertRoute(child)).filter((childRoute) => childRoute !== null);
  41. }
  42. // 只返回非null的route对象
  43. return route;
  44. }
  45. function addRoutes(item) {
  46. const formattedRoute = convertRoute(item);
  47. if (formattedRoute !== null) {
  48. routes.push(formattedRoute);
  49. }
  50. }
  51. if (data && Array.isArray(data)) {
  52. data.forEach((item) => {
  53. addRoutes(item);
  54. });
  55. }
  56. return routes;
  57. };
  58. // 扁平化路由数据
  59. export const flattenRoutes = (routes) => {
  60. let flatRoutes = [];
  61. function recurse(routeList) {
  62. routeList.forEach((route) => {
  63. flatRoutes.push(route);
  64. if (route.children) {
  65. recurse(route.children);
  66. }
  67. });
  68. }
  69. recurse(routes);
  70. return flatRoutes;
  71. };
  72. // 获取有权限的路由
  73. let routesList = [];
  74. let accessRoutes = [];
  75. let accessFlatRoutes = [];
  76. export const initializeRoutes = () => {
  77. try {
  78. routesList = JSON.parse(localST_RoutesList.getItem());
  79. } catch (error) {
  80. console.error('路由解析错误:', error);
  81. }
  82. accessRoutes = formatRoutes(routesList);
  83. accessFlatRoutes = flattenRoutes(accessRoutes);
  84. return { accessRoutes, accessFlatRoutes };
  85. };
  86. // 递归过滤未启用的节点
  87. export const filterNotEnabledTree = (nodes) => {
  88. return nodes
  89. .filter((node) => node.status === '1')
  90. .map((node) => ({
  91. ...node,
  92. children: node.children ? filterNotEnabledTree(node.children) : []
  93. }));
  94. };