useCrudSchemas.ts 9.2 KB

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