useVxeCrudSchemas.ts 10 KB

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