要在微信公众号H5网页跳转到小程序,需要使用微信JS-SDK,引入JS文件:http://res.wx.qq.com/open/js/jweixin-1.6.0.js ,在服务端验签获取配置信息,获取成功以后,引入开放标签openTagList: [‘wx-open-launch-weapp’],就可以使用了。
JS-SDK使用方法请参照官方文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html
<wx-open-launch-weapp
id="launch-btn"
username="gh_xxxxxxxx"
path="pages/home/index.html?user=123&action=abc"
>
<template>
<button class="btn">打开小程序</button>
</template>
</wx-open-launch-weapp>
<script>
var se={};
axios({
method: 'post',
url: '/api/wechatJsSdk',
data: {
'url': window.location.href,
}
}).then(function (res) {
// vant.Toast({message:res.data.msg,closeOnClick:true});
// console.log(res);
if (res.data.code == 1) {
se.wx = res.data.data;
wxconfig();
}
});
function wxconfig(){
wx.config({
debug: false,
appId: se.wx.appId,
timestamp: se.wx.timestamp,
nonceStr: se.wx.nonceStr,
signature: se.wx.signature,
jsApiList: [],
openTagList: ['wx-open-launch-weapp']
});
wx.ready(function () {
//自动执行的
});
wx.error(function (res) {
alert(res.errMsg);
});
}
</script>
同样的方法,还可以实现公众号H5跳转APP。