Vue项目“ 微信分享”自定义右下角的图标

npm install weixin-js-sdk --save

<script>
import wx from 'weixin-js-sdk'; // 引入wxjs
import { shares } from "@/api/course/training";

export default {  
  data () {
    return {    
      info: {},
      wechatConfig: null,      
    };
  },  
  created () {
    //console.log(this.$route.query.id)
    if(this.$route.query.id){
      this.handleView(this.$route.query.id)      
    }    
  },
  mounted() {
    document.title = '详情';        
  },
  methods: {
    getShareInfo(){
      let params = {
        url: location.href.split('#')[0] // 只需要传当前页面地址
      }
      shares(params).then(res => {
        this.wechatConfig = res
        // 在获取到配置信息之后,调用微信Js-SDK的初始化函数
        this.initWechatJsSDK()
      });
    },
    initWechatJsSDK () {
      // 设置微信Js-SDK的配置信息
      wx.config({
        debug: false,
        appId: this.wechatConfig.appId,
        timestamp: this.wechatConfig.timestamp,
        nonceStr: this.wechatConfig.nonceStr,
        signature: this.wechatConfig.signature,
        jsApiList: [
          // 需要使用的微信JS接口列表
          'updateAppMessageShareData',
          //'onMenuShareTimeline',
          // ...
        ]
      })

      // 配置完成后调用微信Js-SDK的ready函数
      wx.ready(() => {
        // 在ready函数中可以调用微信Js-SDK的其他接口
        // 设置分享给朋友的功能
        wx.updateAppMessageShareData({
          title: this.info.name,
          desc: '智创童趣-幼儿园体育数字化智慧平台',
          link: location.href.split('#')[0],
          imgUrl: this.info.cover,
          success: () => {
            // 分享成功的回调函数
          },
          cancel: () => {
            // 用户取消分享的回调函数
          }
        })
      })

      // 在配置出错的情况下调用错误处理函数
      wx.error((err) => {
        console.error(err)
      })
    },

    handleView(id){
      getTrainingDetail(id).then(res => {
        if(res.code == 200){
          this.info = res.data
          if(this.info.name.length > 12){
            document.title = this.info.name.substring(0, 12);
          }else{
            document.title = this.info.name
          }
          this.coverImage = this.info.cover
          this.extractImages()
          this.getShareInfo(); // 调用分享的事件
        }
      });
    }
  }
};
</script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值