|
| 1 | +<template> |
| 2 | + <Dialog :title="modelTitle" v-model="modelVisible" width="800"> |
| 3 | + <!-- 对话框(添加 / 修改) --> |
| 4 | + <el-form ref="formRef" :model="form" :rules="formRules" label-width="80px"> |
| 5 | + <el-form-item label="商户全称" prop="name"> |
| 6 | + <el-input v-model="form.name" placeholder="请输入商户全称" /> |
| 7 | + </el-form-item> |
| 8 | + <el-form-item label="商户简称" prop="shortName"> |
| 9 | + <el-input v-model="form.shortName" placeholder="请输入商户简称" /> |
| 10 | + </el-form-item> |
| 11 | + <el-form-item label="开启状态" prop="status"> |
| 12 | + <el-select v-model="form.status" placeholder="请选择状态" clearable> |
| 13 | + <el-option |
| 14 | + v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" |
| 15 | + :key="dict.value" |
| 16 | + :label="dict.label" |
| 17 | + :value="dict.value" |
| 18 | + /> |
| 19 | + </el-select> |
| 20 | + </el-form-item> |
| 21 | + <el-form-item label="备注" prop="remark"> |
| 22 | + <el-input v-model="form.remark" placeholder="请输入备注" /> |
| 23 | + </el-form-item> |
| 24 | + </el-form> |
| 25 | + <template #footer> |
| 26 | + <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button> |
| 27 | + <el-button @click="modelVisible = false">取 消</el-button> |
| 28 | + </template> |
| 29 | + </Dialog> |
| 30 | +</template> |
| 31 | +<script setup lang="ts"> |
| 32 | +import * as MerchantApi from '@/api/pay/merchant' |
| 33 | +import { DICT_TYPE, getIntDictOptions } from '@/utils/dict' |
| 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 | +const form = ref({ |
| 42 | + id: undefined, |
| 43 | + name: '', |
| 44 | + shortName: '', |
| 45 | + status: undefined, |
| 46 | + remark: '' |
| 47 | +}) |
| 48 | +const formRules = reactive({ |
| 49 | + name: [{ required: true, message: '商户名称不能为空', trigger: 'blur' }], |
| 50 | + shortName: [{ required: true, message: '商户简称不能为空', trigger: 'blur' }], |
| 51 | + status: [{ required: true, message: '状态不能为空', trigger: 'change' }] |
| 52 | +}) |
| 53 | +const formRef = ref() // 表单 Ref |
| 54 | +
|
| 55 | +/** 打开弹窗 */ |
| 56 | +const openModal = async (type: string, id?: number) => { |
| 57 | + modelVisible.value = true |
| 58 | + modelTitle.value = t('action.' + type) |
| 59 | + formType.value = type |
| 60 | + resetForm() |
| 61 | + // 修改时,设置数据 |
| 62 | + if (id) { |
| 63 | + formLoading.value = true |
| 64 | + try { |
| 65 | + form.value = await MerchantApi.getMerchantApi(id) |
| 66 | + } finally { |
| 67 | + formLoading.value = false |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +defineExpose({ openModal }) // 提供 openModal 方法,用于打开弹窗 |
| 72 | +
|
| 73 | +/** 提交表单 */ |
| 74 | +const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调 |
| 75 | +const submitForm = async () => { |
| 76 | + // 校验表单 |
| 77 | + if (!formRef) return |
| 78 | + const valid = await formRef.value.validate() |
| 79 | + if (!valid) return |
| 80 | + // 提交请求 |
| 81 | + formLoading.value = true |
| 82 | + try { |
| 83 | + const data = form.value as unknown as MerchantApi.MerchantVO |
| 84 | + if (formType.value === 'create') { |
| 85 | + await MerchantApi.createMerchantApi(data) |
| 86 | + message.success(t('common.createSuccess')) |
| 87 | + } else { |
| 88 | + await MerchantApi.updateMerchantApi(data) |
| 89 | + message.success(t('common.updateSuccess')) |
| 90 | + } |
| 91 | + modelVisible.value = false |
| 92 | + // 发送操作成功的事件 |
| 93 | + emit('success') |
| 94 | + } finally { |
| 95 | + formLoading.value = false |
| 96 | + } |
| 97 | +} |
| 98 | +
|
| 99 | +/** 重置表单 */ |
| 100 | +const resetForm = () => { |
| 101 | + form.value = { |
| 102 | + id: undefined, |
| 103 | + name: '', |
| 104 | + shortName: '', |
| 105 | + status: undefined, |
| 106 | + remark: '' |
| 107 | + } |
| 108 | + formRef.value?.resetFields() |
| 109 | +} |
| 110 | +</script> |
0 commit comments