Skip to content

Commit 86e0a37

Browse files
committed
支付应用:支付宝的添加代码优化
1 parent dae9bc1 commit 86e0a37

File tree

2 files changed

+59
-63
lines changed

2 files changed

+59
-63
lines changed

src/views/pay/app/components/alipayChannelForm.vue renamed to src/views/pay/app/components/channel/AlipayChannelForm.vue

Lines changed: 52 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<div>
3-
<el-dialog
3+
<Dialog
44
v-model="dialogVisible"
5-
:title="title"
5+
:title="dialogTitle"
66
@closed="close"
77
append-to-body
88
destroy-on-close
@@ -11,7 +11,7 @@
1111
<el-form
1212
ref="formRef"
1313
:model="formData"
14-
:rules="rules"
14+
:formRules="formRules"
1515
label-width="100px"
1616
v-loading="formLoading"
1717
>
@@ -37,9 +37,9 @@
3737
<el-form-item label-width="180px" label="网关地址" prop="config.serverUrl">
3838
<el-radio-group v-model="formData.config.serverUrl">
3939
<el-radio label="https://openapi.alipay.com/gateway.do">线上环境</el-radio>
40-
<el-radio label="https://openapi-sandbox.dl.alipaydev.com/gateway.do"
41-
>沙箱环境</el-radio
42-
>
40+
<el-radio label="https://openapi-sandbox.dl.alipaydev.com/gateway.do">
41+
沙箱环境
42+
</el-radio>
4343
</el-radio-group>
4444
</el-form-item>
4545
<el-form-item label-width="180px" label="算法类型" prop="config.signType">
@@ -95,7 +95,9 @@
9595
:http-request="appCertUpload"
9696
:before-upload="fileBeforeUpload"
9797
>
98-
<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
98+
<el-button type="primary">
99+
<Icon icon="ep:upload" class="mr-5px" /> 点击上传
100+
</el-button>
99101
</el-upload>
100102
</el-form-item>
101103
<el-form-item
@@ -121,7 +123,9 @@
121123
:before-upload="fileBeforeUpload"
122124
:http-request="alipayPublicCertUpload"
123125
>
124-
<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
126+
<el-button type="primary">
127+
<Icon icon="ep:upload" class="mr-5px" /> 点击上传
128+
</el-button>
125129
</el-upload>
126130
</el-form-item>
127131
<el-form-item label-width="180px" label="根证书" prop="config.rootCertContent">
@@ -143,7 +147,9 @@
143147
:before-upload="fileBeforeUpload"
144148
:http-request="rootCertUpload"
145149
>
146-
<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button>
150+
<el-button type="primary">
151+
<Icon icon="ep:upload" class="mr-5px" /> 点击上传
152+
</el-button>
147153
</el-upload>
148154
</el-form-item>
149155
</div>
@@ -155,21 +161,20 @@
155161
<el-button @click="close">取消</el-button>
156162
<el-button type="primary" @click="submitForm">确定</el-button>
157163
</template>
158-
</el-dialog>
164+
</Dialog>
159165
</div>
160166
</template>
161167
<script lang="ts" setup name="AlipayChannelForm">
162-
import { createChannel, getChannel, updateChannel } from '@/api/pay/channel'
163168
import { CommonStatusEnum } from '@/utils/constants'
164169
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
170+
import * as ChannelApi from '@/api/pay/channel'
165171
172+
const { t } = useI18n() // 国际化
166173
const message = useMessage() // 消息弹窗
167174
168-
const emit = defineEmits(['success'])
169-
170-
const dialogVisible = ref(false)
171-
const formLoading = ref(false)
172-
const title = ref('')
175+
const dialogVisible = ref(false) // 弹窗的是否展示
176+
const dialogTitle = ref('') // 弹窗的标题
177+
const formLoading = ref(false) // 表单的加载中:1)修改时的数据加载;2)提交的按钮禁用
173178
const formData = ref<any>({
174179
appId: '',
175180
code: '',
@@ -188,8 +193,7 @@ const formData = ref<any>({
188193
rootCertContent: ''
189194
}
190195
})
191-
192-
const rules = {
196+
const formRules = {
193197
feeRate: [{ required: true, message: '请输入渠道费率', trigger: 'blur' }],
194198
status: [{ required: true, message: '渠道状态不能为空', trigger: 'blur' }],
195199
'config.appId': [{ required: true, message: '请输入开放平台上创建的应用的 ID', trigger: 'blur' }],
@@ -206,55 +210,57 @@ const rules = {
206210
],
207211
'config.rootCertContent': [{ required: true, message: '请上传指定根证书', trigger: 'blur' }]
208212
}
209-
210213
const fileAccept = '.crt'
214+
const formRef = ref() // 表单 Ref
211215
212-
const formRef = ref()
213-
216+
/** 打开弹窗 */
214217
const open = async (appId, code) => {
215218
dialogVisible.value = true
216219
formLoading.value = true
217-
reset(appId, code)
218-
220+
resetForm(appId, code)
221+
// 加载数据
219222
try {
220-
const data = await getChannel(appId, code)
223+
const data = await ChannelApi.getChannel(appId, code)
221224
if (data && data.id) {
222225
formData.value = data
223226
formData.value.config = JSON.parse(data.config)
224227
}
225-
title.value = !formData.value.id ? '创建支付渠道' : '编辑支付渠道'
228+
dialogTitle.value = !formData.value.id ? '创建支付渠道' : '编辑支付渠道'
226229
} finally {
227230
formLoading.value = false
228231
}
229232
}
233+
defineExpose({ open }) // 提供 open 方法,用于打开弹窗
230234
231-
defineExpose({ open })
232-
233-
const close = () => {
234-
dialogVisible.value = false
235-
reset(undefined, undefined)
236-
}
237-
235+
/** 提交表单 */
236+
const emit = defineEmits(['success']) // 定义 success 事件,用于操作成功后的回调
238237
const submitForm = async () => {
238+
// 校验表单
239+
if (!formRef) return
239240
const valid = await formRef.value.validate()
240241
if (!valid) return
241-
242-
const data: any = { ...formData.value }
243-
data.config = JSON.stringify(formData.value.config)
244-
if (!data.id) {
245-
await createChannel(data)
246-
message.success('新增成功')
247-
} else {
248-
await updateChannel(data)
249-
message.success('修改成功')
242+
// 提交请求
243+
formLoading.value = true
244+
try {
245+
const data = { ...formData.value } as unknown as ChannelApi.ChannelVO
246+
data.config = JSON.stringify(formData.value.config)
247+
if (!data.id) {
248+
await ChannelApi.createChannel(data)
249+
message.success(t('common.createSuccess'))
250+
} else {
251+
await ChannelApi.updateChannel(data)
252+
message.success(t('common.updateSuccess'))
253+
}
254+
dialogVisible.value = false
255+
// 发送操作成功的事件
256+
emit('success')
257+
} finally {
258+
formLoading.value = false
250259
}
251-
252-
emit('success')
253-
close()
254260
}
255261
256262
/** 重置表单 */
257-
const reset = (appId, code) => {
263+
const resetForm = (appId, code) => {
258264
formData.value = {
259265
appId: appId,
260266
code: code,
@@ -273,7 +279,7 @@ const reset = (appId, code) => {
273279
rootCertContent: ''
274280
}
275281
}
276-
// formRef.value?.resetFields()
282+
formRef.value?.resetFields()
277283
}
278284
279285
const fileBeforeUpload = (file) => {

src/views/pay/app/index.vue

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ import download from '@/utils/download'
308308
import * as PayappApi from '@/api/pay/app'
309309
import AppForm from './components/AppForm.vue'
310310
import { PayChannelEnum, PayType } from '@/utils/constants'
311-
import AlipayChannelForm from './components/alipayChannelForm.vue'
311+
import AlipayChannelForm from './components/channel/AlipayChannelForm.vue'
312312
import WeixinChannelForm from './components/weixinChannelForm.vue'
313313
import MockChannelForm from './components/mockChannelForm.vue'
314314
import { CommonStatusEnum } from '@/utils/constants'
@@ -338,16 +338,6 @@ const queryParams = reactive({
338338
})
339339
const queryFormRef = ref() // 搜索的表单
340340
const exportLoading = ref(false) // 导出的加载中
341-
const channelParam = reactive({
342-
loading: false,
343-
appId: null, // 应用 ID
344-
payCode: null, // 渠道编码
345-
// 商户对象
346-
payMerchant: {
347-
id: null, // 编号
348-
name: null // 名称
349-
}
350-
}) // 微信组件传参参数
351341
352342
/** 查询列表 */
353343
const getList = async () => {
@@ -373,10 +363,9 @@ const resetQuery = () => {
373363
handleQuery()
374364
}
375365
376-
// 用户状态修改
366+
/** 应用状态修改 */
377367
const handleStatusChange = async (row: any) => {
378368
let text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用'
379-
380369
try {
381370
await message.confirm('确认要"' + text + '""' + row.name + '"应用吗?')
382371
await PayappApi.changeAppStatus({ id: row.id, status: row.status })
@@ -388,6 +377,11 @@ const handleStatusChange = async (row: any) => {
388377
}
389378
390379
/** 添加/修改操作 */
380+
const channelParam = reactive({
381+
loading: false,
382+
appId: null, // 应用 ID
383+
payCode: null // 渠道编码
384+
})
391385
const formRef = ref()
392386
const openForm = (type: string, id?: number) => {
393387
formRef.value.open(type, id)
@@ -440,17 +434,13 @@ const openChannelForm = async (row, payCode, type) => {
440434
channelParam.loading = false
441435
channelParam.appId = row.id
442436
channelParam.payCode = payCode
443-
channelParam.payMerchant = row.payMerchant
444-
445437
switch (type) {
446438
case PayType.ALIPAY:
447439
alipayFormRef.value.open(row.id, payCode)
448440
break
449-
450441
case PayType.WECHAT:
451442
weixinFormRef.value.open(row.id, payCode)
452443
break
453-
454444
case PayType.MOCK:
455445
mockFormRef.value.open(row.id, payCode)
456446
break

0 commit comments

Comments
 (0)