permission.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import store from '@/store'
  2. /**
  3. * 字符权限校验
  4. * @param {Array} value 校验值
  5. * @returns {Boolean}
  6. */
  7. export function checkPermi(value) {
  8. if (value && value instanceof Array && value.length > 0) {
  9. const permissions = store.getters && store.getters.permissions
  10. const permissionDatas = value
  11. const all_permission = "*:*:*"
  12. const hasPermission = permissions.some(permission => {
  13. return all_permission === permission || permissionDatas.includes(permission)
  14. })
  15. if (!hasPermission) {
  16. return false
  17. }
  18. return true
  19. } else {
  20. console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
  21. return false
  22. }
  23. }
  24. /**
  25. * 角色权限校验
  26. * @param {Array} value 校验值
  27. * @returns {Boolean}
  28. */
  29. export function checkRole(value) {
  30. if (value && value instanceof Array && value.length > 0) {
  31. const roles = store.getters && store.getters.roles
  32. const permissionRoles = value
  33. const super_admin = "admin"
  34. const hasRole = roles.some(role => {
  35. return super_admin === role || permissionRoles.includes(role)
  36. })
  37. if (!hasRole) {
  38. return false
  39. }
  40. return true
  41. } else {
  42. console.error(`need roles! Like checkRole="['admin','editor']"`)
  43. return false
  44. }
  45. }