最新文章
【前端技术】微信小程序实现于H5之间的互相跳转
发布时间:2025-05-30
热度:
h5跳转小程序
<web-view src="https://www.baidu.com" bindload="bindload" binderror="binderror"></web-view>
https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
wx.miniProgram.navigateTo({url: '/path/to/page'})
https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html
h5 跳转到小程序(微信环境)
<div style="position: relative;width: 100%;height: 3rem;"> //需要的按钮样式,以绝对定位放在微信跳转按钮的相同位置 <van-button style="position: absolute;top:1rem">打开小程序</van-button > //微信跳转按钮 <wx-open-launch-weapp style="position: absolute;top: 0" id="launch-btn" appid="wx12345678" username="小程序原始id,即小程序对应的以gh_开头的id" path="pages/home/index?user=123&action=abc" referrerinfo="{'key1':'value1','key2':'value2'}" <!-- 可选参数,传递额外信息给小程序 --> > <script type="text/wxtag-template"> <style>.mini-btn { width:100%; margin: 24px auto; height: 3rem; opacity: 0}</style> //设置透明度为0 <p class="mini-btn"></p> </script> </wx-open-launch-weapp> </div> <!-- 引入微信 JS-SDK --> <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script> window.onload = function() { // 页面加载完成后的代码 console.log('页面加载完成'); this.getWxConfig()//发起微信验证的接口请求 }; // 后端验证接口: getSign (验证接口测试最好在线上测试) getSign = (data = {}) => { return request({ url: '后端验证接口地址', method: 'post', data: data }) } getWxConfig () { let that = this let url = window.location.href.split('#')[0] const query = { webUrl: url, userId: this.userId } getSign(query).then(res => { wx.config({ debug: false, // 验证结果弹窗控制,微信环境可能会因为接口安全问题出现失败弹窗,所以直接设置的false, appId: res.data.appId, // 公众号唯一appid nonceStr: res.data.nonceStr, timestamp: res.data.timestamp, signature: res.data.signature, jsApiList: ['scanQRCode'],//随便填写的一个 openTagList: ['wx-open-launch-weapp'] }) wx.ready(function (success) { that.isWxBtn = true //表示验证成功 console.log('success', success) }) wx.error(function (err) { Toast.fail('暂不支持',error); // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log('error', error) }) }) } </script>
https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html#21
https://wxaurl.cn/https://wxmpurl.cn/
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-link.html
location.href = 'weixin://dl/business/?appid=*APPID*&path=*PATH*&query=*QUERY*&env_version=*ENV_VERSION*'
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/url-scheme.html
wx.navigateToMiniProgram({ appId: appId, //必传值 path: path,//别的小程序路径,空为跳到主页 success(res) { // 打开成功 }, fail(res) { //打开失败 } })
https://developers.weixin.qq.com/miniprogram/dev/api/navigate/wx.navigateToMiniProgram.html
wx.navigateToMiniProgram({ appId: appId, //必传值 path: path,//别的小程序路径,空为跳到主页 success(res) { // 打开成功 }, fail(res) { //打开失败 }