字典类型 CRUD

This commit is contained in:
2024-10-31 14:53:29 +08:00
parent b452636c37
commit b207bc0e68
26 changed files with 386 additions and 279 deletions

View File

@@ -0,0 +1,282 @@
<template>
<el-dialog class='table-box' v-model='dialogVisible1' top='114px'
:style="{ height: height, maxHeight: height, overflow: 'hidden' }" :title='title1' :width='width' :modal='false'>
<div class="table-box">
<ProTable :columns="columns" :data="data">
<template #tableHeader="scope">
<el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增')">新增</el-button>
<el-button type="primary" :icon="Upload" plain @click="batchAdd">批量添加</el-button>
<el-button type="primary" :icon="Download" plain @click="downloadFile">导出</el-button>
<el-button type="danger" :icon="Delete" plain :disabled="!scope.isSelected"
@click="batchDelete(scope.selectedListIds)">
批量删除
</el-button>
</template>
<template #operation="scope">
<el-button type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)">编辑</el-button>
<el-button type="primary" link :icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
</template>
</ProTable>
</div>
</el-dialog>
<el-dialog v-model="dialogVisible2" :title="dialogType2 === '新增' ? '新增字典数据' : '编辑字典数据'" v-bind="dialogSmall">
<div>
<el-form :model="dialogForm" ref="dialogFormRef" :rules="rules">
<el-form-item label="字典类型编码" :label-width="100">
<el-input :value="route.params.code" disabled />
</el-form-item>
<el-form-item label="名称" :label-width="100" prop="name">
<el-input v-model="dialogForm.name" placeholder="请输入" autocomplete="off" />
</el-form-item>
<el-form-item label="编码" :label-width="100" prop="code">
<el-input v-model="dialogForm.code" placeholder="请输入" autocomplete="off" />
</el-form-item>
<el-form-item label="值" :label-width="100" prop="value">
<el-input v-model="dialogForm.value" placeholder="请输入" autocomplete="off" />
</el-form-item>
<el-form-item label="显示排序" :label-width="100">
<el-input-number v-model="dialogForm.sort" />
</el-form-item>
</el-form>
</div>
<template #footer>
<div class="dialog-footer">
<el-button @click="close()">取消</el-button>
<el-button type="primary" @click="save()">
保存
</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup lang="tsx" name="dictData">
import { useRoute } from 'vue-router'
import { CirclePlus, Delete, EditPen, Download, Upload } from '@element-plus/icons-vue'
import { Dict } from '@/api/system/dictionary/interface'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import ImportExcel from '@/components/ImportExcel/index.vue'
import { dialogSmall } from '@/utils/elementBind'
import { useDictStore } from '@/stores/modules/dict'
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { dictDataList } from '@/api/system/dictionary/dictExample'
import { FormItemRule } from 'element-plus'
import {
getDictDataList,
updateDictData,
addDictData,
batchAddDictData,
exportDictData,
deleteDictData
} from '@/api/system/dictionary'
const route = useRoute()
const dictStore = useDictStore()
const data = dictDataList
const proTable = ref<ProTableInstance>()
const dictTypeId = ref('')
const dialogVisible1 = ref(false)
const title1 = ref('')
const dialogFormRef = ref()
const columns = reactive<ColumnProps<Dict.ResDictData>[]>([
{ type: 'selection', fixed: 'left', width: 70 },
{ type: 'index', fixed: 'left', width: 70, label: '序号' },
{
prop: 'name',
label: '名称',
width: 180,
search: {
el: 'input'
}
},
{
prop: 'code',
label: '编码',
width: 180,
search: {
el: 'input'
}
},
{
prop: 'value',
label: '值',
width: 180
},
{
prop: 'level',
label: '事件等级',
width: 120,
render: scope => {
return (
<>
{scope.row.level === 0 || scope.row.level === null || scope.row.level === undefined ?
(<span></span>) :
(<el-tag type={scope.row.level === 1 ? 'info' : scope.row.level === 2 ? 'warning' : 'danger'}>
{scope.row.level === 1 ? '普通' : scope.row.level === 2 ? '警告' : '危险'}
</el-tag>)
}
</>
)
}
},
{
prop: 'state',
label: '状态',
minWidth: 100,
enum: dictStore.getDictData('status'),
search: {
el: 'tree-select',
props: { filterable: true }
},
fieldNames: { label: 'label', value: 'code' },
render: scope => {
return (
<>
{
<el-tag type={scope.row.state ? 'success' : 'danger'} > {scope.row.state ? '正常' : '禁用'} </el-tag>
}
</>
)
},
},
{
prop: 'createTime',
label: '创建时间',
width: 180,
search: {
el: 'date-picker',
props: { type: 'daterange', valueFormat: 'YYYY-MM-DD' }
}
},
{
prop: 'operation',
label: '操作',
fixed: 'right',
width: 330
},
])
const rules: Ref<Record<string, Array<FormItemRule>>> = ref({
name: [{ required: true, message: '类型名称必填!', trigger: 'blur' }],
code: [{ required: true, message: '类型编码必填!', trigger: 'blur' }],
value: [{ required: true, message: '类型值必填!', trigger: 'blur' }],
})
const { dialogVisible2, dialogType2, dialogForm } = useCount();
function useCount() {
const dialogVisible2 = ref(false)
const dialogType2 = ref('新增')
const dialogForm = ref({
id: "",
name: "",
code: "",
value: "",
sort: 100,
})
return { dialogVisible2, dialogType2, dialogForm };
}
const props = defineProps({
width: {
type: String,
default: '800px',
},
height: {
type: String,
default: '744px',
},
})
const open = (textTitle: string, id: string) => {
dialogVisible1.value = true
title1.value = textTitle
dictTypeId.value = id
// getDictDataList({ dictTypeId: id }).then(res => {
// data.value = res.data
// }
}
defineExpose({ open })
// 打开 drawer(新增、查看、编辑)
const openDrawer = (title: string, row: Partial<Dict.ResDictType> = {}) => {
dialogVisible2.value = true
dialogType2.value = title
if (title === '编辑') {
row && (dialogForm.value = row as { id: string; name: string; code: string; value: string; sort: number; state: number; });
}
}
// 批量添加字典数据
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
const params = {
title: '字典数据',
tempApi: exportDictData,
importApi: batchAddDictData,
getTableList: proTable.value?.getTableList,
}
dialogRef.value?.acceptParams(params)
}
// 导出字典数据
const downloadFile = async () => {
ElMessageBox.confirm('确认导出字典数据?', '温馨提示', { type: 'warning' }).then(() =>
useDownload(exportDictData, '字典数据列表', proTable.value?.searchParam),
)
}
// 批量删除字典数据
const batchDelete = async (id: string[]) => {
await useHandleData(deleteDictData, { id }, '删除所选字典数据')
proTable.value?.clearSelection()
proTable.value?.getTableList()
}
// 删除字典数据
const handleDelete = async (params: Dict.ResDictType) => {
await useHandleData(deleteDictData, { id: [params.id] }, `删除【${params.name}】字典数据`)
proTable.value?.getTableList()
}
const close = () => {
dialogVisible2.value = false
//清空dialogForm中的值
dialogForm.value = {
id: "",
name: "",
code: "",
value: "",
sort: 100
}
dialogFormRef.value?.resetFields()
}
const save = () => {
try {
dialogFormRef.value?.validate(async (valid: boolean) => {
if (valid) {
let params = JSON.stringify(dialogForm.value)
console.log(params)
if (dialogType2.value === '新增') {
await useHandleData(addDictData, dialogForm.value, '新增字典数据')
} else {
await useHandleData(updateDictData, dialogForm.value, '编辑字典数据')
}
close()
proTable.value?.getTableList()
} else {
ElMessage.error('表单验证失败!')
}
})
} catch (err) {
console.error('验证过程中出现错误', err)
}
}
</script>

