useCrudSchemas.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import { reactive } from 'vue'
  2. import { eachTree, treeMap, filter } from '@/utils/tree'
  3. import { getIntDictOptions } from '@/utils/dict'
  4. import { FormSchema } from '@/types/form'
  5. import { TableColumn } from '@/types/table'
  6. import { DescriptionsSchema } from '@/types/descriptions'
  7. import { ComponentOptions } from '@/types/components'
  8. export type CrudSchema = Omit<TableColumn, 'children'> & {
  9. search?: CrudSearchParams
  10. table?: CrudTableParams
  11. form?: CrudFormParams
  12. detail?: CrudDescriptionsParams
  13. children?: CrudSchema[]
  14. dictType?: string
  15. }
  16. type CrudSearchParams = {
  17. // 是否显示在查询项
  18. show?: boolean
  19. } & Omit<FormSchema, 'field'>
  20. type CrudTableParams = {
  21. // 是否显示表头
  22. show?: boolean
  23. } & Omit<FormSchema, 'field'>
  24. type CrudFormParams = {
  25. // 是否显示表单项
  26. show?: boolean
  27. } & Omit<FormSchema, 'field'>
  28. type CrudDescriptionsParams = {
  29. // 是否显示表单项
  30. show?: boolean
  31. } & Omit<DescriptionsSchema, 'field'>
  32. interface AllSchemas {
  33. searchSchema: FormSchema[]
  34. tableColumns: TableColumn[]
  35. formSchema: FormSchema[]
  36. detailSchema: DescriptionsSchema[]
  37. }
  38. // 过滤所有结构
  39. export const useCrudSchemas = (
  40. crudSchema: CrudSchema[]
  41. ): {
  42. allSchemas: AllSchemas
  43. } => {
  44. // 所有结构数据
  45. const allSchemas = reactive<AllSchemas>({
  46. searchSchema: [],
  47. tableColumns: [],
  48. formSchema: [],
  49. detailSchema: []
  50. })
  51. const searchSchema = filterSearchSchema(crudSchema)
  52. allSchemas.searchSchema = searchSchema || []
  53. const tableColumns = filterTableSchema(crudSchema)
  54. allSchemas.tableColumns = tableColumns || []
  55. const formSchema = filterFormSchema(crudSchema)
  56. allSchemas.formSchema = formSchema
  57. const detailSchema = filterDescriptionsSchema(crudSchema)
  58. allSchemas.detailSchema = detailSchema
  59. return {
  60. allSchemas
  61. }
  62. }
  63. // 过滤 Search 结构
  64. const filterSearchSchema = (crudSchema: CrudSchema[]): FormSchema[] => {
  65. const searchSchema: FormSchema[] = []
  66. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  67. // 判断是否显示
  68. if (schemaItem?.search?.show) {
  69. let component = schemaItem?.search?.component || 'Input'
  70. const options: ComponentOptions[] = []
  71. let comonentProps = {}
  72. if (schemaItem.dictType) {
  73. const allOptions: ComponentOptions = { label: '全部', value: '' }
  74. options.push(allOptions)
  75. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  76. options.push(dict)
  77. })
  78. comonentProps = {
  79. options: options
  80. }
  81. if (!schemaItem.search.component) component = 'Select'
  82. }
  83. const searchSchemaItem = {
  84. // 默认为 input
  85. component: component,
  86. componentProps: comonentProps,
  87. ...schemaItem.search,
  88. field: schemaItem.field,
  89. label: schemaItem.search?.label || schemaItem.label
  90. }
  91. // 删除不必要的字段
  92. delete searchSchemaItem.show
  93. searchSchema.push(searchSchemaItem)
  94. }
  95. })
  96. return searchSchema
  97. }
  98. // 过滤 table 结构
  99. const filterTableSchema = (crudSchema: CrudSchema[]): TableColumn[] => {
  100. const tableColumns = treeMap<CrudSchema>(crudSchema, {
  101. conversion: (schema: CrudSchema) => {
  102. if (schema?.table?.show !== false) {
  103. return {
  104. ...schema.table,
  105. ...schema
  106. }
  107. }
  108. }
  109. })
  110. // 第一次过滤会有 undefined 所以需要二次过滤
  111. return filter<TableColumn>(tableColumns as TableColumn[], (data) => {
  112. if (data.children === void 0) {
  113. delete data.children
  114. }
  115. return !!data.field
  116. })
  117. }
  118. // 过滤 form 结构
  119. const filterFormSchema = (crudSchema: CrudSchema[]): FormSchema[] => {
  120. const formSchema: FormSchema[] = []
  121. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  122. // 判断是否显示
  123. if (schemaItem?.form?.show !== false) {
  124. let component = schemaItem?.form?.component || 'Input'
  125. const options: ComponentOptions[] = []
  126. let comonentProps = {}
  127. if (schemaItem.dictType) {
  128. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  129. options.push(dict)
  130. })
  131. comonentProps = {
  132. options: options
  133. }
  134. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  135. }
  136. const formSchemaItem = {
  137. // 默认为 input
  138. component: component,
  139. componentProps: comonentProps,
  140. ...schemaItem.form,
  141. field: schemaItem.field,
  142. label: schemaItem.form?.label || schemaItem.label
  143. }
  144. // 删除不必要的字段
  145. delete formSchemaItem.show
  146. formSchema.push(formSchemaItem)
  147. }
  148. })
  149. return formSchema
  150. }
  151. // 过滤 descriptions 结构
  152. const filterDescriptionsSchema = (crudSchema: CrudSchema[]): DescriptionsSchema[] => {
  153. const descriptionsSchema: FormSchema[] = []
  154. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  155. // 判断是否显示
  156. if (schemaItem?.detail?.show !== false) {
  157. const descriptionsSchemaItem = {
  158. ...schemaItem.detail,
  159. field: schemaItem.field,
  160. label: schemaItem.detail?.label || schemaItem.label
  161. }
  162. if (schemaItem.dictType) {
  163. descriptionsSchemaItem.dictType = schemaItem.dictType
  164. }
  165. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  166. descriptionsSchemaItem.dateFormat = schemaItem.dateFormat
  167. ? schemaItem?.detail?.dateFormat
  168. : 'YYYY-MM-DD HH:mm:ss'
  169. }
  170. // 删除不必要的字段
  171. delete descriptionsSchemaItem.show
  172. descriptionsSchema.push(descriptionsSchemaItem)
  173. }
  174. })
  175. return descriptionsSchema
  176. }