补充 VP 模块注册 + 前端 API 封装
- app.module.ts: 注册 VirtualPaymentModule - config.ts + api.ts: 添加虚拟支付 API 端点 - useGravityPurchase.ts: VP 购买逻辑 - interview.vue / user.vue: 适配 VP 调用 - admin.controller.ts: 管理端虚拟支付相关调整 - payment.controller.spec.ts: 测试适配
This commit is contained in:
@@ -81,31 +81,41 @@ export function useGravityPurchase(onPaymentSuccess?: () => void) {
|
||||
|
||||
if (isMp.value) {
|
||||
try {
|
||||
let res = await uni.request({
|
||||
url: api('/payment/jsapi-product'), method: 'POST',
|
||||
data: { type, quantity },
|
||||
const loginRes = await new Promise<{ code: string }>((resolve, reject) => {
|
||||
uni.login({ provider: 'weixin', success: r => resolve(r), fail: reject })
|
||||
})
|
||||
if (!loginRes.code) {
|
||||
payLoading.value = false
|
||||
payError.value = '微信登录失败,请重试'
|
||||
uni.showToast({ title: '微信登录失败', icon: 'none' })
|
||||
return
|
||||
}
|
||||
|
||||
const res = await uni.request({
|
||||
url: api('/virtual-payment/create'), method: 'POST',
|
||||
data: { type, quantity, wxCode: loginRes.code },
|
||||
header: { 'Authorization': `Bearer ${token}`, 'Content-Type': 'application/json' },
|
||||
timeout: 30000,
|
||||
})
|
||||
payLoading.value = false
|
||||
|
||||
if (res.statusCode >= 200 && res.statusCode < 300 && res.data?.payParams) {
|
||||
const pp = res.data.payParams as any
|
||||
currentOutTradeNo.value = res.data.outTradeNo || ''
|
||||
uni.requestPayment({
|
||||
provider: 'wxpay',
|
||||
timeStamp: pp.timeStamp,
|
||||
nonceStr: pp.nonceStr,
|
||||
package: pp.package,
|
||||
signType: pp.signType || 'RSA',
|
||||
paySign: pp.paySign,
|
||||
if (res.statusCode >= 200 && res.statusCode < 300 && res.data?.paySig) {
|
||||
const vp = res.data as any
|
||||
currentOutTradeNo.value = vp.outTradeNo
|
||||
|
||||
;(wx as any).requestVirtualPayment({
|
||||
env: vp.env,
|
||||
offerId: vp.offerId,
|
||||
signData: vp.signData,
|
||||
paySig: vp.paySig,
|
||||
signature: vp.signature,
|
||||
success: () => {
|
||||
const no = currentOutTradeNo.value || res.data.outTradeNo
|
||||
const no = currentOutTradeNo.value || vp.outTradeNo
|
||||
pollPayResult(no)
|
||||
},
|
||||
fail: () => {
|
||||
fail: (err: any) => {
|
||||
payError.value = '支付未完成'
|
||||
uni.showToast({ title: '支付未完成', icon: 'none' })
|
||||
uni.showToast({ title: err?.errMsg || '支付未完成', icon: 'none' })
|
||||
},
|
||||
})
|
||||
} else if (!res.statusCode || res.statusCode === 0) {
|
||||
@@ -116,9 +126,9 @@ export function useGravityPurchase(onPaymentSuccess?: () => void) {
|
||||
payError.value = errMsg
|
||||
uni.showToast({ title: errMsg, icon: 'none' })
|
||||
}
|
||||
} catch (e) {
|
||||
} catch (e: any) {
|
||||
payLoading.value = false
|
||||
payError.value = '网络错误,请重试'
|
||||
payError.value = e?.errMsg || '网络错误,请重试'
|
||||
uni.showToast({ title: '网络错误', icon: 'none' })
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user