From 80cfa6dbaf7b6eb191b35bbb8c1ef248e358eb8c Mon Sep 17 00:00:00 2001 From: caoqianming Date: Fri, 21 Nov 2025 14:31:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20base=20=E6=B7=BB=E5=8A=A0auth=E6=8C=87?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.js | 4 +++ utils/auth.js | 73 +++++++++++++++++++++++++++++++++++++++++++++ utils/directives.js | 36 ++++++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 utils/auth.js create mode 100644 utils/directives.js diff --git a/main.js b/main.js index cd2b1c3..5467b27 100644 --- a/main.js +++ b/main.js @@ -25,9 +25,13 @@ import App from './App.vue' import api from './utils/api' import check from './utils/check' import config from './utils/config.js' +import { authDirective } from '@/utils/directives.js' export function createApp() { const app = createSSRApp(App) + + app.directive('auth', authDirective) + app.config.globalProperties.$api = api app.config.globalProperties.$check = check app.config.globalProperties.$config = config diff --git a/utils/auth.js b/utils/auth.js new file mode 100644 index 0000000..3465c7e --- /dev/null +++ b/utils/auth.js @@ -0,0 +1,73 @@ +/** + * 权限管理类 + */ +class AuthManager { + constructor() { + this.permissions = uni.getStorageSync('userPermissions') || [] + } + + /** + * 设置用户权限 + * @param {Array} permissions - 权限数组 + */ + setPermissions(permissions) { + this.permissions = permissions + uni.setStorageSync('userPermissions', permissions) + console.log('权限已设置:', permissions) + } + + /** + * 清除权限 + */ + clearPermissions() { + this.permissions = [] + uni.removeStorageSync('userPermissions') + } + + /** + * 检查权限 + * @param {string|Array} permission - 单个权限或权限数组 + * @returns {boolean} + */ + check(permission) { + if (!permission) return true + + // 如果是数组,检查是否有任一权限 + if (Array.isArray(permission)) { + return permission.some(p => this.hasPermission(p)) + } + + // 单个权限检查 + return this.hasPermission(permission) + } + + /** + * 检查所有权限 + * @param {Array} permissions - 权限数组 + * @returns {boolean} + */ + checkAll(permissions) { + if (!Array.isArray(permissions)) return false + return permissions.every(p => this.hasPermission(p)) + } + + /** + * 内部权限检查方法 + * @param {string} permission + * @returns {boolean} + */ + hasPermission(permission) { + return this.permissions.includes(permission) + } + + /** + * 获取当前权限列表 + * @returns {Array} + */ + getPermissions() { + return [...this.permissions] + } +} + +// 创建单例实例 +export const auth = new AuthManager() \ No newline at end of file diff --git a/utils/directives.js b/utils/directives.js new file mode 100644 index 0000000..97acb97 --- /dev/null +++ b/utils/directives.js @@ -0,0 +1,36 @@ +import { auth } from '@/utils/auth.js' + +/** + * 权限指令 + * 使用方式: + * v-auth="'user:add'" // 单个权限 + * v-auth="['user:add', 'user:edit']" // 多个权限(满足其一即可) + */ +export const authDirective = { + mounted(el, binding) { + checkAuth(el, binding) + }, + updated(el, binding) { + checkAuth(el, binding) + } +} + +/** + * 权限检查逻辑 + */ +function checkAuth(el, binding) { + const hasAuth = auth.check(binding.value) + + if (!hasAuth) { + // 没有权限时隐藏元素 + el.style.display = 'none' + + // 或者完全移除元素(根据需求选择) + // if (el.parentNode) { + // el.parentNode.removeChild(el) + // } + } else { + // 有权限时确保显示 + el.style.display = '' + } +} \ No newline at end of file