button.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import { BtnClassName, BtnListType } from '@/common/components/btnList/interface';
  2. import { EnumRouterName } from '@/common/constants/enumRouterName';
  3. import { ModalName } from '@/common/constants/modalName';
  4. import { OperationTabMenu } from '@/services/go/commonService/interface';
  5. import { sessionStorageUtil } from "@/utils/storage";
  6. import { inject, ref, Ref, toRaw, unref } from 'vue';
  7. import { openModal } from "../modal";
  8. import { ButtonListKey } from './interface';
  9. /**
  10. * 获取class 名
  11. * @param val
  12. * @returns
  13. */
  14. export function getClassName(val: string): BtnClassName {
  15. let result: BtnClassName = 'btnDeafault'
  16. const btnDanger = ['disable', 'cancle', 'cancel', 'delete', 'logout', 'locked', 'refuse']
  17. const operBtn = ['add', 'modify', 'reset', 'credit', 'payment', 'confirm_withdrawal', 'complete_stocking', 'upload_logistics', 'buy', 'listed', 'delisting',
  18. 'receipt', 'confirm_pickup']
  19. const map = new Map<BtnClassName, string[]>([
  20. ['btnDanger', btnDanger],
  21. ['operBtn', operBtn],
  22. ])
  23. for (const [key, value] of map) {
  24. for (const item of value) {
  25. if (val.includes(item)) {
  26. result = key
  27. break;
  28. }
  29. }
  30. }
  31. return result
  32. }
  33. export function getThirdMenuData(): OperationTabMenu[] {
  34. const permissionData = inject('thirdMenuList') as Ref<OperationTabMenu[]>;
  35. const temp = unref(permissionData)
  36. const name = 'permissionData'
  37. // 存入sessionStorageUtil 是为了处理页面刷新的情况(这个时候重新从服务获取数据,但页面已经先加载了,vue 中的 依赖注入在异步中不能建立通信)
  38. const data: OperationTabMenu[] = temp.length ? toRaw(temp) : sessionStorageUtil.getItem(name)
  39. sessionStorageUtil.setItem(name, data)
  40. console.log('当前页面配置的数据: ', data)
  41. return data;
  42. }
  43. // 旧的获取配置按钮逻辑,
  44. export function handleBtnList(list: OperationTabMenu | undefined, menuType: keyof ButtonListKey, hasDetail: boolean, commonName: string[] = ['新增']) {
  45. const commonBtn = ref<BtnListType[]>([]); // 通用按钮列表,不用选中数据才显示
  46. const forDataBtn = ref<BtnListType[]>([]); // 针对数据按钮列表,选中某条数据才显示
  47. if (list && list.children) {
  48. list.children.forEach(e => {
  49. const { code, type, title, isshow } = e;
  50. if (type === 2 && isshow) { // 按钮类型
  51. const { openAction } = openModal(code as keyof ModalName);
  52. const item = { lable: title, callback: openAction, className: getClassName(code), code: code }
  53. if (commonName.includes(title)) { //
  54. commonBtn.value.push(item)
  55. } else {
  56. forDataBtn.value.push(item)
  57. }
  58. }
  59. })
  60. } else {
  61. console.warn(`menuType: ${menuType}未找到`)
  62. }
  63. // 详情
  64. if (hasDetail) {
  65. const { openAction } = openModal('detail')
  66. forDataBtn.value.push({ lable: '详情', callback: openAction, className: getClassName(''), code: 'detail' })
  67. }
  68. return { commonBtn, forDataBtn }
  69. }
  70. // 新的获取按钮逻辑,有时间把旧的都替换掉
  71. export function _handleBtnList_(list: OperationTabMenu | undefined, hasDetail: boolean,) {
  72. const result = ref<BtnListType[][]>([])
  73. const temp: [number, OperationTabMenu[] | undefined] = [0, list?.children]
  74. while (temp[1] && temp[1].length) {
  75. temp[1].forEach((e) => {
  76. const { code, type, title, isshow } = e;
  77. const index = temp[0]
  78. if (!Array.isArray(result.value[index])) {
  79. result.value[index] = []
  80. }
  81. if (type === 2 && isshow && code !== 'none_btn') {
  82. // 按钮类型 并且显示
  83. // 当code为 none_btn, 是空按钮,(返回按钮确保数据结构统一)
  84. const item = { lable: title, code, className: getClassName(code) }
  85. result.value[index].push(item)
  86. }
  87. })
  88. const children = temp[1][0].children
  89. if (children && children.length) {
  90. temp[0] = temp[0] + 1
  91. temp[1] = children
  92. } else {
  93. temp[1] = []
  94. }
  95. }
  96. // 详情
  97. if (hasDetail) {
  98. const { openAction } = openModal('detail')
  99. const item = { lable: '详情', code: 'detail', className: getClassName('') }
  100. const len = result.value.length;
  101. if (len) {
  102. result.value[len - 1].push(item)
  103. } else {
  104. result.value = [[], [item]]
  105. }
  106. }
  107. if (result.value.length === 0) {
  108. result.value = [[], []]
  109. }
  110. console.log('按钮列表数据', result);
  111. return result
  112. }
  113. export function _handleBtnList(list: OperationTabMenu | undefined, hasDetail: boolean,) {
  114. const result = ref<BtnListType[][]>([])
  115. const temp: [number, OperationTabMenu[] | undefined] = [0, list?.children]
  116. while (temp[1] && temp[1].length) {
  117. temp[1].forEach((e) => {
  118. const { code, type, title, isshow } = e;
  119. const index = temp[0]
  120. if (!Array.isArray(result.value[index])) {
  121. result.value[index] = []
  122. }
  123. if (type === 2 && isshow) { // 按钮类型 并且显示
  124. const { openAction } = openModal(code as keyof ModalName);
  125. const item = { lable: title, callback: openAction, className: getClassName(code), code: code }
  126. result.value[index].push(item)
  127. }
  128. })
  129. const children = temp[1][0].children
  130. if (children && children.length) {
  131. temp[0] = temp[0] + 1
  132. temp[1] = children
  133. } else {
  134. temp[1] = []
  135. }
  136. }
  137. // 详情
  138. if (hasDetail) {
  139. const { openAction } = openModal('detail')
  140. const item = { lable: '详情', callback: openAction, className: getClassName(''), code: 'detail' }
  141. const len = result.value.length;
  142. if (len) {
  143. result.value[len - 1].push(item)
  144. } else {
  145. result.value = [[], [item]]
  146. }
  147. }
  148. return result
  149. }
  150. // 获取单据按钮列表
  151. export function getOrderBtnList(list: OperationTabMenu[], hasDetail = false) {
  152. const result = ref<BtnListType[]>([])
  153. list.forEach(el => {
  154. const { code, type, title, isshow } = el;
  155. if (type === 2 && isshow) {
  156. const item = { lable: title, code, className: getClassName(code) }
  157. result.value.push(item)
  158. }
  159. });
  160. // 详情
  161. if (hasDetail) {
  162. const item = { lable: '详情', code: 'detail', className: getClassName('') }
  163. result.value.push(item)
  164. }
  165. console.log('单据按钮列表', result);
  166. return result
  167. }
  168. /**
  169. * 获取表格操作按钮列表
  170. * @param menuType
  171. * @param hasDetail 操作按钮是否需要详情按钮(详情按钮服务 不配置)
  172. * @returns
  173. */
  174. export function getBtnList(menuType: keyof ButtonListKey, hasDetail: boolean, commonName: string[] = ['新增']) {
  175. const data = getThirdMenuData()
  176. const list = data.find((e) => e.code === menuType);
  177. return handleBtnList(list, menuType, hasDetail, commonName)
  178. }
  179. export function _getBtnList(menuType: keyof ButtonListKey, hasDetail: boolean): Ref<BtnListType[][]> {
  180. const data = getThirdMenuData()
  181. const list = data.find((e) => e.code === menuType);
  182. return _handleBtnList(list, hasDetail)
  183. }
  184. /**
  185. * 根据当前tab页面的code 枚举查找对应的数据
  186. * @param menuType
  187. * @returns
  188. */
  189. export function findChildList(menuType: EnumRouterName): OperationTabMenu | undefined {
  190. const data = getThirdMenuData()
  191. console.log('data', data)
  192. let list: OperationTabMenu | undefined = undefined
  193. // 这里处理有两层路由菜单
  194. function findFn(arr: OperationTabMenu[]) {
  195. for (let item of arr) {
  196. if (item.code === menuType) {
  197. list = item
  198. return
  199. } else {
  200. findFn(item.children)
  201. }
  202. }
  203. return null
  204. }
  205. findFn(data)
  206. return list
  207. }
  208. // 处理有三级路由的特殊情况,例如仓单贸易
  209. export function getBtnList_(menuType: EnumRouterName, hasDetail: boolean) {
  210. const list = findChildList(menuType)
  211. return _handleBtnList_(list, hasDetail)
  212. }
  213. /****************************** ======================================= *************************/
  214. /**
  215. * 往后统一 用此方法,注意,配置 菜单json数据的时候,按钮列表统一配置为一级列表,不再配置为树结果
  216. */
  217. export function getButtonList(menuType: EnumRouterName, hasDetail: boolean) {
  218. const list = findChildList(menuType)
  219. const result = [] // 操作按钮列表
  220. if (list && list.children) {
  221. list.children.forEach(el => {
  222. const { code, type, title, isshow } = el;
  223. if (isshow) {
  224. if (type === 2) {
  225. const item = { lable: title, className: getClassName(code), code: code }
  226. result.push(item)
  227. }
  228. }
  229. })
  230. }
  231. // 详情
  232. if (hasDetail) {
  233. const item = { lable: '详情', code: 'detail', className: getClassName('') }
  234. result.push(item)
  235. }
  236. return result
  237. }