12312
This commit is contained in:
+36
-19
@@ -27,30 +27,47 @@ const pay = (vm, { orderId, onSuccess, onFail }) => {
|
||||
}
|
||||
|
||||
// 正式流程:微信 JSAPI 支付
|
||||
if (!isWechatBrowser()) {
|
||||
uni.showToast({ icon: 'none', title: '请在微信中打开完成支付' })
|
||||
onFail && onFail({ errMsg: 'not wechat browser' })
|
||||
const openId = uni.getStorageSync('openId')
|
||||
if (!openId) {
|
||||
uni.showToast({ icon: 'none', title: '缺少登录身份,请重新进入' })
|
||||
onFail && onFail({ errMsg: 'no openId' })
|
||||
return
|
||||
}
|
||||
const openId = uni.getStorageSync('openId')
|
||||
vm.$u.api.prepayWx({ orderId, openId }).then(res => {
|
||||
console.log('[pay] prepay orderId=' + orderId + ' openId=' + openId)
|
||||
vm.$u.api.prepayWx({ orderId, openId, type: 1 }).then(res => {
|
||||
if (!res) return
|
||||
const p = res.data || {}
|
||||
console.log('[pay] prepay resp', p)
|
||||
// 后端返回微信支付所需参数后,用此桥接唤起支付(微信浏览器内置,无需额外引入)
|
||||
WeixinJSBridge.invoke('getBrandWCPayRequest', {
|
||||
appId: p.appId,
|
||||
timeStamp: String(p.timeStamp),
|
||||
nonceStr: p.nonceStr,
|
||||
package: p.pack || p.packageValue,
|
||||
signType: p.signType || 'RSA',
|
||||
paySign: p.paySign
|
||||
}, bridgeRes => {
|
||||
if (bridgeRes.err_msg === 'get_brand_wcpay_request:ok') {
|
||||
onSuccess && onSuccess(bridgeRes)
|
||||
} else {
|
||||
onFail && onFail(bridgeRes)
|
||||
}
|
||||
})
|
||||
// appId 优先取后端返回值,为空时用 config.wxAppid 兜底
|
||||
const invokePay = () => {
|
||||
WeixinJSBridge.invoke('getBrandWCPayRequest', {
|
||||
appId: p.appId || config.wxAppid,
|
||||
timeStamp: String(p.timeStamp),
|
||||
nonceStr: p.nonceStr || p.nonce,
|
||||
package: p.packageValue || p.pack || p['package'],
|
||||
signType: p.signType || 'RSA',
|
||||
paySign: p.paySign
|
||||
}, bridgeRes => {
|
||||
if (bridgeRes.err_msg === 'get_brand_wcpay_request:ok') {
|
||||
onSuccess && onSuccess(bridgeRes)
|
||||
} else {
|
||||
onFail && onFail(bridgeRes)
|
||||
}
|
||||
})
|
||||
}
|
||||
// 非微信环境 prepay 已拿到参数,唤起桥接这一步才提示需在微信中完成
|
||||
if (!isWechatBrowser()) {
|
||||
uni.showToast({ icon: 'none', title: '请在微信中打开完成支付' })
|
||||
onFail && onFail({ errMsg: 'not wechat browser' })
|
||||
return
|
||||
}
|
||||
// 桥接可能未就绪(冷启动进页立刻支付),挂 WeixinJSBridgeReady 事件等它就绪
|
||||
if (typeof WeixinJSBridge === 'undefined') {
|
||||
document.addEventListener('WeixinJSBridgeReady', invokePay, false)
|
||||
} else {
|
||||
invokePay()
|
||||
}
|
||||
}).catch(() => {
|
||||
onFail && onFail({ errMsg: 'prepay error' })
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user