微信公众号分享配置
import Wx from './utils/js_sdk.js'//可下载js_sdk到本地,或者vue install下载
import ajax from './utils/request.js'//自己封装的axios
Vue.prototype.wxShare = function (myurl, title, desc, imgUrl,shareSuccess) {
let vm = this;
if (!window.initUrl) {//解决分享之后签名报错的问题https://blog.csdn.net/zsl471260400/article/details/80236569
window.initUrl = location.href.split('#')[0]
console.log(window.initUrl)
}
let urlname =window.initUrl
/**
* 获取微信token信息
* @return {[type]} [description]
*/
function getToken(callback) {
ajax('后端接口', {'参数'})
.then(json => {
if (json.code == 20000) {
Wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: json.data.appId, // 必填,公众号的唯一标识
timestamp: json.data.timestamp, // 必填,生成签名的时间戳
nonceStr: json.data.nonceStr, // 必填,生成签名的随机串
signature: json.data.signature,// 必填,签名,见附录1
jsApiList: ['updateTimelineShareData', 'updateAppMessageShareData', 'onMenuShareWeibo', 'onMenuShareQZone', 'hideMenuItems','showMenuItems','chooseImage','previewImage','uploadImage', 'downloadImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
callback();
} else {
// alert('分享功能暂不能使用')
}
}, () => {
// alert('网络异常,获取微信参数失败')
});
}
// 分享内容、标题、链接等配置
let shareConfig = {
title: title,
desc: desc,
link: myurl||'',
imgUrl: imgUrl,
success: function () {
if (shareSuccess) {
shareSuccess();
}
},
cancel: function () {
alert('分享已取消');
}
};
// 初始化分享函数调用
function initShare() {
Wx.ready(function () {
Wx.showMenuItems({
menuList: ['menuItem:copyUrl', 'menuItem:share:timeline', 'menuItem:share:appMessage','menuItem:share:qq','menuItem:share:QZone'] // 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮,所有menu项见附录3
});
// 分享到朋友圈
Wx.updateTimelineShareData(shareConfig);
// 分享给朋友
Wx.updateAppMessageShareData(shareConfig);
// 分享到QQ
Wx.onMenuShareQQ(shareConfig);
// 分享到腾讯微博
Wx.onMenuShareWeibo(shareConfig);
// 分享到QQ空间
Wx.onMenuShareQZone(shareConfig);
});
}
getToken(initShare);
};
页面使用:
let myurl=location.href;//分享链接地址
let title=this.goodsDetail.share_title||this.goodsDetail.goods_name;//分享标题
let desc=this.goodsDetail.share_content||this.goodsDetail.goods_title;//分享描述
let imgUrl=this.goodsDetail.share_img||this.goodsDetail.main_img//分享缩略图
this.wxShare(myurl, title, desc, imgUrl,true)