useVxeCrudSchemas.ts 9.6 KB

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