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.
410 lines
17 KiB
410 lines
17 KiB
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: 13 } //
|
|
},
|
|
//金币管理
|
|
{
|
|
path: '/goldManage',
|
|
name: 'goldManage',
|
|
children: [
|
|
// 审核
|
|
{
|
|
path: '/audit',
|
|
name: "audit",
|
|
component: () => import("../views/audit/gold/audit.vue"),
|
|
meta: { permissionId: 14 },
|
|
// redirect: '/index',
|
|
children: [
|
|
// 充值审核
|
|
{
|
|
path: 'rechargeAudit',
|
|
name: "rechargeAudit",
|
|
component: () => import("../views/audit/gold/rechargeAudit.vue"),
|
|
meta: { permissionId: [28,30,31,32,33,34,35] } // 对应"查看充值审核"id=11、"充值审批"id=12
|
|
},
|
|
// 退款审核
|
|
{
|
|
path: 'refundAudit',
|
|
name: "refundAudit",
|
|
component: () => import("../views/audit/gold/refundAudit.vue"),
|
|
meta: { permissionId: [29,36,37,38,39,40,41] } // 对应"查看退款审核"id=13、"退款审批"id=14
|
|
},
|
|
]
|
|
}, {
|
|
path: '/beanAudit',
|
|
name: "beanAudit",
|
|
component: () => import("../views/audit/bean/beanAudit.vue"),
|
|
meta: { permissionId: 15 },
|
|
children: [
|
|
// 充值审核
|
|
{
|
|
path: 'addbeanAudit',
|
|
name: "addbeanAudit",
|
|
component: () => import("../views/audit/bean/beanAudit.vue"),
|
|
meta: { permissionId: [42,43,44,45,46,47] } // 对应"查看充值审核"id=11、"充值审批"id=12
|
|
},
|
|
]
|
|
},
|
|
|
|
|
|
// 金币消耗
|
|
{
|
|
path: '/coinConsume',
|
|
name: "coinConsume",
|
|
component: () => import("../views/consume/gold/coinConsume.vue"),
|
|
// redirect: '/coinConsume/add',
|
|
meta: { permissionId: 19 },
|
|
children: [
|
|
// 金币新增消耗
|
|
{
|
|
path: 'add',
|
|
name: "addCoinConsume",
|
|
component: () => import("../views/consume/gold/addCoinConsume.vue"),
|
|
meta: { permissionId: 55 } // 对应"提交金币消耗"id=19
|
|
},
|
|
// 金币消耗明细详情
|
|
{
|
|
path: 'detail',
|
|
name: "coinConsumeDetail",
|
|
component: () => import("../views/consume/gold/coinConsumeDetail.vue"),
|
|
meta: { permissionId: 56 } // 对应"查看金币消耗明细"id=20
|
|
}
|
|
]
|
|
},
|
|
// 金豆消耗
|
|
{
|
|
path: '/beanConsume',
|
|
name: "beanConsume",
|
|
component: () => import("../views/consume/bean/beanConsume.vue"),
|
|
meta: { permissionId: 20 },
|
|
children: [
|
|
// 金豆新增消耗
|
|
{
|
|
path: 'add',
|
|
name: "addBeanConsume",
|
|
component: () => import("../views/consume/bean/addBeanConsume.vue"),
|
|
meta: { permissionId: 57 } // 对应"提交金豆消耗"id=49
|
|
},
|
|
// 直播
|
|
{
|
|
path: 'live',
|
|
name: "liveStream",
|
|
component: () => import("../views/consume/bean/liveStream.vue"),
|
|
meta: { permissionId: 58 } // 对应"直播"id=50
|
|
},
|
|
// 铁粉
|
|
{
|
|
path: 'fan',
|
|
name: "dieHardFan",
|
|
component: () => import("../views/consume/bean/dieHardFan.vue"),
|
|
meta: { permissionId: 59 } // 对应"铁粉"id=51
|
|
},
|
|
// 文章视频
|
|
{
|
|
path: 'article',
|
|
name: "articleVideo",
|
|
component: () => import("../views/consume/bean/articleVideo.vue"),
|
|
meta: { permissionId: 60 } // 对应"文章视频"id=52
|
|
}
|
|
|
|
]
|
|
},
|
|
|
|
// 汇率管理
|
|
{
|
|
path: '/rate',
|
|
name: "rate",
|
|
component: () => import("../views/managerecharge/rate.vue"),
|
|
meta: { permissionId: [16,48,49] } // 对应"汇率查看"id=15、"汇率修改"id=16
|
|
},
|
|
// 金币充值
|
|
{
|
|
path: '/coinRecharge',
|
|
name: "coinRecharge",
|
|
component: () => import("../views/recharge/gold/coinRecharge.vue"),
|
|
meta: { permissionId: 17 },
|
|
// redirect: '/coinRecharge/add',
|
|
children: [
|
|
// 金币新增充值
|
|
{
|
|
path: 'add',
|
|
name: "addCoinRecharge",
|
|
component: () => import("../views/recharge/gold/addCoinRecharge.vue"),
|
|
meta: { permissionId: 50 } // 对应"提交金币充值"id=17
|
|
},
|
|
// 金币充值明细详情
|
|
{
|
|
path: 'detail',
|
|
name: "coinRechargeDetail",
|
|
component: () => import("../views/recharge/gold/coinRechargeDetail.vue"),
|
|
meta: { permissionId: 51 } // 对应"查看金币充值明细"id=18
|
|
}
|
|
]
|
|
},
|
|
|
|
// 金豆充值
|
|
{
|
|
path: '/beanRecharge',
|
|
name: "beanRecharge",
|
|
component: () => import("../views/recharge/bean/beanRecharge.vue"),
|
|
// redirect: '/coinRecharge/add',
|
|
meta: { permissionId: 18 },
|
|
children: [
|
|
// 金豆新增充值
|
|
{
|
|
path: 'add',
|
|
name: "addBeanRecharge",
|
|
component: () => import("../views/recharge/bean/addBeanRecharge.vue"),
|
|
meta: { permissionId: 52 } // 对应"提交金豆充值"id=46
|
|
},
|
|
// 金豆系统充值
|
|
{
|
|
path: 'system',
|
|
name: "beanSystemRecharge",
|
|
component: () => import("../views/recharge/bean/beanSystemRecharge.vue"),
|
|
meta: { permissionId: 53 } // 对应"查看金豆系统充值明细"id=47
|
|
},
|
|
// 金豆线上充值
|
|
{
|
|
path: 'online',
|
|
name: "beanOnlineRecharge",
|
|
component: () => import("../views/recharge/bean/beanOnlineRecharge.vue"),
|
|
meta: { permissionId: 54 } // 对应"查看金豆线上充值明细"id=48
|
|
}
|
|
]
|
|
},
|
|
|
|
// 金币退款
|
|
{
|
|
path: '/coinRefund',
|
|
name: "coinRefund",
|
|
component: () => import("../views/refund/gold/coinRefund.vue"),
|
|
meta: { permissionId: 9 },
|
|
children: [
|
|
// 金币新增退款
|
|
{
|
|
path: 'add',
|
|
name: "addCoinRefund",
|
|
component: () => import("../views/refund/gold/addCoinRefund.vue"),
|
|
meta: { permissionId: 61 }
|
|
},
|
|
// 金币退款明细详情
|
|
{
|
|
path: 'detail',
|
|
name: "coinRefundDetail",
|
|
component: () => import("../views/refund/gold/coinRefundDetail.vue"),
|
|
meta: { permissionId: 62 }
|
|
}
|
|
]
|
|
},
|
|
|
|
// 客户账户明细
|
|
{
|
|
path: '/usergold',
|
|
name: "usergold",
|
|
component: () => import("../views/usergold/gold/clientCount.vue"),
|
|
meta: { permissionId: 10 },
|
|
children: [
|
|
// 金币明细
|
|
{
|
|
path: 'detail',
|
|
name: "clientCountDetail",
|
|
component: () => import("../views/usergold/gold/clientCountDetail.vue"),
|
|
meta: { permissionId: 63 }
|
|
},
|
|
// 金币余额
|
|
{
|
|
path: 'balance',
|
|
name: "clientCountBalance",
|
|
component: () => import("../views/usergold/gold/clientCountBalance.vue"),
|
|
meta: { permissionId: 64 }
|
|
},
|
|
]
|
|
},
|
|
// {
|
|
// path: 'userbean',
|
|
// name: "userbean",
|
|
// component: () => import("../views/usergold/bean/userbean.vue"),
|
|
// meta: { permissionId: 45 }
|
|
// },
|
|
{
|
|
path: '/history',
|
|
name: "history",
|
|
component: () => import("../views/history/history.vue"),
|
|
meta: { permissionId: 12 },
|
|
children: [
|
|
// {
|
|
// path: 'newHistory',
|
|
// name: "newHistory",
|
|
// component: () => import("../views/history/newHistory.vue"),
|
|
// meta: { permissionId: 12 }
|
|
// },
|
|
// {
|
|
// path: 'oldHistory',
|
|
// name: "oldHistory",
|
|
// component: () => import("../views/history/oldHistory.vue"),
|
|
// meta: { permissionId: 12 }
|
|
// }
|
|
]
|
|
},
|
|
|
|
// 权限管理
|
|
{
|
|
path: '/permissions',
|
|
name: "permissions",
|
|
component: () => import("../views/permissions/permissions.vue"),
|
|
meta: { permissionId: 11 },
|
|
children: [
|
|
// 用户权限
|
|
{
|
|
path: 'userPermission',
|
|
name: "userPermission",
|
|
component: () => import("../views/permissions/userPermission.vue"),
|
|
meta: { permissionId: [24,65,66,67,68,69] }
|
|
},
|
|
// 角色权限
|
|
{
|
|
path: 'rolePermission',
|
|
name: "rolePermission",
|
|
component: () => import("../views/permissions/rolePermission.vue"),
|
|
meta: { permissionId: [25,70,71,72] }
|
|
},
|
|
]
|
|
},
|
|
]
|
|
},
|
|
{
|
|
path:'moneyManage',
|
|
name:'moneyManage',
|
|
children:[
|
|
|
|
]
|
|
},
|
|
|
|
// 没有权限
|
|
{
|
|
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;
|