useVxeCrudSchemas.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import { DescriptionsSchema } from '@/types/descriptions'
  2. import { getIntDictOptions } from '@/utils/dict'
  3. import { reactive } from 'vue'
  4. import {
  5. FormItemRenderOptions,
  6. VxeColumnPropTypes,
  7. VxeFormItemProps,
  8. VxeGridPropTypes,
  9. VxeTableDefines
  10. } from 'vxe-table'
  11. import { eachTree } from 'xe-utils'
  12. import { useI18n } from '@/hooks/web/useI18n'
  13. import { VxeTableColumn } from '@/types/table'
  14. import { FormSchema } from '@/types/form'
  15. import { ComponentOptions } from '@/types/components'
  16. export type VxeCrudSchema = Omit<VxeTableColumn, 'children'> & {
  17. field: string
  18. title?: string
  19. formatter?: VxeColumnPropTypes.Formatter
  20. search?: CrudSearchParams
  21. table?: CrudTableParams
  22. form?: CrudFormParams
  23. detail?: CrudDescriptionsParams
  24. print?: CrudPrintParams
  25. children?: VxeCrudSchema[]
  26. dictType?: string
  27. }
  28. type CrudSearchParams = {
  29. // 是否显示在查询项
  30. show?: boolean
  31. } & Omit<VxeFormItemProps, 'field'>
  32. type CrudTableParams = {
  33. // 是否显示表头
  34. show?: boolean
  35. } & Omit<VxeTableDefines.ColumnOptions, 'field'>
  36. type CrudFormParams = {
  37. // 是否显示表单项
  38. show?: boolean
  39. } & Omit<FormSchema, 'field'>
  40. type CrudDescriptionsParams = {
  41. // 是否显示表单项
  42. show?: boolean
  43. } & Omit<DescriptionsSchema, 'field'>
  44. type CrudPrintParams = {
  45. // 是否显示表单项
  46. show?: boolean
  47. } & Omit<VxeTableDefines.ColumnInfo[], 'field'>
  48. export type VxeAllSchemas = {
  49. searchSchema: VxeFormItemProps[]
  50. tableSchema: VxeGridPropTypes.Columns
  51. formSchema: FormSchema[]
  52. detailSchema: DescriptionsSchema[]
  53. printSchema: VxeTableDefines.ColumnInfo[]
  54. }
  55. // 过滤所有结构
  56. export const useVxeCrudSchemas = (
  57. crudSchema: VxeCrudSchema[]
  58. ): {
  59. allSchemas: VxeAllSchemas
  60. } => {
  61. // 所有结构数据
  62. const allSchemas = reactive<VxeAllSchemas>({
  63. searchSchema: [],
  64. tableSchema: [],
  65. formSchema: [],
  66. detailSchema: [],
  67. printSchema: []
  68. })
  69. const searchSchema = filterSearchSchema(crudSchema)
  70. allSchemas.searchSchema = searchSchema || []
  71. const tableSchema = filterTableSchema(crudSchema)
  72. allSchemas.tableSchema = tableSchema || []
  73. const formSchema = filterFormSchema(crudSchema)
  74. allSchemas.formSchema = formSchema
  75. const detailSchema = filterDescriptionsSchema(crudSchema)
  76. allSchemas.detailSchema = detailSchema
  77. const printSchema = filterPrintSchema(crudSchema)
  78. allSchemas.printSchema = printSchema
  79. return {
  80. allSchemas
  81. }
  82. }
  83. // 过滤 Search 结构
  84. const filterSearchSchema = (crudSchema: VxeCrudSchema[]): VxeFormItemProps[] => {
  85. const searchSchema: VxeFormItemProps[] = []
  86. const { t } = useI18n()
  87. eachTree(crudSchema, (schemaItem: VxeCrudSchema) => {
  88. // 判断是否显示
  89. if (schemaItem?.search?.show) {
  90. let itemRenderName = schemaItem?.search?.itemRender?.name || '$input'
  91. const options: any[] = []
  92. let itemRender: FormItemRenderOptions = {
  93. name: itemRenderName,
  94. props: { placeholder: t('common.inputText') }
  95. }
  96. if (schemaItem.dictType) {
  97. const allOptions = { label: '全部', value: '' }
  98. options.push(allOptions)
  99. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  100. options.push(dict)
  101. })
  102. itemRender.options = options
  103. if (!schemaItem.search.itemRender?.name) itemRenderName = '$select'
  104. itemRender = {
  105. name: itemRenderName,
  106. options: options,
  107. props: { placeholder: t('common.selectText') }
  108. }
  109. }
  110. const searchSchemaItem = {
  111. // 默认为 input
  112. span: 8,
  113. folding: searchSchema.length > 2,
  114. itemRender: itemRender,
  115. ...schemaItem.search,
  116. field: schemaItem.field,
  117. title: schemaItem.search?.title || schemaItem.title
  118. }
  119. // 删除不必要的字段
  120. delete searchSchemaItem.show
  121. searchSchema.push(searchSchemaItem)
  122. }
  123. })
  124. // 添加搜索按钮
  125. const buttons: VxeFormItemProps = {
  126. span: 24,
  127. align: 'center',
  128. collapseNode: searchSchema.length > 3,
  129. itemRender: {
  130. name: '$buttons',
  131. children: [
  132. { props: { type: 'submit', content: t('common.query'), status: 'primary' } },
  133. { props: { type: 'reset', content: t('common.reset') } }
  134. ]
  135. }
  136. }
  137. searchSchema.push(buttons)
  138. return searchSchema
  139. }
  140. // 过滤 table 结构
  141. const filterTableSchema = (crudSchema: VxeCrudSchema[]): VxeGridPropTypes.Columns => {
  142. const tableSchema: VxeGridPropTypes.Columns = []
  143. eachTree(crudSchema, (schemaItem: VxeCrudSchema) => {
  144. // 判断是否显示
  145. if (schemaItem?.table?.show !== false) {
  146. const tableSchemaItem = {
  147. ...schemaItem.table,
  148. field: schemaItem.field,
  149. title: schemaItem.table?.title || schemaItem.title
  150. }
  151. if (schemaItem?.formatter) {
  152. tableSchemaItem.formatter = schemaItem.formatter
  153. }
  154. // 删除不必要的字段
  155. delete tableSchemaItem.show
  156. tableSchema.push(tableSchemaItem)
  157. }
  158. })
  159. return tableSchema
  160. }
  161. // 过滤 form 结构
  162. const filterFormSchema = (crudSchema: VxeCrudSchema[]): FormSchema[] => {
  163. const formSchema: FormSchema[] = []
  164. eachTree(crudSchema, (schemaItem: VxeCrudSchema) => {
  165. // 判断是否显示
  166. if (schemaItem?.form?.show !== false) {
  167. let component = schemaItem?.form?.component || 'Input'
  168. const options: ComponentOptions[] = []
  169. let comonentProps = {}
  170. if (schemaItem.dictType) {
  171. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  172. options.push(dict)
  173. })
  174. comonentProps = {
  175. options: options
  176. }
  177. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  178. }
  179. const formSchemaItem = {
  180. // 默认为 input
  181. component: component,
  182. componentProps: comonentProps,
  183. ...schemaItem.form,
  184. field: schemaItem.field,
  185. label: schemaItem.form?.label || schemaItem.title
  186. }
  187. // 删除不必要的字段
  188. delete formSchemaItem.show
  189. formSchema.push(formSchemaItem)
  190. }
  191. })
  192. return formSchema
  193. }
  194. // 过滤 descriptions 结构
  195. const filterDescriptionsSchema = (crudSchema: VxeCrudSchema[]): DescriptionsSchema[] => {
  196. const descriptionsSchema: DescriptionsSchema[] = []
  197. eachTree(crudSchema, (schemaItem: VxeCrudSchema) => {
  198. // 判断是否显示
  199. if (schemaItem?.detail?.show !== false) {
  200. const descriptionsSchemaItem = {
  201. ...schemaItem.detail,
  202. field: schemaItem.field,
  203. label: schemaItem.detail?.label || schemaItem.title
  204. }
  205. // 删除不必要的字段
  206. delete descriptionsSchemaItem.show
  207. descriptionsSchema.push(descriptionsSchemaItem)
  208. }
  209. })
  210. return descriptionsSchema
  211. }
  212. // 过滤 打印 结构
  213. const filterPrintSchema = (crudSchema: VxeCrudSchema[]): any[] => {
  214. const printSchema: any[] = []
  215. eachTree(crudSchema, (schemaItem: VxeCrudSchema) => {
  216. // 判断是否显示
  217. if (schemaItem?.print?.show !== false) {
  218. const printSchemaItem = {
  219. field: schemaItem.field
  220. }
  221. printSchema.push(printSchemaItem)
  222. }
  223. })
  224. return printSchema
  225. }