Initial commit: Telegram Management System
Some checks failed
Deploy / deploy (push) Has been cancelled
Some checks failed
Deploy / deploy (push) Has been cancelled
Full-stack web application for Telegram management - Frontend: Vue 3 + Vben Admin - Backend: NestJS - Features: User management, group broadcast, statistics 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
119
frontend-vben/apps/web-antd/src/router/guard/permission.ts
Normal file
119
frontend-vben/apps/web-antd/src/router/guard/permission.ts
Normal file
@@ -0,0 +1,119 @@
|
||||
import type { Router } from 'vue-router';
|
||||
import { usePermissionStore } from '#/stores';
|
||||
import { useAccessStore } from '#/stores';
|
||||
import { getPermissionConfig } from '#/config/permission';
|
||||
import { message } from 'ant-design-vue';
|
||||
|
||||
/**
|
||||
* 创建权限守卫
|
||||
*/
|
||||
export function createPermissionGuard(router: Router) {
|
||||
const config = getPermissionConfig();
|
||||
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
// 权限功能未开启,直接放行
|
||||
if (!config.enabled) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
const accessStore = useAccessStore();
|
||||
const permissionStore = usePermissionStore();
|
||||
|
||||
// 白名单路由,直接放行
|
||||
const whiteList = ['/auth/login', '/auth/register', '/404', '/403'];
|
||||
if (whiteList.includes(to.path)) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
|
||||
// 检查是否已登录
|
||||
if (!accessStore.accessToken) {
|
||||
next(`/auth/login?redirect=${to.path}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果权限信息未初始化,先初始化
|
||||
if (!permissionStore.userPermission) {
|
||||
try {
|
||||
await permissionStore.initPermission();
|
||||
} catch (error) {
|
||||
console.error('初始化权限失败:', error);
|
||||
next('/auth/login');
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查路由权限
|
||||
const hasPermission = checkRoutePermission(to, permissionStore);
|
||||
|
||||
if (!hasPermission) {
|
||||
// 无权限访问
|
||||
if (config.showNoPermissionTip) {
|
||||
message.error('您没有权限访问该页面');
|
||||
}
|
||||
|
||||
// 跳转到无权限页面或首页
|
||||
next(config.noPermissionRedirect || '/');
|
||||
return;
|
||||
}
|
||||
|
||||
// 如果是动态路由且未添加,先添加动态路由
|
||||
if (!permissionStore.isDynamicRoutesAdded) {
|
||||
try {
|
||||
const routes = await permissionStore.buildPermissionRoutes();
|
||||
routes.forEach((route) => {
|
||||
router.addRoute(route);
|
||||
});
|
||||
permissionStore.setDynamicRoutesAdded(true);
|
||||
|
||||
// 动态路由添加后,重新导航到当前路由
|
||||
next({ ...to, replace: true });
|
||||
return;
|
||||
} catch (error) {
|
||||
console.error('添加动态路由失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查路由权限
|
||||
*/
|
||||
function checkRoutePermission(route: any, permissionStore: any): boolean {
|
||||
// 超级管理员拥有所有权限
|
||||
if (permissionStore.isSuperAdmin) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// 检查路由元信息中的权限配置
|
||||
const meta = route.meta || {};
|
||||
|
||||
// 检查权限编码
|
||||
if (meta.permissions) {
|
||||
const permissions = Array.isArray(meta.permissions)
|
||||
? meta.permissions
|
||||
: [meta.permissions];
|
||||
if (!permissionStore.hasPermission(permissions)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 检查角色
|
||||
if (meta.roles) {
|
||||
const roles = Array.isArray(meta.roles) ? meta.roles : [meta.roles];
|
||||
if (!permissionStore.hasRole(roles)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 如果路由没有配置权限信息,根据配置决定是否放行
|
||||
if (!meta.permissions && !meta.roles) {
|
||||
// 可以根据需要配置默认行为
|
||||
return true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user