useCrudSchemas.ts 6.7 KB

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