h5跳转小程序

461 阅读1分钟

微信环境->小程序

浏览器(非小程序环境),需要appId:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no" />
    <script src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <style>
      wx-open-launch-weapp {
        width: 100%;
        height: 50px;
        display: inline-block;
        background-color: blue;
      }
    </style>
  </head>
  <body>
    <div>
      1、param empty ===> 完整补充了config参数,所有内容都要
      <br>
      2、签名不通过 ===> 配合具体获取签名的接口,到时候再说
      <br>
      3、卡进度条,页面读不完 ===> 去掉ready
      <br>
      4、config:ok ===> 验签通过
      <br>
      5、看不到wx-open-launch-weapp ===> 尝试加容器,给予高度,不行,但是点击容器区域,有反应,只是看不到按钮
      ===> 需要config配置的账号类型是服务号
      <br>
      6、尝试官方demo
    </div>
    <script>
        var oHead = document.querySelector('body');
        var oScript = document.createElement('script');
        oScript.type = 'text/javascript';
        oScript.src = '//yun.duiba.com.cn/h5-mami/lib/vconsole.min.js';
        oHead.appendChild(oScript);
        oScript.onload = function() {
            var vConsole = new VConsole();
        }
        </script>
    <script>
        fetch('/wechatMiniApp/ticket/info?apk=3BheKWf5sog6ySQoNFmXpCMy53Mm&url=' + location.href).then(res => res.json()).then(res => {
        var data = res.data;
        // fetch('/wechatShare/getShareInfo/v2?url=' + location.href).then(res => res.json()).then(res => {
        //   var data = res;
         // alert(JSON.stringify(data));
        wx.config({
          debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印
          appId: data.wxAppId || data.wxappid, // 必填,公众号的唯一标识
          timestamp: +data.wxTimestamp || data.wxtimestamp , // 必填,生成签名的时间戳
          nonceStr: data.wxNonceStr || data.wxnonceStr, // 必填,生成签名的随机串
          signature: data.wxSignature || data.wxsignature,// 必填,签名
          jsApiList: ['onMenuShareTimeline'], // 必填,需要使用的JS接口列表
          openTagList: ['wx-open-launch-weapp'] // 可选,需要使用的开放标签列表,例如['wx-open-launch-app']
        });
      })
        // wx.ready(function () {
        // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中
        //   alert('ready success');
        // });
    	
      // wx.error(function (res) {
        // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名
      //   alert('err ===>', res);
      // });
    </script>
    <div style="height: 60px;">
      <wx-open-launch-weapp
        id="launch-btn"
        username="gh_9ec21d5fcb2d"
       >
        <!-- <script type="text/wxtag-template"> -->
          <template>
            <style>.btn { padding: 12px }</style>
            <button class="btn">打开小程序</button>            
          </template>
        <!-- </script> -->
      </wx-open-launch-weapp>
      <script>
        var btn = document.getElementById('launch-btn');
        var path = "/pages/mall/mall.html"
        btn.addEventListener('launch', function (e) {
          //alert('success');
        });
        btn.addEventListener('ready', function (e) {
          btn.setAttribute("path",path);
          console.log(btn.path)
          
          //alert('ready');
        });
        btn.addEventListener('error', function (e) {
          //alert('fail', e.detail);
        });
      </script>
    </div>
  </body>
</html>

小程序webview跳转自己的原生页面:wx.miniProgram.navigateTo

小程序webview跳转第三方小程序,需要appId:先跳转小程序原生页面,原生页面再跳转第三方小程序

非微信环境->小程序

urlSheme,需要后端配合