button.ts 4.8 KB

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