View File

@@ -0,0 +1,132 @@
<template>
<el-dialog v-model='dialogVisible' :title='dialogTitle' v-bind='dialogSmall'>
<div>
<el-form :model='formContent' ref='dialogFormRef' :rules='rules'>
<el-form-item label='类型名称' :label-width='100' prop='name'>
<el-input v-model='formContent.name' placeholder='请输入' autocomplete='off' />
</el-form-item>
<el-form-item label='类型编码' :label-width='100' prop='code'>
<el-input v-model='formContent.code' placeholder='请输入' autocomplete='off' />
</el-form-item>
<el-form-item label='显示排序' :label-width='100'>
<el-input-number v-model='formContent.sort' />
</el-form-item>
<el-form-item label='备注' :label-width='100'>
<el-input v-model='formContent.remark' placeholder='请输入备注' autocomplete='off' :rows='2'
type='textarea' />
</el-form-item>
</el-form>
</div>
<template #footer>
<div class='dialog-footer'>
<el-button @click='close()'>取消</el-button>
<el-button type='primary' @click='save()'>
保存
</el-button>
</div>
</template>
</el-dialog>
</template>
<script setup lang='ts'>
import { dialogSmall } from '@/utils/elementBind'
import { Dict } from '@/api/system/dictionary/interface'
import { FormItemRule } from 'element-plus'
import { addDictType, updateDictType } from '@/api/system/dictionary/dictType'
// 定义弹出组件元信息
const dialogFormRef = ref()
function useMetaInfo() {
const dialogVisible = ref(false)
const titleType = ref('add')
const formContent = ref<Dict.ResDictType>({
id: '',
name: '',
code: '',
sort: 100,
openLevel: 0,
openDescribe: 0,
remark: '',
state: 1,
})
return { dialogVisible, titleType, formContent }
}
const { dialogVisible, titleType, formContent } = useMetaInfo()
// 清空formContent
const resetFormContent = () => {
formContent.value = {
id: '',
name: '',
code: '',
sort: 100,
openLevel: 0,
openDescribe: 0,
remark: '',
state: 1,
}
}
let dialogTitle = computed(() => {
return titleType.value === 'add' ? '新增字典类型' : '编辑字典类型'
})
// 定义表单校验规则
const rules: Ref<Record<string, Array<FormItemRule>>> = ref({
name: [{ required: true, message: '类型名称必填!', trigger: 'blur' }],
code: [{ required: true, message: '类型编码必填!', trigger: 'blur' }],
})
// 关闭弹窗
const close = () => {
dialogVisible.value = false
// 清空dialogForm中的值
resetFormContent()
// 重置表单
dialogFormRef.value?.resetFields()
}
// 保存数据
const save = () => {
try {
dialogFormRef.value?.validate(async (valid: boolean) => {
if (valid) {
if (formContent.value.id) {
await updateDictType(formContent.value)
} else {
await addDictType(formContent.value)
}
ElMessage.success({ message: `${dialogTitle.value}成功!` })
close()
// 刷新表格
await props.refreshTable!()
} else {
ElMessage.error('表单验证失败!')
}
})
} catch (err) {
console.error('验证过程中出现错误', err)
}
}
// 打开弹窗,可能是新增,也可能是编辑
const open = (sign: string, data: Dict.ResDictType) => {
titleType.value = sign
dialogVisible.value = true
if (data.id) {
formContent.value = { ...data }
} else {
resetFormContent()
}
}
// 对外映射
defineExpose({ open })
const props = defineProps<{
refreshTable: (() => Promise<void>) | undefined;
}>()
</script>

