171 lines
4.9 KiB
Vue
Raw Normal View History

2022-11-12 15:52:43 +08:00
<template>
<ContentWrap>
<!-- 列表 -->
2022-11-12 15:52:43 +08:00
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
<!-- 操作新增 -->
2022-11-12 15:52:43 +08:00
<template #toolbar_buttons>
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:error-code:create']"
@click="handleCreate()"
/>
</template>
<template #actionbtns_default="{ row }">
<!-- 操作修改 -->
2022-11-12 15:52:43 +08:00
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
v-hasPermi="['system:error-code:update']"
@click="handleUpdate(row.id)"
/>
<!-- 操作详情 -->
2022-11-12 15:52:43 +08:00
<XTextButton
preIcon="ep:view"
:title="t('action.detail')"
v-hasPermi="['system:error-code:update']"
2022-11-13 15:13:38 +08:00
@click="handleDetail(row.id)"
2022-11-12 15:52:43 +08:00
/>
<!-- 操作删除 -->
2022-11-12 15:52:43 +08:00
<XTextButton
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['system:error-code:delete']"
@click="handleDelete(row.id)"
/>
</template>
</vxe-grid>
</ContentWrap>
<!-- 弹窗 -->
2022-11-12 15:52:43 +08:00
<XModal id="errorCodeModel" v-model="dialogVisible" :title="dialogTitle">
<template #default>
<!-- 对话框(添加 / 修改) -->
<Form
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
ref="formRef"
/>
<!-- 对话框(详情) -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailRef"
2022-11-12 17:33:48 +08:00
/>
2022-11-12 15:52:43 +08:00
</template>
<template #footer>
<!-- 按钮保存 -->
2022-11-12 15:52:43 +08:00
<XButton
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitForm"
/>
<!-- 按钮关闭 -->
2022-11-12 15:52:43 +08:00
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
</template>
</XModal>
</template>
2022-07-18 19:06:37 +08:00
<script setup lang="ts">
// 全局相关的 import
2022-07-18 19:06:37 +08:00
import { ref, unref } from 'vue'
2022-11-12 15:52:43 +08:00
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeGridInstance } from 'vxe-table'
import { FormExpose } from '@/components/Form'
// 业务相关的 import
import { rules, allSchemas } from './errorCode.data'
import * as ErrorCodeApi from '@/api/system/errorCode'
2022-07-18 19:06:37 +08:00
2022-11-12 15:52:43 +08:00
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗
// 列表相关的变量
const xGrid = ref<VxeGridInstance>() // grid Ref
const { gridOptions } = useVxeGrid<ErrorCodeApi.ErrorCodeVO>({
allSchemas: allSchemas,
getListApi: ErrorCodeApi.getErrorCodePageApi
})
// 弹窗相关的变量
2022-07-18 19:06:37 +08:00
const dialogVisible = ref(false) // 是否显示弹出层
const dialogTitle = ref('edit') // 弹出层标题
2022-11-12 15:52:43 +08:00
const actionType = ref('') // 操作按钮的类型
const actionLoading = ref(false) // 按钮 Loading
2022-07-18 19:06:37 +08:00
const formRef = ref<FormExpose>() // 表单 Ref
2022-11-12 15:52:43 +08:00
const detailRef = ref() // 详情 Ref
2022-07-18 19:06:37 +08:00
// 设置标题
const setDialogTile = (type: string) => {
dialogTitle.value = t('action.' + type)
actionType.value = type
dialogVisible.value = true
}
// 新增操作
const handleCreate = () => {
setDialogTile('create')
// 重置表单
unref(formRef)?.getElFormRef()?.resetFields()
}
// 修改操作
2022-11-12 15:52:43 +08:00
const handleUpdate = async (rowId: number) => {
2022-07-18 19:06:37 +08:00
setDialogTile('update')
// 设置数据
2022-11-12 15:52:43 +08:00
const res = await ErrorCodeApi.getErrorCodeApi(rowId)
2022-07-18 19:06:37 +08:00
unref(formRef)?.setValues(res)
}
2022-11-13 15:13:38 +08:00
// 详情操作
const handleDetail = async (rowId: number) => {
setDialogTile('detail')
// 设置数据
const res = await ErrorCodeApi.getErrorCodeApi(rowId)
detailRef.value = res
}
2022-11-12 15:52:43 +08:00
// 删除操作
const handleDelete = async (rowId: number) => {
message
.delConfirm()
.then(async () => {
await ErrorCodeApi.deleteErrorCodeApi(rowId)
message.success(t('common.delSuccess'))
})
.finally(() => {
// 刷新列表
2022-11-12 15:52:43 +08:00
xGrid.value?.commitProxy('query')
})
}
// 提交新增/修改的表单
2022-07-18 19:06:37 +08:00
const submitForm = async () => {
2022-10-17 11:24:22 +08:00
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
2022-11-12 15:52:43 +08:00
actionLoading.value = true
2022-10-17 11:24:22 +08:00
// 提交请求
try {
2022-11-13 15:13:38 +08:00
const data = unref(formRef)?.formModel as ErrorCodeApi.ErrorCodeVO
2022-10-17 11:24:22 +08:00
if (actionType.value === 'create') {
await ErrorCodeApi.createErrorCodeApi(data)
2022-11-12 15:52:43 +08:00
message.success(t('common.createSuccess'))
2022-10-17 11:24:22 +08:00
} else {
await ErrorCodeApi.updateErrorCodeApi(data)
2022-11-12 15:52:43 +08:00
message.success(t('common.updateSuccess'))
2022-10-17 11:24:22 +08:00
}
dialogVisible.value = false
} finally {
2022-11-12 15:52:43 +08:00
actionLoading.value = false
// 刷新列表
2022-11-12 15:52:43 +08:00
xGrid.value?.commitProxy('query')
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>