|
| 1 | +<template> |
| 2 | + <Dialog :title="modelTitle" v-model="modelVisible"> |
| 3 | + <el-form |
| 4 | + ref="formRef" |
| 5 | + :model="formData" |
| 6 | + :rules="formRules" |
| 7 | + label-width="100px" |
| 8 | + v-loading="formLoading" |
| 9 | + > |
| 10 | + <el-form-item label="应用名" prop="applicationName"> |
| 11 | + <el-input v-model="formData.applicationName" placeholder="请输入应用名" clearable /> |
| 12 | + </el-form-item> |
| 13 | + <el-form-item label="错误码编码" prop="code"> |
| 14 | + <el-input v-model="formData.code" placeholder="请输入错误码编码" clearable /> |
| 15 | + </el-form-item> |
| 16 | + <el-form-item label="错误码提示" prop="message"> |
| 17 | + <el-input v-model="formData.message" placeholder="请输入错误码提示" clearable /> |
| 18 | + </el-form-item> |
| 19 | + <el-form-item label="备注" prop="memo"> |
| 20 | + <el-input v-model="formData.memo" placeholder="请输入备注" clearable /> |
| 21 | + </el-form-item> |
| 22 | + </el-form> |
| 23 | + <template #footer> |
| 24 | + <div class="dialog-footer"> |
| 25 | + <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> |
| 26 | + <el-button @click="modelVisible = false">取 消</el-button> |
| 27 | + </div> |
| 28 | + </template> |
| 29 | + </Dialog> |
| 30 | +</template> |
| 31 | +<script setup lang="ts"> |
| 32 | +import * as ErrorCodeApi from '@/api/system/errorCode' |
| 33 | +
|
| 34 | +const { t } = useI18n() // 国际化 |
| 35 | +const message = useMessage() // 消息弹窗 |
| 36 | +
|
| 37 | +const modelVisible = ref(false) // 弹窗的是否展示 |
| 38 | +const modelTitle = ref('') // 弹窗的标题 |
| 39 | +const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用 |
| 40 | +const formType = ref('') // 表单的类型:create - 新增;update - 修改 |
| 41 | +// 表单参数 |
| 42 | +const formData = ref({ |
| 43 | + id: undefined, |
| 44 | + code: undefined, |
| 45 | + applicationName: '', |
| 46 | + message: '', |
| 47 | + memo: '' |
| 48 | +}) |
| 49 | +// 表单校验 |
| 50 | +const formRules = reactive({ |
| 51 | + applicationName: [{ required: true, message: '应用名不能为空', trigger: 'blur' }], |
| 52 | + code: [{ required: true, message: '错误码编码不能为空', trigger: 'blur' }], |
| 53 | + message: [{ required: true, message: '错误码提示不能为空', trigger: 'blur' }] |
| 54 | +}) |
| 55 | +const formRef = ref() // 表单 Ref |
| 56 | +
|
| 57 | +/** 打开弹窗 */ |
| 58 | +const openModal = async (type: string, id?: number) => { |
| 59 | + modelVisible.value = true |
| 60 | + modelTitle.value = t('action.' + type) |
| 61 | + formType.value = type |
| 62 | + resetForm() |
| 63 | + // 修改时,设置数据 |
| 64 | + if (id) { |
| 65 | + formLoading.value = true |
| 66 | + try { |
| 67 | + formData.value = await ErrorCodeApi.getErrorCodeApi(id) |
| 68 | + } finally { |
| 69 | + formLoading.value = false |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | +defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗 |
| 74 | +
|
| 75 | +/** 提交表单 */ |
| 76 | +const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
| 77 | +
|
| 78 | +/** 提交按钮 */ |
| 79 | +const submitForm = async () => { |
| 80 | + // 校验表单 |
| 81 | + if (!formRef) return |
| 82 | + const valid = await formRef.value.validate() |
| 83 | + if (!valid) return |
| 84 | + // 提交请求 |
| 85 | + formLoading.value = true |
| 86 | + try { |
| 87 | + const data = formData.value as ErrorCodeApi.ErrorCodeVO |
| 88 | + if (formType.value === 'create') { |
| 89 | + await ErrorCodeApi.createErrorCodeApi(data) |
| 90 | + message.success(t('common.createSuccess')) |
| 91 | + } else { |
| 92 | + await ErrorCodeApi.updateErrorCodeApi(data) |
| 93 | + message.success(t('common.updateSuccess')) |
| 94 | + } |
| 95 | + modelVisible.value = false |
| 96 | + // 发送操作成功的事件 |
| 97 | + emit('success') |
| 98 | + } finally { |
| 99 | + formLoading.value = false |
| 100 | + } |
| 101 | +} |
| 102 | +
|
| 103 | +/** 表单重置 */ |
| 104 | +const resetForm = () => { |
| 105 | + formData.value = { |
| 106 | + id: undefined, |
| 107 | + applicationName: '', |
| 108 | + code: undefined, |
| 109 | + message: '', |
| 110 | + memo: '' |
| 111 | + } |
| 112 | + formRef.value?.resetFields() |
| 113 | +} |
| 114 | +</script> |
0 commit comments