useVxeCrudSchemas.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 = {
  17. // 主键ID
  18. primaryKey?: string
  19. primaryType?: VxeColumnPropTypes.Type
  20. // 是否开启操作栏插槽
  21. action?: boolean
  22. columns: VxeCrudColumns[]
  23. }
  24. type VxeCrudColumns = Omit<VxeTableColumn, 'children'> & {
  25. field: string
  26. title?: string
  27. formatter?: VxeColumnPropTypes.Formatter
  28. isSearch?: boolean
  29. search?: VxeFormItemProps
  30. isTable?: boolean
  31. table?: VxeTableDefines.ColumnOptions
  32. isForm?: boolean
  33. form?: FormSchema
  34. isDetail?: boolean
  35. detail?: DescriptionsSchema
  36. print?: CrudPrintParams
  37. children?: VxeCrudColumns[]
  38. dictType?: string
  39. }
  40. type CrudPrintParams = {
  41. // 是否显示表单项
  42. show?: boolean
  43. } & Omit<VxeTableDefines.ColumnInfo[], 'field'>
  44. export type VxeAllSchemas = {
  45. searchSchema: VxeFormItemProps[]
  46. tableSchema: VxeGridPropTypes.Columns
  47. formSchema: FormSchema[]
  48. detailSchema: DescriptionsSchema[]
  49. printSchema: VxeTableDefines.ColumnInfo[]
  50. }
  51. // 过滤所有结构
  52. export const useVxeCrudSchemas = (
  53. crudSchema: VxeCrudSchema
  54. ): {
  55. allSchemas: VxeAllSchemas
  56. } => {
  57. // 所有结构数据
  58. const allSchemas = reactive<VxeAllSchemas>({
  59. searchSchema: [],
  60. tableSchema: [],
  61. formSchema: [],
  62. detailSchema: [],
  63. printSchema: []
  64. })
  65. const searchSchema = filterSearchSchema(crudSchema)
  66. allSchemas.searchSchema = searchSchema || []
  67. const tableSchema = filterTableSchema(crudSchema)
  68. allSchemas.tableSchema = tableSchema || []
  69. const formSchema = filterFormSchema(crudSchema)
  70. allSchemas.formSchema = formSchema
  71. const detailSchema = filterDescriptionsSchema(crudSchema)
  72. allSchemas.detailSchema = detailSchema
  73. const printSchema = filterPrintSchema(crudSchema)
  74. allSchemas.printSchema = printSchema
  75. return {
  76. allSchemas
  77. }
  78. }
  79. // 过滤 Search 结构
  80. const filterSearchSchema = (crudSchema: VxeCrudSchema): VxeFormItemProps[] => {
  81. const { t } = useI18n()
  82. const searchSchema: VxeFormItemProps[] = []
  83. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  84. // 判断是否显示
  85. if (schemaItem?.isSearch) {
  86. let itemRenderName = schemaItem?.search?.itemRender?.name || '$input'
  87. const options: any[] = []
  88. let itemRender: FormItemRenderOptions = {
  89. name: itemRenderName,
  90. props: { placeholder: t('common.inputText') }
  91. }
  92. if (schemaItem.dictType) {
  93. const allOptions = { label: '全部', value: '' }
  94. options.push(allOptions)
  95. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  96. options.push(dict)
  97. })
  98. itemRender.options = options
  99. if (!schemaItem?.search?.itemRender?.name) itemRenderName = '$select'
  100. itemRender = {
  101. name: itemRenderName,
  102. options: options,
  103. props: { placeholder: t('common.selectText') }
  104. }
  105. }
  106. const searchSchemaItem = {
  107. // 默认为 input
  108. folding: searchSchema.length > 2,
  109. itemRender: schemaItem.itemRender ? schemaItem.itemRender : itemRender,
  110. field: schemaItem.field,
  111. title: schemaItem.search?.title || schemaItem.title,
  112. span: 8
  113. }
  114. searchSchema.push(searchSchemaItem)
  115. }
  116. })
  117. // 添加搜索按钮
  118. const buttons: VxeFormItemProps = {
  119. span: 24,
  120. align: 'center',
  121. collapseNode: searchSchema.length > 3,
  122. itemRender: {
  123. name: '$buttons',
  124. children: [
  125. { props: { type: 'submit', content: t('common.query'), status: 'primary' } },
  126. { props: { type: 'reset', content: t('common.reset') } }
  127. ]
  128. }
  129. }
  130. searchSchema.push(buttons)
  131. return searchSchema
  132. }
  133. // 过滤 table 结构
  134. const filterTableSchema = (crudSchema: VxeCrudSchema): VxeGridPropTypes.Columns => {
  135. const { t } = useI18n()
  136. const tableSchema: VxeGridPropTypes.Columns = []
  137. // 主键ID
  138. if (crudSchema.primaryKey) {
  139. const tableSchemaItem = {
  140. title: t('common.index'),
  141. field: crudSchema.primaryKey,
  142. type: crudSchema.primaryType ? crudSchema.primaryType : 'seq',
  143. width: '50px'
  144. }
  145. tableSchema.push(tableSchemaItem)
  146. }
  147. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  148. // 判断是否显示
  149. if (schemaItem?.isTable !== false) {
  150. const tableSchemaItem = {
  151. ...schemaItem.table,
  152. field: schemaItem.field,
  153. title: schemaItem.table?.title || schemaItem.title
  154. }
  155. if (schemaItem?.formatter) {
  156. tableSchemaItem.formatter = schemaItem.formatter
  157. }
  158. if (schemaItem?.dictType) {
  159. tableSchemaItem.cellRender = {
  160. name: 'XDict',
  161. content: schemaItem.dictType
  162. }
  163. }
  164. tableSchema.push(tableSchemaItem)
  165. }
  166. })
  167. // 操作栏插槽
  168. if (crudSchema.action && crudSchema.action == true) {
  169. const tableSchemaItem = {
  170. title: t('table.action'),
  171. field: 'actionbtns',
  172. width: '240px',
  173. slots: {
  174. default: 'actionbtns_default'
  175. }
  176. }
  177. tableSchema.push(tableSchemaItem)
  178. }
  179. return tableSchema
  180. }
  181. // 过滤 form 结构
  182. const filterFormSchema = (crudSchema: VxeCrudSchema): FormSchema[] => {
  183. const formSchema: FormSchema[] = []
  184. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  185. // 判断是否显示
  186. if (schemaItem?.isForm !== false) {
  187. let component = schemaItem?.form?.component || 'Input'
  188. const options: ComponentOptions[] = []
  189. let comonentProps = {}
  190. if (schemaItem.dictType) {
  191. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  192. options.push(dict)
  193. })
  194. comonentProps = {
  195. options: options
  196. }
  197. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  198. }
  199. const formSchemaItem = {
  200. // 默认为 input
  201. component: component,
  202. componentProps: comonentProps,
  203. ...schemaItem.form,
  204. field: schemaItem.field,
  205. label: schemaItem.form?.label || schemaItem.title
  206. }
  207. formSchema.push(formSchemaItem)
  208. }
  209. })
  210. return formSchema
  211. }
  212. // 过滤 descriptions 结构
  213. const filterDescriptionsSchema = (crudSchema: VxeCrudSchema): DescriptionsSchema[] => {
  214. const descriptionsSchema: DescriptionsSchema[] = []
  215. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  216. // 判断是否显示
  217. if (schemaItem?.isDetail !== false) {
  218. const descriptionsSchemaItem = {
  219. ...schemaItem.detail,
  220. field: schemaItem.field,
  221. label: schemaItem.detail?.label || schemaItem.title
  222. }
  223. descriptionsSchema.push(descriptionsSchemaItem)
  224. }
  225. })
  226. return descriptionsSchema
  227. }
  228. // 过滤 打印 结构
  229. const filterPrintSchema = (crudSchema: VxeCrudSchema): any[] => {
  230. const printSchema: any[] = []
  231. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  232. // 判断是否显示
  233. if (schemaItem?.print?.show !== false) {
  234. const printSchemaItem = {
  235. field: schemaItem.field
  236. }
  237. printSchema.push(printSchemaItem)
  238. }
  239. })
  240. return printSchema
  241. }