useVxeCrudSchemas.ts 8.2 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
  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
  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) {
  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. }
  176. if (schemaItem?.dictType) {
  177. tableSchemaItem.cellRender = {
  178. name: 'XDict',
  179. content: schemaItem.dictType
  180. }
  181. }
  182. tableSchema.push(tableSchemaItem)
  183. }
  184. })
  185. // 操作栏插槽
  186. if (crudSchema.action && crudSchema.action == true) {
  187. const tableSchemaItem = {
  188. title: t('table.action'),
  189. field: 'actionbtns',
  190. width: crudSchema.actionWidth ? crudSchema.actionWidth : '240px',
  191. slots: {
  192. default: 'actionbtns_default'
  193. }
  194. }
  195. tableSchema.push(tableSchemaItem)
  196. }
  197. return tableSchema
  198. }
  199. // 过滤 form 结构
  200. const filterFormSchema = (crudSchema: VxeCrudSchema): FormSchema[] => {
  201. const formSchema: FormSchema[] = []
  202. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  203. // 判断是否显示
  204. if (schemaItem?.isForm !== false) {
  205. let component = schemaItem?.form?.component || 'Input'
  206. const options: ComponentOptions[] = []
  207. let comonentProps = {}
  208. if (schemaItem.dictType) {
  209. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  210. options.push(dict)
  211. })
  212. comonentProps = {
  213. options: options
  214. }
  215. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  216. }
  217. const formSchemaItem = {
  218. // 默认为 input
  219. component: component,
  220. componentProps: comonentProps,
  221. ...schemaItem.form,
  222. field: schemaItem.field,
  223. label: schemaItem.form?.label || schemaItem.title
  224. }
  225. formSchema.push(formSchemaItem)
  226. }
  227. })
  228. return formSchema
  229. }
  230. // 过滤 descriptions 结构
  231. const filterDescriptionsSchema = (crudSchema: VxeCrudSchema): DescriptionsSchema[] => {
  232. const descriptionsSchema: DescriptionsSchema[] = []
  233. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  234. // 判断是否显示
  235. if (schemaItem?.isDetail !== false) {
  236. const descriptionsSchemaItem = {
  237. ...schemaItem.detail,
  238. field: schemaItem.field,
  239. label: schemaItem.detail?.label || schemaItem.title
  240. }
  241. if (schemaItem.dictType) {
  242. descriptionsSchemaItem.dictType = schemaItem.dictType
  243. }
  244. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  245. // descriptionsSchemaItem.dateFormat = schemaItem.detail.dateFormat
  246. // ? schemaItem?.detail?.dateFormat
  247. // : 'YYYY-MM-DD HH:mm:ss'
  248. descriptionsSchemaItem.dateFormat = '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. }