feat: base 添加auth指令
This commit is contained in:
parent
ec16b88318
commit
80cfa6dbaf
4
main.js
4
main.js
|
|
@ -25,9 +25,13 @@ import App from './App.vue'
|
||||||
import api from './utils/api'
|
import api from './utils/api'
|
||||||
import check from './utils/check'
|
import check from './utils/check'
|
||||||
import config from './utils/config.js'
|
import config from './utils/config.js'
|
||||||
|
import { authDirective } from '@/utils/directives.js'
|
||||||
|
|
||||||
export function createApp() {
|
export function createApp() {
|
||||||
const app = createSSRApp(App)
|
const app = createSSRApp(App)
|
||||||
|
|
||||||
|
app.directive('auth', authDirective)
|
||||||
|
|
||||||
app.config.globalProperties.$api = api
|
app.config.globalProperties.$api = api
|
||||||
app.config.globalProperties.$check = check
|
app.config.globalProperties.$check = check
|
||||||
app.config.globalProperties.$config = config
|
app.config.globalProperties.$config = config
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
|
@ -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 = ''
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue