auth.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import store from '@/store'
  2. function authPermission(permission) {
  3. const all_permission = "*:*:*"
  4. const permissions = store.getters && store.getters.permissions
  5. if (permission && permission.length > 0) {
  6. return permissions.some(v => {
  7. return all_permission === v || v === permission
  8. })
  9. } else {
  10. return false
  11. }
  12. }
  13. function authRole(role) {
  14. const super_admin = "admin"
  15. const roles = store.getters && store.getters.roles
  16. if (role && role.length > 0) {
  17. return roles.some(v => {
  18. return super_admin === v || v === role
  19. })
  20. } else {
  21. return false
  22. }
  23. }
  24. export default {
  25. // 验证用户是否具备某权限
  26. hasPermi(permission) {
  27. return authPermission(permission)
  28. },
  29. // 验证用户是否含有指定权限,只需包含其中一个
  30. hasPermiOr(permissions) {
  31. return permissions.some(item => {
  32. return authPermission(item)
  33. })
  34. },
  35. // 验证用户是否含有指定权限,必须全部拥有
  36. hasPermiAnd(permissions) {
  37. return permissions.every(item => {
  38. return authPermission(item)
  39. })
  40. },
  41. // 验证用户是否具备某角色
  42. hasRole(role) {
  43. return authRole(role)
  44. },
  45. // 验证用户是否含有指定角色,只需包含其中一个
  46. hasRoleOr(roles) {
  47. return roles.some(item => {
  48. return authRole(item)
  49. })
  50. },
  51. // 验证用户是否含有指定角色,必须全部拥有
  52. hasRoleAnd(roles) {
  53. return roles.every(item => {
  54. return authRole(item)
  55. })
  56. }
  57. }