vue开发微信自定义分享记录

微信官方文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#2

项目中单独写了个wechat.js作为全局引入的,因为不只一个页面需要做分享,根据需要开发即可

import Vue from 'vue';
import wx from 'weixin-js-sdk';

Vue.use(wx);

export default {
  data() {
    return {
      // jsApiList: ['updateAppMessageShareData', 'updateTimelineShareData', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo', 'onMenuShareQZone', 'scanQRCode', 'getLocation']
    };
  },
  methods: {
    wxConfig() {
      let _this = this;
      let params = new FormData();
      params.append('url', window.location.href.split('#')[0]);
      _this.$http.post('/api/wechat/jssdk', params).then(function (_lists) {
        wx.config({
          debug: false, // true 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
          appId: _lists.appId, // 必填,公众号的唯一标识
          timestamp: _lists.timestamp, // 必填,生成签名的时间戳
          nonceStr: _lists.nonceStr,  // 必填,生成签名的随机串
          signature: _lists.signature, // 必填,签名
          jsApiList: _this.jsApiList // 必填,需要使用的JS接口列表
        });
      });
    },
    wxShare(data) {
      let _this = this;
      _this.wxConfig();      
      wx.ready(function () {  // ready接口处理成功验证
        let title = data.title;
        let desc = data.desc;
        let link = data.link;
        let imgUrl = data.imgUrl;
        if (wx.onMenuShareAppMessage) {
          wx.onMenuShareAppMessage({
            title: title,
            desc: desc,
            link: link,
            imgUrl: imgUrl,
            type: '', // 分享类型,music、video或link,不填默认为link
            dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
            success: function () {
              _this.addPoint();
            }
          });
          wx.onMenuShareTimeline({
            title: title,
            link: link,
            imgUrl: imgUrl,
            success: function () {
              console.log('onMenuShareTimeline');
            }
          });
        } else {
          wx.updateAppMessageShareData({
            title: title, // 分享标题
            desc: desc, // 分享描述
            link: link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: imgUrl, // 分享图标
            success: function () {
              console.log('updateAppMessageShareData');
            }
          });
          wx.updateTimelineShareData({
            title: title,
            desc: desc, // 分享描述
            link: link,
            imgUrl: imgUrl,
            success: function () {
              console.log('updateTimelineShareData');
            }
          });
        }
        wx.onMenuShareQQ({
          title: title,
          desc: desc,
          link: link,
          imgUrl: imgUrl,
          success: function () {
            console.log('onMenuShareQQ');
          }
        });
        wx.onMenuShareWeibo({
          title: title,
          desc: desc,
          link: link,
          imgUrl: imgUrl,
          success: function () {
            console.log('onMenuShareWeibo');
          }
        });
        wx.onMenuShareQZone({
          title: title,
          desc: desc,
          link: link,
          imgUrl: imgUrl,
          success: function () {
            console.log('onMenuShareQZone');
          }
        });
      });
    }
  }
};

main.js文件

import wechat from '@/mixins/wechat';
Vue.mixin(wechat);// 混入

接下来只要在需要分享的页面引入就可

async created() {
      this.isWxBrowser = this.isWeixin();
      if (this.isWxBrowser) {
        // 分享
        const shareData = {
          title: '自定义分享标题分享标题分享标题', // 分享标题
          desc: '自定义分享内容分享内容分享内容分享内容分享内容分享内容分享内容', // 分享内容
          link: 'http://xxx.xxxx.xxx/wxLogin', // 分享链接
          imgUrl: 'http://xxx.xxxx.xxx/static/image/userIcon.fc181a0.png' // 分享内容显示的图片(图片必须是正方形的链接)
        };
        this.wxShare(shareData);
      }
    },

注意:分享链接和分享的图片地址必须和配置的安全域名一致,否则自定义分享无效的!!!

中间踩了很多坑,仔细看看官方文档多多百度总能找到解决办法的!!!

另补充:微信H5页面内容缓存问题,安卓ok IOS有问题

解决方法:用!window.location.href="...."来跳转传参。

https://www.cnblogs.com/golddemon/p/9445134.html

https://www.jianshu.com/p/eac41f8b0896

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值