Files
iot-device-management-frontend/apps/web-antd/src/views/pay/app/modules/channel-form.vue

95 lines
2.4 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
2025-05-26 16:52:11 +08:00
import type { PayChannelApi } from '#/api/pay/channel';
import { computed, ref } from 'vue';
import { useVbenModal } from '@vben/common-ui';
import { $t } from '@vben/locales';
2025-05-27 10:39:06 +08:00
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
2025-05-26 16:52:11 +08:00
import { createChannel, getChannel, updateChannel } from '#/api/pay/channel';
2025-05-26 16:52:11 +08:00
import { channelSchema } from './data';
2025-05-26 16:52:11 +08:00
const emit = defineEmits(['success']);
const formData = ref<PayChannelApi.Channel>();
const formType = ref<string>('');
const title = computed(() => {
2025-05-26 16:52:11 +08:00
return formData.value?.id
? $t('ui.actionTitle.edit', '应用')
: $t('ui.actionTitle.create', '应用');
});
2025-05-26 16:52:11 +08:00
const [Form, formApi] = useVbenForm({
commonConfig: {
componentProps: {
class: 'w-full',
},
2025-05-26 16:52:11 +08:00
formItemClass: 'col-span-2',
labelWidth: 160,
},
2025-05-26 16:52:11 +08:00
layout: 'horizontal',
showDefaultActions: false,
});
2025-05-26 16:52:11 +08:00
const [Modal, modalApi] = useVbenModal({
async onConfirm() {
const { valid } = await formApi.validate();
if (!valid) {
return;
}
modalApi.lock();
// 提交表单
const data = (await formApi.getValues()) as PayChannelApi.Channel;
try {
await (formData.value?.id ? updateChannel(data) : createChannel(data));
// 关闭并提示
await modalApi.close();
emit('success');
message.success($t('ui.actionMessage.operationSuccess'));
} finally {
modalApi.unlock();
}
},
onOpenChange: async (isOpen) => {
if (!isOpen) {
2025-05-26 16:52:11 +08:00
formData.value = undefined;
return;
}
2025-05-26 16:52:11 +08:00
// 加载数据
const { id, payCode } = modalApi.getData() as {
id?: number;
payCode?: string;
};
2025-05-26 16:52:11 +08:00
if (!id || !payCode) {
return;
}
modalApi.lock();
if (payCode.includes('alipay_')) {
formType.value = 'alipay';
} else if (payCode.includes('mock')) {
formType.value = 'mock';
} else if (payCode.includes('wallet')) {
formType.value = 'wallet';
} else if (payCode.includes('wx')) {
formType.value = 'wx';
}
2025-05-26 16:52:11 +08:00
try {
formData.value = await getChannel(id, payCode);
// 设置到 values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<template>
2025-05-26 16:52:11 +08:00
<Modal :close-on-click-modal="false" :title="title" class="w-[40%]">
2025-05-27 10:39:06 +08:00
<Form :schema="channelSchema(formType)" />
2025-05-26 16:52:11 +08:00
</Modal>
</template>