authFunction.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { useUserInfo } from '/@/stores/userInfo';
  2. import { judgementSameArr } from '/@/utils/arrayOperation';
  3. import { resolveDirective, withDirectives, VNode } from 'vue';
  4. /**
  5. * 单个权限验证
  6. * @param value 权限值
  7. * @returns 有权限,返回 `true`,反之则反
  8. */
  9. export function auth(value: string): boolean {
  10. const stores = useUserInfo();
  11. return stores.userInfos.authBtnList.some((v: string) => v === value);
  12. }
  13. /**
  14. * 多个权限验证,满足一个则为 true
  15. * @param value 权限值
  16. * @returns 有权限,返回 `true`,反之则反
  17. */
  18. export function auths(value: Array<string>): boolean {
  19. let flag = false;
  20. const stores = useUserInfo();
  21. stores.userInfos.authBtnList.map((val: string) => {
  22. value.map((v: string) => {
  23. if (val === v) flag = true;
  24. });
  25. });
  26. return flag;
  27. }
  28. /**
  29. * 多个权限验证,全部满足则为 true
  30. * @param value 权限值
  31. * @returns 有权限,返回 `true`,反之则反
  32. */
  33. export function authAll(value: Array<string>): boolean {
  34. const stores = useUserInfo();
  35. return judgementSameArr(value, stores.userInfos.authBtnList);
  36. }
  37. /**
  38. * 单个权限验证,是否满足,返回VNode
  39. * @param VNode 元素
  40. * @param value 权限值
  41. * @returns VNode
  42. */
  43. export function hAuth<T extends VNode>(el: T, value: string): T {
  44. return withDirectives(el, [[resolveDirective('auth'), value]]);
  45. }
  46. /**
  47. * 多个权限验证,判断是否满足一个,返回VNode
  48. * @param VNode 元素
  49. * @param value 权限值
  50. * @returns VNode
  51. */
  52. export function hAuths<T extends VNode>(el: T, value: Array<string>): T {
  53. return withDirectives(el, [[resolveDirective('auths'), value]]);
  54. }
  55. /**
  56. * 多个权限验证,判断是否全部满足,返回VNode
  57. * @param VNode 元素
  58. * @param value 权限值
  59. * @returns VNode
  60. */
  61. export function hAuthAll<T extends VNode>(el: T, value: Array<string>): T {
  62. return withDirectives(el, [[resolveDirective('auth-all'), value]]);
  63. }