需求:通过外部浏览器的H5广告页,点击页面会跳转到微信并打开相应小程序,并调起支付功能,支付改成后关闭小程序
1.后端需看官方文档,拿java举例,应写如下代码
HttpRequest request = HttpUtil.createPost("https://域名/wxa/generate_urllink?access_token=" + getAccessToken(appId, appSecret));
JSONObject param = new JSONObject();
param.put("path", "/pages/order/index");
param.put("query", "outTradeNo=" + outTradeNo);
param.put("env_version", "trial");
request.body(param.toJSONString());
String resp = request.execute().body();
log.info("generateUrlLink req: {}, resp: {}", param.toJSONString(), resp);
if (StringUtils.isNotBlank(resp)) {
JSONObject jsonObject = JSONObject.parseObject(resp);
if (!jsonObject.containsKey("errcode") || "0".equals(jsonObject.getString("errcode"))) {
return jsonObject.getString("url_link");
}
}
1.path字段是需要跳转到小程序的指定页面进行直接拉支付,一般都是新建一个页面,作为展示页,前端在onload阶段就获取信息进行拉起微信支付
2.query是后端从浏览器跳转过来时加在页面路径后的参数
3.appid和appSecret一定要写,注意:如果一段时间不用的话appSecret失效,需要管理员到小程序后台进行重置appSecret
以上是后端需要做的事,前端做的相应简单
1.新建一个空白页,专门作为从广告页跳转过来支付的过渡页面
```javascript
pay(){
let that = this
wx.request({
url: 'https://域名/pay/payWithMini',
data: {
outTradeNo: that.outTradeNo,
payType: 1,
wxCode: that.code
},
method: 'POST',
success (d) {
that.goodsTitle =d.data.data.goodsTitle
that.orderPrice = d.data.data.orderPrice
wx.requestPayment({
timeStamp: d.data.data.timeStamp,
nonceStr: d.data.data.nonceStr,
package: d.data.data.packageStr,
signType: d.data.data.signType,
paySign: d.data.data.paySign,
success (res2) {
that.$refs.inputDialog.open() //自动触发支付成功的弹框,并提醒用户点击退出按钮来关闭小程序
},
fail (res2) {
console.log('支付失败')
}
})
}
})
},
},
onLoad(e) {
let that = this
wx.login({
success (res) {
if (res.code) {
that.code = res.code
that.outTradeNo = e.outTradeNo
that.pay()
} else {
console.log('登录失败!' + res.errMsg)
}
}
});
}
2.关闭小程序
exitApp() {
wx.exitMiniProgram({
success: function () {}
})
},
此方法为,点击弹框按钮,触发关闭小程序的功能