useCrudSchemas.ts 6.3 KB

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