useVxeCrudSchemas.ts 11 KB

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