auth.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. }