Skip to content

Commit

Permalink
feat: 综合示例重构
Browse files Browse the repository at this point in the history
  • Loading branch information
kailong321200875 committed Jul 23, 2023
1 parent c181887 commit 9a0259d
Show file tree
Hide file tree
Showing 16 changed files with 511 additions and 452 deletions.
48 changes: 7 additions & 41 deletions src/components/ContentDetailWrap/src/ContentDetailWrap.vue
@@ -1,11 +1,7 @@
<script setup lang="ts">
import { ElCard, ElButton } from 'element-plus'
import { ElCard } from 'element-plus'
import { propTypes } from '@/utils/propTypes'
import { useDesign } from '@/hooks/web/useDesign'
import { ref, onMounted, defineEmits } from 'vue'
import { Sticky } from '@/components/Sticky'
import { useI18n } from '@/hooks/web/useI18n'
const { t } = useI18n()
const { getPrefixCls } = useDesign()
Expand All @@ -15,45 +11,15 @@ defineProps({
title: propTypes.string.def(''),
message: propTypes.string.def('')
})
const emit = defineEmits(['back'])
const offset = ref(85)
const contentDetailWrap = ref()
onMounted(() => {
offset.value = contentDetailWrap.value.getBoundingClientRect().top
})
</script>

<template>
<div :class="[`${prefixCls}-container`, 'relative bg-[#fff]']" ref="contentDetailWrap">
<Sticky :offset="offset">
<div
:class="[
`${prefixCls}-header`,
'flex b-b-1 h-50px items-center text-center bg-white pr-10px'
]"
>
<div :class="[`${prefixCls}-header__back`, 'flex pl-10px pr-10px ']">
<el-button @click="emit('back')">
<Icon icon="ep:arrow-left" class="mr-5px" />
{{ t('common.back') }}
</el-button>
</div>
<div :class="[`${prefixCls}-header__title`, 'flex flex-1 justify-center']">
<slot name="title">
<label class="text-16px font-700">{{ title }}</label>
</slot>
</div>
<div :class="[`${prefixCls}-header__right`, 'flex pl-10px pr-10px']">
<slot name="right"></slot>
</div>
<div :class="[`${prefixCls}-container`, 'relative']">
<ElCard :class="[`${prefixCls}-body`, 'mb-20px']" shadow="never">
<div class="mb-20px pb-20px" style="border-bottom: 1px solid var(--el-border-color)">
<slot name="header"></slot>
</div>
</Sticky>
<div style="padding: var(--app-content-padding)">
<ElCard :class="[`${prefixCls}-body`, 'mb-20px']" shadow="never">
<div>
<slot></slot>
</div>
</ElCard>
</div>
<slot></slot>
</ElCard>
</div>
</template>
2 changes: 1 addition & 1 deletion src/components/Descriptions/src/Descriptions.vue
Expand Up @@ -113,7 +113,7 @@ export default defineComponent({
label: () => (item.slots?.label ? item.slots?.label(item) : item.label),
default: () =>
item.slots?.default
? item.slots?.default(item)
? item.slots?.default(props.data)
: props.data[item.field]
}}
</ElDescriptionsItem>
Expand Down
2 changes: 1 addition & 1 deletion src/components/Form/src/Form.vue
Expand Up @@ -111,7 +111,7 @@ export default defineComponent({
const formItemComponents = ref({})
// 表单数据
const formModel = ref<Recordable>({})
const formModel = ref<Recordable>(props.model)
onMounted(() => {
emit('register', unref(elFormRef)?.$parent, unref(elFormRef))
Expand Down
4 changes: 2 additions & 2 deletions src/components/Search/src/Search.vue
Expand Up @@ -48,7 +48,7 @@ const emit = defineEmits(['search', 'reset', 'register', 'validate'])
const visible = ref(true)
// 表单数据
const formModel = ref<Recordable>({})
const formModel = ref<Recordable>(props.model)
const newSchema = computed(() => {
const propsComputed = unref(getProps)
Expand Down Expand Up @@ -179,7 +179,7 @@ const setSchema = (schemaProps: FormSetProps[]) => {
// 对表单赋值
const setValues = async (data: Recordable = {}) => {
formModel.value = Object.assign(unref(formModel), data)
formModel.value = Object.assign(props.model, unref(formModel), data)
const formExpose = await getFormExpose()
formExpose?.setValues(data)
}
Expand Down
150 changes: 32 additions & 118 deletions src/hooks/web/useCrudSchemas.ts
@@ -1,12 +1,8 @@
import { reactive } from 'vue'
import { eachTree, treeMap, filter } from '@/utils/tree'
import { findIndex } from '@/utils'
import { useDictStoreWithOut } from '@/store/modules/dict'
import { useI18n } from '@/hooks/web/useI18n'
import type { AxiosPromise } from 'axios'
import { FormSchema } from '@/types/form'
import { TableColumn } from '@/types/table'
import { DescriptionsSchema } from '@/types/descriptions'
import { FormSchema } from '@/components/Form'
import { TableColumn } from '@/components/Table'
import { DescriptionsSchema } from '@/components/Descriptions'

export type CrudSchema = Omit<TableColumn, 'children'> & {
search?: CrudSearchParams
Expand All @@ -16,39 +12,25 @@ export type CrudSchema = Omit<TableColumn, 'children'> & {
children?: CrudSchema[]
}

type CrudSearchParams = {
interface CrudSearchParams extends Omit<FormSchema, 'field'> {
// 是否显示在查询项
show?: boolean
// 字典名称,会去取全局的字典
dictName?: string
// 接口
api?: () => Promise<any>
// 搜索字段
field?: string
} & Omit<FormSchema, 'field'>

type CrudTableParams = {
}

interface CrudTableParams extends Omit<TableColumn, 'field'> {
// 是否显示表头
show?: boolean
} & Omit<FormSchema, 'field'>
}

type CrudFormParams = {
// 字典名称,会去取全局的字典
dictName?: string
// 接口
api?: () => Promise<any>
interface CrudFormParams extends Omit<FormSchema, 'field'> {
// 是否显示表单项
show?: boolean
} & Omit<FormSchema, 'field'>
}

type CrudDescriptionsParams = {
interface CrudDescriptionsParams extends Omit<DescriptionsSchema, 'field'> {
// 是否显示表单项
show?: boolean
} & Omit<DescriptionsSchema, 'field'>

const dictStore = useDictStoreWithOut()

const { t } = useI18n()
}

interface AllSchemas {
searchSchema: FormSchema[]
Expand All @@ -71,13 +53,14 @@ export const useCrudSchemas = (
detailSchema: []
})

const searchSchema = filterSearchSchema(crudSchema, allSchemas)
const searchSchema = filterSearchSchema(crudSchema)
// @ts-ignore
allSchemas.searchSchema = searchSchema || []

const tableColumns = filterTableSchema(crudSchema)
allSchemas.tableColumns = tableColumns || []

const formSchema = filterFormSchema(crudSchema, allSchemas)
const formSchema = filterFormSchema(crudSchema)
allSchemas.formSchema = formSchema

const detailSchema = filterDescriptionsSchema(crudSchema)
Expand All @@ -89,55 +72,26 @@ export const useCrudSchemas = (
}

// 过滤 Search 结构
const filterSearchSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
const filterSearchSchema = (crudSchema: CrudSchema[]): FormSchema[] => {
const searchSchema: FormSchema[] = []
const length = crudSchema.length

// 获取字典列表队列
const searchRequestTask: Array<() => Promise<void>> = []

eachTree(crudSchema, (schemaItem: CrudSchema) => {
for (let i = 0; i < length; i++) {
const schemaItem = crudSchema[i]
// 判断是否显示
if (schemaItem?.search?.show) {
const searchSchemaItem = {
// 默认为 input
component: schemaItem.search.component || 'Input',
componentProps: {},
component: schemaItem.search.component,
...schemaItem.search,
field: schemaItem?.search?.field || schemaItem.field,
label: schemaItem.search?.label || schemaItem.label
}

if (searchSchemaItem.dictName) {
// 如果有 dictName 则证明是从字典中获取数据
const dictArr = dictStore.getDictObj[searchSchemaItem.dictName]
searchSchemaItem.componentProps!.options = filterOptions(dictArr)
} else if (searchSchemaItem.api) {
searchRequestTask.push(async () => {
const res = await (searchSchemaItem.api as () => AxiosPromise)()
if (res) {
const index = findIndex(allSchemas.searchSchema, (v: FormSchema) => {
return v.field === searchSchemaItem.field
})
if (index !== -1) {
allSchemas.searchSchema[index]!.componentProps!.options = filterOptions(
res,
searchSchemaItem.componentProps.optionsAlias?.labelField
)
}
}
})
field: schemaItem.field,
label: schemaItem.label
}

// 删除不必要的字段
delete searchSchemaItem.show
delete searchSchemaItem.dictName

searchSchema.push(searchSchemaItem)
}
})

for (const task of searchRequestTask) {
task()
}

return searchSchema
Expand Down Expand Up @@ -166,56 +120,28 @@ const filterTableSchema = (crudSchema: CrudSchema[]): TableColumn[] => {
}

// 过滤 form 结构
const filterFormSchema = (crudSchema: CrudSchema[], allSchemas: AllSchemas): FormSchema[] => {
const filterFormSchema = (crudSchema: CrudSchema[]): FormSchema[] => {
const formSchema: FormSchema[] = []
const length = crudSchema.length

// 获取字典列表队列
const formRequestTask: Array<() => Promise<void>> = []

eachTree(crudSchema, (schemaItem: CrudSchema) => {
for (let i = 0; i < length; i++) {
const formItem = crudSchema[i]
// 判断是否显示
if (schemaItem?.form?.show !== false) {
if (formItem?.form?.show) {
const formSchemaItem = {
// 默认为 input
component: schemaItem?.form?.component || 'Input',
componentProps: {},
...schemaItem.form,
field: schemaItem.field,
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
)
}
}
})
component: formItem.form.component,
...formItem.form,
field: formItem.field,
label: formItem.label
}

// 删除不必要的字段
delete formSchemaItem.show
delete formSchemaItem.dictName

formSchema.push(formSchemaItem)
}
})

for (const task of formRequestTask) {
task()
}

return formSchema
}

Expand All @@ -241,15 +167,3 @@ const filterDescriptionsSchema = (crudSchema: CrudSchema[]): DescriptionsSchema[

return descriptionsSchema
}

// 给options添加国际化
const filterOptions = (options: Recordable, labelField?: string) => {
return options?.map((v: Recordable) => {
if (labelField) {
v['labelField'] = t(v.labelField)
} else {
v['label'] = t(v.label)
}
return v
})
}

0 comments on commit 9a0259d

Please sign in to comment.