useCrudSchemas.ts 8.7 KB

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