Skip to content

Commit a2fdff4

Browse files
committed
支付应用:模拟支付的添加代码优化
1 parent b3b1d97 commit a2fdff4

File tree

6 files changed

+168
-186
lines changed

6 files changed

+168
-186
lines changed

src/utils/constants.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,15 +125,6 @@ export const PayChannelEnum = {
125125
}
126126
}
127127

128-
/**
129-
* 支付类型枚举
130-
*/
131-
export const PayType = {
132-
WECHAT: 'WECHAT',
133-
ALIPAY: 'ALIPAY',
134-
MOCK: 'MOCK'
135-
}
136-
137128
/**
138129
* 支付订单状态枚举
139130
*/

src/views/pay/app/components/channel/AlipayChannelForm.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@
151151
</el-form-item>
152152
</el-form>
153153
<template #footer>
154-
<el-button @click="close">取消</el-button>
155-
<el-button type="primary" @click="submitForm">确定</el-button>
154+
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
155+
<el-button @click="dialogVisible = false">取 消</el-button>
156156
</template>
157157
</Dialog>
158158
</div>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<template>
2+
<div>
3+
<Dialog v-model="dialogVisible" :title="dialogTitle" @closed="close" width="800px">
4+
<el-form
5+
ref="formRef"
6+
:model="formData"
7+
:rules="formRules"
8+
label-width="100px"
9+
v-loading="formLoading"
10+
>
11+
<el-form-item label-width="180px" label="渠道状态" prop="status">
12+
<el-radio-group v-model="formData.status">
13+
<el-radio
14+
v-for="dict in getDictOptions(DICT_TYPE.COMMON_STATUS)"
15+
:key="parseInt(dict.value)"
16+
:label="parseInt(dict.value)"
17+
>
18+
{{ dict.label }}
19+
</el-radio>
20+
</el-radio-group>
21+
</el-form-item>
22+
<el-form-item label-width="180px" label="备注" prop="remark">
23+
<el-input v-model="formData.remark" :style="{ width: '100%' }" />
24+
</el-form-item>
25+
</el-form>
26+
<template #footer>
27+
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
28+
<el-button @click="dialogVisible = false">取 消</el-button>
29+
</template>
30+
</Dialog>
31+
</div>
32+
</template>
33+
<script lang="ts" setup>
34+
import { CommonStatusEnum } from '@/utils/constants'
35+
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
36+
import * as ChannelApi from '@/api/pay/channel'
37+
38+
defineOptions({ name: 'MockChannelForm' })
39+
40+
const { t } = useI18n() // 国际化
41+
const message = useMessage() // 消息弹窗
42+
43+
const dialogVisible = ref(false) // 弹窗的是否展示
44+
const dialogTitle = ref('') // 弹窗的标题
45+
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
46+
const formData = ref<any>({
47+
appId: '',
48+
code: '',
49+
status: undefined,
50+
feeRate: 0,
51+
remark: '',
52+
config: {
53+
name: 'mock-conf'
54+
}
55+
})
56+
const formRules = {
57+
status: [{ required: true, message: '渠道状态不能为空', trigger: 'blur' }]
58+
}
59+
const formRef = ref() // 表单 Ref
60+
61+
/** 打开弹窗 */
62+
const open = async (appId, code) => {
63+
dialogVisible.value = true
64+
formLoading.value = true
65+
resetForm(appId, code)
66+
// 加载数据
67+
try {
68+
const data = await ChannelApi.getChannel(appId, code)
69+
70+
if (data && data.id) {
71+
formData.value = data
72+
formData.value.config = JSON.parse(data.config)
73+
}
74+
dialogTitle.value = !formData.value.id ? '创建支付渠道' : '编辑支付渠道'
75+
} finally {
76+
formLoading.value = false
77+
}
78+
}
79+
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
80+
81+
/** 提交表单 */
82+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
83+
const submitForm = async () => {
84+
// 校验表单
85+
if (!formRef) return
86+
const valid = await formRef.value.validate()
87+
if (!valid) return
88+
// 提交请求
89+
formLoading.value = true
90+
try {
91+
const data = { ...formData.value } as unknown as ChannelApi.ChannelVO
92+
data.config = JSON.stringify(formData.value.config)
93+
if (!data.id) {
94+
await ChannelApi.createChannel(data)
95+
message.success(t('common.createSuccess'))
96+
} else {
97+
await ChannelApi.updateChannel(data)
98+
message.success(t('common.updateSuccess'))
99+
}
100+
dialogVisible.value = false
101+
// 发送操作成功的事件
102+
emit('success')
103+
} finally {
104+
formLoading.value = false
105+
}
106+
}
107+
108+
/** 重置表单 */
109+
const resetForm = (appId, code) => {
110+
formData.value = {
111+
appId: appId,
112+
code: code,
113+
status: CommonStatusEnum.ENABLE,
114+
remark: '',
115+
feeRate: 0,
116+
config: {
117+
name: 'mock-conf'
118+
}
119+
}
120+
formRef.value?.resetFields()
121+
}
122+
</script>

src/views/pay/app/components/channel/WeixinChannelForm.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@
158158
</el-form-item>
159159
</el-form>
160160
<template #footer>
161-
<el-button @click="close">取消</el-button>
162-
<el-button type="primary" @click="submitForm">确定</el-button>
161+
<el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
162+
<el-button @click="dialogVisible = false">取 消</el-button>
163163
</template>
164164
</Dialog>
165165
</div>

src/views/pay/app/components/mockChannelForm.vue

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)