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 { getBoolDictOptions, getDictOptions, getIntDictOptions } from '@/utils/dict'
  10. import { FormSchema } from '@/types/form'
  11. import { VxeTableColumn } from '@/types/table'
  12. import { ComponentOptions } from '@/types/components'
  13. import { DescriptionsSchema } from '@/types/descriptions'
  14. export type VxeCrudSchema = {
  15. primaryKey?: string // 主键ID
  16. primaryTitle?: string // 主键标题 默认为序号
  17. primaryType?: VxeColumnPropTypes.Type | 'id' // 还支持 "id" | "seq" | "radio" | "checkbox" | "expand" | "html" | null
  18. firstColumn?: VxeColumnPropTypes.Type // 第一列显示类型
  19. action?: boolean // 是否开启表格内右侧操作栏插槽
  20. actionTitle?: string // 操作栏标题 默认为操作
  21. actionWidth?: string // 操作栏插槽宽度,一般2个字带图标 text 类型按钮 50-70
  22. columns: VxeCrudColumns[]
  23. searchSpan?: number
  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. dictClass?: '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 span = crudSchema.searchSpan ? crudSchema.searchSpan : 6
  101. const spanLength = 24 / span
  102. const searchSchema: VxeFormItemProps[] = []
  103. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  104. // 判断是否显示
  105. if (schemaItem?.isSearch || schemaItem.search?.show) {
  106. let itemRenderName = schemaItem?.search?.itemRender?.name || '$input'
  107. const options: any[] = []
  108. let itemRender: FormItemRenderOptions = {}
  109. if (schemaItem.dictType) {
  110. const allOptions = { label: '全部', value: '' }
  111. options.push(allOptions)
  112. getDictOptions(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. } else {
  123. if (schemaItem.search?.itemRender) {
  124. itemRender = schemaItem.search.itemRender
  125. } else {
  126. itemRender = {
  127. name: itemRenderName,
  128. props:
  129. itemRenderName == '$input'
  130. ? { placeholder: t('common.inputText') }
  131. : { placeholder: t('common.selectText') }
  132. }
  133. }
  134. }
  135. const searchSchemaItem = {
  136. // 默认为 input
  137. folding: searchSchema.length > spanLength - 1,
  138. itemRender: schemaItem.itemRender ? schemaItem.itemRender : itemRender,
  139. field: schemaItem.field,
  140. title: schemaItem.search?.title || schemaItem.title,
  141. slots: schemaItem.search?.slots,
  142. span: span
  143. }
  144. searchSchema.push(searchSchemaItem)
  145. }
  146. })
  147. if (searchSchema.length > 0) {
  148. // 添加搜索按钮
  149. const buttons: VxeFormItemProps = {
  150. span: 24,
  151. align: 'right',
  152. collapseNode: searchSchema.length > spanLength,
  153. itemRender: {
  154. name: '$buttons',
  155. children: [
  156. { props: { type: 'submit', content: t('common.query'), status: 'primary' } },
  157. { props: { type: 'reset', content: t('common.reset') } }
  158. ]
  159. }
  160. }
  161. searchSchema.push(buttons)
  162. }
  163. return searchSchema
  164. }
  165. // 过滤 table 结构
  166. const filterTableSchema = (crudSchema: VxeCrudSchema): VxeGridPropTypes.Columns => {
  167. const { t } = useI18n()
  168. const tableSchema: VxeGridPropTypes.Columns = []
  169. // 第一列
  170. if (crudSchema.firstColumn) {
  171. const tableSchemaItem = {
  172. type: crudSchema.firstColumn,
  173. width: '50px'
  174. }
  175. tableSchema.push(tableSchemaItem)
  176. }
  177. // 主键ID
  178. if (crudSchema.primaryKey && crudSchema.primaryType) {
  179. const primaryTitle = crudSchema.primaryTitle ? crudSchema.primaryTitle : t('common.index')
  180. const primaryWidth = primaryTitle.length * 30 + 'px'
  181. let tableSchemaItem: { [x: string]: any } = {
  182. title: primaryTitle,
  183. field: crudSchema.primaryKey,
  184. width: primaryWidth
  185. }
  186. if (crudSchema.primaryType != 'id') {
  187. tableSchemaItem = {
  188. ...tableSchemaItem,
  189. type: crudSchema.primaryType
  190. }
  191. }
  192. tableSchema.push(tableSchemaItem)
  193. }
  194. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  195. // 判断是否显示
  196. if (schemaItem?.isTable !== false && schemaItem?.table?.show !== false) {
  197. const tableSchemaItem = {
  198. ...schemaItem.table,
  199. field: schemaItem.field,
  200. title: schemaItem.table?.title || schemaItem.title,
  201. minWidth: '80px'
  202. }
  203. tableSchemaItem.showOverflow = 'tooltip'
  204. if (schemaItem?.formatter) {
  205. tableSchemaItem.formatter = schemaItem.formatter
  206. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  207. }
  208. if (schemaItem?.dictType) {
  209. tableSchemaItem.cellRender = {
  210. name: 'XDict',
  211. content: schemaItem.dictType
  212. }
  213. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  214. }
  215. tableSchema.push(tableSchemaItem)
  216. }
  217. })
  218. // 操作栏插槽
  219. if (crudSchema.action && crudSchema.action == true) {
  220. const tableSchemaItem = {
  221. title: crudSchema.actionTitle ? crudSchema.actionTitle : t('table.action'),
  222. field: 'actionbtns',
  223. fixed: 'right' as unknown as VxeColumnPropTypes.Fixed,
  224. width: crudSchema.actionWidth ? crudSchema.actionWidth : '200px',
  225. slots: {
  226. default: 'actionbtns_default'
  227. }
  228. }
  229. tableSchema.push(tableSchemaItem)
  230. }
  231. return tableSchema
  232. }
  233. // 过滤 form 结构
  234. const filterFormSchema = (crudSchema: VxeCrudSchema): FormSchema[] => {
  235. const formSchema: FormSchema[] = []
  236. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  237. // 判断是否显示
  238. if (schemaItem?.isForm !== false && schemaItem?.form?.show !== false) {
  239. // 默认为 input
  240. let component = schemaItem?.form?.component || 'Input'
  241. let defaultValue: any = ''
  242. if (schemaItem.form?.value) {
  243. defaultValue = schemaItem.form?.value
  244. } else {
  245. if (component === 'InputNumber') {
  246. defaultValue = 0
  247. }
  248. }
  249. let comonentProps = {}
  250. if (schemaItem.dictType) {
  251. const options: ComponentOptions[] = []
  252. if (schemaItem.dictClass && schemaItem.dictClass === 'number') {
  253. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  254. options.push(dict)
  255. })
  256. } else if (schemaItem.dictClass && schemaItem.dictClass === 'boolean') {
  257. getBoolDictOptions(schemaItem.dictType).forEach((dict) => {
  258. options.push(dict)
  259. })
  260. } else {
  261. getDictOptions(schemaItem.dictType).forEach((dict) => {
  262. options.push(dict)
  263. })
  264. }
  265. comonentProps = {
  266. options: options
  267. }
  268. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  269. }
  270. const formSchemaItem = {
  271. component: component,
  272. componentProps: comonentProps,
  273. value: defaultValue,
  274. ...schemaItem.form,
  275. field: schemaItem.field,
  276. label: schemaItem.form?.label || schemaItem.title
  277. }
  278. formSchema.push(formSchemaItem)
  279. }
  280. })
  281. return formSchema
  282. }
  283. // 过滤 descriptions 结构
  284. const filterDescriptionsSchema = (crudSchema: VxeCrudSchema): DescriptionsSchema[] => {
  285. const descriptionsSchema: DescriptionsSchema[] = []
  286. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  287. // 判断是否显示
  288. if (schemaItem?.isDetail !== false && schemaItem.detail?.show !== false) {
  289. const descriptionsSchemaItem = {
  290. ...schemaItem.detail,
  291. field: schemaItem.field,
  292. label: schemaItem.detail?.label || schemaItem.title
  293. }
  294. if (schemaItem.dictType) {
  295. descriptionsSchemaItem.dictType = schemaItem.dictType
  296. }
  297. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  298. // 优先使用 detail 下的配置,如果没有默认为 YYYY-MM-DD HH:mm:ss
  299. descriptionsSchemaItem.dateFormat = schemaItem?.detail?.dateFormat
  300. ? schemaItem?.detail?.dateFormat
  301. : 'YYYY-MM-DD HH:mm:ss'
  302. }
  303. descriptionsSchema.push(descriptionsSchemaItem)
  304. }
  305. })
  306. return descriptionsSchema
  307. }
  308. // 过滤 打印 结构
  309. const filterPrintSchema = (crudSchema: VxeCrudSchema): any[] => {
  310. const printSchema: any[] = []
  311. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  312. // 判断是否显示
  313. if (schemaItem?.print?.show !== false) {
  314. const printSchemaItem = {
  315. field: schemaItem.field
  316. }
  317. printSchema.push(printSchemaItem)
  318. }
  319. })
  320. return printSchema
  321. }