vue-pro/ruoyi-ui/src/utils/permission.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2019-10-08 09:14:38 +08:00
import store from '@/store'
/**
2020-02-24 09:28:16 +08:00
* 字符权限校验
* @param {Array} value 校验值
2019-10-08 09:14:38 +08:00
* @returns {Boolean}
*/
2020-02-24 09:28:16 +08:00
export function checkPermi(value) {
if (value && value instanceof Array && value.length > 0) {
const permissions = store.getters && store.getters.permissions
const permissionDatas = value
2020-12-21 19:43:57 +08:00
const all_permission = "*:*:*";
2020-02-24 09:28:16 +08:00
const hasPermission = permissions.some(permission => {
2020-12-21 19:43:57 +08:00
return all_permission === permission || permissionDatas.includes(permission)
2020-02-24 09:28:16 +08:00
})
if (!hasPermission) {
return false
}
return true
} else {
console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
return false
}
}
/**
* 角色权限校验
* @param {Array} value 校验值
* @returns {Boolean}
*/
export function checkRole(value) {
2019-10-08 09:14:38 +08:00
if (value && value instanceof Array && value.length > 0) {
const roles = store.getters && store.getters.roles
const permissionRoles = value
2020-12-21 19:43:57 +08:00
const super_admin = "admin";
2019-10-08 09:14:38 +08:00
2020-02-24 09:28:16 +08:00
const hasRole = roles.some(role => {
2020-12-21 19:43:57 +08:00
return super_admin === role || permissionRoles.includes(role)
2019-10-08 09:14:38 +08:00
})
2020-02-24 09:28:16 +08:00
if (!hasRole) {
2019-10-08 09:14:38 +08:00
return false
}
return true
} else {
2020-02-24 09:28:16 +08:00
console.error(`need roles! Like checkRole="['admin','editor']"`)
2019-10-08 09:14:38 +08:00
return false
}
2020-02-24 09:28:16 +08:00
}