useVxeCrudSchemas.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. import { DescriptionsSchema } from '@/types/descriptions'
  2. import { getBoolDictOptions, getDictOptions, getIntDictOptions } from '@/utils/dict'
  3. import { reactive } from 'vue'
  4. import {
  5. FormItemRenderOptions,
  6. VxeColumnPropTypes,
  7. VxeFormItemProps,
  8. VxeGridPropTypes,
  9. VxeTableDefines
  10. } from 'vxe-table'
  11. import { eachTree } from 'xe-utils'
  12. import { useI18n } from '@/hooks/web/useI18n'
  13. import { VxeTableColumn } from '@/types/table'
  14. import { FormSchema } from '@/types/form'
  15. import { ComponentOptions } from '@/types/components'
  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 > 2,
  135. itemRender: schemaItem.itemRender ? schemaItem.itemRender : itemRender,
  136. field: schemaItem.field,
  137. title: schemaItem.search?.title || schemaItem.title,
  138. span: 8
  139. }
  140. searchSchema.push(searchSchemaItem)
  141. }
  142. })
  143. // 添加搜索按钮
  144. const buttons: VxeFormItemProps = {
  145. span: 24,
  146. align: 'center',
  147. collapseNode: searchSchema.length > 3,
  148. itemRender: {
  149. name: '$buttons',
  150. children: [
  151. { props: { type: 'submit', content: t('common.query'), status: 'primary' } },
  152. { props: { type: 'reset', content: t('common.reset') } }
  153. ]
  154. }
  155. }
  156. searchSchema.push(buttons)
  157. return searchSchema
  158. }
  159. // 过滤 table 结构
  160. const filterTableSchema = (crudSchema: VxeCrudSchema): VxeGridPropTypes.Columns => {
  161. const { t } = useI18n()
  162. const tableSchema: VxeGridPropTypes.Columns = []
  163. // 主键ID
  164. if (crudSchema.primaryKey && crudSchema.primaryType) {
  165. const tableSchemaItem = {
  166. title: crudSchema.primaryTitle ? crudSchema.primaryTitle : t('common.index'),
  167. field: crudSchema.primaryKey,
  168. type: crudSchema.primaryType ? crudSchema.primaryType : null,
  169. width: '80px'
  170. }
  171. tableSchema.push(tableSchemaItem)
  172. }
  173. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  174. // 判断是否显示
  175. if (schemaItem?.isTable !== false && schemaItem?.table?.show !== false) {
  176. const tableSchemaItem = {
  177. ...schemaItem.table,
  178. field: schemaItem.field,
  179. title: schemaItem.table?.title || schemaItem.title
  180. }
  181. tableSchemaItem.showOverflow = 'tooltip'
  182. if (schemaItem?.formatter) {
  183. tableSchemaItem.formatter = schemaItem.formatter
  184. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  185. }
  186. if (schemaItem?.dictType) {
  187. tableSchemaItem.cellRender = {
  188. name: 'XDict',
  189. content: schemaItem.dictType
  190. }
  191. tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
  192. }
  193. tableSchema.push(tableSchemaItem)
  194. }
  195. })
  196. // 操作栏插槽
  197. if (crudSchema.action && crudSchema.action == true) {
  198. const tableSchemaItem = {
  199. title: crudSchema.actionTitle ? crudSchema.actionTitle : t('table.action'),
  200. field: 'actionbtns',
  201. width: crudSchema.actionWidth ? crudSchema.actionWidth : '200px',
  202. slots: {
  203. default: 'actionbtns_default'
  204. }
  205. }
  206. tableSchema.push(tableSchemaItem)
  207. }
  208. return tableSchema
  209. }
  210. // 过滤 form 结构
  211. const filterFormSchema = (crudSchema: VxeCrudSchema): FormSchema[] => {
  212. const formSchema: FormSchema[] = []
  213. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  214. // 判断是否显示
  215. if (schemaItem?.isForm !== false || schemaItem?.form?.show == true) {
  216. // 默认为 input
  217. let component = schemaItem?.form?.component || 'Input'
  218. let defaultValue: any = ''
  219. if (schemaItem.form?.value) {
  220. defaultValue = schemaItem.form?.value
  221. } else {
  222. if (component === 'InputNumber') {
  223. defaultValue = 0
  224. }
  225. }
  226. let comonentProps = {}
  227. if (schemaItem.dictType) {
  228. const options: ComponentOptions[] = []
  229. if (schemaItem.dictClass && schemaItem.dictClass === 'number') {
  230. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  231. options.push(dict)
  232. })
  233. } else if (schemaItem.dictClass && schemaItem.dictClass === 'boolean') {
  234. getBoolDictOptions(schemaItem.dictType).forEach((dict) => {
  235. options.push(dict)
  236. })
  237. } else {
  238. getDictOptions(schemaItem.dictType).forEach((dict) => {
  239. options.push(dict)
  240. })
  241. }
  242. comonentProps = {
  243. options: options
  244. }
  245. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  246. }
  247. const formSchemaItem = {
  248. component: component,
  249. componentProps: comonentProps,
  250. value: defaultValue,
  251. ...schemaItem.form,
  252. field: schemaItem.field,
  253. label: schemaItem.form?.label || schemaItem.title
  254. }
  255. formSchema.push(formSchemaItem)
  256. }
  257. })
  258. return formSchema
  259. }
  260. // 过滤 descriptions 结构
  261. const filterDescriptionsSchema = (crudSchema: VxeCrudSchema): DescriptionsSchema[] => {
  262. const descriptionsSchema: DescriptionsSchema[] = []
  263. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  264. // 判断是否显示
  265. if (schemaItem?.isDetail !== false || schemaItem.detail?.show !== false) {
  266. const descriptionsSchemaItem = {
  267. ...schemaItem.detail,
  268. field: schemaItem.field,
  269. label: schemaItem.detail?.label || schemaItem.title
  270. }
  271. if (schemaItem.dictType) {
  272. descriptionsSchemaItem.dictType = schemaItem.dictType
  273. }
  274. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  275. descriptionsSchemaItem.dateFormat = schemaItem.dateFormat
  276. ? schemaItem?.detail?.dateFormat
  277. : 'YYYY-MM-DD HH:mm:ss'
  278. }
  279. descriptionsSchema.push(descriptionsSchemaItem)
  280. }
  281. })
  282. return descriptionsSchema
  283. }
  284. // 过滤 打印 结构
  285. const filterPrintSchema = (crudSchema: VxeCrudSchema): any[] => {
  286. const printSchema: any[] = []
  287. eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
  288. // 判断是否显示
  289. if (schemaItem?.print?.show !== false) {
  290. const printSchemaItem = {
  291. field: schemaItem.field
  292. }
  293. printSchema.push(printSchemaItem)
  294. }
  295. })
  296. return printSchema
  297. }