From ca3ce54630b723d87415b14c642440d6734876ff Mon Sep 17 00:00:00 2001 From: kailong321200875 <321200875@qq.com> Date: Fri, 1 Jul 2022 21:44:00 +0800 Subject: [PATCH] perf: add useCrudSchemas demo --- mock/role/index.ts | 9 ++ src/hooks/web/useCrudSchemas.ts | 48 ++++++- src/router/index.ts | 8 ++ src/views/hooks/useCrudSchemas.vue | 222 +++++++++++++++++++++++++++++ 4 files changed, 280 insertions(+), 7 deletions(-) create mode 100644 src/views/hooks/useCrudSchemas.vue diff --git a/mock/role/index.ts b/mock/role/index.ts index 99e444d..f9ed8cf 100644 --- a/mock/role/index.ts +++ b/mock/role/index.ts @@ -286,6 +286,14 @@ const adminList = [ meta: { title: 'useWatermark' } + }, + { + path: 'useCrudSchemas', + component: 'views/hooks/useCrudSchemas', + name: 'UseCrudSchemas', + meta: { + title: 'useCrudSchemas' + } } ] }, @@ -488,6 +496,7 @@ const testList: string[] = [ '/Components/Sticky', '/hooks', '/hooks/useWatermark', + '/hooks/useCrudSchemas', '/level', '/level/menu1', '/level/menu1/menu1-1', diff --git a/src/hooks/web/useCrudSchemas.ts b/src/hooks/web/useCrudSchemas.ts index aae2bf4..4a4a04f 100644 --- a/src/hooks/web/useCrudSchemas.ts +++ b/src/hooks/web/useCrudSchemas.ts @@ -18,8 +18,8 @@ type CrudSearchParams = { show?: boolean // 字典名称,会去取全局的字典 dictName?: string - // 接口路径 - dictUrl?: string + // 接口 + api?: () => Promise } & Omit type CrudTableParams = { @@ -28,6 +28,10 @@ type CrudTableParams = { } & Omit type CrudFormParams = { + // 字典名称,会去取全局的字典 + dictName?: string + // 接口 + api?: () => Promise // 是否显示表单项 show?: boolean } & Omit @@ -68,7 +72,7 @@ export const useCrudSchemas = ( const tableColumns = filterTableSchema(crudSchema) allSchemas.tableColumns = tableColumns || [] - const formSchema = filterFormSchema(crudSchema) + const formSchema = filterFormSchema(crudSchema, allSchemas) allSchemas.formSchema = formSchema const detailSchema = filterDescriptionsSchema(crudSchema) @@ -157,27 +161,57 @@ const filterTableSchema = (crudSchema: CrudSchema[]): TableColumn[] => { } // 过滤 form 结构 -const filterFormSchema = (crudSchema: CrudSchema[]): FormSchema[] => { +const filterFormSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => { const formSchema: FormSchema[] = [] + // 获取字典列表队列 + const formRequestTask: Array<() => Promise> = [] + eachTree(crudSchema, (schemaItem: CrudSchema) => { // 判断是否显示 - if (schemaItem?.form?.show !== false) { + if (schemaItem?.form?.show) { const formSchemaItem = { // 默认为 input - component: (schemaItem.form && schemaItem.form.component) || 'Input', + component: schemaItem.form.component || 'Input', + componentProps: {}, ...schemaItem.form, field: schemaItem.field, - label: schemaItem.form?.label || schemaItem.label + label: schemaItem.search?.label || schemaItem.label + } + + if (formSchemaItem.dictName) { + // 如果有 dictName 则证明是从字典中获取数据 + const dictArr = dictStore.getDictObj[formSchemaItem.dictName] + formSchemaItem.componentProps!.options = filterOptions(dictArr) + } else if (formSchemaItem.api) { + formRequestTask.push(async () => { + const res = await (formSchemaItem.api as () => AxiosPromise)() + if (res) { + const index = findIndex(allSchemas.formSchema, (v: FormSchema) => { + return v.field === formSchemaItem.field + }) + if (index !== -1) { + allSchemas.formSchema[index]!.componentProps!.options = filterOptions( + res, + formSchemaItem.componentProps.optionsAlias?.labelField + ) + } + } + }) } // 删除不必要的字段 delete formSchemaItem.show + delete formSchemaItem.dictName formSchema.push(formSchemaItem) } }) + for (const task of formRequestTask) { + task() + } + return formSchema } diff --git a/src/router/index.ts b/src/router/index.ts index 1c2eb29..080321d 100644 --- a/src/router/index.ts +++ b/src/router/index.ts @@ -337,6 +337,14 @@ export const asyncRouterMap: AppRouteRecordRaw[] = [ meta: { title: 'useWatermark' } + }, + { + path: 'useCrudSchemas', + component: () => import('@/views/hooks/useCrudSchemas.vue'), + name: 'UseCrudSchemas', + meta: { + title: 'useCrudSchemas' + } } ] }, diff --git a/src/views/hooks/useCrudSchemas.vue b/src/views/hooks/useCrudSchemas.vue new file mode 100644 index 0000000..afac0d7 --- /dev/null +++ b/src/views/hooks/useCrudSchemas.vue @@ -0,0 +1,222 @@ + + +