permission.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. console.log(value,value.length,'valuevaluevalue')
  31. if (value && value instanceof Array && value.length > 0) {
  32. const roles = store.getters && store.getters.roles
  33. console.log(roles,'rolesrolesrolesroles')
  34. const permissionRoles = value
  35. const super_admin = "admin"
  36. const hasRole = roles.some(role => {
  37. return super_admin === role || permissionRoles.includes(role)
  38. })
  39. if (!hasRole) {
  40. return false
  41. }
  42. return true
  43. } else {
  44. console.error(`need roles! Like checkRole="['admin','editor']"`)
  45. return false
  46. }
  47. }