1、注册config。(config参数由后端接口提供)
2、info 分享的内容 。(包括链接,标题,图片)
3、debug模式是否开启。
4、处理非微信内部浏览器分享。
function wechatShare(config, info, debug, click) {
// config 配置分享的参数
// info 分享的内容
// debug 调试模式开启 or 关闭
// click 处理非微信浏览器分享
//分享配置
wx.config({
debug: debug ? debug : false,
appId: config.appid,
timestamp: config.timestamp,
nonceStr: config.nonceStr,
signature: config.signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'updateAppMessageShareData',
'updateTimelineShareData'
]
});
//点击后的分享
if(click) {
// 处理非微信内部浏览器分享
} else {
//分享内容
wx.ready(function() {
wx.checkJsApi({
jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'], //
success: function(res) {
// 以键值对的形式返回,可用的api值true,不可用为false
}
});
//
//在微信浏览器内的分享
// 分享到朋友圈
// wx.onMenuShareTimeline({
// title: info.title,
// desc: info.desc,
// link: info.link,
// imgUrl: info.imgUrl,
// }, function(res) {
// alert("分享朋友圈成功");
// });
// // 分享给好友
// wx.onMenuShareAppMessage({
// title: info.title,
// desc: info.desc,
// link: info.link,
// imgUrl: info.imgUrl,
// }, function(res) {
// alert("分享好友成功");
// });
//
wx.error(function(res) {
console.log(res.errMsg);
});
//
wx.updateAppMessageShareData({
title: info.title, // 分享标题
desc: info.desc, // 分享描述
link: info.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: info.imgUrl, // 分享图标
success: function () {
// 设置成功
}
})
wx.updateTimelineShareData({
title: info.title, // 分享标题
link: info.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: info.imgUrl, // 分享图标
success: function () {
// 设置成功
}
})
});
}
}