useAuthButtons.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { computed } from "vue";
  2. import { useAuthStore } from "@/stores/modules/auth";
  3. import { useUserStore } from "@/stores/modules/user";
  4. import { Edit, Plus, Menu } from "@element-plus/icons-vue";
  5. import { useRoute, useRouter } from "vue-router";
  6. import { detailAction } from "@/components/LjDetail/interface";
  7. import { isArray, isString } from "@/utils/is";
  8. interface BusinessButtonProps extends detailAction {
  9. /**
  10. * @argument 继承:ElButton的type
  11. */
  12. type?: any;
  13. /**
  14. * @argument 路由地址
  15. */
  16. path?: string;
  17. }
  18. /**
  19. * @description 页面按钮
  20. * @param {Object} t i18n
  21. * */
  22. export const useAuthButtons = (t?: any) => {
  23. // const route = useRoute();
  24. const authStore = useAuthStore();
  25. const authPower: (number | string)[] = authStore.authButtonListGet || [];
  26. const authOption = authStore.authOptionListGet || [];
  27. // console.log("route.name :>> ", route.name);
  28. // const BUTTONS = computed(() => {
  29. // let currentPageAuthButton: { [key: string]: boolean } = {};
  30. // authButtons.forEach(item => (currentPageAuthButton[item] = true));
  31. // return currentPageAuthButton;
  32. // });
  33. /**
  34. * 查询权限值
  35. * @param {number | string} num 权限值 | super
  36. * @returns {boolean} 是否存在
  37. */
  38. const CheckPower = (data: number | string) => {
  39. if (isString(data)) {
  40. const { userInfo } = useUserStore();
  41. return userInfo.usercode?.toLowerCase() === "super";
  42. }
  43. return authPower.includes(data);
  44. };
  45. /**
  46. * 查询系统选项
  47. * @param {string} val 系统选项
  48. * @returns {string}
  49. */
  50. const CheckOption = (val: string | number) => {
  51. let _option = authOption.find((item: any) => Number(item.optionid) === Number(val));
  52. return _option?.optionvalue;
  53. };
  54. /**
  55. * @description 业务按钮:新建
  56. */
  57. const buttonNew = (data: BusinessButtonProps): detailAction => {
  58. const router = useRouter();
  59. return {
  60. ...data,
  61. label: data.label ?? t("common.add"),
  62. icon: data.icon ?? Plus,
  63. // type: data.type ?? "success",
  64. // type: undefined,
  65. // plain: true,
  66. disabledTextCallBack: (item: any) => {
  67. return data.disabledTextCallBack ? data.disabledTextCallBack(item) : "";
  68. },
  69. clickFunc: (item: any) => {
  70. console.log("item :>> ", item);
  71. if (data.clickFunc) {
  72. data.clickFunc(item);
  73. } else {
  74. router.push({
  75. path: data.path ?? "/"
  76. });
  77. }
  78. }
  79. };
  80. };
  81. /**
  82. * @description 业务按钮:初审
  83. */
  84. const buttonDefault = (data: BusinessButtonProps): detailAction => {
  85. return {
  86. ...data,
  87. icon: data.icon ?? undefined
  88. // type: data.type ?? "",
  89. // type: undefined
  90. // plain: true
  91. };
  92. };
  93. /**
  94. * @description 过滤无权限,按钮组
  95. * @param action 筛选合法操作
  96. * @returns array
  97. */
  98. const funcFilterPower = (action: any) => {
  99. console.log("funcFilterPower _action :>> ", action);
  100. if (!action) return [];
  101. let _action = action.map((item: any) => {
  102. if (!Object.keys(item).includes("power")) {
  103. return item;
  104. }
  105. if (isArray(item)) {
  106. return item.filter((i: any) => {
  107. return CheckPower(i.power);
  108. });
  109. }
  110. return CheckPower(item.power) ? item : {};
  111. });
  112. _action = _action.filter((item: any) => {
  113. if (isArray(item)) {
  114. return item.length;
  115. }
  116. return Object.keys(item).length;
  117. });
  118. return _action;
  119. };
  120. /**
  121. * @description 过滤无权限,单据功能,
  122. * @param action 筛选合法操作
  123. * @returns array
  124. */
  125. const funcRightActionPower = (action: any) => {
  126. let rst = action;
  127. if (isArray(rst)) {
  128. rst = rst.map((itm: any) => {
  129. console.log(itm);
  130. if (itm?.groupAction && itm.groupAction.length) {
  131. itm.groupAction = funcRightActionPower(itm.groupAction);
  132. return itm;
  133. } else {
  134. return funcRightActionPower(itm);
  135. }
  136. });
  137. rst = rst.filter((item: any) => {
  138. if (isArray(item)) {
  139. return item.length;
  140. }
  141. return Object.keys(item).length;
  142. });
  143. } else {
  144. if (rst?.groupAction && rst.groupAction.length) {
  145. rst = funcRightActionPower(rst.groupAction);
  146. } else if (Object.keys(rst).includes("power")) {
  147. rst = CheckPower(rst.power) ? rst : {};
  148. }
  149. }
  150. return rst;
  151. };
  152. return {
  153. CheckPower,
  154. CheckOption,
  155. buttonNew,
  156. buttonDefault,
  157. funcFilterPower,
  158. funcRightActionPower
  159. };
  160. };