通过授权的方式获取code,传给后端换取getBrandWCPayRequest调起支付所需的参数,因为location.replace跳转到OAuth2网页授权登陆的原因,会造成ios微信底部出现forward/back按钮,影响ui
isWeChat() {
return /MicroMessenger/i.test(navigator.userAgent);
},
getWeChatCode() {
if (this.isWeChat()) {
const appId = 'xxxxxxxxxxxx'
const redirectUri = encodeURIComponent(window.location.href)
uni.setStorageSync('redirectUri', window.location.href)
const scope = 'snsapi_base'
const state = 'STATE123'
window.location.replace(`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appId}&redirect_uri=${redirectUri}&response_type=code&scope=${scope}&state=${state}#wechat_redirect`)
} else {
this.$tip.confirm('需要在微信环境下才能使用', false)
}
},
checkWeChatCode() {
let code = this.getUrlCode('code')
if (code) {
let params = {
user_id: uni.getStorageSync('userId'),
code: code,
package_id: uni.getStorageSync('packageId'),
}
this.$http.post('/package/buy', params).then(res => {
if (res.status === 'success') {
let self = this
window.WeixinJSBridge.invoke('getBrandWCPayRequest', res.data, function (result) {
if (result.err_msg === "get_brand_wcpay_request:ok") {
self.$tip.confirm('支付成功', false).then(() => {
self.queryInfo()
})
} else if (result.err_msg === "get_brand_wcpay_request:cancel") {
self.$tip.confirm('已取消支付', false)
} else {
self.$tip.confirm('支付失败', false)
}
});
} else {
this.$tip.confirm(res.message, false);
}
})
}
},