View File

@@ -0,0 +1,141 @@
<template>
<div class='table-box' ref='popupBaseView'>
<ProTable
ref='proTable'
:columns='columns'
:request-api='getDictTypeList'
>
<template #tableHeader='scope'>
<el-button type='primary' :icon='CirclePlus' @click="openDialog('add')">新增</el-button>
<el-button type='primary' :icon='Download' plain>导出</el-button>
<el-button type='danger' :icon='Delete' plain :disabled='!scope.isSelected'
@click='batchDelete(scope.selectedListIds)'>
批量删除
</el-button>
</template>
<template #operation='scope'>
<el-button type='primary' link :icon='View' @click='toDictData(scope.row)'>查看</el-button>
<el-button type='primary' link :icon='EditPen' @click="openDialog('edit', scope.row)">编辑</el-button>
<el-button type='primary' link :icon='Delete' @click='handleDelete(scope.row)'>删除</el-button>
</template>
</ProTable>
</div>
<Data :width='viewWidth' :height='viewHeight' ref='openView' />
<type-popup :refresh-table='proTable?.getTableList' ref='typePopup' />
</template>
<script setup lang='tsx' name='dict'>
import { CirclePlus, Delete, EditPen, Download, View } from '@element-plus/icons-vue'
import { Dict } from '@/api/system/dictionary/interface'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import TypePopup from '@/views/system/dictionary/dictType/components/typePopup.vue'
import Data from '@/views/system/dictionary/data.vue'
import { useDictStore } from '@/stores/modules/dict'
import { useHandleData } from '@/hooks/useHandleData'
import { useViewSize } from '@/hooks/useViewSize'
import {
getDictTypeList,
deleteDictType,
} from '@/api/system/dictionary/dictType'
const dictStore = useDictStore()
const { popupBaseView, viewWidth, viewHeight } = useViewSize()
const proTable = ref<ProTableInstance>()
const typePopup = ref()
const openView = ref()
const columns = reactive<ColumnProps<Dict.ResDictType>[]>([
{ type: 'selection', fixed: 'left', width: 70 },
{ type: 'index', fixed: 'left', width: 70, label: '序号' },
{
prop: 'name',
label: '类型名称',
width: 180,
search: {
el: 'input',
},
},
{
prop: 'code',
label: '类型编码',
width: 180,
search: {
el: 'input',
},
},
{
prop: 'remark',
label: '描述',
width: 340,
},
{
prop: 'state',
label: '状态',
enum: dictStore.getDictData('status'),
search: {
el: 'tree-select',
props: { filterable: true },
},
fieldNames: { label: 'label', value: 'code' },
render: scope => {
return (
<>
{
<el-tag type={scope.row.state ? 'success' : 'danger'}> {scope.row.state ? '正常' : '禁用'} </el-tag>
}
</>
)
},
},
{
prop: 'createTime',
label: '创建时间',
width: 180,
search: {
el: 'date-picker',
props: { type: 'daterange', valueFormat: 'YYYY-MM-DD' },
},
},
{
prop: 'operation',
label: '操作',
fixed: 'right',
width: 330,
},
])
// 打开 drawer(新增、编辑)
const openDialog = (titleType: string, row: Partial<Dict.ResDictType> = {}) => {
typePopup.value?.open(titleType, row)
}
// 导出字典类型
// const downloadFile = async () => {
// ElMessageBox.confirm('确认导出字典类型数据?', '温馨提示', { type: 'warning' }).then(() =>
// useDownload(exportDictType, '字典类型列表', proTable.value?.searchParam),
// )
// }
// 批量删除字典类型
const batchDelete = async (id: string[]) => {
await useHandleData(deleteDictType, id, '删除所选字典类型')
proTable.value?.clearSelection()
proTable.value?.getTableList()
}
// 删除字典类型
const handleDelete = async (params: Dict.ResDictType) => {
await useHandleData(deleteDictType, [params.id], `删除【${params.name}】字典类型`)
proTable.value?.getTableList()
}
//查看字典类型包含的字典数据
const toDictData = (row: Dict.ResDictType) => {
openView.value.open('字典数据', row.id)
}
</script>