You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

147 lines
3.8 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import { IMenu } from '@/types/system/menus'
  2. import { FiledNames, FlattenData, MenuItem, TreeItem } from '@/global'
  3. import { getIcon } from '@/components/icon'
  4. import { TreeDataNode } from 'antd'
  5. //vite环境变量, 判断是否是开发环境
  6. export const isDev = import.meta.env.MODE === 'development'
  7. // 格式化菜单数据, 把children转换成routes
  8. export const formatMenuData = (data: IMenu[]) => {
  9. const result: MenuItem[] = []
  10. for (const item of data) {
  11. if (item.icon && typeof item.icon === 'string') {
  12. item.icon = getIcon(item.icon as string, { size: '14', theme: 'filled' })
  13. }
  14. if (!item.children || !item.children.length) {
  15. result.push({
  16. ...item,
  17. key: item.name,
  18. name: item.title
  19. })
  20. } else {
  21. const { children, name, ...other } = item
  22. result.push({
  23. ...other,
  24. key: name,
  25. name: other.title,
  26. children: formatMenuData(children),
  27. routes: formatMenuData(children),
  28. })
  29. }
  30. }
  31. return result
  32. }
  33. //把MenuItem[]转换成antd树形结构
  34. export const formatterMenuData = (data: MenuItem[]): TreeDataNode[] => {
  35. const result: TreeDataNode[] = []
  36. for (const item of data) {
  37. if (item.children && item.children.length) {
  38. const { children, ...other } = item
  39. result.push({
  40. ...other,
  41. key: item.id!,
  42. title: item.name!,
  43. children: formatterMenuData(children),
  44. })
  45. } else {
  46. result.push({
  47. ...item,
  48. key: item.id!,
  49. title: item.name!,
  50. })
  51. }
  52. }
  53. return result
  54. }
  55. //把tree转成平铺数组
  56. const defaultTreeFieldNames: FiledNames = {
  57. key: 'id',
  58. title: 'title',
  59. children: 'children'
  60. }
  61. export function flattenTree<T>(tree: TreeItem<T>[], fieldNames?: FiledNames) {
  62. const result: FlattenData<T>[] = []
  63. if (!fieldNames) {
  64. fieldNames = defaultTreeFieldNames
  65. }
  66. function flattenRecursive(item: TreeItem<T>, level: number, fieldNames: FiledNames) {
  67. const data: FlattenData<T> = {
  68. ...item,
  69. key: item[fieldNames.key!],
  70. title: item[fieldNames.title!],
  71. level,
  72. }
  73. const children = item[fieldNames.children!]
  74. if (children) {
  75. children.forEach((child) => flattenRecursive(child, level + 1, fieldNames))
  76. data.children = children
  77. }
  78. result.push(data)
  79. }
  80. tree.forEach((item) => flattenRecursive(item, 0, fieldNames))
  81. return result
  82. }
  83. export const convertToBool = (value: any): boolean => {
  84. // 特殊处理字符串 '0'、'true' 和 'false'
  85. if (typeof value === 'string') {
  86. switch (value.toLowerCase()) {
  87. case '0':
  88. return false
  89. case 'true':
  90. return true
  91. case 'false':
  92. return false
  93. default:
  94. // 对于其他非空字符串,转换为 true
  95. return Boolean(value)
  96. }
  97. }
  98. // 处理常见 falsy 值
  99. if (value === undefined || value === null ||
  100. value === false || value === 0 || value === '' || Number.isNaN(value)) {
  101. return false
  102. }
  103. // 对于对象或数组,我们通常认为非空即为 true
  104. if (Array.isArray(value) || typeof value === 'object') {
  105. return !!Object.keys(value).length
  106. }
  107. // 其他情况,包括数字(非零)、字符串(已经被上述逻辑处理)和其他 truthy 值
  108. return Boolean(value)
  109. }
  110. //数组去重
  111. export const unique = (arr: any[]): any[] => {
  112. return Array.from(new Set(arr))
  113. }
  114. export const getValueCount = (obj: any, filterObj: any = {}) => {
  115. // 获取对象中所有值的数量
  116. let count = 0
  117. for (const key in obj) {
  118. if (Object.prototype.hasOwnProperty.call(obj, key) && obj[key]) {
  119. //如果是数组,则必须长度大于0
  120. if (!filterObj?.[key]) {
  121. if (Array.isArray(obj[key]) && obj[key].length === 0) {
  122. continue
  123. }
  124. count++
  125. }
  126. }
  127. }
  128. return count
  129. }