获取订单号
let data = {
phone,
payPrice:
userName,
meritsRegion,
userAddress,
customVision,
id
};
let orderCode = await addActivity(data);
1. 支付宝
最简单的是支付宝支付,后台会返回一个form表单,我们用js提交即可
const form = await zfb_web_pay({
orderCode,
amount
});
const div = document.createElement('div');
div.id = 'alipay';
div.innerHTML = form;
document.body.appendChild(div);
this.$nextTick(function() {
document
.getElementById('alipay')
.children[0].submit();
document.body.removeChild(div);
});
2. 微信外部支付
微信外部支付是通过返回一个url地址来调起微信app从而进行支付的
const { mweb_url } = await wx_WEB({
orderCode,
amount
});
window.location.href = mweb_url;
3.微信内部JSAPI支付
微信内部支付是最麻烦的,因为需要前端跳转一个url获取code,然后根据此code去调接口换取openId
beforeRouteEnter(to, from, next) {
next(vm => {
if (isWxBrowser()) {
if (!vm.code) {
const url =
'https://open.weixin.qq.com/connect/oauth2/authorize?appid=${公众平台的openId}&redirect_uri=' +
encodeURIComponent(window.location.href) +
'&response_type=code&scope=snsapi_base#wechat_redirect';
window.location.href = url;
} else {
vm.getOpenId(vm.code);
}
}
});
},
computed: {
code() {
return getQueryString('code')
}
},
methods:{
getOpenId(code) {
getOpenId({
code
})
.then(res => {
this.openId = res;
})
.catch(err => {
console.log(err);
});
},
getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
var r = window.location.search.substr(1).match(reg)
if (r != null) return unescape(r[2])
return null
}
}
const wxPayInfo = await wx_WEBNEI({
orderCode,
openId: this.openId
});
weixinPay(wxPayInfo);
weixinPay(data) {
if (typeof WeixinJSBridge == 'undefined') {
if (document.addEventListener) {
document.addEventListener(
'WeixinJSBridgeReady',
onBridgeReady(data),
false
);
} else if (document.attachEvent) {
document.attachEvent(
'WeixinJSBridgeReady',
onBridgeReady(data)
);
document.attachEvent(
'onWeixinJSBridgeReady',
onBridgeReady(data)
);
}
} else {
onBridgeReady(data);
}
}
function onBridgeReady(data) {
window.WeixinJSBridge.invoke(
'getBrandWCPayRequest',
{
appId: data.appId,
timeStamp: data.timeStamp,
nonceStr: data.nonceStr,
package: data.package,
signType: data.signType,
paySign: data.sign
},
function (res) {
if (res.err_msg == 'get_brand_wcpay_request:ok') {
Toast.success('支付成功');
} else {
Toast.fail('支付失败!');
}
}
);
}