272 lines
8.4 KiB
Vue
Raw Normal View History

2022-11-10 14:38:16 +08:00
<template>
<div class="flex">
<el-card class="w-1/2 dict" :gutter="12" shadow="always">
<template #header>
<div class="card-header">
<span>字典分类</span>
</div>
</template>
2022-11-22 14:34:21 +08:00
<vxe-grid
ref="xTypeGrid"
v-bind="typeGridOptions"
@cell-click="cellClickEvent"
class="xtable-scrollbar"
2022-11-10 14:38:16 +08:00
>
2022-11-22 14:34:21 +08:00
<template #toolbar_buttons>
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:dict:create']"
@click="handleTypeCreate()"
/>
2022-11-10 14:38:16 +08:00
</template>
2022-11-22 14:34:21 +08:00
<template #actionbtns_default="{ row }">
2022-11-10 14:38:16 +08:00
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
v-hasPermi="['system:dict:update']"
2022-11-22 14:34:21 +08:00
@click="handleTypeUpdate(row.id)"
2022-11-10 14:38:16 +08:00
/>
<XTextButton
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['system:dict:delete']"
2022-11-22 14:34:21 +08:00
@click="handleTypeDelete(row.id)"
2022-11-10 14:38:16 +08:00
/>
</template>
2022-11-22 14:34:21 +08:00
</vxe-grid>
2022-11-10 14:38:16 +08:00
</el-card>
<el-card class="w-1/2 dict" style="margin-left: 10px" :gutter="12" shadow="hover">
<template #header>
<div class="card-header">
<span>字典数据</span>
</div>
</template>
<!-- 列表 -->
<div v-if="!tableTypeSelect">
<span>请从左侧选择</span>
</div>
<div v-if="tableTypeSelect">
2022-11-22 14:34:21 +08:00
<!-- 列表 -->
<vxe-grid ref="xDataGrid" v-bind="dataGridOptions" class="xtable-scrollbar">
<template #toolbar_buttons>
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:dict:create']"
@click="handleDataCreate()"
/>
2022-11-10 14:38:16 +08:00
</template>
2022-11-22 14:34:21 +08:00
<template #actionbtns_default="{ row }">
2022-11-10 14:38:16 +08:00
<XTextButton
v-hasPermi="['system:dict:update']"
preIcon="ep:edit"
:title="t('action.edit')"
2022-11-22 14:34:21 +08:00
@click="handleDataUpdate(row.id)"
2022-11-10 14:38:16 +08:00
/>
<XTextButton
v-hasPermi="['system:dict:delete']"
preIcon="ep:delete"
:title="t('action.del')"
2022-11-22 14:34:21 +08:00
@click="handleDataDelete(row.id)"
2022-11-10 14:38:16 +08:00
/>
</template>
2022-11-22 14:34:21 +08:00
</vxe-grid>
2022-11-10 14:38:16 +08:00
</div>
</el-card>
2022-11-15 12:25:19 +08:00
<XModal id="dictModel" v-model="dialogVisible" :title="dialogTitle">
2022-11-14 09:15:11 +08:00
<Form
v-if="['typeCreate', 'typeUpdate'].includes(actionType)"
:schema="DictTypeSchemas.allSchemas.formSchema"
:rules="DictTypeSchemas.dictTypeRules"
ref="typeFormRef"
2022-11-17 11:24:51 +08:00
>
<template #type>
<template v-if="dictTypeValue">
<el-tag>{{ dictTypeValue }}</el-tag>
</template>
<template v-else><el-input v-model="dictTypeValue" /> </template>
</template>
</Form>
2022-11-14 09:15:11 +08:00
<Form
v-if="['dataCreate', 'dataUpdate'].includes(actionType)"
:schema="DictDataSchemas.allSchemas.formSchema"
:rules="DictDataSchemas.dictDataRules"
ref="dataFormRef"
/>
2022-11-10 14:38:16 +08:00
<!-- 操作按钮 -->
<template #footer>
<XButton
v-if="['typeCreate', 'typeUpdate'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitTypeForm"
/>
<XButton
v-if="['dataCreate', 'dataUpdate'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitDataForm"
/>
<XButton :title="t('dialog.close')" @click="dialogVisible = false" />
</template>
2022-11-15 12:25:19 +08:00
</XModal>
2022-11-10 14:38:16 +08:00
</div>
</template>
2022-11-23 22:26:25 +08:00
<script setup lang="ts" name="Dict">
2022-11-22 14:34:21 +08:00
import { ref, unref, reactive } from 'vue'
2022-07-18 19:06:37 +08:00
import { useI18n } from '@/hooks/web/useI18n'
2022-11-22 14:34:21 +08:00
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeGridInstance, VxeTableEvents } from 'vxe-table'
2022-07-18 19:06:37 +08:00
import { FormExpose } from '@/components/Form'
2022-11-22 14:34:21 +08:00
import { ElInput, ElTag, ElCard } from 'element-plus'
2022-07-18 19:06:37 +08:00
import * as DictTypeSchemas from './dict.type'
import * as DictDataSchemas from './dict.data'
import * as DictTypeApi from '@/api/system/dict/dict.type'
import * as DictDataApi from '@/api/system/dict/dict.data'
import { DictDataVO, DictTypeVO } from '@/api/system/dict/types'
2022-11-22 14:34:21 +08:00
2022-07-18 19:06:37 +08:00
const { t } = useI18n() // 国际化
2022-11-22 14:34:21 +08:00
const message = useMessage() // 消息弹窗
const xTypeGrid = ref<VxeGridInstance>() // 列表 Grid Ref
2022-07-18 19:06:37 +08:00
const {
2022-11-22 14:34:21 +08:00
gridOptions: typeGridOptions,
getList: typeGetList,
deleteData: typeDeleteData
} = useVxeGrid<DictTypeVO>({
allSchemas: DictTypeSchemas.allSchemas,
2022-07-18 19:06:37 +08:00
getListApi: DictTypeApi.getDictTypePageApi,
2022-11-22 14:34:21 +08:00
deleteApi: DictTypeApi.deleteDictTypeApi
})
const xDataGrid = ref<VxeGridInstance>() // 列表 Grid Ref
const queryParams = reactive({
dictType: null
2022-07-18 19:06:37 +08:00
})
const {
2022-11-22 14:34:21 +08:00
gridOptions: dataGridOptions,
getList: dataGetList,
deleteData: dataDeleteData
} = useVxeGrid<DictDataVO>({
allSchemas: DictDataSchemas.allSchemas,
queryParams: queryParams,
getListApi: DictDataApi.getDictDataPageApi,
deleteApi: DictDataApi.deleteDictDataApi
})
// ========== 字典分类列表相关 ==========
2022-11-17 11:24:51 +08:00
const dictTypeValue = ref('')
2022-07-18 19:06:37 +08:00
// 字典分类修改操作
const handleTypeCreate = () => {
2022-11-17 11:24:51 +08:00
dictTypeValue.value = ''
2022-07-18 19:06:37 +08:00
setDialogTile('typeCreate')
}
2022-11-22 14:34:21 +08:00
const handleTypeUpdate = async (rowId: number) => {
2022-07-18 19:06:37 +08:00
setDialogTile('typeUpdate')
// 设置数据
2022-11-22 14:34:21 +08:00
const res = await DictTypeApi.getDictTypeApi(rowId)
2022-11-17 11:24:51 +08:00
dictTypeValue.value = res.type
2022-07-18 19:06:37 +08:00
unref(typeFormRef)?.setValues(res)
}
// 字典数据修改操作
const handleDataCreate = () => {
setDialogTile('dataCreate')
}
2022-11-22 14:34:21 +08:00
const handleDataUpdate = async (rowId: number) => {
2022-07-18 19:06:37 +08:00
setDialogTile('dataUpdate')
// 设置数据
2022-11-22 14:34:21 +08:00
const res = await DictDataApi.getDictDataApi(rowId)
2022-07-18 19:06:37 +08:00
unref(dataFormRef)?.setValues(res)
}
// 字典分类点击行事件
const parentType = ref('')
2022-11-22 14:34:21 +08:00
const tableTypeSelect = ref(false)
const cellClickEvent: VxeTableEvents.CellClick = async ({ row }) => {
2022-07-18 19:06:37 +08:00
tableTypeSelect.value = true
2022-11-22 14:34:21 +08:00
queryParams.dictType = row['type']
await dataGetList(xDataGrid)
parentType.value = row['type']
2022-07-18 19:06:37 +08:00
}
// 弹出框
const dialogVisible = ref(false) // 是否显示弹出层
const dialogTitle = ref('edit') // 弹出层标题
const actionLoading = ref(false) // 遮罩层
const typeFormRef = ref<FormExpose>() // 分类表单 Ref
const dataFormRef = ref<FormExpose>() // 数据表单 Ref
const actionType = ref('') // 操作按钮的类型
2022-11-17 11:24:51 +08:00
2022-07-18 19:06:37 +08:00
// 设置标题
const setDialogTile = (type: string) => {
dialogTitle.value = t('action.' + type)
actionType.value = type
dialogVisible.value = true
}
2022-11-22 14:34:21 +08:00
// 删除操作
const handleTypeDelete = async (rowId: number) => {
await typeDeleteData(xTypeGrid, rowId)
}
const handleDataDelete = async (rowId: number) => {
await dataDeleteData(xDataGrid, rowId)
}
2022-07-18 19:06:37 +08:00
// 提交按钮
const submitTypeForm = async () => {
2022-10-17 11:24:22 +08:00
const elForm = unref(typeFormRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
actionLoading.value = true
// 提交请求
try {
const data = unref(typeFormRef)?.formModel as DictTypeVO
if (actionType.value === 'typeCreate') {
2022-11-17 11:24:51 +08:00
data.type = dictTypeValue.value
2022-10-17 11:24:22 +08:00
await DictTypeApi.createDictTypeApi(data)
2022-11-22 14:34:21 +08:00
message.success(t('common.createSuccess'))
2022-10-17 11:24:22 +08:00
} else if (actionType.value === 'typeUpdate') {
await DictTypeApi.updateDictTypeApi(data)
2022-11-22 14:34:21 +08:00
message.success(t('common.updateSuccess'))
2022-10-17 11:24:22 +08:00
}
dialogVisible.value = false
} finally {
actionLoading.value = false
2022-11-22 14:34:21 +08:00
typeGetList(xTypeGrid)
2022-10-17 11:24:22 +08:00
}
2022-07-18 19:06:37 +08:00
}
2022-10-17 11:24:22 +08:00
})
2022-07-18 19:06:37 +08:00
}
const submitDataForm = async () => {
2022-10-17 11:24:22 +08:00
const elForm = unref(dataFormRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
actionLoading.value = true
// 提交请求
try {
const data = unref(dataFormRef)?.formModel as DictDataVO
if (actionType.value === 'dataCreate') {
data.dictType = parentType.value
await DictDataApi.createDictDataApi(data)
2022-11-22 14:34:21 +08:00
message.success(t('common.createSuccess'))
2022-10-17 11:24:22 +08:00
} else if (actionType.value === 'dataUpdate') {
await DictDataApi.updateDictDataApi(data)
2022-11-22 14:34:21 +08:00
message.success(t('common.updateSuccess'))
2022-10-17 11:24:22 +08:00
}
dialogVisible.value = false
} finally {
actionLoading.value = false
2022-11-22 14:34:21 +08:00
dataGetList(xDataGrid)
2022-10-17 11:24:22 +08:00
}
2022-07-18 19:06:37 +08:00
}
2022-10-17 11:24:22 +08:00
})
2022-07-18 19:06:37 +08:00
}
</script>