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.

198 lines
4.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1 year ago
2 years ago
  1. import { IMenu } from '@/types/system/menus'
  2. import { FiledNames, FlattenData, MenuItem } from '@/global'
  3. import { getIcon } from '@/components/icon'
  4. import { TreeDataNode, MenuItemProps } from 'antd'
  5. //vite环境变量, 判断是否是开发环境
  6. export const isDev = import.meta.env.MODE === 'development'
  7. // 格式化菜单数据, 把children转换成routes
  8. export const formatMenuData = (data: IMenu[], parentName: string[]) => {
  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. parentName,
  20. })
  21. } else {
  22. const { children, name, ...other } = item
  23. result.push({
  24. ...other,
  25. key: name,
  26. name: other.title,
  27. children: formatMenuData(children, [ ...parentName, name ]),
  28. routes: formatMenuData(children, [ ...parentName, name ]),
  29. })
  30. }
  31. }
  32. return result
  33. }
  34. //把MenuItem[]转换成antd树形结构
  35. export const formatterMenuData = (data: MenuItem[]): TreeDataNode[] => {
  36. const result: TreeDataNode[] = []
  37. for (const item of data) {
  38. if (item.children && item.children.length) {
  39. const { children, ...other } = item
  40. result.push({
  41. ...other,
  42. key: item.id!,
  43. title: item.name!,
  44. children: formatterMenuData(children),
  45. })
  46. } else {
  47. result.push({
  48. ...item,
  49. key: item.id!,
  50. title: item.name!,
  51. })
  52. }
  53. }
  54. return result
  55. }
  56. //将tree转成Menu结构
  57. export const convertToMenu = (data: any[], format?: (item: any) => any) => {
  58. const result: MenuItemProps[] = []
  59. format = format ?? ((item: any) => item)
  60. for (const item of data) {
  61. if (item.hidden) {
  62. continue
  63. }
  64. const _item = format(item)
  65. if (_item.children && _item.children.length) {
  66. result.push({
  67. ..._item,
  68. children: convertToMenu(_item.children, format),
  69. })
  70. } else {
  71. result.push(_item)
  72. }
  73. }
  74. return result
  75. }
  76. //把tree转成平铺数组
  77. const defaultTreeFieldNames: FiledNames = {
  78. key: 'id',
  79. title: 'title',
  80. children: 'children'
  81. }
  82. export function flattenTree<T>(tree: T[], fieldNames?: FiledNames) {
  83. const result: T[] = []
  84. fieldNames = {
  85. ...defaultTreeFieldNames,
  86. ...fieldNames
  87. }
  88. function flattenRecursive(item: T, level: number, fieldNames: FiledNames) {
  89. const data: FlattenData<T> = {
  90. ...item,
  91. key: item[fieldNames.key!],
  92. title: item[fieldNames.title!],
  93. level,
  94. }
  95. const children = item[fieldNames.children!]
  96. if (children) {
  97. children.forEach((child) => flattenRecursive(child, level + 1, fieldNames))
  98. data.children = children
  99. }
  100. result.push(data as T)
  101. }
  102. tree.forEach((item) => flattenRecursive(item, 0, fieldNames))
  103. return result
  104. }
  105. export const convertToBool = (value: any): boolean => {
  106. // 特殊处理字符串 '0'、'true' 和 'false'
  107. if (typeof value === 'string') {
  108. switch (value.toLowerCase()) {
  109. case '0':
  110. return false
  111. case 'true':
  112. return true
  113. case 'false':
  114. return false
  115. default:
  116. // 对于其他非空字符串,转换为 true
  117. return Boolean(value)
  118. }
  119. }
  120. // 处理常见 falsy 值
  121. if (value === undefined || value === null ||
  122. value === false || value === 0 || value === '' || Number.isNaN(value)) {
  123. return false
  124. }
  125. // 对于对象或数组,我们通常认为非空即为 true
  126. if (Array.isArray(value) || typeof value === 'object') {
  127. return !!Object.keys(value).length
  128. }
  129. // 其他情况,包括数字(非零)、字符串(已经被上述逻辑处理)和其他 truthy 值
  130. return Boolean(value)
  131. }
  132. //数组去重
  133. export const unique = (arr: any[]): any[] => {
  134. return Array.from(new Set(arr))
  135. }
  136. export const getValueCount = (obj: any, filterObj: any = {}) => {
  137. // 获取对象中所有值的数量
  138. let count = 0
  139. for (const key in obj) {
  140. if ([ 'page', 'pageSize', 'pageIndex' ].includes(key)) {
  141. continue
  142. }
  143. if (Object.prototype.hasOwnProperty.call(obj, key) && obj[key]) {
  144. //如果是数组,则必须长度大于0
  145. if (!filterObj?.[key]) {
  146. if (Array.isArray(obj[key]) && obj[key].length === 0) {
  147. continue
  148. }
  149. count++
  150. }
  151. }
  152. }
  153. return count
  154. }
  155. export const unSetColumnRules = (columns: any[]) => {
  156. return columns.map(col => {
  157. if (col.formItemProps?.rules?.length) {
  158. col.formItemProps.rules = []
  159. }
  160. return col
  161. })
  162. }
  163. export const getColumns = (columns: any[], key: string) => {
  164. return columns.find(col => col.key === key)
  165. }
  166. //生成ProTableColumns的宽度相关属性
  167. export const genProTableColumnWidthProps = (width: string | number) => {
  168. return {
  169. width,
  170. fieldProps: {
  171. style: { width: '100%' }
  172. },
  173. }
  174. }