赞
踩
实际开发中,小程序和H5往往有很多业务场景需要来回跳转,这里主要介绍三种跳转方式供大家参考。
场景:微信小程序登录有时候需要和公众号进行绑定,获取公众号code和appid传给后台进行绑定
<template>
<web-view src="https://www.xxx.cn/auth.html"></web-view>
</template>
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script type="text/javascript">
let url = window.location.href;
if(url.includes(code)) {
if (wx.miniProgram) {
wx.miniProgram.reLaunch({
url: `/pages/home/index?code=${url中的code}&appId=${url中的appid}`,
success: function() {
......
},
fall: function() {
......
}
})
}
} else {
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己的appid&redirect_uri=还是当前页面&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
}
</script>
<div id="app">
<wx-open-launch-weapp id="launch-btn" username="原始ID" path="赋值自己的path页面">
<template>
<button class="btn">跳转小程序</button>
</template>
</wx-open-launch-weapp>
</div>
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
<script type="text/javascript">
let url = window.location.href;
//如果url中包含code,说明授权成功,开始注册微信config
if(url.includes(code)) {
//通过接口拿到appId,nonceStr,signature,timestamp
wx.config({
debug: false,
appId,
timestamp,
nonceStr,
signature,
jsApiList: ['chooseImage', 'previewImage'], //必写,否则不显示
openTagList: ['wx-open-launch-weapp']//必写,否则不显示
});
} else {
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=自己的appid&redirect_uri=还是当前页面&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
}
</script>
该接口用于获取小程序 scheme 码,适用于短信、邮件、外部网页、微信内等拉起小程序的业务场景。通过该接口,可以选择生成到期失效和永久有效的小程序码,有数量限制,目前仅针对国内非个人主体的小程序开放。
POST https://api.weixin.qq.com/wxa/generatescheme?access_token=ACCESS_TOKEN
{
"msg": "操作成功",
"code": 200,
"data": {
"openlink": "weixin://dl/business/?t=WqDMv7uIy7g"
}
}
我这里是写个html文件通过a标签访问
<a href="weixin://dl/business/?t=WqDMv7uIy7g">跳转小程序</a>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。