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

168 lines
4.2 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-27 22:15:24 +08:00
import { CommonStatusEnum } from '#/utils';
2025-05-26 16:52:11 +08:00
import { channelSchema } from './data';
2025-05-26 16:52:11 +08:00
const emit = defineEmits(['success']);
2025-05-27 22:15:24 +08:00
const formData = ref<any>();
2025-05-26 16:52:11 +08:00
const formType = ref<string>('');
const title = computed(() => {
2025-05-27 22:15:24 +08:00
return formData.value?.id === 0
? $t('ui.actionTitle.create', '应用')
: $t('ui.actionTitle.edit', '应用');
});
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;
2025-05-27 22:15:24 +08:00
// 只保留表单中实际存在的字段,且值不为 undefined
const data2 = Object.fromEntries(
Object.entries(data).filter(([key, value]) => {
// 检查字段是否在表单中存在,且值不为 undefined
return key in data && value !== undefined;
}),
);
const data3 = { ...formData.value, ...data2 };
data3.config = JSON.stringify(data3.config);
2025-05-26 16:52:11 +08:00
try {
2025-05-27 22:15:24 +08:00
await (data3.id ? updateChannel(data3) : createChannel(data3));
2025-05-26 16:52:11 +08:00
// 关闭并提示
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();
2025-05-27 22:15:24 +08:00
formType.value = payCode;
2025-05-26 16:52:11 +08:00
if (payCode.includes('alipay_')) {
2025-05-27 22:15:24 +08:00
formData.value = {
appId: id,
code: payCode,
status: CommonStatusEnum.ENABLE,
remark: '',
feeRate: null,
config: {
appId: '',
serverUrl: null,
signType: 'RSA2',
mode: null,
privateKey: '',
alipayPublicKey: '',
appCertContent: '',
alipayPublicCertContent: '',
rootCertContent: '',
encryptType: '',
encryptKey: '',
},
};
2025-05-26 16:52:11 +08:00
} else if (payCode.includes('mock')) {
2025-05-27 22:15:24 +08:00
formData.value = {
appId: id,
code: payCode,
status: CommonStatusEnum.ENABLE,
remark: '',
feeRate: 0,
config: {
name: 'mock-conf',
},
};
2025-05-26 16:52:11 +08:00
} else if (payCode.includes('wallet')) {
2025-05-27 22:15:24 +08:00
formData.value = {
appId: id,
code: payCode,
status: CommonStatusEnum.ENABLE,
remark: '',
feeRate: 0,
config: {
name: 'mock-conf',
},
};
2025-05-26 16:52:11 +08:00
} else if (payCode.includes('wx')) {
2025-05-27 22:15:24 +08:00
formData.value = {
appId: id,
code: payCode,
status: CommonStatusEnum.ENABLE,
feeRate: undefined,
remark: '',
config: {
appId: '',
mchId: '',
apiVersion: '',
mchKey: '',
keyContent: '',
privateKeyContent: '',
certSerialNo: '',
apiV3Key: '',
publicKeyContent: '',
publicKeyId: '',
},
};
}
2025-05-26 16:52:11 +08:00
try {
2025-05-27 22:15:24 +08:00
const res = await getChannel(id, payCode);
formData.value = {
...res,
config: {
...JSON.parse(res.config),
},
};
2025-05-26 16:52:11 +08:00
// 设置到 values
await formApi.setValues(formData.value);
} finally {
modalApi.unlock();
}
},
});
</script>
<!-- TODO @xingyu支付宝的证书也是支持上传的哈 -->
<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>