button.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { ModalName } from '@/common/constants/modalName';
  2. import { OperationTabMenu } from '@/services/go/commonService/interface';
  3. import { sessionStorageUtil } from "@/utils/storage";
  4. import { inject, ref, Ref, toRaw, unref } from 'vue';
  5. import { openModal } from "../modal";
  6. import { BtnClassName, BtnList, ButtonListKey } from './interface';
  7. /**
  8. * 获取class 名
  9. * @param val
  10. * @returns
  11. */
  12. function getClassName(val: string): BtnClassName {
  13. let result: BtnClassName = 'btnDeafault'
  14. const btnDanger = ['disable', 'cancle', 'cancel', 'delete', 'logout', 'locked']
  15. const operBtn = ['add', 'modify', 'reset', 'credit']
  16. const map = new Map<BtnClassName, string[]>([
  17. ['btnDanger', btnDanger],
  18. ['operBtn', operBtn],
  19. ])
  20. for (const [key, value] of map) {
  21. for (const item of value) {
  22. if (val.includes(item)) {
  23. result = key
  24. break;
  25. }
  26. }
  27. }
  28. return result
  29. }
  30. export function getThirdMenuData(): OperationTabMenu[] {
  31. const permissionData = inject('thirdMenuList') as Ref<OperationTabMenu[]>;
  32. const temp = unref(permissionData)
  33. const name = 'permissionData'
  34. // 存入sessionStorageUtil 是为了处理页面刷新的情况(这个时候重新从服务获取数据,但页面已经先加载了,vue 中的 依赖注入在异步中不能建立通信)
  35. const data: OperationTabMenu[] = temp.length ? toRaw(temp) : sessionStorageUtil.getItem(name)
  36. sessionStorageUtil.setItem(name, data)
  37. return data;
  38. }
  39. export function handleBtnList(list: OperationTabMenu | undefined, menuType: keyof ButtonListKey, hasDetail: boolean, commonName: string[] = ['新增']) {
  40. const commonBtn = ref<BtnList[]>([]); // 通用按钮列表,不用选中数据才显示
  41. const forDataBtn = ref<BtnList[]>([]); // 针对数据按钮列表,选中某条数据才显示
  42. if (list && list.children) {
  43. list.children.forEach(e => {
  44. const { code, type, title, isshow } = e;
  45. if (type === 2 && isshow) { // 按钮类型
  46. const { openAction } = openModal(code as keyof ModalName);
  47. const item = { lable: title, callback: openAction, className: getClassName(code) }
  48. if (commonName.includes(title)) { //
  49. commonBtn.value.push(item)
  50. } else {
  51. forDataBtn.value.push(item)
  52. }
  53. }
  54. })
  55. } else {
  56. console.warn(`menuType: ${menuType}未找到`)
  57. }
  58. // 详情
  59. if (hasDetail) {
  60. const { openAction } = openModal('detail')
  61. forDataBtn.value.push({ lable: '详情', callback: openAction, className: getClassName('') })
  62. }
  63. return { commonBtn, forDataBtn }
  64. }
  65. export function _handleBtnList(list: OperationTabMenu | undefined, hasDetail: boolean,) {
  66. const result = ref<BtnList[][]>([])
  67. const temp: [number, OperationTabMenu[] | undefined] = [0, list?.children]
  68. while (temp[1] && temp[1].length) {
  69. temp[1].forEach((e) => {
  70. const { code, type, title, isshow } = e;
  71. const index = temp[0]
  72. if (!Array.isArray(result.value[index])) {
  73. result.value[index] = []
  74. }
  75. if (type === 2 && isshow) { // 按钮类型 并且显示
  76. const { openAction } = openModal(code as keyof ModalName);
  77. const item = { lable: title, callback: openAction, className: getClassName(code) }
  78. result.value[index].push(item)
  79. }
  80. })
  81. const children = temp[1][0].children
  82. if (children && children.length) {
  83. temp[0] = temp[0] + 1
  84. temp[1] = children
  85. } else {
  86. temp[1] = []
  87. }
  88. }
  89. // 详情
  90. if (hasDetail) {
  91. const len = result.value.length;
  92. const { openAction } = openModal('detail')
  93. result.value[len - 1].push({ lable: '详情', callback: openAction, className: getClassName('') })
  94. }
  95. return result
  96. }
  97. /**
  98. * 获取表格操作按钮列表
  99. * @param menuType
  100. * @param hasDetail 操作按钮是否需要详情按钮(详情按钮服务 不配置)
  101. * @returns
  102. */
  103. export function getBtnList(menuType: keyof ButtonListKey, hasDetail: boolean, commonName: string[] = ['新增']) {
  104. const data = getThirdMenuData()
  105. const list = data.find((e) => e.code === menuType);
  106. return handleBtnList(list, menuType, hasDetail, commonName)
  107. }
  108. export function _getBtnList(menuType: keyof ButtonListKey, hasDetail: boolean) {
  109. const data = getThirdMenuData()
  110. const list = data.find((e) => e.code === menuType);
  111. return _handleBtnList(list, hasDetail)
  112. }