gaoluyang
2026-06-24 bfdc0e0e6d5e47aa501f9b6fadd143d9c97cf00a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<script lang="ts" setup>
import type { PayChannelApi } from '#/api/pay/channel';
 
import { computed, ref } from 'vue';
 
import { useVbenModal } from '../../../../packages/effects/common-ui/src';
import { CommonStatusEnum } from '../../../../packages/constants/src';
import { $t } from '../../../../packages/locales/src';
 
import { message } from 'ant-design-vue';
 
import { useVbenForm } from '#/adapter/form';
import { createChannel, getChannel, updateChannel } from '#/api/pay/channel';
 
import { useChannelFormSchema } from '../data';
 
const emit = defineEmits(['success']);
const formData = ref<any>();
const title = computed(() => {
  return formData.value?.id === 0
    ? $t('ui.actionTitle.create', ['渠道'])
    : $t('ui.actionTitle.edit', ['渠道']);
});
 
const [Form, formApi] = useVbenForm({
  commonConfig: {
    componentProps: {
      class: 'w-full',
    },
    formItemClass: 'col-span-2',
    labelWidth: 160,
  },
  layout: 'horizontal',
  showDefaultActions: false,
});
 
const [Modal, modalApi] = useVbenModal({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) {
      return;
    }
    modalApi.lock();
    // 提交表单
    const data = (await formApi.getValues()) as PayChannelApi.Channel;
    data.config = JSON.stringify(data.config || {});
    try {
      await (data.id ? updateChannel(data) : createChannel(data));
      // 关闭并提示
      await modalApi.close();
      emit('success');
      message.success($t('ui.actionMessage.operationSuccess'));
    } finally {
      modalApi.unlock();
    }
  },
  async onOpenChange(isOpen: boolean) {
    if (!isOpen) {
      formData.value = undefined;
      return;
    }
    // 加载数据
    const { appId, code } = modalApi.getData() as {
      appId?: number;
      code?: string;
    };
    if (!appId || !code) {
      return;
    }
    modalApi.lock();
    formData.value = {
      appId,
      code,
      status: CommonStatusEnum.ENABLE,
      remark: '',
      feeRate: 0,
      config: {},
    };
    if (code.includes('alipay_')) {
      formData.value.config = {
        appId: undefined,
        serverUrl: undefined,
        signType: 'RSA2',
        mode: undefined,
        privateKey: undefined,
        alipayPublicKey: undefined,
        appCertContent: undefined,
        alipayPublicCertContent: undefined,
        rootCertContent: undefined,
        encryptType: undefined,
        encryptKey: undefined,
      };
    } else if (code.includes('mock')) {
      formData.value.config = {
        name: 'mock-conf',
      };
    } else if (code.includes('wallet')) {
      formData.value.config = {
        config: {
          name: 'wallet-conf',
        },
      };
    } else if (code.includes('wx')) {
      formData.value.config = {
        appId: undefined,
        mchId: undefined,
        apiVersion: undefined,
        mchKey: undefined,
        keyContent: undefined,
        privateKeyContent: undefined,
        certSerialNo: undefined,
        apiV3Key: undefined,
        publicKeyContent: undefined,
        publicKeyId: undefined,
      };
    }
 
    try {
      const res = await getChannel(appId, code);
      if (res) {
        formData.value = {
          ...res,
          config: JSON.parse(res.config),
        };
      }
      // 设置到 values
      await formApi.setValues(formData.value);
    } finally {
      modalApi.unlock();
    }
  },
});
</script>
 
<template>
  <Modal :title="title" class="w-2/5">
    <Form :schema="useChannelFormSchema(formData?.code)" />
  </Modal>
</template>