permission.uts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {state} from '@/store'
  2. /**
  3. * 字符权限校验
  4. * @param {Array} value 校验值
  5. * @returns {Boolean}
  6. */
  7. export function checkPermi(value:string[]):boolean {
  8. if (value && value instanceof Array && Array.isArray(value) && value.length > 0) {
  9. const permissions:string[] = state.permissions
  10. const permissionDatas:string[] = value
  11. const all_permission:string = "*:*:*"
  12. const hasPermission:boolean = permissions.some((permission:string) => {
  13. return (all_permission === permission || permissionDatas.includes(permission)) as boolean
  14. }) as boolean
  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:string[]):boolean {
  30. if (value && value instanceof Array && Array.isArray(value) && value.length > 0) {
  31. const roles:string[] = state.roles
  32. const permissionRoles:string[] = value
  33. const super_admin:string = "admin"
  34. const hasRole = roles.some(role => {
  35. return (super_admin === role || permissionRoles.includes(role)) as boolean
  36. }) as boolean
  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. }