useVxeCrudSchemas.ts 8.5 KB

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