Newer
Older
urbanLifeline_YanAn / src / utils / permission.js
@zhangqy zhangqy on 3 Oct 1 KB first commit
  1. import useUserStore from '@/store/modules/user'
  2.  
  3. /**
  4. * 字符权限校验
  5. * @param {Array} value 校验值
  6. * @returns {Boolean}
  7. */
  8. export function checkPermi(value) {
  9. if (value && value instanceof Array && value.length > 0) {
  10. const permissions = useUserStore().permissions
  11. const permissionDatas = value
  12. const all_permission = "*:*:*";
  13.  
  14. const hasPermission = permissions.some(permission => {
  15. return all_permission === permission || permissionDatas.includes(permission)
  16. })
  17.  
  18. if (!hasPermission) {
  19. return false
  20. }
  21. return true
  22. } else {
  23. console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
  24. return false
  25. }
  26. }
  27.  
  28. /**
  29. * 角色权限校验
  30. * @param {Array} value 校验值
  31. * @returns {Boolean}
  32. */
  33. export function checkRole(value) {
  34. if (value && value instanceof Array && value.length > 0) {
  35. const roles = useUserStore().roles
  36. const permissionRoles = value
  37. const super_admin = "admin";
  38.  
  39. const hasRole = roles.some(role => {
  40. return super_admin === role || permissionRoles.includes(role)
  41. })
  42.  
  43. if (!hasRole) {
  44. return false
  45. }
  46. return true
  47. } else {
  48. console.error(`need roles! Like checkRole="['admin','editor']"`)
  49. return false
  50. }
  51. }