useCrudSchemas.ts 9.7 KB

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