permission.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
  2. const { t } = useI18n() // 国际化
  3. /**
  4. * 字符权限校验
  5. * @param {Array} value 校验值
  6. * @returns {Boolean}
  7. */
  8. export function checkPermi(value: string[]) {
  9. if (value && value instanceof Array && value.length > 0) {
  10. const { wsCache } = useCache()
  11. const permissionDatas = value
  12. const all_permission = '*:*:*'
  13. const userInfo = wsCache.get(CACHE_KEY.USER)
  14. const permissions = userInfo?.permissions || []
  15. const hasPermission = permissions.some((permission: string) => {
  16. return all_permission === permission || permissionDatas.includes(permission)
  17. })
  18. return !!hasPermission
  19. } else {
  20. console.error(t('permission.hasPermission'))
  21. return false
  22. }
  23. }
  24. /**
  25. * 角色权限校验
  26. * @param {string[]} value 校验值
  27. * @returns {Boolean}
  28. */
  29. export function checkRole(value: string[]) {
  30. if (value && value instanceof Array && value.length > 0) {
  31. const { wsCache } = useCache()
  32. const permissionRoles = value
  33. const super_admin = 'super_admin'
  34. const userInfo = wsCache.get(CACHE_KEY.USER)
  35. const roles = userInfo?.roles || []
  36. const hasRole = roles.some((role: string) => {
  37. return super_admin === role || permissionRoles.includes(role)
  38. })
  39. return !!hasRole
  40. } else {
  41. console.error(t('permission.hasRole'))
  42. return false
  43. }
  44. }