微信小程序和H5之间相互跳转

时间:2024-02-26 10:16:33

1.微信小程序跳转小程序 wx.navigateToMiniProgram

<script src=\'https://res.wx.qq.com/open/js/jweixin-1.3.0.js\'></script>

navigateToMiniProgram(mAppId)
{
  wx.navigateToMiniProgram({
    appId: mAppId,
    path: \'page/index/index?id=123\',
    extraData: {
      foo: \'bar\'
    },
    envVersion: \'release\',
    success(res) {
    // 打开成功
    ret_handler(res);
    },
    fail(res) {
    }
  })
},

2.h5跳转到小程序wx.miniProgram.navigateTo

<script src=\'https://res.wx.qq.com/open/js/jweixin-1.3.0.js\'></script>
// 点击立即体验跳转到小程序(如果要跳转的小程序路径是tab页就用wx.miniProgram.switchTab,否则无效)
$(\'.experience\').click(function () {
  wx.miniProgram.navigateTo({
    url: \'/pages/index/index\',
  });
})

3.小程序里打开h5页面(webview)

  小程序里代码:    

    <web-view src=\'{{url}}\'></web-view>
    
  let token = encodeURI(wx.getStorageSync(\'token\'));
  let openid = encodeURI(wx.getStorageSync(\'userinfo\').openid);
  that.setData({
    url: pageUrl + \'?token=\' + token + \'&openid=\' + openid,
  })
 
  h5代码:
    
   function getQueryString(name) {
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                var r = window.location.search.substr(1).match(reg);
                if (r != null) {
                    return decodeURIComponent(r[2]);
                }
                return null;
            }
            let token = getQueryString("token") || "";
            let openid = getQueryString("openid") || "";