import { createRouter, createWebHashHistory } from 'vue-router'; import axios from "axios"; import { storeToRefs } from "pinia"; import { useAdminStore } from "@/store/index.js"; // 路由定义(包含权限映射 meta.permissionId) const routes = [ { path: '/', redirect: "/login" }, { path: "/login", name: "login", component: () => import("../views/login.vue"), }, { meta: { requireAuth: true }, path: '/', component: () => import("../views/home.vue"), children: [ // 工作台 { path: 'workbench', name: "workbench", component: () => import("../views/workspace/index.vue"), meta: { permissionId: 2 } }, //金币管理 { path: '/goldManage', name: 'goldManage', meta: { permissionId: 3 }, children: [ // 审核 { path: '/audit', name: "audit", component: () => import("../views/audit/gold/audit.vue"), meta: { permissionId: 4 }, children: [ // 充值审核 { path: 'rechargeAudit', name: "rechargeAudit", component: () => import("../views/audit/gold/rechargeAudit.vue"), meta: { permissionId: [6, 7, 8, 9, 10, 11, 12] } }, // 退款审核 { path: 'refundAudit', name: "refundAudit", component: () => import("../views/audit/gold/refundAudit.vue"), meta: { permissionId: [13, 14, 15, 16, 17, 18, 19] } }, ] }, { path: '/beanAudit', name: "beanAudit", component: () => import("../views/audit/bean/beanAudit.vue"), meta: { permissionId: 20 }, children: [ // 充值审核 { path: 'addbeanAudit', name: "addbeanAudit", component: () => import("../views/audit/bean/beanAudit.vue"), meta: { permissionId: [21, 22, 23, 24, 25, 26] } }, ] }, // 金币消耗 { path: '/coinConsume', name: "coinConsume", component: () => import("../views/consume/gold/coinConsume.vue"), meta: { permissionId: 39 }, children: [ // 金币新增消耗 { path: 'add', name: "addCoinConsume", component: () => import("../views/consume/gold/addCoinConsume.vue"), meta: { permissionId: 41 } }, // 金币消耗明细详情 { path: 'detail', name: "coinConsumeDetail", component: () => import("../views/consume/gold/coinConsumeDetail.vue"), meta: { permissionId: 40 } } ] }, // 金豆消耗 { path: '/beanConsume', name: "beanConsume", component: () => import("../views/consume/bean/beanConsume.vue"), meta: { permissionId: 42 }, children: [ // 金豆新增消耗 { path: 'add', name: "addBeanConsume", component: () => import("../views/consume/bean/addBeanConsume.vue"), meta: { permissionId: 46 } }, // 直播 { path: 'live', name: "liveStream", component: () => import("../views/consume/bean/liveStream.vue"), meta: { permissionId: 43 } }, // 铁粉 { path: 'fan', name: "dieHardFan", component: () => import("../views/consume/bean/dieHardFan.vue"), meta: { permissionId: 44 } }, // 文章视频 { path: 'article', name: "articleVideo", component: () => import("../views/consume/bean/articleVideo.vue"), meta: { permissionId: 45 } } ] }, // 汇率管理 { path: '/rate', name: "rate", component: () => import("../views/managerecharge/rate.vue"), meta: { permissionId: [27, 28, 29] } }, // 金币充值 { path: '/coinRecharge', name: "coinRecharge", component: () => import("../views/recharge/gold/coinRecharge.vue"), meta: { permissionId: 31 }, children: [ // 金币新增充值 { path: 'add', name: "addCoinRecharge", component: () => import("../views/recharge/gold/addCoinRecharge.vue"), meta: { permissionId: 33 } }, // 金币充值明细 { path: 'detail', name: "coinRechargeDetail", component: () => import("../views/recharge/gold/coinRechargeDetail.vue"), meta: { permissionId: 32 } } ] }, // 金豆充值 { path: '/beanRecharge', name: "beanRecharge", component: () => import("../views/recharge/bean/beanRecharge.vue"), meta: { permissionId: 34 }, children: [ // 金豆新增充值 { path: 'add', name: "addBeanRecharge", component: () => import("../views/recharge/bean/addBeanRecharge.vue"), meta: { permissionId: 37 } }, // 金豆系统充值 { path: 'system', name: "beanSystemRecharge", component: () => import("../views/recharge/bean/beanSystemRecharge.vue"), meta: { permissionId: 35 } }, // 金豆线上充值 { path: 'online', name: "beanOnlineRecharge", component: () => import("../views/recharge/bean/beanOnlineRecharge.vue"), meta: { permissionId: 36 } } ] }, // 金币退款 { path: '/coinRefund', name: "coinRefund", component: () => import("../views/refund/gold/coinRefund.vue"), meta: { permissionId: 47 }, children: [ // 金币新增退款 { path: 'add', name: "addCoinRefund", component: () => import("../views/refund/gold/addCoinRefund.vue"), meta: { permissionId: 48 } }, // 金币退款明细详情 { path: 'detail', name: "coinRefundDetail", component: () => import("../views/refund/gold/coinRefundDetail.vue"), meta: { permissionId: 49 } } ] }, // 金币客户账户明细 { path: '/usergold', name: "usergold", component: () => import("../views/usergold/gold/clientCount.vue"), meta: { permissionId: 51 }, children: [ // 金币明细 { path: 'detail', name: "clientCountDetail", component: () => import("../views/usergold/gold/clientCountDetail.vue"), meta: { permissionId: 52 } }, // 金币余额 { path: 'balance', name: "clientCountBalance", component: () => import("../views/usergold/gold/clientCountBalance.vue"), meta: { permissionId: 53 } }, ] }, { path: '/userbean', name: "userbean", component: () => import("../views/usergold/bean/userbean.vue"), meta: { permissionId: 54 } }, { path: '/history', name: "history", component: () => import("../views/history/history.vue"), meta: { permissionId: 55 }, children: [ { path: 'newHistory', name: "newHistory", component: () => import("../views/history/newHistory.vue"), meta: { permissionId: 56 } }, { path: 'oldHistory', name: "oldHistory", component: () => import("../views/history/oldHistory.vue"), meta: { permissionId: 57 } } ] }, // 权限管理 { path: '/permissions', name: "permissions", component: () => import("../views/permissions/permissions.vue"), meta: { permissionId: 128 }, children: [ // 用户权限 { path: 'userPermission', name: "userPermission", component: () => import("../views/permissions/userPermission.vue"), meta: { permissionId: 129 } }, // 角色权限 { path: 'rolePermission', name: "rolePermission", component: () => import("../views/permissions/rolePermission.vue"), meta: { permissionId: 130 } } ] } ] }, // 现金管理 { path: '/moneyManage', name: 'moneyManage', meta: { permissionId: 58 }, children: [ // 收款明细 { path: 'receiveDetail', name: "receiveDetail", meta: { permissionId: 74 }, children: [ // 客服页面 { path: 'receiveService', name: "receiveService", component: () => import("../views/moneyManage/receiveDetail/receiveService.vue"), meta: { permissionId: 60 } }, // 地区负责人页面 { path: 'receiveManager', name: "receiveManager", component: () => import("../views/moneyManage/receiveDetail/receiveManage.vue"), meta: { permissionId: [67, 79] } }, {//地区财务 path: 'receiveFinance', name: "receiveFinance", component: () => import("../views/moneyManage/receiveDetail/receiveFinance.vue"), meta: { permissionId: [67, 79] } }, //总部管理员及财务 { path: 'receiveHeader', name: "receiveHeader", component: () => import("../views/moneyManage/receiveDetail/receiveHead.vue"), meta: { permissionId: 91 } }, ] }, // 退款明细 { path: 'refundDetail', name: "refundDetail", component: () => import("../views/moneyManage/refundDetail/refundDetail.vue"), meta: { permissionId: 98 }, children: [ // 客服页面 { path: 'refundService', name: "refundService", component: () => import("../views/moneyManage/refundDetail/refundService.vue"), meta: { permissionId: 99 } }, // 地区财务页面 { path: 'refundFinance', name: "refundFinance", component: () => import("../views/moneyManage/refundDetail/refundFinance.vue"), meta: { permissionId: 103 } }, // 地区负责人页面 { path: 'refundCharge', name: "refundCharge", component: () => import("../views/moneyManage/refundDetail/refundCharge.vue"), meta: { permissionId: 107 } }, //总部管理员及财务 { path: 'refundHeader', name: "refundHeader", component: () => import("../views/moneyManage/refundDetail/refundHeader.vue"), meta: { permissionId: 111 } }, ] }, // 执行明细 { path: 'executor', name: "executor", component: () => import("../views/moneyManage/executor/executor.vue"), meta: { permissionId: 115 } }, ] }, // 频道管理 { path: 'channelManage', name: 'channelManage', meta: { permissionId: 124 }, children: [ // 打赏 { path: 'reward', name: "reward", component: () => import("../views/channelManage/reward/reward.vue"), meta: { permissionId: 125 } }, // 铁粉 { path: 'fans', name: "fans", component: () => import("../views/channelManage/fans/fans.vue"), meta: { permissionId: 126 } }, // 购物车 { path: 'cart', name: "cart", component: () => import("../views/channelManage/cart/cart.vue"), meta: { permissionId: 127 } } ] }, // 活动管理 { path: 'activityManage', name: "activityManage", component: () => import("../views/activityManage/activity.vue"), meta: { permissionId: 119 } }, // 没有权限 { path: '/noPermission', name: "noPermission", component: () => import("../views/noPermissionPage.vue") } ] }, // 跳转页面(无需权限) { path: '/PasswordSuccess', name: "PasswordSuccess", component: () => import("../components/PasswordSuccess.vue") } ]; // 创建路由实例 const router = createRouter({ history: createWebHashHistory(), routes }); // 工具函数:从菜单树提取所有权限ID const getAllPermissionIds = (menuTree) => { let permissionIds = []; const traverse = (menuList) => { menuList.forEach(menu => { permissionIds.push(menu.id); if (menu.children && menu.children.length > 0) { traverse(menu.children); } }); }; traverse(menuTree); return permissionIds; }; // 全局路由守卫 router.beforeEach(async (to, from, next) => { const adminStore = useAdminStore() const { adminData, menuTree } = storeToRefs(adminStore) const token = localStorage.getItem("token"); const machineId = localStorage.getItem("machineId"); // 1. 未登录:强制跳转到登录页 if (to.name !== "login" && !token) { next(`/login?machineId=${machineId || ''}`); return; } // 2. 已登录:处理权限验证 if (token) { // 获取管理员信息 let roleId = null; console.log('路由的adminData:', adminData.value) try { roleId = adminData.value.roleId; if (!roleId) { localStorage.removeItem('token'); // 清除token,强制重新登录 next(`/login?machineId=${machineId || ''}`); return; } } catch (error) { localStorage.removeItem('token'); // 清除token,强制重新登录 adminStore.clearState() next(`/login?machineId=${machineId || ''}`); return; } let userPermissionIds = []; // 拿权限id userPermissionIds = getAllPermissionIds(menuTree.value) console.log("userPermissionIds", userPermissionIds) // 2.4 权限验证(逻辑不变) console.log('to.meta:', to.meta) const requiresPermission = to.meta && to.meta.permissionId; if (requiresPermission) { const hasPermission = Array.isArray(requiresPermission) ? requiresPermission.some(id => userPermissionIds.includes(id)) : userPermissionIds.includes(requiresPermission); if (!hasPermission) { next('/noPermission'); return; } } } // 3. 正常跳转 next(); }); export default router;