vue h5 微信分享功能(艾思科技出品)

  1. 首先安装插件
npm install weixin-jsapi
  1. 在项目目录新建wx.js文件
    在这里插入图片描述
    wx.js代码
import wx from 'weixin-jsapi' //引入刚才下载的包
import {getWxConfig} from '../api/red_package/index' //导入自己写的后台接口,获取wx配置

const ShareImpl = function (option) {
    //option是分享的配置内容*/
    const url = window.location.href.split("#")[0]; //获取当前的url
  
    getWxConfig({url:url}).then(res => { //请求后台接口
        console.log(res)
        wx.config({
            debug: true, //是否开启debug
            appId: res.data.appId,//你的appid
            timestamp: res.data.timestamp, //时间戳
            nonceStr: res.data.nonceStr,//随机字符串
            signature: res.data.signature,//秘钥(秘钥生成规则请看我另一篇文章(php获取微信AccessToken,生成秘钥))
            jsApiList: [ //请求的api接口列表,微信的,详情去看微信官方文档
                    'updateAppMessageShareData',
                    'updateTimelineShareData',
                    'getLocation',
                    'openLocation',
                    'onMenuShareAppMessage',
                    'onMenuShareTimeline'
            ]
        });
        wx.ready(function () { //如果wx.config配置正常,会自动走此处
            console.log('ready执行完毕')
            //自定义“分享给朋友”及“分享到QQ”按钮的分享内容
            wx.updateAppMessageShareData({
                title: option.shareTitle,//分享的标题
                desc: option.shareDesc,//分享的描述信息
                link: option.shareUrl,//分享的url
                imgUrl: option.shareImg//分享的图片
            });
            //自定义“分享到朋友圈”及“分享到QQ空间”按钮的分
            wx.updateTimelineShareData({
                title: option.shareTitle,
                desc: option.shareDesc,
                link: option.shareUrl,
                imgUrl: option.shareImg,
            });
        });
        wx.error(function (res) { //如果wx.config配置失败,会走到此处
            console.log('error',res);
        })
    });

};

export default ShareImpl //将此方法导出
  1. 在main.js中引入刚才写好的方法
    在这里插入图片描述
  2. 在需要分享的页面中调用
  mounted() {
            var option = {
                shareTitle : '',
                shareDesc : '',
                shareUrl : '',
                shareImg: ''
            };
            this.ShareImpl(option)
        },
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值