useVxeCrudSchemas.ts 6.7 KB

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