H5跳回微信小程序页面

官方文档

JSSDK

// web-view下的页面内
function ready() {
  console.log(window.__wxjs_environment === 'miniprogram') // true
}
if (!window.WeixinJSBridge || !WeixinJSBridge.invoke) {
  document.addEventListener('WeixinJSBridgeReady', ready, false)
} else {
  ready()
}

// 或者
wx.miniProgram.getEnv(function(res) {
  console.log(res.miniprogram) // true
})

H5 跳转微信小程序页面

H5(uniapp)

  • App.vue 引入 jweixin-1.3.2.js
onLaunch(option) {
  // #ifdef H5
  const script = document.createElement('script')
  script.src = 'https://res.wx.qq.com/open/js/jweixin-1.3.2.js'
  script.type = 'text/javascript'
  document.body.appendChild(script)
  // #endif

}
  • index.vue 跳转小程序页并带上参数
jumpMiniPage() {
  const token = ''
  const id = 1
  wx.miniProgram.getEnv((res) => {
    if (res.miniprogram) {
      wx.miniProgram.navigateTo({
        url: `/pages/payment/index?token=${token}&id=${id}`
      })
    }
  })
}

微信小程序

  • 通过 web-view 加载 H5
  • /pages/index/index
<view>
  <web-view src="{{url}}"></web-view>
</view>
data: {
  baseUrl: 'https://域名',
  url: null
},
onLoad(options) {
  console.log(options)
  const {token, id} = options
  const {baseUrl} = this.data
  if(token !== undefined) {
    // 将 token 与 id 传回H5页面	
    this.setData({url: `${baseUrl}?token=${token}&id=${id}`})
  } else {
    this.setData({url: baseUrl})
  }
},
  • /pages/payment/index 页面
data: {
  token: null,
  id: null
},
onLoad(options) {
  console.log(options)
  this.setData({...options})
  const {token, id} = options
  // 小程序业务逻辑

},
jumpIndexPage() {
  const {token, id} = this.data
  wx.redirectTo({
    url: `/pages/index/index?token=${token}&id=${id}`
  })
}

本文作者:逢生博客

本文链接:https://www.cnblogs.com/wufengsheng/p/17960160

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   逢生博客  阅读(172)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起
  1. 1 晚安 顏人中
  2. 2 出山 花粥 / 王胜娚
  3. 3 我们俩 郭顶
  4. 4 日落大道 梁博
  5. 5 像鱼 王贰浪
  6. 6 把回忆拼好给你 王贰浪
  7. 7 时光背面的我 刘至佳 / 韩瞳
  8. 8 愿你余生漫长 王贰浪
  9. 9 追寻你 王天戈 / 川青
  10. 10 夜空中最亮的星 逃跑计划
  11. 11 孤勇者 陈奕迅
  12. 12 不为谁而作的歌 林俊杰
  13. 13 消愁 毛不易
  14. 14 这一生关于你的风景 隔壁老樊
晚安 - 顏人中
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.