支付应用:支付宝的添加代码优化

This commit is contained in:
YunaiV 2023-08-05 13:50:02 +08:00
parent dae9bc131c
commit 86e0a375fa
2 changed files with 65 additions and 69 deletions

View File

@ -1,8 +1,8 @@
<template> <template>
<div> <div>
<el-dialog <Dialog
v-model="dialogVisible" v-model="dialogVisible"
:title="title" :title="dialogTitle"
@closed="close" @closed="close"
append-to-body append-to-body
destroy-on-close destroy-on-close
@ -11,7 +11,7 @@
<el-form <el-form
ref="formRef" ref="formRef"
:model="formData" :model="formData"
:rules="rules" :formRules="formRules"
label-width="100px" label-width="100px"
v-loading="formLoading" v-loading="formLoading"
> >
@ -37,9 +37,9 @@
<el-form-item label-width="180px" label="网关地址" prop="config.serverUrl"> <el-form-item label-width="180px" label="网关地址" prop="config.serverUrl">
<el-radio-group v-model="formData.config.serverUrl"> <el-radio-group v-model="formData.config.serverUrl">
<el-radio label="https://openapi.alipay.com/gateway.do">线上环境</el-radio> <el-radio label="https://openapi.alipay.com/gateway.do">线上环境</el-radio>
<el-radio label="https://openapi-sandbox.dl.alipaydev.com/gateway.do" <el-radio label="https://openapi-sandbox.dl.alipaydev.com/gateway.do">
>沙箱环境</el-radio 沙箱环境
> </el-radio>
</el-radio-group> </el-radio-group>
</el-form-item> </el-form-item>
<el-form-item label-width="180px" label="算法类型" prop="config.signType"> <el-form-item label-width="180px" label="算法类型" prop="config.signType">
@ -95,7 +95,9 @@
:http-request="appCertUpload" :http-request="appCertUpload"
:before-upload="fileBeforeUpload" :before-upload="fileBeforeUpload"
> >
<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button> <el-button type="primary">
<Icon icon="ep:upload" class="mr-5px" /> 点击上传
</el-button>
</el-upload> </el-upload>
</el-form-item> </el-form-item>
<el-form-item <el-form-item
@ -121,7 +123,9 @@
:before-upload="fileBeforeUpload" :before-upload="fileBeforeUpload"
:http-request="alipayPublicCertUpload" :http-request="alipayPublicCertUpload"
> >
<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button> <el-button type="primary">
<Icon icon="ep:upload" class="mr-5px" /> 点击上传
</el-button>
</el-upload> </el-upload>
</el-form-item> </el-form-item>
<el-form-item label-width="180px" label="根证书" prop="config.rootCertContent"> <el-form-item label-width="180px" label="根证书" prop="config.rootCertContent">
@ -143,7 +147,9 @@
:before-upload="fileBeforeUpload" :before-upload="fileBeforeUpload"
:http-request="rootCertUpload" :http-request="rootCertUpload"
> >
<el-button size="small" type="primary" icon="el-icon-upload">点击上传</el-button> <el-button type="primary">
<Icon icon="ep:upload" class="mr-5px" /> 点击上传
</el-button>
</el-upload> </el-upload>
</el-form-item> </el-form-item>
</div> </div>
@ -155,21 +161,20 @@
<el-button @click="close">取消</el-button> <el-button @click="close">取消</el-button>
<el-button type="primary" @click="submitForm">确定</el-button> <el-button type="primary" @click="submitForm">确定</el-button>
</template> </template>
</el-dialog> </Dialog>
</div> </div>
</template> </template>
<script lang="ts" setup name="AlipayChannelForm"> <script lang="ts" setup name="AlipayChannelForm">
import { createChannel, getChannel, updateChannel } from '@/api/pay/channel'
import { CommonStatusEnum } from '@/utils/constants' import { CommonStatusEnum } from '@/utils/constants'
import { DICT_TYPE, getDictOptions } from '@/utils/dict' import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import * as ChannelApi from '@/api/pay/channel'
const { t } = useI18n() //
const message = useMessage() // const message = useMessage() //
const emit = defineEmits(['success']) const dialogVisible = ref(false) //
const dialogTitle = ref('') //
const dialogVisible = ref(false) const formLoading = ref(false) // 12
const formLoading = ref(false)
const title = ref('')
const formData = ref<any>({ const formData = ref<any>({
appId: '', appId: '',
code: '', code: '',
@ -188,8 +193,7 @@ const formData = ref<any>({
rootCertContent: '' rootCertContent: ''
} }
}) })
const formRules = {
const rules = {
feeRate: [{ required: true, message: '请输入渠道费率', trigger: 'blur' }], feeRate: [{ required: true, message: '请输入渠道费率', trigger: 'blur' }],
status: [{ required: true, message: '渠道状态不能为空', trigger: 'blur' }], status: [{ required: true, message: '渠道状态不能为空', trigger: 'blur' }],
'config.appId': [{ required: true, message: '请输入开放平台上创建的应用的 ID', trigger: 'blur' }], 'config.appId': [{ required: true, message: '请输入开放平台上创建的应用的 ID', trigger: 'blur' }],
@ -206,55 +210,57 @@ const rules = {
], ],
'config.rootCertContent': [{ required: true, message: '请上传指定根证书', trigger: 'blur' }] 'config.rootCertContent': [{ required: true, message: '请上传指定根证书', trigger: 'blur' }]
} }
const fileAccept = '.crt' const fileAccept = '.crt'
const formRef = ref() // Ref
const formRef = ref() /** 打开弹窗 */
const open = async (appId, code) => { const open = async (appId, code) => {
dialogVisible.value = true dialogVisible.value = true
formLoading.value = true formLoading.value = true
reset(appId, code) resetForm(appId, code)
//
try { try {
const data = await getChannel(appId, code) const data = await ChannelApi.getChannel(appId, code)
if (data && data.id) { if (data && data.id) {
formData.value = data formData.value = data
formData.value.config = JSON.parse(data.config) formData.value.config = JSON.parse(data.config)
} }
title.value = !formData.value.id ? '创建支付渠道' : '编辑支付渠道' dialogTitle.value = !formData.value.id ? '创建支付渠道' : '编辑支付渠道'
} finally {
formLoading.value = false
}
}
defineExpose({ open }) // open
/** 提交表单 */
const emit = defineEmits(['success']) // success
const submitForm = async () => {
//
if (!formRef) return
const valid = await formRef.value.validate()
if (!valid) return
//
formLoading.value = true
try {
const data = { ...formData.value } as unknown as ChannelApi.ChannelVO
data.config = JSON.stringify(formData.value.config)
if (!data.id) {
await ChannelApi.createChannel(data)
message.success(t('common.createSuccess'))
} else {
await ChannelApi.updateChannel(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
//
emit('success')
} finally { } finally {
formLoading.value = false formLoading.value = false
} }
} }
defineExpose({ open })
const close = () => {
dialogVisible.value = false
reset(undefined, undefined)
}
const submitForm = async () => {
const valid = await formRef.value.validate()
if (!valid) return
const data: any = { ...formData.value }
data.config = JSON.stringify(formData.value.config)
if (!data.id) {
await createChannel(data)
message.success('新增成功')
} else {
await updateChannel(data)
message.success('修改成功')
}
emit('success')
close()
}
/** 重置表单 */ /** 重置表单 */
const reset = (appId, code) => { const resetForm = (appId, code) => {
formData.value = { formData.value = {
appId: appId, appId: appId,
code: code, code: code,
@ -273,7 +279,7 @@ const reset = (appId, code) => {
rootCertContent: '' rootCertContent: ''
} }
} }
// formRef.value?.resetFields() formRef.value?.resetFields()
} }
const fileBeforeUpload = (file) => { const fileBeforeUpload = (file) => {

View File

@ -308,7 +308,7 @@ import download from '@/utils/download'
import * as PayappApi from '@/api/pay/app' import * as PayappApi from '@/api/pay/app'
import AppForm from './components/AppForm.vue' import AppForm from './components/AppForm.vue'
import { PayChannelEnum, PayType } from '@/utils/constants' import { PayChannelEnum, PayType } from '@/utils/constants'
import AlipayChannelForm from './components/alipayChannelForm.vue' import AlipayChannelForm from './components/channel/AlipayChannelForm.vue'
import WeixinChannelForm from './components/weixinChannelForm.vue' import WeixinChannelForm from './components/weixinChannelForm.vue'
import MockChannelForm from './components/mockChannelForm.vue' import MockChannelForm from './components/mockChannelForm.vue'
import { CommonStatusEnum } from '@/utils/constants' import { CommonStatusEnum } from '@/utils/constants'
@ -338,16 +338,6 @@ const queryParams = reactive({
}) })
const queryFormRef = ref() // const queryFormRef = ref() //
const exportLoading = ref(false) // const exportLoading = ref(false) //
const channelParam = reactive({
loading: false,
appId: null, // ID
payCode: null, //
//
payMerchant: {
id: null, //
name: null //
}
}) //
/** 查询列表 */ /** 查询列表 */
const getList = async () => { const getList = async () => {
@ -373,10 +363,9 @@ const resetQuery = () => {
handleQuery() handleQuery()
} }
// /** 应用状态修改 */
const handleStatusChange = async (row: any) => { const handleStatusChange = async (row: any) => {
let text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用' let text = row.status === CommonStatusEnum.ENABLE ? '启用' : '停用'
try { try {
await message.confirm('确认要"' + text + '""' + row.name + '"应用吗?') await message.confirm('确认要"' + text + '""' + row.name + '"应用吗?')
await PayappApi.changeAppStatus({ id: row.id, status: row.status }) await PayappApi.changeAppStatus({ id: row.id, status: row.status })
@ -388,6 +377,11 @@ const handleStatusChange = async (row: any) => {
} }
/** 添加/修改操作 */ /** 添加/修改操作 */
const channelParam = reactive({
loading: false,
appId: null, // ID
payCode: null //
})
const formRef = ref() const formRef = ref()
const openForm = (type: string, id?: number) => { const openForm = (type: string, id?: number) => {
formRef.value.open(type, id) formRef.value.open(type, id)
@ -440,17 +434,13 @@ const openChannelForm = async (row, payCode, type) => {
channelParam.loading = false channelParam.loading = false
channelParam.appId = row.id channelParam.appId = row.id
channelParam.payCode = payCode channelParam.payCode = payCode
channelParam.payMerchant = row.payMerchant
switch (type) { switch (type) {
case PayType.ALIPAY: case PayType.ALIPAY:
alipayFormRef.value.open(row.id, payCode) alipayFormRef.value.open(row.id, payCode)
break break
case PayType.WECHAT: case PayType.WECHAT:
weixinFormRef.value.open(row.id, payCode) weixinFormRef.value.open(row.id, payCode)
break break
case PayType.MOCK: case PayType.MOCK:
mockFormRef.value.open(row.id, payCode) mockFormRef.value.open(row.id, payCode)
break break