index.ts 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { BtnClassName, BtnList } from '@/common/components/buttonList/interface';
  2. import { ModalName } from '@/common/constants/modalName';
  3. import { OperationTabMenu } from '@/services/go/commonService/interface';
  4. import { sessionStorageUtil } from "@/utils/storage";
  5. import { inject, onUnmounted, Ref, ref } from "vue";
  6. import { MenuType } from "../buttonPermission/interface";
  7. import { openModal } from "../modal";
  8. /**
  9. * 获取class 名
  10. * @param val
  11. * @returns
  12. */
  13. function getClassName(val: string): BtnClassName {
  14. let result: BtnClassName = 'btnDeafault'
  15. if (val.includes('disable') || val.includes('cancle') || val.includes('delete')) {
  16. result = 'btnDanger'
  17. } else if (val === '') {
  18. result = 'btnDeafault'
  19. } else if (val.includes('add')) {
  20. result = 'operBtn'
  21. } else {
  22. result = 'btnPrimary'
  23. }
  24. return result
  25. }
  26. export function getBtnList(menuType: keyof MenuType, hasDetail: boolean) {
  27. // 获取 数据
  28. const permissionData = inject('thirdMenuList') as Ref<OperationTabMenu[]>;
  29. const name = 'permissionData'
  30. // 存入sessionStorageUtil 是为了处理页面刷新的情况(这个时候重新从服务获取数据,但页面已经先加载了,vue 中的 依赖注入在异步中不能建立通信)
  31. const data: OperationTabMenu[] = permissionData.value.length ? permissionData.value : sessionStorageUtil.getItem(name)
  32. sessionStorageUtil.setItem(name, permissionData.value)
  33. const commonBtn = ref<BtnList[]>([]); // 通用按钮列表,不用选中数据才显示
  34. const forDataBtn = ref<BtnList[]>([]); // 针对数据按钮列表,选中某条数据才显示
  35. const list = data.find((e) => e.code === menuType);
  36. if (list && list.children) {
  37. list.children.forEach(e => {
  38. const { code, type, title } = e;
  39. if (type === 2) { // 按钮类型
  40. const { openAction } = openModal(code as keyof ModalName);
  41. const item = { lable: title, callback: openAction, className: getClassName(code) }
  42. const commonName = ['新增'] // 目前通用的按钮只要新增,需要添加其它的时候需要往这里添加
  43. if (commonName.includes(title)) { //
  44. commonBtn.value.push(item)
  45. } else {
  46. forDataBtn.value.push(item)
  47. }
  48. }
  49. })
  50. } else {
  51. console.warn(`menuType: ${menuType}未找到`)
  52. }
  53. // 详情
  54. if (hasDetail) {
  55. const { openAction } = openModal('detail')
  56. forDataBtn.value.push({ lable: '详情', callback: openAction, className: getClassName('') })
  57. }
  58. onUnmounted(() => {
  59. sessionStorageUtil.removeItem(name)
  60. });
  61. return { commonBtn, forDataBtn }
  62. }