useVxeCrudSchemas.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import {
  2. FormItemRenderOptions,
  3. VxeColumnPropTypes,
  4. VxeFormItemProps,
  5. VxeGridPropTypes,
  6. VxeTableDefines
  7. } from 'vxe-table'
  8. import { eachTree } from 'xe-utils'
  9. import { useI18n } from '@/hooks/web/useI18n'
  10. import { getBoolDictOptions, getDictOptions, getIntDictOptions } from '@/utils/dict'
  11. import { FormSchema } from '@/types/form'
  12. import { VxeTableColumn } from '@/types/table'
  13. import { ComponentOptions } from '@/types/components'
  14. import { DescriptionsSchema } from '@/types/descriptions'
  15. export type VxeCrudSchema = {
  16. primaryKey?: string // 主键ID
  17. primaryTitle?: string // 主键标题 默认为序号
  18. primaryType?: VxeColumnPropTypes.Type | 'id' // 还支持 "id" | "seq" | "radio" | "checkbox" | "expand" | "html" | null
  19. firstColumn?: VxeColumnPropTypes.Type // 第一列显示类型
  20. action?: boolean // 是否开启表格内右侧操作栏插槽
  21. actionTitle?: string // 操作栏标题 默认为操作
  22. actionWidth?: string // 操作栏插槽宽度,一般2个字带图标 text 类型按钮 50-70
  23. columns: VxeCrudColumns[]
  24. searchSpan?: number
  25. }
  26. type VxeCrudColumns = Omit<VxeTableColumn, 'children'> & {
  27. field: string // 字段名
  28. title?: string // 标题名
  29. formatter?: VxeColumnPropTypes.Formatter // vxe formatter格式化
  30. isSearch?: boolean // 是否在查询显示
  31. search?: CrudSearchParams // 查询的详细配置
  32. isTable?: boolean // 是否在列表显示
  33. table?: CrudTableParams // 列表的详细配置
  34. isForm?: boolean // 是否在表单显示
  35. form?: CrudFormParams // 表单的详细配置
  36. isDetail?: boolean // 是否在详情显示
  37. detail?: CrudDescriptionsParams // 详情的详细配置
  38. print?: CrudPrintParams // vxe 打印的字段
  39. children?: VxeCrudColumns[] // 子级
  40. dictType?: string // 字典类型
  41. dictClass?: 'string' | 'number' | 'boolean' // 字典数据类型 string | number | boolean
  42. }
  43. type CrudSearchParams = {
  44. // 是否显示在查询项
  45. show?: boolean
  46. } & Omit<VxeFormItemProps, 'field'>
  47. type CrudTableParams = {
  48. // 是否显示表头
  49. show?: boolean
  50. } & Omit<VxeTableDefines.ColumnOptions, 'field'>
  51. type CrudFormParams = {
  52. // 是否显示表单项
  53. show?: boolean
  54. } & Omit<FormSchema, 'field'>
  55. type CrudDescriptionsParams = {
  56. // 是否显示表单项
  57. show?: boolean
  58. } & Omit<DescriptionsSchema, 'field'>
  59. type CrudPrintParams = {
  60. // 是否显示打印项
  61. show?: boolean
  62. } & Omit<VxeTableDefines.ColumnInfo[], 'field'>
  63. export type VxeAllSchemas = {
  64. searchSchema: VxeFormItemProps[]
  65. tableSchema: VxeGridPropTypes.Columns
  66. formSchema: FormSchema[]
  67. detailSchema: DescriptionsSchema[]
  68. printSchema: VxeTableDefines.ColumnInfo[]
  69. }
  70. // 过滤所有结构
  71. export const useVxeCrudSchemas = (
  72. crudSchema: VxeCrudSchema
  73. ): {
  74. allSchemas: VxeAllSchemas
  75. } => {
  76. // 所有结构数据
  77. const allSchemas = reactive<VxeAllSchemas>({
  78. searchSchema: [],
  79. tableSchema: [],
  80. formSchema: [],
  81. detailSchema: [],
  82. printSchema: []
  83. })
  84. const searchSchema = filterSearchSchema(crudSchema)
  85. allSchemas.searchSchema = searchSchema || []
  86. const tableSchema = filterTableSchema(crudSchema)
  87. allSchemas.tableSchema = tableSchema || []
  88. const formSchema = filterFormSchema(crudSchema)
  89. allSchemas.formSchema = formSchema
  90. const detailSchema = filterDescriptionsSchema(crudSchema)
  91. allSchemas.detailSchema = detailSchema
  92. const printSchema = filterPrintSchema(crudSchema)
  93. allSchemas.printSchema = printSchema
  94. return {
  95. allSchemas
  96. }
  97. }
  98. // 过滤 Search 结构
  99. const filterSearchSchema = (crudSchema: VxeCrudSchema): VxeFormItemProps[] => {
  100. const { t } = useI18n()
  101. const span = crudSchema.searchSpan ? crudSchema.searchSpan : 6
  102. const spanLength = 24 / span
  103. const searchSchema: VxeFormItemProps[] = []
  104. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  105. // 判断是否显示
  106. if (schemaItem?.isSearch || schemaItem.search?.show) {
  107. let itemRenderName = schemaItem?.search?.itemRender?.name || '$input'
  108. const options: any[] = []
  109. let itemRender: FormItemRenderOptions = {}
  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. } else {
  124. if (schemaItem.search?.itemRender) {
  125. itemRender = schemaItem.search.itemRender
  126. } else {
  127. itemRender = {
  128. name: itemRenderName,
  129. props:
  130. itemRenderName == '$input'
  131. ? { placeholder: t('common.inputText') }
  132. : { placeholder: t('common.selectText') }
  133. }
  134. }
  135. }
  136. const searchSchemaItem = {
  137. // 默认为 input
  138. folding: searchSchema.length > spanLength - 1,
  139. itemRender: schemaItem.itemRender ? schemaItem.itemRender : itemRender,
  140. field: schemaItem.field,
  141. title: schemaItem.search?.title || schemaItem.title,
  142. slots: schemaItem.search?.slots,
  143. span: span
  144. }
  145. searchSchema.push(searchSchemaItem)
  146. }
  147. })
  148. if (searchSchema.length > 0) {
  149. // 添加搜索按钮
  150. const buttons: VxeFormItemProps = {
  151. span: 24,
  152. align: 'right',
  153. collapseNode: searchSchema.length > spanLength,
  154. itemRender: {
  155. name: '$buttons',
  156. children: [
  157. { props: { type: 'submit', content: t('common.query'), status: 'primary' } },
  158. { props: { type: 'reset', content: t('common.reset') } }
  159. ]
  160. }
  161. }
  162. searchSchema.push(buttons)
  163. }
  164. return searchSchema
  165. }
  166. // 过滤 table 结构
  167. const filterTableSchema = (crudSchema: VxeCrudSchema): VxeGridPropTypes.Columns => {
  168. const { t } = useI18n()
  169. const tableSchema: VxeGridPropTypes.Columns = []
  170. // 第一列
  171. if (crudSchema.firstColumn) {
  172. const tableSchemaItem = {
  173. type: crudSchema.firstColumn,
  174. width: '50px'
  175. }
  176. tableSchema.push(tableSchemaItem)
  177. }
  178. // 主键ID
  179. if (crudSchema.primaryKey && crudSchema.primaryType) {
  180. const primaryTitle = crudSchema.primaryTitle ? crudSchema.primaryTitle : t('common.index')
  181. const primaryWidth = primaryTitle.length * 30 + 'px'
  182. let tableSchemaItem: { [x: string]: any } = {
  183. title: primaryTitle,
  184. field: crudSchema.primaryKey,
  185. width: primaryWidth
  186. }
  187. if (crudSchema.primaryType != 'id') {
  188. tableSchemaItem = {
  189. ...tableSchemaItem,
  190. type: crudSchema.primaryType
  191. }
  192. }
  193. tableSchema.push(tableSchemaItem)
  194. }
  195. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  196. // 判断是否显示
  197. if (schemaItem?.isTable !== false && schemaItem?.table?.show !== false) {
  198. const tableSchemaItem = {
  199. ...schemaItem.table,
  200. field: schemaItem.field,
  201. title: schemaItem.table?.title || schemaItem.title,
  202. minWidth: '80px'
  203. }
  204. tableSchemaItem.showOverflow = 'tooltip'
  205. if (schemaItem?.formatter) {
  206. tableSchemaItem.formatter = schemaItem.formatter
  207. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  208. }
  209. if (schemaItem?.dictType) {
  210. tableSchemaItem.cellRender = {
  211. name: 'XDict',
  212. content: schemaItem.dictType
  213. }
  214. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  215. }
  216. tableSchema.push(tableSchemaItem)
  217. }
  218. })
  219. // 操作栏插槽
  220. if (crudSchema.action && crudSchema.action == true) {
  221. const tableSchemaItem = {
  222. title: crudSchema.actionTitle ? crudSchema.actionTitle : t('table.action'),
  223. field: 'actionbtns',
  224. fixed: 'right' as unknown as VxeColumnPropTypes.Fixed,
  225. width: crudSchema.actionWidth ? crudSchema.actionWidth : '200px',
  226. slots: {
  227. default: 'actionbtns_default'
  228. }
  229. }
  230. tableSchema.push(tableSchemaItem)
  231. }
  232. return tableSchema
  233. }
  234. // 过滤 form 结构
  235. const filterFormSchema = (crudSchema: VxeCrudSchema): FormSchema[] => {
  236. const formSchema: FormSchema[] = []
  237. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  238. // 判断是否显示
  239. if (schemaItem?.isForm !== false && schemaItem?.form?.show !== false) {
  240. // 默认为 input
  241. let component = schemaItem?.form?.component || 'Input'
  242. let defaultValue: any = ''
  243. if (schemaItem.form?.value) {
  244. defaultValue = schemaItem.form?.value
  245. } else {
  246. if (component === 'InputNumber') {
  247. defaultValue = 0
  248. }
  249. }
  250. let comonentProps = {}
  251. if (schemaItem.dictType) {
  252. const options: ComponentOptions[] = []
  253. if (schemaItem.dictClass && schemaItem.dictClass === 'number') {
  254. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  255. options.push(dict)
  256. })
  257. } else if (schemaItem.dictClass && schemaItem.dictClass === 'boolean') {
  258. getBoolDictOptions(schemaItem.dictType).forEach((dict) => {
  259. options.push(dict)
  260. })
  261. } else {
  262. getDictOptions(schemaItem.dictType).forEach((dict) => {
  263. options.push(dict)
  264. })
  265. }
  266. comonentProps = {
  267. options: options
  268. }
  269. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  270. }
  271. const formSchemaItem = {
  272. component: component,
  273. componentProps: comonentProps,
  274. value: defaultValue,
  275. ...schemaItem.form,
  276. field: schemaItem.field,
  277. label: schemaItem.form?.label || schemaItem.title
  278. }
  279. formSchema.push(formSchemaItem)
  280. }
  281. })
  282. return formSchema
  283. }
  284. // 过滤 descriptions 结构
  285. const filterDescriptionsSchema = (crudSchema: VxeCrudSchema): DescriptionsSchema[] => {
  286. const descriptionsSchema: DescriptionsSchema[] = []
  287. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  288. // 判断是否显示
  289. if (schemaItem?.isDetail !== false && schemaItem.detail?.show !== false) {
  290. const descriptionsSchemaItem = {
  291. ...schemaItem.detail,
  292. field: schemaItem.field,
  293. label: schemaItem.detail?.label || schemaItem.title
  294. }
  295. if (schemaItem.dictType) {
  296. descriptionsSchemaItem.dictType = schemaItem.dictType
  297. }
  298. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  299. // 优先使用 detail 下的配置,如果没有默认为 YYYY-MM-DD HH:mm:ss
  300. descriptionsSchemaItem.dateFormat = schemaItem?.detail?.dateFormat
  301. ? schemaItem?.detail?.dateFormat
  302. : 'YYYY-MM-DD HH:mm:ss'
  303. }
  304. descriptionsSchema.push(descriptionsSchemaItem)
  305. }
  306. })
  307. return descriptionsSchema
  308. }
  309. // 过滤 打印 结构
  310. const filterPrintSchema = (crudSchema: VxeCrudSchema): any[] => {
  311. const printSchema: any[] = []
  312. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  313. // 判断是否显示
  314. if (schemaItem?.print?.show !== false) {
  315. const printSchemaItem = {
  316. field: schemaItem.field
  317. }
  318. printSchema.push(printSchemaItem)
  319. }
  320. })
  321. return printSchema
  322. }