useCrudSchemas.ts 8.8 KB

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