微信小程序和H5之間相互跳轉

1.微信小程序跳轉小程序 wx.navigateToMiniProgramweb

<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)微信

  小程序裏代碼:    app

    <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") || "";
相關文章
相關標籤/搜索