useCrudSchemas.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. import { reactive } from 'vue'
  2. import { AxiosPromise } from 'axios'
  3. import { findIndex } from '@/utils'
  4. import { eachTree, filter, treeMap } from '@/utils/tree'
  5. import { getBoolDictOptions, getDictOptions, getIntDictOptions } from '@/utils/dict'
  6. import { FormSchema } from '@/types/form'
  7. import { TableColumn } from '@/types/table'
  8. import { DescriptionsSchema } from '@/types/descriptions'
  9. import { ComponentOptions, ComponentProps } from '@/types/components'
  10. import { DictTag } from '@/components/DictTag'
  11. export type CrudSchema = Omit<TableColumn, 'children'> & {
  12. isSearch?: boolean // 是否在查询显示
  13. search?: CrudSearchParams // 查询的详细配置
  14. isTable?: boolean // 是否在列表显示
  15. table?: CrudTableParams // 列表的详细配置
  16. isForm?: boolean // 是否在表单显示
  17. form?: CrudFormParams // 表单的详细配置
  18. isDetail?: boolean // 是否在详情显示
  19. detail?: CrudDescriptionsParams // 详情的详细配置
  20. children?: CrudSchema[]
  21. dictType?: string // 字典类型
  22. dictClass?: 'string' | 'number' | 'boolean' // 字典数据类型 string | number | boolean
  23. }
  24. type CrudSearchParams = {
  25. // 是否显示在查询项
  26. show?: boolean
  27. // 接口
  28. api?: () => Promise<any>
  29. // 搜索字段
  30. field?: string
  31. } & Omit<FormSchema, 'field'>
  32. type CrudTableParams = {
  33. // 是否显示表头
  34. show?: boolean
  35. // 列宽配置
  36. width?: number | string
  37. // 列是否固定在左侧或者右侧
  38. fixed?: 'left' | 'right'
  39. } & Omit<FormSchema, 'field'>
  40. type CrudFormParams = {
  41. // 是否显示表单项
  42. show?: boolean
  43. // 接口
  44. api?: () => Promise<any>
  45. } & Omit<FormSchema, 'field'>
  46. type CrudDescriptionsParams = {
  47. // 是否显示表单项
  48. show?: boolean
  49. } & Omit<DescriptionsSchema, 'field'>
  50. interface AllSchemas {
  51. searchSchema: FormSchema[]
  52. tableColumns: TableColumn[]
  53. formSchema: FormSchema[]
  54. detailSchema: DescriptionsSchema[]
  55. }
  56. const { t } = useI18n()
  57. // 过滤所有结构
  58. export const useCrudSchemas = (
  59. crudSchema: CrudSchema[]
  60. ): {
  61. allSchemas: AllSchemas
  62. } => {
  63. // 所有结构数据
  64. const allSchemas = reactive<AllSchemas>({
  65. searchSchema: [],
  66. tableColumns: [],
  67. formSchema: [],
  68. detailSchema: []
  69. })
  70. const searchSchema = filterSearchSchema(crudSchema, allSchemas)
  71. allSchemas.searchSchema = searchSchema || []
  72. const tableColumns = filterTableSchema(crudSchema)
  73. allSchemas.tableColumns = tableColumns || []
  74. const formSchema = filterFormSchema(crudSchema, allSchemas)
  75. allSchemas.formSchema = formSchema
  76. const detailSchema = filterDescriptionsSchema(crudSchema)
  77. allSchemas.detailSchema = detailSchema
  78. return {
  79. allSchemas
  80. }
  81. }
  82. // 过滤 Search 结构
  83. const filterSearchSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
  84. const searchSchema: FormSchema[] = []
  85. // 获取字典列表队列
  86. const searchRequestTask: Array<() => Promise<void>> = []
  87. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  88. // 判断是否显示
  89. if (schemaItem?.isSearch || schemaItem.search?.show) {
  90. let component = schemaItem?.search?.component || 'Input'
  91. const options: ComponentOptions[] = []
  92. let comonentProps: ComponentProps = {}
  93. if (schemaItem.dictType) {
  94. const allOptions: ComponentOptions = { label: '全部', value: '' }
  95. options.push(allOptions)
  96. getDictOptions(schemaItem.dictType).forEach((dict) => {
  97. options.push(dict)
  98. })
  99. comonentProps = {
  100. options: options
  101. }
  102. if (!schemaItem.search?.component) component = 'Select'
  103. }
  104. const searchSchemaItem = {
  105. // 默认为 input
  106. component: component,
  107. componentProps: comonentProps,
  108. ...schemaItem.search,
  109. field: schemaItem.field,
  110. label: schemaItem.search?.label || schemaItem.label
  111. }
  112. if (searchSchemaItem.api) {
  113. searchRequestTask.push(async () => {
  114. const res = await (searchSchemaItem.api as () => AxiosPromise)()
  115. if (res) {
  116. const index = findIndex(allSchemas.searchSchema, (v: FormSchema) => {
  117. return v.field === searchSchemaItem.field
  118. })
  119. if (index !== -1) {
  120. allSchemas.searchSchema[index]!.componentProps!.options = filterOptions(
  121. res,
  122. searchSchemaItem.componentProps.optionsAlias?.labelField
  123. )
  124. }
  125. }
  126. })
  127. }
  128. // 删除不必要的字段
  129. delete searchSchemaItem.show
  130. searchSchema.push(searchSchemaItem)
  131. }
  132. })
  133. for (const task of searchRequestTask) {
  134. task()
  135. }
  136. return searchSchema
  137. }
  138. // 过滤 table 结构
  139. const filterTableSchema = (crudSchema: CrudSchema[]): TableColumn[] => {
  140. const tableColumns = treeMap<CrudSchema>(crudSchema, {
  141. conversion: (schema: CrudSchema) => {
  142. if (schema?.isTable !== false && schema?.table?.show !== false) {
  143. // add by 芋艿:增加对 dict 字典数据的支持
  144. if (!schema.formatter && schema.dictType) {
  145. schema.formatter = (_: Recordable, __: TableColumn, cellValue: any) => {
  146. return h(DictTag, {
  147. type: schema.dictType!, // ! 表示一定不为空
  148. value: cellValue
  149. })
  150. }
  151. }
  152. return {
  153. ...schema.table,
  154. ...schema
  155. }
  156. }
  157. }
  158. })
  159. // 第一次过滤会有 undefined 所以需要二次过滤
  160. return filter<TableColumn>(tableColumns as TableColumn[], (data) => {
  161. if (data.children === void 0) {
  162. delete data.children
  163. }
  164. return !!data.field
  165. })
  166. }
  167. // 过滤 form 结构
  168. const filterFormSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
  169. const formSchema: FormSchema[] = []
  170. // 获取字典列表队列
  171. const formRequestTask: Array<() => Promise<void>> = []
  172. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  173. // 判断是否显示
  174. if (schemaItem?.isForm !== false && schemaItem?.form?.show !== false) {
  175. let component = schemaItem?.form?.component || 'Input'
  176. let defaultValue: any = ''
  177. if (schemaItem.form?.value) {
  178. defaultValue = schemaItem.form?.value
  179. } else {
  180. if (component === 'InputNumber') {
  181. defaultValue = 0
  182. }
  183. }
  184. let comonentProps: ComponentProps = {}
  185. if (schemaItem.dictType) {
  186. const options: ComponentOptions[] = []
  187. if (schemaItem.dictClass && schemaItem.dictClass === 'number') {
  188. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  189. options.push(dict)
  190. })
  191. } else if (schemaItem.dictClass && schemaItem.dictClass === 'boolean') {
  192. getBoolDictOptions(schemaItem.dictType).forEach((dict) => {
  193. options.push(dict)
  194. })
  195. } else {
  196. getDictOptions(schemaItem.dictType).forEach((dict) => {
  197. options.push(dict)
  198. })
  199. }
  200. comonentProps = {
  201. options: options
  202. }
  203. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  204. }
  205. const formSchemaItem = {
  206. // 默认为 input
  207. component: component,
  208. componentProps: comonentProps,
  209. value: defaultValue,
  210. ...schemaItem.form,
  211. field: schemaItem.field,
  212. label: schemaItem.form?.label || schemaItem.label
  213. }
  214. if (formSchemaItem.api) {
  215. formRequestTask.push(async () => {
  216. const res = await (formSchemaItem.api as () => AxiosPromise)()
  217. if (res) {
  218. const index = findIndex(allSchemas.formSchema, (v: FormSchema) => {
  219. return v.field === formSchemaItem.field
  220. })
  221. if (index !== -1) {
  222. allSchemas.formSchema[index]!.componentProps!.options = filterOptions(
  223. res,
  224. formSchemaItem.componentProps.optionsAlias?.labelField
  225. )
  226. }
  227. }
  228. })
  229. }
  230. // 删除不必要的字段
  231. delete formSchemaItem.show
  232. formSchema.push(formSchemaItem)
  233. }
  234. })
  235. for (const task of formRequestTask) {
  236. task()
  237. }
  238. return formSchema
  239. }
  240. // 过滤 descriptions 结构
  241. const filterDescriptionsSchema = (crudSchema: CrudSchema[]): DescriptionsSchema[] => {
  242. const descriptionsSchema: FormSchema[] = []
  243. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  244. // 判断是否显示
  245. if (schemaItem?.isDetail !== false && schemaItem.detail?.show !== false) {
  246. const descriptionsSchemaItem = {
  247. ...schemaItem.detail,
  248. field: schemaItem.field,
  249. label: schemaItem.detail?.label || schemaItem.label
  250. }
  251. if (schemaItem.dictType) {
  252. descriptionsSchemaItem.dictType = schemaItem.dictType
  253. }
  254. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  255. // 优先使用 detail 下的配置,如果没有默认为 YYYY-MM-DD HH:mm:ss
  256. descriptionsSchemaItem.dateFormat = schemaItem?.detail?.dateFormat
  257. ? schemaItem?.detail?.dateFormat
  258. : 'YYYY-MM-DD HH:mm:ss'
  259. }
  260. // 删除不必要的字段
  261. delete descriptionsSchemaItem.show
  262. descriptionsSchema.push(descriptionsSchemaItem)
  263. }
  264. })
  265. return descriptionsSchema
  266. }
  267. // 给options添加国际化
  268. const filterOptions = (options: Recordable, labelField?: string) => {
  269. return options?.map((v: Recordable) => {
  270. if (labelField) {
  271. v['labelField'] = t(v.labelField)
  272. } else {
  273. v['label'] = t(v.label)
  274. }
  275. return v
  276. })
  277. }