hasRole.ts 740 B

1234567891011121314151617181920212223242526
  1. import type { App } from "vue";
  2. import { CACHE_KEY, useCache } from "@/hooks/useCache";
  3. export function hasRole(app: App<Element>) {
  4. app.directive("hasRole", (el, binding) => {
  5. const { wsCache } = useCache();
  6. const { value } = binding;
  7. const super_admin = "super_admin";
  8. const userInfo = wsCache.get(CACHE_KEY.USER);
  9. const roles = userInfo?.roles || [];
  10. if (value && value instanceof Array && value.length > 0) {
  11. const roleFlag = value;
  12. const hasRole = roles.some((role: string) => {
  13. return super_admin === role || roleFlag.includes(role);
  14. });
  15. if (!hasRole) {
  16. el.parentNode && el.parentNode.removeChild(el);
  17. }
  18. } else {
  19. throw new Error("");
  20. }
  21. });
  22. }