useVxeCrudSchemas.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. 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. // 主键ID
  171. if (crudSchema.primaryKey && crudSchema.primaryType) {
  172. const primaryTitle = crudSchema.primaryTitle ? crudSchema.primaryTitle : t('common.index')
  173. const primaryWidth = primaryTitle.length * 30 + 'px'
  174. let tableSchemaItem: { [x: string]: any } = {
  175. title: primaryTitle,
  176. field: crudSchema.primaryKey,
  177. width: primaryWidth
  178. }
  179. if (crudSchema.primaryType != 'id') {
  180. tableSchemaItem = {
  181. ...tableSchemaItem,
  182. type: crudSchema.primaryType
  183. }
  184. }
  185. tableSchema.push(tableSchemaItem)
  186. }
  187. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  188. // 判断是否显示
  189. if (schemaItem?.isTable !== false && schemaItem?.table?.show !== false) {
  190. const tableSchemaItem = {
  191. ...schemaItem.table,
  192. field: schemaItem.field,
  193. title: schemaItem.table?.title || schemaItem.title,
  194. minWidth: '80px'
  195. }
  196. tableSchemaItem.showOverflow = 'tooltip'
  197. if (schemaItem?.formatter) {
  198. tableSchemaItem.formatter = schemaItem.formatter
  199. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  200. }
  201. if (schemaItem?.dictType) {
  202. tableSchemaItem.cellRender = {
  203. name: 'XDict',
  204. content: schemaItem.dictType
  205. }
  206. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  207. }
  208. tableSchema.push(tableSchemaItem)
  209. }
  210. })
  211. // 操作栏插槽
  212. if (crudSchema.action && crudSchema.action == true) {
  213. const tableSchemaItem = {
  214. title: crudSchema.actionTitle ? crudSchema.actionTitle : t('table.action'),
  215. field: 'actionbtns',
  216. fixed: 'right' as unknown as VxeColumnPropTypes.Fixed,
  217. width: crudSchema.actionWidth ? crudSchema.actionWidth : '200px',
  218. slots: {
  219. default: 'actionbtns_default'
  220. }
  221. }
  222. tableSchema.push(tableSchemaItem)
  223. }
  224. return tableSchema
  225. }
  226. // 过滤 form 结构
  227. const filterFormSchema = (crudSchema: VxeCrudSchema): FormSchema[] => {
  228. const formSchema: FormSchema[] = []
  229. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  230. // 判断是否显示
  231. if (schemaItem?.isForm !== false && schemaItem?.form?.show !== false) {
  232. // 默认为 input
  233. let component = schemaItem?.form?.component || 'Input'
  234. let defaultValue: any = ''
  235. if (schemaItem.form?.value) {
  236. defaultValue = schemaItem.form?.value
  237. } else {
  238. if (component === 'InputNumber') {
  239. defaultValue = 0
  240. }
  241. }
  242. let comonentProps = {}
  243. if (schemaItem.dictType) {
  244. const options: ComponentOptions[] = []
  245. if (schemaItem.dictClass && schemaItem.dictClass === 'number') {
  246. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  247. options.push(dict)
  248. })
  249. } else if (schemaItem.dictClass && schemaItem.dictClass === 'boolean') {
  250. getBoolDictOptions(schemaItem.dictType).forEach((dict) => {
  251. options.push(dict)
  252. })
  253. } else {
  254. getDictOptions(schemaItem.dictType).forEach((dict) => {
  255. options.push(dict)
  256. })
  257. }
  258. comonentProps = {
  259. options: options
  260. }
  261. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  262. }
  263. const formSchemaItem = {
  264. component: component,
  265. componentProps: comonentProps,
  266. value: defaultValue,
  267. ...schemaItem.form,
  268. field: schemaItem.field,
  269. label: schemaItem.form?.label || schemaItem.title
  270. }
  271. formSchema.push(formSchemaItem)
  272. }
  273. })
  274. return formSchema
  275. }
  276. // 过滤 descriptions 结构
  277. const filterDescriptionsSchema = (crudSchema: VxeCrudSchema): DescriptionsSchema[] => {
  278. const descriptionsSchema: DescriptionsSchema[] = []
  279. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  280. // 判断是否显示
  281. if (schemaItem?.isDetail !== false && schemaItem.detail?.show !== false) {
  282. const descriptionsSchemaItem = {
  283. ...schemaItem.detail,
  284. field: schemaItem.field,
  285. label: schemaItem.detail?.label || schemaItem.title
  286. }
  287. if (schemaItem.dictType) {
  288. descriptionsSchemaItem.dictType = schemaItem.dictType
  289. }
  290. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  291. // 优先使用 detail 下的配置,如果没有默认为 YYYY-MM-DD HH:mm:ss
  292. descriptionsSchemaItem.dateFormat = schemaItem?.detail?.dateFormat
  293. ? schemaItem?.detail?.dateFormat
  294. : 'YYYY-MM-DD HH:mm:ss'
  295. }
  296. descriptionsSchema.push(descriptionsSchemaItem)
  297. }
  298. })
  299. return descriptionsSchema
  300. }
  301. // 过滤 打印 结构
  302. const filterPrintSchema = (crudSchema: VxeCrudSchema): any[] => {
  303. const printSchema: any[] = []
  304. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  305. // 判断是否显示
  306. if (schemaItem?.print?.show !== false) {
  307. const printSchemaItem = {
  308. field: schemaItem.field
  309. }
  310. printSchema.push(printSchemaItem)
  311. }
  312. })
  313. return printSchema
  314. }