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.

112 lines
3.1 KiB

  1. import { RFormTypes } from '@/types/r-form/model'
  2. import { ProColumns } from '@ant-design/pro-components'
  3. import Switch from '@/components/switch'
  4. import { Checkbox, DatePicker, Input, Radio, Select, TreeSelect } from 'antd'
  5. import request from '@/request'
  6. import { convertToBool } from '@/utils'
  7. import { has, get } from 'lodash'
  8. import { mapTree } from '@/utils/tree.ts'
  9. import { getI18nTitle } from '@/i18n.ts'
  10. const getValueType = (column: ProColumns) => {
  11. return column.valueType
  12. }
  13. //根据type返回对应的组件
  14. const getComponent = (column: ProColumns) => {
  15. const type = getValueType(column) as any
  16. switch (type) {
  17. case 'input':
  18. return Input
  19. case 'select':
  20. return Select
  21. case 'date':
  22. return DatePicker
  23. case 'switch':
  24. return Switch
  25. case 'radio':
  26. return Radio
  27. case 'checkbox':
  28. return Checkbox
  29. case 'textarea':
  30. return Input.TextArea
  31. case 'tree':
  32. case 'treeSelect':
  33. return TreeSelect
  34. default:
  35. return Input
  36. }
  37. }
  38. export const transformAntdTableProColumns = (columns: ProColumns[], overwriteColumns?: ProColumns[], i18n?: string) => {
  39. const overwriteKeys = [] as string[]
  40. return (columns || []).map(item => {
  41. const type = getValueType(item)
  42. const overwrite = overwriteColumns?.find(i => i.dataIndex === item.dataIndex)
  43. if (overwrite) {
  44. overwriteKeys.push(item.dataIndex)
  45. }
  46. return {
  47. ...item,
  48. title: getI18nTitle(i18n!, item),
  49. request: item.request ? async (params) => {
  50. const {
  51. transform = true,
  52. url: _url,
  53. method,
  54. params: p,
  55. fieldNames,
  56. resultPath
  57. } = item.request as unknown as RFormTypes.IRequest
  58. const { value, label, disabled: disabledKey, children = 'children' } = fieldNames || {}
  59. const url = (_url.startsWith('/') || _url.startsWith('http')) ? _url : `/${_url}`
  60. return request[method?.toLowerCase() || 'get'](url, {
  61. ...params,
  62. ...p,
  63. }).then(res => {
  64. try {
  65. const data = resultPath && has(res.data, resultPath) ? get(res.data, resultPath) : res.data
  66. if (!transform) {
  67. return data
  68. }
  69. return mapTree(data || [], (i: any) => {
  70. const disabled = disabledKey && has(i, disabledKey) ? get(i, disabledKey) : ('status' in i ? !convertToBool(i.status) : false)
  71. const title = i.i18n ? getI18nTitle(i.i18n, i.label || i[label || 'name']) : i.label || i[label || 'name']
  72. return {
  73. title,
  74. label: title,
  75. value: i.value ?? i[value || 'id'],
  76. disabled,
  77. data: i
  78. }
  79. }, { children })
  80. } catch (e) {
  81. console.log(`${url}请求异常:`, e)
  82. return []
  83. }
  84. })
  85. } : undefined,
  86. // renderFormItem: (_scheam, config, dom) => {
  87. //
  88. // },
  89. render: (text: any, _record: any) => {
  90. return text
  91. },
  92. ...overwrite
  93. } as ProColumns
  94. }).concat(overwriteColumns?.filter(i => !overwriteKeys.includes(i.dataIndex)) || [])
  95. }