useCrudSchemas.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, merge } 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. // updated by AKing: 解决了当使用默认的dict选项时,form中事件不能触发的问题
  106. const searchSchemaItem = merge(
  107. {
  108. // 默认为 input
  109. component,
  110. ...schemaItem.search,
  111. field: schemaItem.field,
  112. label: schemaItem.search?.label || schemaItem.label
  113. },
  114. { componentProps: comonentProps }
  115. )
  116. if (searchSchemaItem.api) {
  117. searchRequestTask.push(async () => {
  118. const res = await (searchSchemaItem.api as () => AxiosPromise)()
  119. if (res) {
  120. const index = findIndex(allSchemas.searchSchema, (v: FormSchema) => {
  121. return v.field === searchSchemaItem.field
  122. })
  123. if (index !== -1) {
  124. allSchemas.searchSchema[index]!.componentProps!.options = filterOptions(
  125. res,
  126. searchSchemaItem.componentProps.optionsAlias?.labelField
  127. )
  128. }
  129. }
  130. })
  131. }
  132. // 删除不必要的字段
  133. delete searchSchemaItem.show
  134. searchSchema.push(searchSchemaItem)
  135. }
  136. })
  137. for (const task of searchRequestTask) {
  138. task()
  139. }
  140. return searchSchema
  141. }
  142. // 过滤 table 结构
  143. const filterTableSchema = (crudSchema: CrudSchema[]): TableColumn[] => {
  144. const tableColumns = treeMap<CrudSchema>(crudSchema, {
  145. conversion: (schema: CrudSchema) => {
  146. if (schema?.isTable !== false && schema?.table?.show !== false) {
  147. // add by 芋艿:增加对 dict 字典数据的支持
  148. if (!schema.formatter && schema.dictType) {
  149. schema.formatter = (_: Recordable, __: TableColumn, cellValue: any) => {
  150. return h(DictTag, {
  151. type: schema.dictType!, // ! 表示一定不为空
  152. value: cellValue
  153. })
  154. }
  155. }
  156. return {
  157. ...schema.table,
  158. ...schema
  159. }
  160. }
  161. }
  162. })
  163. // 第一次过滤会有 undefined 所以需要二次过滤
  164. return filter<TableColumn>(tableColumns as TableColumn[], (data) => {
  165. if (data.children === void 0) {
  166. delete data.children
  167. }
  168. return !!data.field
  169. })
  170. }
  171. // 过滤 form 结构
  172. const filterFormSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
  173. const formSchema: FormSchema[] = []
  174. // 获取字典列表队列
  175. const formRequestTask: Array<() => Promise<void>> = []
  176. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  177. // 判断是否显示
  178. if (schemaItem?.isForm !== false && schemaItem?.form?.show !== false) {
  179. let component = schemaItem?.form?.component || 'Input'
  180. let defaultValue: any = ''
  181. if (schemaItem.form?.value) {
  182. defaultValue = schemaItem.form?.value
  183. } else {
  184. if (component === 'InputNumber') {
  185. defaultValue = 0
  186. }
  187. }
  188. let comonentProps: ComponentProps = {}
  189. if (schemaItem.dictType) {
  190. const options: ComponentOptions[] = []
  191. if (schemaItem.dictClass && schemaItem.dictClass === 'number') {
  192. getIntDictOptions(schemaItem.dictType).forEach((dict) => {
  193. options.push(dict)
  194. })
  195. } else if (schemaItem.dictClass && schemaItem.dictClass === 'boolean') {
  196. getBoolDictOptions(schemaItem.dictType).forEach((dict) => {
  197. options.push(dict)
  198. })
  199. } else {
  200. getDictOptions(schemaItem.dictType).forEach((dict) => {
  201. options.push(dict)
  202. })
  203. }
  204. comonentProps = {
  205. options: options
  206. }
  207. if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
  208. }
  209. // updated by AKing: 解决了当使用默认的dict选项时,form中事件不能触发的问题
  210. const formSchemaItem = merge(
  211. {
  212. // 默认为 input
  213. component,
  214. value: defaultValue,
  215. ...schemaItem.form,
  216. field: schemaItem.field,
  217. label: schemaItem.form?.label || schemaItem.label
  218. },
  219. { componentProps: comonentProps }
  220. )
  221. if (formSchemaItem.api) {
  222. formRequestTask.push(async () => {
  223. const res = await (formSchemaItem.api as () => AxiosPromise)()
  224. if (res) {
  225. const index = findIndex(allSchemas.formSchema, (v: FormSchema) => {
  226. return v.field === formSchemaItem.field
  227. })
  228. if (index !== -1) {
  229. allSchemas.formSchema[index]!.componentProps!.options = filterOptions(
  230. res,
  231. formSchemaItem.componentProps.optionsAlias?.labelField
  232. )
  233. }
  234. }
  235. })
  236. }
  237. // 删除不必要的字段
  238. delete formSchemaItem.show
  239. formSchema.push(formSchemaItem)
  240. }
  241. })
  242. for (const task of formRequestTask) {
  243. task()
  244. }
  245. return formSchema
  246. }
  247. // 过滤 descriptions 结构
  248. const filterDescriptionsSchema = (crudSchema: CrudSchema[]): DescriptionsSchema[] => {
  249. const descriptionsSchema: FormSchema[] = []
  250. eachTree(crudSchema, (schemaItem: CrudSchema) => {
  251. // 判断是否显示
  252. if (schemaItem?.isDetail !== false && schemaItem.detail?.show !== false) {
  253. const descriptionsSchemaItem = {
  254. ...schemaItem.detail,
  255. field: schemaItem.field,
  256. label: schemaItem.detail?.label || schemaItem.label
  257. }
  258. if (schemaItem.dictType) {
  259. descriptionsSchemaItem.dictType = schemaItem.dictType
  260. }
  261. if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
  262. // 优先使用 detail 下的配置,如果没有默认为 YYYY-MM-DD HH:mm:ss
  263. descriptionsSchemaItem.dateFormat = schemaItem?.detail?.dateFormat
  264. ? schemaItem?.detail?.dateFormat
  265. : 'YYYY-MM-DD HH:mm:ss'
  266. }
  267. // 删除不必要的字段
  268. delete descriptionsSchemaItem.show
  269. descriptionsSchema.push(descriptionsSchemaItem)
  270. }
  271. })
  272. return descriptionsSchema
  273. }
  274. // 给options添加国际化
  275. const filterOptions = (options: Recordable, labelField?: string) => {
  276. return options?.map((v: Recordable) => {
  277. if (labelField) {
  278. v['labelField'] = t(v.labelField)
  279. } else {
  280. v['label'] = t(v.label)
  281. }
  282. return v
  283. })
  284. }
  285. // 将 tableColumns 指定 fields 放到最前面
  286. export const sortTableColumns = (tableColumns: TableColumn[], field: string) => {
  287. const fieldIndex = tableColumns.findIndex((item) => item.field === field)
  288. const fieldColumn = cloneDeep(tableColumns[fieldIndex])
  289. tableColumns.splice(fieldIndex, 1)
  290. // 添加到开头
  291. tableColumns.unshift(fieldColumn)
  292. }