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

(cherry picked from commit a2fdff47df)
This commit is contained in:
YunaiV 2023-08-05 14:15:10 +08:00 committed by shizhong
parent b613a69e5c
commit 544adb9811
6 changed files with 168 additions and 186 deletions

View File

@ -125,15 +125,6 @@ export const PayChannelEnum = {
}
}
/**
*
*/
export const PayType = {
WECHAT: 'WECHAT',
ALIPAY: 'ALIPAY',
MOCK: 'MOCK'
}
/**
*
*/

View File

@ -151,8 +151,8 @@
</el-form-item>
</el-form>
<template #footer>
<el-button @click="close">取消</el-button>
<el-button type="primary" @click="submitForm">确定</el-button>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</div>

View File

@ -0,0 +1,122 @@
<template>
<div>
<Dialog v-model="dialogVisible" :title="dialogTitle" @closed="close" width="800px">
<el-form
ref="formRef"
:model="formData"
:rules="formRules"
label-width="100px"
v-loading="formLoading"
>
<el-form-item label-width="180px" label="渠道状态" prop="status">
<el-radio-group v-model="formData.status">
<el-radio
v-for="dict in getDictOptions(DICT_TYPE.COMMON_STATUS)"
:key="parseInt(dict.value)"
:label="parseInt(dict.value)"
>
{{ dict.label }}
</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label-width="180px" label="备注" prop="remark">
<el-input v-model="formData.remark" :style="{ width: '100%' }" />
</el-form-item>
</el-form>
<template #footer>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</div>
</template>
<script lang="ts" setup>
import { CommonStatusEnum } from '@/utils/constants'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import * as ChannelApi from '@/api/pay/channel'
defineOptions({ name: 'MockChannelForm' })
const { t } = useI18n() //
const message = useMessage() //
const dialogVisible = ref(false) //
const dialogTitle = ref('') //
const formLoading = ref(false) // 12
const formData = ref<any>({
appId: '',
code: '',
status: undefined,
feeRate: 0,
remark: '',
config: {
name: 'mock-conf'
}
})
const formRules = {
status: [{ required: true, message: '渠道状态不能为空', trigger: 'blur' }]
}
const formRef = ref() // Ref
/** 打开弹窗 */
const open = async (appId, code) => {
dialogVisible.value = true
formLoading.value = true
resetForm(appId, code)
//
try {
const data = await ChannelApi.getChannel(appId, code)
if (data && data.id) {
formData.value = data
formData.value.config = JSON.parse(data.config)
}
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 {
formLoading.value = false
}
}
/** 重置表单 */
const resetForm = (appId, code) => {
formData.value = {
appId: appId,
code: code,
status: CommonStatusEnum.ENABLE,
remark: '',
feeRate: 0,
config: {
name: 'mock-conf'
}
}
formRef.value?.resetFields()
}
</script>

View File

@ -158,8 +158,8 @@
</el-form-item>
</el-form>
<template #footer>
<el-button @click="close">取消</el-button>
<el-button type="primary" @click="submitForm">确定</el-button>
<el-button :disabled="formLoading" type="primary" @click="submitForm"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</div>

View File

@ -1,130 +0,0 @@
<template>
<div>
<el-dialog
v-model:visible="dialogVisible"
:title="title"
@closed="close"
append-to-body
destroy-on-close
width="800px"
>
<el-form
ref="formRef"
:model="formData"
:rules="rules"
label-width="100px"
v-loading="formLoading"
>
<el-form-item label-width="180px" label="渠道状态" prop="status">
<el-radio-group v-model="formData.status">
<el-radio
v-for="dict in getDictOptions(DICT_TYPE.COMMON_STATUS)"
:key="parseInt(dict.value)"
:label="parseInt(dict.value)"
>
{{ dict.label }}
</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label-width="180px" label="备注" prop="remark">
<el-input v-model="formData.remark" :style="{ width: '100%' }" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="close">取消</el-button>
<el-button type="primary" @click="submitForm">确定</el-button>
</template>
</el-dialog>
</div>
</template>
<script lang="ts" setup name="MockChannelForm">
import { createChannel, getChannel, updateChannel } from '@/api/pay/channel'
import { CommonStatusEnum } from '@/utils/constants'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
const message = useMessage() //
const dialogVisible = ref(false)
const formLoading = ref(false)
const title = ref('')
const formData = ref<any>({
appId: '',
code: '',
status: undefined,
feeRate: 0,
remark: '',
config: {
name: 'mock-conf'
}
})
const rules = {
status: [{ required: true, message: '渠道状态不能为空', trigger: 'blur' }]
}
const formRef = ref()
const emit = defineEmits(['success'])
const open = async (appId, code) => {
dialogVisible.value = true
formLoading.value = true
reset(appId, code)
try {
const data = await getChannel(appId, code)
if (data && data.id) {
formData.value = data
formData.value.config = JSON.parse(data.config)
}
title.value = !formData.value.id ? '创建支付渠道' : '编辑支付渠道'
} finally {
formLoading.value = false
}
}
const close = () => {
dialogVisible.value = false
reset(undefined, undefined)
}
const submitForm = async () => {
const valid = await formRef.value?.validate()
if (!valid) {
return
}
const data = { ...formData.value }
data.config = JSON.stringify(formData.value.config)
if (!data.id) {
createChannel(data).then(() => {
message.success('新增成功')
emit('success')
close()
})
} else {
updateChannel(data).then(() => {
message.success('修改成功')
emit('success')
close()
})
}
}
/** 重置表单 */
const reset = (appId, code) => {
formData.value = {
appId: appId,
code: code,
status: CommonStatusEnum.ENABLE,
remark: '',
feeRate: 0,
config: {
name: 'mock-conf'
}
}
formRef.value?.resetFields()
}
defineExpose({ open })
</script>

View File

@ -88,7 +88,7 @@
<el-button
type="success"
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.ALIPAY_APP.code)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_APP.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_APP.code)"
circle
>
<Icon icon="ep:check" />
@ -97,7 +97,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_APP.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_APP.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -109,7 +109,7 @@
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.ALIPAY_PC.code)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_PC.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_PC.code)"
>
<Icon icon="ep:check" />
</el-button>
@ -117,7 +117,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_PC.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_PC.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -129,7 +129,7 @@
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.ALIPAY_WAP.code)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_WAP.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_WAP.code)"
>
<Icon icon="ep:check" />
</el-button>
@ -137,7 +137,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_WAP.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_WAP.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -149,7 +149,7 @@
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.ALIPAY_QR.code)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_QR.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_QR.code)"
>
<Icon icon="ep:check" />
</el-button>
@ -157,7 +157,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_QR.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_QR.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -169,7 +169,7 @@
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.ALIPAY_BAR.code)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_BAR.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_BAR.code)"
>
<Icon icon="ep:check" />
</el-button>
@ -177,7 +177,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_BAR.code, PayType.ALIPAY)"
@click="openChannelForm(scope.row, PayChannelEnum.ALIPAY_BAR.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -191,7 +191,7 @@
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.WX_LITE.code)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_LITE.code, PayType.WECHAT)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_LITE.code)"
>
<Icon icon="ep:check" />
</el-button>
@ -199,7 +199,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.WX_LITE.code, PayType.WECHAT)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_LITE.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -211,7 +211,7 @@
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.WX_PUB.code)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_PUB.code, PayType.WECHAT)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_PUB.code)"
>
<Icon icon="ep:check" />
</el-button>
@ -219,7 +219,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.WX_PUB.code, PayType.WECHAT)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_PUB.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -231,7 +231,7 @@
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.WX_APP.code)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_APP.code, PayType.WECHAT)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_APP.code)"
>
<Icon icon="ep:check" />
</el-button>
@ -239,7 +239,7 @@
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.WX_APP.code, PayType.WECHAT)"
@click="openChannelForm(scope.row, PayChannelEnum.WX_APP.code)"
>
<Icon icon="ep:close" />
</el-button>
@ -252,17 +252,19 @@
<el-button
type="success"
circle
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.MOCK.code)"
v-if="isChannelExists(scope.row.channelCodes, PayChannelEnum.MOCK)"
@click="openChannelForm(scope.row, PayChannelEnum.MOCK.code)"
><Icon icon="ep:check"
/></el-button>
>
<Icon icon="ep:check" />
</el-button>
<el-button
v-else
type="danger"
circle
@click="openChannelForm(scope.row, PayChannelEnum.MOCK.code)"
><Icon icon="ep:close"
/></el-button>
>
<Icon icon="ep:close" />
</el-button>
</template>
</el-table-column>
</el-table-column>
@ -310,7 +312,7 @@ import AppForm from './components/AppForm.vue'
import { PayChannelEnum, PayType } from '@/utils/constants'
import AlipayChannelForm from './components/channel/AlipayChannelForm.vue'
import WeixinChannelForm from './components/channel/WeixinChannelForm.vue'
import MockChannelForm from './components/mockChannelForm.vue'
import MockChannelForm from './components/channel/MockChannelForm.vue'
import { CommonStatusEnum } from '@/utils/constants'
defineOptions({ name: 'PayApp' })
@ -318,10 +320,6 @@ defineOptions({ name: 'PayApp' })
const message = useMessage() //
const { t } = useI18n() //
const alipayFormRef = ref()
const weixinFormRef = ref()
const mockFormRef = ref()
const loading = ref(true) //
const total = ref(0) //
const list = ref([]) //
@ -376,11 +374,6 @@ const handleStatusChange = async (row: any) => {
}
/** 添加/修改操作 */
const channelParam = reactive({
loading: false,
appId: null, // ID
payCode: null //
})
const formRef = ref()
const openForm = (type: string, id?: number) => {
formRef.value.open(type, id)
@ -429,20 +422,26 @@ const isChannelExists = (channels, channelCode) => {
/**
* 新增支付渠道信息
*/
const openChannelForm = async (row, payCode, type) => {
channelParam.loading = false
const alipayFormRef = ref()
const weixinFormRef = ref()
const mockFormRef = ref()
const channelParam = reactive({
appId: null, // ID
payCode: null //
})
const openChannelForm = async (row, payCode) => {
channelParam.appId = row.id
channelParam.payCode = payCode
switch (type) {
case PayType.ALIPAY:
alipayFormRef.value.open(row.id, payCode)
break
case PayType.WECHAT:
weixinFormRef.value.open(row.id, payCode)
break
case PayType.MOCK:
mockFormRef.value.open(row.id, payCode)
break
if (payCode.indexOf('alipay_') === 0) {
alipayFormRef.value.open(row.id, payCode)
return
}
if (payCode.indexOf('wx_') === 0) {
weixinFormRef.value.open(row.id, payCode)
return
}
if (payCode.indexOf('mock') === 0) {
mockFormRef.value.open(row.id, payCode)
}
}