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

1.微信小程序跳转小程序 wx.navigateToMiniProgram
注意:小程序的app.json里
“navigateToMiniProgramAppIdList”: [
“你要跳转的小程序appid”,
],

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

// 点击立即体验跳转到小程序(如果要跳转的小程序路径是tab页就用wx.miniProgram.switchTab,否则无效)
$(’.btn’).click(function () {
  wx.miniProgram.navigateTo({
    url: ‘/pages/index/index’,
  });
})
3.小程序里打开h5页面(webview)

小程序里代码:
    
  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”) || “”;

你可能感兴趣的:(小程序)