permission.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * 按钮级权限控制
  3. */
  4. import store from '@/store';
  5. /* 数组是否有某些值 */
  6. const arrayHas = function (array, value) {
  7. if (!value) {
  8. return true;
  9. }
  10. if (!array) {
  11. return false;
  12. }
  13. if (Array.isArray(value)) {
  14. for (let i = 0; i < value.length; i++) {
  15. if (array.indexOf(value[i]) === -1) {
  16. return false;
  17. }
  18. }
  19. return true;
  20. }
  21. return array.indexOf(value) !== -1;
  22. };
  23. /* 数组是否有任意值 */
  24. const arrayHasAny = function (array, value) {
  25. if (!value) {
  26. return true;
  27. }
  28. if (!array) {
  29. return false;
  30. }
  31. if (Array.isArray(value)) {
  32. for (let i = 0; i < value.length; i++) {
  33. if (array.indexOf(value[i]) !== -1) {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. return array.indexOf(value) !== -1;
  40. };
  41. /**
  42. * 是否有某些角色
  43. * @param value 角色字符或字符数组
  44. */
  45. export function hasRole(value) {
  46. return arrayHas(store.state.user?.roles, value);
  47. }
  48. /**
  49. * 是否有任意角色
  50. * @param value 角色字符或字符数组
  51. */
  52. export function hasAnyRole(value) {
  53. return arrayHasAny(store.state.user?.roles, value);
  54. }
  55. /**
  56. * 是否有某些权限
  57. * @param value 权限字符或字符数组
  58. */
  59. export function hasPermission(value) {
  60. return arrayHas(store.state.user?.authorities, value);
  61. }
  62. /**
  63. * 是否有任意权限
  64. * @param value 权限字符或字符数组
  65. */
  66. export function hasAnyPermission(value) {
  67. return arrayHasAny(store.state.user?.authorities, value);
  68. }
  69. export default {
  70. /**
  71. * 安装Vue插件,扩展Vue实例和指令
  72. *
  73. * @param Vue Vue构造函数
  74. */
  75. install(Vue) {
  76. // 添加全局方法
  77. Vue.prototype.$hasRole = hasRole;
  78. Vue.prototype.$hasAnyRole = hasAnyRole;
  79. Vue.prototype.$hasPermission = hasPermission;
  80. Vue.prototype.$hasAnyPermission = hasAnyPermission;
  81. // 添加自定义指令
  82. Vue.directive('role', {
  83. inserted: (el, binding) => {
  84. if (!hasRole(binding.value)) {
  85. el.parentNode && el.parentNode.removeChild(el);
  86. }
  87. }
  88. });
  89. Vue.directive('any-role', {
  90. inserted: (el, binding) => {
  91. if (!hasAnyRole(binding.value)) {
  92. el.parentNode && el.parentNode.removeChild(el);
  93. }
  94. }
  95. });
  96. Vue.directive('permission', {
  97. inserted: (el, binding) => {
  98. if (!hasPermission(binding.value)) {
  99. el.parentNode && el.parentNode.removeChild(el);
  100. }
  101. }
  102. });
  103. Vue.directive('any-permission', {
  104. inserted: (el, binding) => {
  105. if (!hasAnyPermission(binding.value)) {
  106. el.parentNode && el.parentNode.removeChild(el);
  107. }
  108. }
  109. });
  110. }
  111. };