permission.uts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. return hasPermission
  16. } else {
  17. console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
  18. return false
  19. }
  20. }
  21. /**
  22. * 角色权限校验
  23. * @param {Array} value 校验值
  24. * @returns {Boolean}
  25. */
  26. export function checkRole(value:string[]):boolean {
  27. if (value && value instanceof Array && Array.isArray(value) && value.length > 0) {
  28. const roles:string[] = state.roles
  29. const permissionRoles:string[] = value
  30. const super_admin:string = "admin"
  31. const hasRole = roles.some(role => {
  32. return (super_admin === role || permissionRoles.includes(role)) as boolean
  33. }) as boolean
  34. return hasRole;
  35. } else {
  36. console.error(`need roles! Like checkRole="['admin','editor']"`)
  37. return false
  38. }
  39. }