Files
MrleeAdmin/app/plugins/permission.ts
T
Mr.April d84d972e73 feat: 适配5.x后端,重构前端架构
主要变更:
1. 重构API目录结构,采用模块化设计,适配5.x后端接口规范
2. 优化布局组件,提升用户界面体验
3. 增强国际化支持,完善多语言配置
4. 新增权限控制功能,支持前端路由权限管理
5. 优化认证流程,支持JWT认证机制
6. 新增中间件和插件,提升系统扩展性
7. 优化状态管理,采用更清晰的模块化设计
8. 新增搜索框、广告等组件,丰富页面功能
9. 更改包管理工具从npm到pnpm,提升依赖管理效率
10. 完善配置系统,支持多环境配置

适配5.x后端特性:
- 采用新的API接口规范
- 支持JWT认证机制
- 适配后端权限控制体系
- 支持后端国际化接口
- 优化数据交互方式
2026-04-07 11:01:11 +08:00

71 lines
1.6 KiB
TypeScript

import type { App, Directive, Plugin } from 'vue'
import { useMemberStore } from '~/stores/member'
/**
* 权限指令配置
*/
interface AuthDirectiveOptions {
permissions: string | string[]
}
/**
* 检查用户是否有权限
* @param permissions 权限码或权限码数组
* @returns 是否有权限
*/
export function hasAuth(permissions: string | string[]): boolean {
const memberStore = useMemberStore()
const userPermissions = memberStore.info?.permissions || []
// 如果用户拥有所有权限,直接返回 true
if (userPermissions.includes('*')) {
return true
}
// 处理字符串权限
if (typeof permissions === 'string') {
return userPermissions.includes(permissions)
}
// 处理数组权限,只要拥有任一权限就返回 true
return permissions.some(permission => userPermissions.includes(permission))
}
/**
* 权限指令
*/
const authDirective: Directive = {
mounted(el: HTMLElement, binding: any) {
const hasPermission = hasAuth(binding.value)
if (!hasPermission) {
el.style.display = 'none'
}
},
updated(el: HTMLElement, binding: any) {
const hasPermission = hasAuth(binding.value)
if (!hasPermission) {
el.style.display = 'none'
} else {
el.style.display = ''
}
}
}
/**
* 权限插件
*/
const permissionPlugin: Plugin = {
install(app: App) {
// 注册 v-auth 指令
app.directive('auth', authDirective)
// 注册全局 hasAuth 函数
app.config.globalProperties.$hasAuth = hasAuth
// 提供给组合式 API 使用
app.provide('hasAuth', hasAuth)
}
}
export default permissionPlugin