You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.9 KiB

  1. // 菜单树过滤
  2. export function filterMenu(menuList) {
  3. return menuList
  4. // 过滤不是4级的 123 为菜单
  5. .filter(menu => menu.menuType !== 4)
  6. .map(menu => ({
  7. ...menu,
  8. children: menu.children ? filterMenu(menu.children) : []
  9. }))
  10. .sort((a, b) => a.priority - b.priority); // 按 id 升序
  11. }
  12. // 辅助函数:查找第一个可访问的菜单项
  13. export function findFirstAccessibleMenu(menuList) {
  14. if (!menuList || menuList.length === 0) return null
  15. for (const menu of menuList) {
  16. if (menu.menuType === 1) { // 根
  17. const childResult = findFirstAccessibleMenu(menu.children)
  18. if (childResult) return childResult
  19. } else if (menu.menuType === 2) { // 目录
  20. return menu
  21. } else if (menu.menuType === 3) { // 菜单
  22. return menu
  23. }
  24. }
  25. return null
  26. }
  27. // 路由映射
  28. export const getRoutePath = (menu) => {
  29. // 路由映射表:key为接口menuName,value为对应路由路径
  30. const routeMap = {
  31. '工作台': '/workspace',
  32. '审核页面': '/audit',
  33. '财务审核': '/audit',
  34. '汇率管理': '/rate',
  35. '消耗管理': '/coinConsume',
  36. '消耗页面': '/coinConsume',
  37. '权限管理': '/permissions',
  38. '充值管理': '/coinRecharge',
  39. '充值页面': '/coinRecharge',
  40. '退款管理': '/coinRefund',
  41. '退款页面': '/coinRefund',
  42. '客户账户明细': '/usergold',
  43. };
  44. // 未匹配的菜单默认使用id作为路由(可根据实际需求调整)
  45. return routeMap[menu.menuName] || '/noPermissionPage'
  46. }
  47. // 这是获取用户信息的接口
  48. const adminData = ref({
  49. name: ''
  50. })
  51. import API from "@/util/http.js";
  52. import {ref} from "vue";
  53. export const getAdminData = async function () {
  54. try {
  55. const result = await API({url: '/admin/userinfo', data: {}})
  56. adminData.value = result
  57. console.log('请求成功', result)
  58. console.log('用户信息', adminData.value)
  59. return result
  60. } catch (error) {
  61. console.log('请求失败', error)
  62. }
  63. }
  64. // 权限检查函数
  65. export const hasPermission = (to, menuList) => {
  66. if (!menuList || menuList.length === 0) return false;
  67. // 转换路由路径为菜单名称
  68. const routePath = to.path;
  69. const menuName = Object.keys(getRoutePath).find(key => getRoutePath[key] === routePath);
  70. if (!menuName) return false;
  71. // 扁平化菜单树以便查找
  72. const flattenMenu = (menus) => {
  73. return menus.reduce((acc, menu) => {
  74. acc.push(menu);
  75. if (menu.children) {
  76. acc = acc.concat(flattenMenu(menu.children));
  77. }
  78. return acc;
  79. }, []);
  80. };
  81. const flatMenuList = flattenMenu(menuList);
  82. return flatMenuList.some(menu => menu.menuName === menuName);
  83. };