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.
spring-boot-online-exam/frontend/src/utils/helper/permission.js

68 lines
2.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// 定义权限枚举
const PERMISSION_ENUM = {
'add': { key: 'add', label: '新增' },
'delete': { key: 'delete', label: '删除' },
'edit': { key: 'edit', label: '修改' },
'query': { key: 'query', label: '查询' },
'get': { key: 'get', label: '详情' },
'enable': { key: 'enable', label: '启用' },
'disable': { key: 'disable', label: '禁用' },
'import': { key: 'import', label: '导入' },
'export': { key: 'export', label: '导出' }
}
// 定义插件
function plugin (Vue) {
// 如果插件已经安装,则返回
if (plugin.installed) {
return
}
// 如果Vue实例中没有$auth属性则定义$auth属性
!Vue.prototype.$auth && Object.defineProperties(Vue.prototype, {
$auth: {
get () {
// 获取当前实例
const _this = this
// 返回一个函数,用于判断权限
return (permissions) => {
// 将权限字符串拆分为permission和action
const [permission, action] = permissions.split('.')
// 获取当前用户的权限列表
const permissionList = _this.$store.getters.roles.permissions
// 在权限列表中查找对应的permission
return permissionList.find((val) => {
return val.permissionId === permission
}).actionList.findIndex((val) => {
// 在actionList中查找对应的action
return val === action
}) > -1
}
}
}
})
// 如果Vue实例中没有$enum属性则定义$enum属性
!Vue.prototype.$enum && Object.defineProperties(Vue.prototype, {
$enum: {
get () {
// const _this = this;
// 返回一个函数,用于获取枚举值
return (val) => {
// 初始化result为PERMISSION_ENUM
let result = PERMISSION_ENUM
// 如果val存在则将val按'.'拆分为数组
val && val.split('.').forEach(v => {
// 在result中查找对应的值
result = result && result[v] || null
})
// 返回result
return result
}
}
}
})
}
export default plugin