vue微信分享的实现(在当前页面分享其他页面)

2022-10-20,,

首先以分享给朋友为例

1、先看官方文档

wx.onmenushareappmessage({

  title: '', // 分享标题

  desc: '', // 分享描述

  link: '', // 分享链接,该链接域名或路径必须与当前页面对应的公众号js安全域名一致

  imgurl: '', // 分享图标

  type: '', // 分享类型,music、video或link,不填默认为link

  dataurl: '', // 如果type是music或video,则要提供数据链接,默认为空

  success: function () {

    // 用户确认分享后执行的回调函数

  },

  cancel: function () {

    // 用户取消分享后执行的回调函数

  }

});

2、vue分享踩的坑

* 1、微信分享中获取动态的url
* 2、 微信二次分享自动添加的参数     form=singlemessage
* 3、vue中各个页面都可以调用分享

3、直接代码分析

为了保证每个页面都可以调起微信分享,需要在vue根组件中,添加 watch监听

代码

watch: {
    // 监听 $route 变化调用分享链接
    "$route"(to, from) {
      let currentrouter = this.$router.currentroute.fullpath;  //
      if(currentrouter.indexof('usershare') == -1){   //如果不是usershare分享页面,则分享另外一个接口
        this.shareout();
      }else{
        this.shareouttwo();     //当前页面是usershare页面时分享调用另外一个接口   
      }
    }
  },

4、shareout()函数

      let signstr = '';      //sha1加密字符串
      let timestamp = 1473254558; //时间戳
      let noncestr = 'shupao';
      var obj = {
        title:"",        //标题
        desc:"文字描述",     //描述
        link:"http://www.xxxxxx.com/wx/pub/sr/simpleregister.do",
        imgurl:"http://xxxxxxxxx.com/picactive.jpg"
      };
      this.$ydkajax({
        sentype: "get",
        url: this.$domain + '/wx/pub/common/getjsapiticket.json', //自己服务器获取jsapi_ticket接口
        params: null,
        successfc: (response) => {
          //拼接sha1加密字符串
          signstr = 'jsapi_ticket=' + response.data.data + '&noncestr=' + noncestr + '&timestamp=' + timestamp + '&url=' + window.location.href;
          var signature = sha1(signstr);
          wx.config({
            debug: false,
            appid: "wx6957b3a945a05e90",   //appid
            timestamp: timestamp,      //时间戳
            noncestr: noncestr,       //加密需要字符串(自己定义的)    
            signature: signature,      //sha1加密后字符串
            jsapilist: [ 'onmenusharetimeline', 'onmenushareappmessage']
          });
          wx.ready(function () {
            //分享到朋友圈"
            wx.onmenusharetimeline({
              title: obj.title,
              link: obj.link, // 分享链接
              imgurl: obj.imgurl, // 分享图标
              success: function () {
                // console.log('分享到朋友圈成功')
              },
              cancel: function () {
                // console.log('分享到朋友圈失败')
              }
            });
            //分享给朋友
            wx.onmenushareappmessage({
              title: obj.title, // 分享标题
              desc: obj.desc, // 分享描述
              link: obj.link, // 分享链接
              imgurl: obj.imgurl, // 分享图标
              success: function () {
                // console.log('分享到朋友成功')
              },
              cancel: function () {
                // console.log('分享到朋友失败')
              }
            });
          })
        },
        islayer: false
      })

5、需要注意的事

*1、url是直接通过 window.location.href 获取的,不是使用 window.location.href.split(“#”)[0]来获取, 因为我的vue项目是通过hash模式来进行路由跳转的 , 直接使用 window.location.href.split(“#”)[0]会导致签名失败

//拼接sha1加密字符串
signstr = 'jsapi_ticket=' + response.data.data + '&noncestr=' + noncestr + '&timestamp=' + timestamp + '&url=' + window.location.href

*2、而且我们要在当前页面分享出去之后 , 其他用户打开之后 不是当前分享出去的页面 ,这就需要 调整 shareout()函数中 obj对象中的 link参数为其他页面链接

6、link参数

上述 5 问题中的加密字符串汇总的 url 和 分享对象中 link中的页面链接可以不用保持一样,因为本来就是要在当前页面分享出去其他页面的链接。网上我看到有人说这两个必须要保持一样,其实没有必要, 除非你只是简单的在vue项目中的其中一个页面做分享 , 然后只分享当前页面才需要让二者保持一致性。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

《vue微信分享的实现(在当前页面分享其他页面).doc》

下载本文的Word格式文档,以方便收藏与打印。