vue h5跳转微信小程序

共有2个方法
方法1:wx-open-launch-weapp
**注意:
1.公众号必须是服务号
2.h5访问地址必须跟服务号绑定
满足这2个权限条件,要不然wx-open-launch-weapp不会被渲染,不显示等问题
**

(1).引入微信的sdk
在需要调用JS接口的页面引入如下JS文件:http://res.wx.qq.com/open/js/jweixin-1.6.0.js (支持https)
如需进一步提升服务稳定性,当上述资源不可访问时,可改访问:http://res2.wx.qq.com/open/js/jweixin-1.6.0.js (支持https)
备注:支持使用 AMD/CMD 标准模块加载方法加载。


(2)配置微信 config

//util/wxShare.js

const shareGlobal = async (url, title, link, imgUrl, desc, dataUrl) => {
  wx.checkJsApi({
    jsApiList: ['chooseImage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
    success(res) {
      // 以键值对的形式返回,可用的api值true,不可用为false
      // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
      console.log(`此客户端支持此版本:${res}`)
    },
    fail(err) {
      console.log(`当前客户端不支持此版本:${err}`)
    }
  })

  // 全局修改页面的分享信息,接口需要的url,标题,跳转路径,图片,描述,数据链接
  try {
    console.log('url :>> ', url)
    const path = url.split('#')[0] // 删除query参数
    console.log(
      {
        url,
        title,
        link,
        imgUrl,
        desc,
        dataUrl,
        path
      },
      'shareGlobal入参'
    )
    const response = await xxx({
      url: path
    }) // 获取微信参数
    if (response && response.signature) {
      // 如果有签名,注册微信接口
      console.log(response, '接口返回值')
      wx.config({
        debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
        appId: response.appId, // 必填,公众号的唯一标识
        timestamp: response.timestamp, // 必填,生成签名的时间戳
        nonceStr: response.nonceStr, // 必填,生成签名的随机串
        signature: response.signature, // 必填,签名
        jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline'], // 必填,需要使用的JS接口列表
        openTagList: ['wx-open-launch-weapp']
      })
      wx.ready(function () {
        // config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
        console.log(
          {
            url,
            title,
            link,
            imgUrl,
            desc,
            dataUrl,
            path
          },
          'wx.ready打印参数作用域'
        )
        wx.onMenuShareAppMessage({
          title, // 分享标题
          desc: desc || '', // 分享描述
          link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl, // 分享图标
          type: 'link', // 分享类型,music、video或link,不填默认为link
          dataUrl: dataUrl || '', // 如果type是music或video,则要提供数据链接,默认为空
          success(res) {
            // 用户点击了分享后执行的回调函数
            console.log(res, '转发给朋友成功')
          },
          fail(err) {
            console.log(`转发朋友失败:${err}`)
          }
        })
        wx.onMenuShareTimeline({
          title, // 分享标题
          link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl, // 分享图标
          success(res) {
            // 用户点击了分享后执行的回调函数
            console.log(res, '分享朋友圈成功')
          },
          fail(err) {
            console.log(`分享朋友圈:${err}`)
          }
        })
      })
      wx.error(function (res) {
        console.log(res, '执行了wx.error')
        // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
      })
    }
  } catch (error) {
    console.log(error, 'catch-error')
  }
}
export { shareGlobal }

//appid 小程序appid  
//username 是小程序的gh_xxx 
// appid 和username 可以二选一 appid 层级最高 其次是username

    
    

方法2 使用url Scheme
注意:这个跳转小程序只能正式环境跳转,体验版和开发版无法跳转
(1)封装slAwait.js

//util/slAwait.js 
export function slAwait(promise: Promise): Promise<[T, null] | [undefined, U]>
       {   return promise.then<[T, null]>((res: T) => [res,
       null]).catch<[undefined, U]>((err: U) => [undefined, err]) } ```
       

(2)通过后台接口获取微信link Scheme
查看微信小程序文档

   ///api/scheme
   export const getWXopenLink = (data) => {  
    return
       fetch({
           url: /user/app/weixin/getScheme',
           method: 'POST',
           data,
            }) } 

返回结果
vue h5跳转微信小程序_第1张图片

    
跳转小程序

你可能感兴趣的:(vue.js,微信小程序,前端)