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.

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