useCrudSchemas.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import { reactive } from 'vue'
  2. import { eachTree, treeMap, filter } from '@/utils/tree'
  3. import { findIndex } from '@/utils'
  4. import { useDictStoreWithOut } from '@/store/modules/dict'
  5. import { useI18n } from '@/hooks/web/useI18n'
  6. import type { AxiosPromise } from 'axios'
  7. import { FormSchema } from '@/types/form'
  8. import { TableColumn } from '@/types/table'
  9. import { DescriptionsSchema } from '@/types/descriptions'
  10. export type CrudSchema = Omit<TableColumn, 'children'> & {
  11. search?: CrudSearchParams
  12. table?: CrudTableParams
  13. form?: CrudFormParams
  14. detail?: CrudDescriptionsParams
  15. children?: CrudSchema[]
  16. }
  17. type CrudSearchParams = {
  18. // 是否显示在查询项
  19. show?: boolean
  20. // 字典名称,会去取全局的字典
  21. dictName?: string
  22. // 接口
  23. api?: () => Promise<any>
  24. } & Omit<FormSchema, 'field'>
  25. type CrudTableParams = {
  26. // 是否显示表头
  27. show?: boolean
  28. } & Omit<FormSchema, 'field'>
  29. type CrudFormParams = {
  30. // 字典名称,会去取全局的字典
  31. dictName?: string
  32. // 接口
  33. api?: () => Promise<any>
  34. // 是否显示表单项
  35. show?: boolean
  36. } & Omit<FormSchema, 'field'>
  37. type CrudDescriptionsParams = {
  38. // 是否显示表单项
  39. show?: boolean
  40. } & Omit<DescriptionsSchema, 'field'>
  41. const dictStore = useDictStoreWithOut()
  42. const { t } = useI18n()
  43. interface AllSchemas {
  44. searchSchema: FormSchema[]
  45. tableColumns: TableColumn[]
  46. formSchema: FormSchema[]
  47. detailSchema: DescriptionsSchema[]
  48. }
  49. // 过滤所有结构
  50. export const useCrudSchemas = (
  51. crudSchema: CrudSchema[]
  52. ): {
  53. allSchemas: AllSchemas
  54. } => {
  55. // 所有结构数据
  56. const allSchemas = reactive<AllSchemas>({
  57. searchSchema: [],
  58. tableColumns: [],
  59. formSchema: [],
  60. detailSchema: []
  61. })
  62. const searchSchema = filterSearchSchema(crudSchema, allSchemas)
  63. allSchemas.searchSchema = searchSchema || []
  64. const tableColumns = filterTableSchema(crudSchema)
  65. allSchemas.tableColumns = tableColumns || []
  66. const formSchema = filterFormSchema(crudSchema, allSchemas)
  67. allSchemas.formSchema = formSchema
  68. const detailSchema = filterDescriptionsSchema(crudSchema)
  69. allSchemas.detailSchema = detailSchema
  70. return {
  71. allSchemas
  72. }
  73. }
  74. // 过滤 Search 结构
  75. const filterSearchSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
  76. const searchSchema: FormSchema[] = []
  77. // 获取字典列表队列
  78. const searchRequestTask: Array<() => Promise<void>> = []
  79. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  80. // 判断是否显示
  81. if (schemaItem?.search?.show) {
  82. const searchSchemaItem = {
  83. // 默认为 input
  84. component: schemaItem.search.component || 'Input',
  85. componentProps: {},
  86. ...schemaItem.search,
  87. field: schemaItem.field,
  88. label: schemaItem.search?.label || schemaItem.label
  89. }
  90. if (searchSchemaItem.dictName) {
  91. // 如果有 dictName 则证明是从字典中获取数据
  92. const dictArr = dictStore.getDictObj[searchSchemaItem.dictName]
  93. searchSchemaItem.componentProps!.options = filterOptions(dictArr)
  94. } else if (searchSchemaItem.api) {
  95. searchRequestTask.push(async () => {
  96. const res = await (searchSchemaItem.api as () => AxiosPromise)()
  97. if (res) {
  98. const index = findIndex(allSchemas.searchSchema, (v: FormSchema) => {
  99. return v.field === searchSchemaItem.field
  100. })
  101. if (index !== -1) {
  102. allSchemas.searchSchema[index]!.componentProps!.options = filterOptions(
  103. res,
  104. searchSchemaItem.componentProps.optionsAlias?.labelField
  105. )
  106. }
  107. }
  108. })
  109. }
  110. // 删除不必要的字段
  111. delete searchSchemaItem.show
  112. delete searchSchemaItem.dictName
  113. searchSchema.push(searchSchemaItem)
  114. }
  115. })
  116. for (const task of searchRequestTask) {
  117. task()
  118. }
  119. return searchSchema
  120. }
  121. // 过滤 table 结构
  122. const filterTableSchema = (crudSchema: CrudSchema[]): TableColumn[] => {
  123. const tableColumns = treeMap<CrudSchema>(crudSchema, {
  124. conversion: (schema: CrudSchema) => {
  125. if (schema?.table?.show !== false) {
  126. return {
  127. ...schema.table,
  128. ...schema
  129. }
  130. }
  131. }
  132. })
  133. // 第一次过滤会有 undefined 所以需要二次过滤
  134. return filter<TableColumn>(tableColumns as TableColumn[], (data) => {
  135. if (data.children === void 0) {
  136. delete data.children
  137. }
  138. return !!data.field
  139. })
  140. }
  141. // 过滤 form 结构
  142. const filterFormSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
  143. const formSchema: FormSchema[] = []
  144. // 获取字典列表队列
  145. const formRequestTask: Array<() => Promise<void>> = []
  146. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  147. // 判断是否显示
  148. if (schemaItem?.form?.show !== false) {
  149. const formSchemaItem = {
  150. // 默认为 input
  151. component: schemaItem?.form?.component || 'Input',
  152. componentProps: {},
  153. ...schemaItem.form,
  154. field: schemaItem.field,
  155. label: schemaItem.search?.label || schemaItem.label
  156. }
  157. if (formSchemaItem.dictName) {
  158. // 如果有 dictName 则证明是从字典中获取数据
  159. const dictArr = dictStore.getDictObj[formSchemaItem.dictName]
  160. formSchemaItem.componentProps!.options = filterOptions(dictArr)
  161. } else if (formSchemaItem.api) {
  162. formRequestTask.push(async () => {
  163. const res = await (formSchemaItem.api as () => AxiosPromise)()
  164. if (res) {
  165. const index = findIndex(allSchemas.formSchema, (v: FormSchema) => {
  166. return v.field === formSchemaItem.field
  167. })
  168. if (index !== -1) {
  169. allSchemas.formSchema[index]!.componentProps!.options = filterOptions(
  170. res,
  171. formSchemaItem.componentProps.optionsAlias?.labelField
  172. )
  173. }
  174. }
  175. })
  176. }
  177. // 删除不必要的字段
  178. delete formSchemaItem.show
  179. delete formSchemaItem.dictName
  180. formSchema.push(formSchemaItem)
  181. }
  182. })
  183. for (const task of formRequestTask) {
  184. task()
  185. }
  186. console.log(formSchema)
  187. return formSchema
  188. }
  189. // 过滤 descriptions 结构
  190. const filterDescriptionsSchema = (crudSchema: CrudSchema[]): DescriptionsSchema[] => {
  191. const descriptionsSchema: FormSchema[] = []
  192. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  193. // 判断是否显示
  194. if (schemaItem?.detail?.show !== false) {
  195. const descriptionsSchemaItem = {
  196. ...schemaItem.detail,
  197. field: schemaItem.field,
  198. label: schemaItem.detail?.label || schemaItem.label
  199. }
  200. // 删除不必要的字段
  201. delete descriptionsSchemaItem.show
  202. descriptionsSchema.push(descriptionsSchemaItem)
  203. }
  204. })
  205. return descriptionsSchema
  206. }
  207. // 给options添加国际化
  208. const filterOptions = (options: Recordable, labelField?: string) => {
  209. return options.map((v: Recordable) => {
  210. if (labelField) {
  211. v['labelField'] = t(v.labelField)
  212. } else {
  213. v['label'] = t(v.label)
  214. }
  215. return v
  216. })
  217. }