ipms-sjy/yudao-ui-admin-vue3/src/hooks/web/useVxeCrudSchemas.ts

300 lines
9.1 KiB
TypeScript
Raw Normal View History

2022-11-03 14:33:30 +08:00
import { DescriptionsSchema } from '@/types/descriptions'
import { getIntDictOptions } from '@/utils/dict'
import { reactive } from 'vue'
import {
FormItemRenderOptions,
2022-11-07 18:13:04 +08:00
VxeColumnPropTypes,
2022-11-03 14:33:30 +08:00
VxeFormItemProps,
VxeGridPropTypes,
VxeTableDefines
} from 'vxe-table'
import { eachTree } from 'xe-utils'
import { useI18n } from '@/hooks/web/useI18n'
import { VxeTableColumn } from '@/types/table'
2022-11-07 18:13:04 +08:00
import { FormSchema } from '@/types/form'
import { ComponentOptions } from '@/types/components'
2022-11-03 14:33:30 +08:00
2022-11-12 15:52:43 +08:00
export type VxeCrudSchema = {
2022-11-16 10:28:28 +08:00
primaryKey?: string // 主键ID
2022-11-16 12:34:08 +08:00
primaryTitle?: string // 主键标题 默认为序号
2022-11-16 10:28:28 +08:00
primaryType?: VxeColumnPropTypes.Type // 不填写为数据库编号 还支持 "seq" | "radio" | "checkbox" | "expand" | "html" | null
action?: boolean // 是否开启操作栏插槽
2022-11-16 12:34:08 +08:00
actionTitle?: string // 操作栏标题 默认为操作
actionWidth?: string // 操作栏插槽宽度一般2个字带图标 text 类型按钮 50-70
2022-11-12 15:52:43 +08:00
columns: VxeCrudColumns[]
}
type VxeCrudColumns = Omit<VxeTableColumn, 'children'> & {
2022-11-14 13:19:33 +08:00
field: string // 字段名
title?: string // 标题名
formatter?: VxeColumnPropTypes.Formatter // vxe formatter格式化
isSearch?: boolean // 是否在查询显示
search?: CrudSearchParams // 查询的详细配置
isTable?: boolean // 是否在列表显示
table?: CrudTableParams // 列表的详细配置
isForm?: boolean // 是否在表单显示
form?: CrudFormParams // 表单的详细配置
isDetail?: boolean // 是否在详情显示
detail?: CrudDescriptionsParams // 详情的详细配置
print?: CrudPrintParams // vxe 打印的字段
children?: VxeCrudColumns[] // 子级
dictType?: string // 字典类型
2022-11-03 14:33:30 +08:00
}
2022-11-12 23:33:29 +08:00
type CrudSearchParams = {
// 是否显示在查询项
show?: boolean
} & Omit<VxeFormItemProps, 'field'>
type CrudTableParams = {
// 是否显示表头
show?: boolean
} & Omit<VxeTableDefines.ColumnOptions, 'field'>
type CrudFormParams = {
// 是否显示表单项
show?: boolean
} & Omit<FormSchema, 'field'>
type CrudDescriptionsParams = {
// 是否显示表单项
show?: boolean
} & Omit<DescriptionsSchema, 'field'>
2022-11-07 14:58:53 +08:00
type CrudPrintParams = {
// 是否显示表单项
show?: boolean
} & Omit<VxeTableDefines.ColumnInfo[], 'field'>
2022-11-07 18:13:04 +08:00
export type VxeAllSchemas = {
2022-11-03 14:33:30 +08:00
searchSchema: VxeFormItemProps[]
tableSchema: VxeGridPropTypes.Columns
2022-11-07 18:13:04 +08:00
formSchema: FormSchema[]
2022-11-03 14:33:30 +08:00
detailSchema: DescriptionsSchema[]
printSchema: VxeTableDefines.ColumnInfo[]
}
// 过滤所有结构
export const useVxeCrudSchemas = (
2022-11-12 15:52:43 +08:00
crudSchema: VxeCrudSchema
2022-11-03 14:33:30 +08:00
): {
allSchemas: VxeAllSchemas
} => {
// 所有结构数据
const allSchemas = reactive<VxeAllSchemas>({
searchSchema: [],
tableSchema: [],
formSchema: [],
detailSchema: [],
printSchema: []
})
const searchSchema = filterSearchSchema(crudSchema)
allSchemas.searchSchema = searchSchema || []
const tableSchema = filterTableSchema(crudSchema)
allSchemas.tableSchema = tableSchema || []
const formSchema = filterFormSchema(crudSchema)
allSchemas.formSchema = formSchema
const detailSchema = filterDescriptionsSchema(crudSchema)
allSchemas.detailSchema = detailSchema
const printSchema = filterPrintSchema(crudSchema)
allSchemas.printSchema = printSchema
return {
allSchemas
}
}
// 过滤 Search 结构
2022-11-12 15:52:43 +08:00
const filterSearchSchema = (crudSchema: VxeCrudSchema): VxeFormItemProps[] => {
2022-11-03 14:33:30 +08:00
const { t } = useI18n()
2022-11-12 16:52:02 +08:00
const searchSchema: VxeFormItemProps[] = []
2022-11-12 15:52:43 +08:00
eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
2022-11-03 14:33:30 +08:00
// 判断是否显示
2022-11-15 20:12:32 +08:00
if (schemaItem?.isSearch || schemaItem.search?.show) {
2022-11-03 14:33:30 +08:00
let itemRenderName = schemaItem?.search?.itemRender?.name || '$input'
const options: any[] = []
let itemRender: FormItemRenderOptions = {
name: itemRenderName,
props: { placeholder: t('common.inputText') }
}
if (schemaItem.dictType) {
const allOptions = { label: '全部', value: '' }
options.push(allOptions)
getIntDictOptions(schemaItem.dictType).forEach((dict) => {
options.push(dict)
})
itemRender.options = options
2022-11-12 17:08:23 +08:00
if (!schemaItem?.search?.itemRender?.name) itemRenderName = '$select'
2022-11-03 14:33:30 +08:00
itemRender = {
name: itemRenderName,
options: options,
props: { placeholder: t('common.selectText') }
}
}
const searchSchemaItem = {
// 默认为 input
2022-11-10 14:38:16 +08:00
folding: searchSchema.length > 2,
2022-11-12 16:52:02 +08:00
itemRender: schemaItem.itemRender ? schemaItem.itemRender : itemRender,
2022-11-03 14:33:30 +08:00
field: schemaItem.field,
2022-11-12 16:46:24 +08:00
title: schemaItem.search?.title || schemaItem.title,
span: 8
2022-11-03 14:33:30 +08:00
}
searchSchema.push(searchSchemaItem)
}
})
// 添加搜索按钮
const buttons: VxeFormItemProps = {
span: 24,
align: 'center',
2022-11-10 14:38:16 +08:00
collapseNode: searchSchema.length > 3,
2022-11-03 14:33:30 +08:00
itemRender: {
name: '$buttons',
children: [
{ props: { type: 'submit', content: t('common.query'), status: 'primary' } },
{ props: { type: 'reset', content: t('common.reset') } }
]
}
}
searchSchema.push(buttons)
return searchSchema
}
// 过滤 table 结构
2022-11-12 15:52:43 +08:00
const filterTableSchema = (crudSchema: VxeCrudSchema): VxeGridPropTypes.Columns => {
const { t } = useI18n()
2022-11-03 14:33:30 +08:00
const tableSchema: VxeGridPropTypes.Columns = []
2022-11-12 15:52:43 +08:00
// 主键ID
if (crudSchema.primaryKey) {
const tableSchemaItem = {
2022-11-16 12:34:08 +08:00
title: crudSchema.primaryTitle ? crudSchema.primaryTitle : t('common.index'),
2022-11-12 15:52:43 +08:00
field: crudSchema.primaryKey,
2022-11-16 10:28:28 +08:00
type: crudSchema.primaryType ? crudSchema.primaryType : null,
2022-11-12 15:52:43 +08:00
width: '50px'
}
tableSchema.push(tableSchemaItem)
}
eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
2022-11-03 14:33:30 +08:00
// 判断是否显示
2022-11-12 17:08:23 +08:00
if (schemaItem?.isTable !== false) {
2022-11-03 14:33:30 +08:00
const tableSchemaItem = {
...schemaItem.table,
field: schemaItem.field,
title: schemaItem.table?.title || schemaItem.title
}
2022-11-12 17:33:48 +08:00
tableSchemaItem.showOverflow = 'tooltip'
2022-11-07 18:13:04 +08:00
if (schemaItem?.formatter) {
tableSchemaItem.formatter = schemaItem.formatter
2022-11-16 17:03:32 +08:00
tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
2022-11-07 18:13:04 +08:00
}
2022-11-12 15:52:43 +08:00
if (schemaItem?.dictType) {
tableSchemaItem.cellRender = {
name: 'XDict',
content: schemaItem.dictType
}
2022-11-16 17:03:32 +08:00
tableSchemaItem.width = tableSchemaItem.width ? tableSchemaItem.width : 160
2022-11-12 15:52:43 +08:00
}
2022-11-03 14:33:30 +08:00
tableSchema.push(tableSchemaItem)
}
})
2022-11-12 15:52:43 +08:00
// 操作栏插槽
if (crudSchema.action && crudSchema.action == true) {
const tableSchemaItem = {
2022-11-16 12:34:08 +08:00
title: crudSchema.actionTitle ? crudSchema.actionTitle : t('table.action'),
2022-11-12 15:52:43 +08:00
field: 'actionbtns',
2022-11-15 13:59:22 +08:00
width: crudSchema.actionWidth ? crudSchema.actionWidth : '200px',
2022-11-12 15:52:43 +08:00
slots: {
default: 'actionbtns_default'
}
}
tableSchema.push(tableSchemaItem)
}
2022-11-03 14:33:30 +08:00
return tableSchema
}
// 过滤 form 结构
2022-11-12 15:52:43 +08:00
const filterFormSchema = (crudSchema: VxeCrudSchema): FormSchema[] => {
2022-11-07 18:13:04 +08:00
const formSchema: FormSchema[] = []
2022-11-12 15:52:43 +08:00
eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
2022-11-03 14:33:30 +08:00
// 判断是否显示
2022-11-16 16:56:38 +08:00
if (schemaItem?.isForm !== false || schemaItem?.form?.show == true) {
2022-11-14 13:19:33 +08:00
// 默认为 input
2022-11-07 18:13:04 +08:00
let component = schemaItem?.form?.component || 'Input'
const options: ComponentOptions[] = []
let comonentProps = {}
2022-11-03 14:33:30 +08:00
if (schemaItem.dictType) {
2022-11-07 18:13:04 +08:00
getIntDictOptions(schemaItem.dictType).forEach((dict) => {
options.push(dict)
})
comonentProps = {
options: options
2022-11-03 14:33:30 +08:00
}
2022-11-15 13:59:22 +08:00
if (!(schemaItem.form && schemaItem.form.component)) component = 'Select'
2022-11-03 14:33:30 +08:00
}
const formSchemaItem = {
...schemaItem.form,
field: schemaItem.field,
2022-11-14 13:19:33 +08:00
label: schemaItem.form?.label || schemaItem.title,
component: component,
componentProps: comonentProps
2022-11-03 14:33:30 +08:00
}
formSchema.push(formSchemaItem)
}
})
return formSchema
}
// 过滤 descriptions 结构
2022-11-12 15:52:43 +08:00
const filterDescriptionsSchema = (crudSchema: VxeCrudSchema): DescriptionsSchema[] => {
2022-11-03 14:33:30 +08:00
const descriptionsSchema: DescriptionsSchema[] = []
2022-11-12 15:52:43 +08:00
eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
2022-11-03 14:33:30 +08:00
// 判断是否显示
2022-11-12 17:08:23 +08:00
if (schemaItem?.isDetail !== false) {
2022-11-03 14:33:30 +08:00
const descriptionsSchemaItem = {
...schemaItem.detail,
field: schemaItem.field,
label: schemaItem.detail?.label || schemaItem.title
}
2022-11-12 17:33:48 +08:00
if (schemaItem.dictType) {
descriptionsSchemaItem.dictType = schemaItem.dictType
}
if (schemaItem.detail?.dateFormat || schemaItem.formatter == 'formatDate') {
2022-11-15 13:59:22 +08:00
descriptionsSchemaItem.dateFormat = schemaItem.dateFormat
? schemaItem?.detail?.dateFormat
: 'YYYY-MM-DD HH:mm:ss'
2022-11-12 17:33:48 +08:00
}
2022-11-03 14:33:30 +08:00
descriptionsSchema.push(descriptionsSchemaItem)
}
})
return descriptionsSchema
}
// 过滤 打印 结构
2022-11-12 15:52:43 +08:00
const filterPrintSchema = (crudSchema: VxeCrudSchema): any[] => {
2022-11-03 14:33:30 +08:00
const printSchema: any[] = []
2022-11-12 15:52:43 +08:00
eachTree(crudSchema.columns, (schemaItem: VxeCrudColumns) => {
2022-11-03 14:33:30 +08:00
// 判断是否显示
2022-11-07 14:58:53 +08:00
if (schemaItem?.print?.show !== false) {
2022-11-03 14:33:30 +08:00
const printSchemaItem = {
field: schemaItem.field
}
printSchema.push(printSchemaItem)
}
})
return printSchema
}