useVxeCrudSchemas.ts 11 KB

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