首先我们配置一些基本参数:
wx.config({
debug: false,
appId: 'APPID',
timestamp: 'TIMESTAMP',
nonceStr: 'NONCESTR',
signature: 'SIGNATURE',
jsApiList: [
// 所有要调用的 API 都要加到这个列表中
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'translateVoice',
'startRecord',
'stopRecord',
'onVoiceRecordEnd',
'playVoice',
'onVoicePlayEnd',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType',
'openLocation',
'getLocation',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode',
'chooseWXPay',
'openProductSpecificView',
'addCard',
'chooseCard',
'openCard',
'editAddress',
'getLatestAddress'
]
});
其次把分享内容存入widnow对象:
window.wxinfo = {
'imgUrl': "分享图标",
'lineLink': "分享链接",
'shareTitle': "分享标题",
'descContent': "分享内容--温馨提示:内容需要去除换行,否则会导致js报错",
"setShareRecord":"成功回调",
};
把以下代码打包成.js文件引入到界面:
'use strict';
window.wxShare = {
init: function init() {
window.wxShare.methods.wxShareTo();
this.attachEvent();
},
attachEvent: function attachEvent() {
},
methods: {
wxShareTo: function wxShareTo() {
wx.ready(function () {
wx.checkJsApi({
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'onMenuShareQZone',
'hideMenuItems'
],
success: function (res) {
// alert(JSON.stringify(res));
}
});
wx.onMenuShareAppMessage({
title: window.wxinfo.shareTitle,
desc: window.wxinfo.descContent,
link: window.wxinfo.lineLink,
imgUrl: window.wxinfo.imgUrl,
trigger: function (res) {
// alert('用户点击发送给朋友');
},
success: function (res) {
window.wxShare.methods.setShareRecord("onMenuShareAppMessage",0);
// alert('已分享');
},
cancel: function (res) {
window.wxShare.methods.setShareRecord("onMenuShareAppMessage",1);
// alert('已取消');
},
fail: function (res) {
// alert(JSON.stringify(res));
}
});
wx.onMenuShareTimeline({
title: window.wxinfo.shareTitle,
desc: window.wxinfo.descContent,
link: window.wxinfo.lineLink,
imgUrl: window.wxinfo.imgUrl,
trigger: function (res) {
// alert('用户点击分享到朋友圈');
},
success: function (res) {
window.wxShare.methods.setShareRecord("onMenuShareTimeline",0);
// alert('已分享');
},
cancel: function (res) {
window.wxShare.methods.setShareRecord("onMenuShareTimeline",1);
// alert('已取消');
},
fail: function (res) {
// alert(JSON.stringify(res));
}
});
wx.onMenuShareQQ({
title: window.wxinfo.shareTitle,
desc: window.wxinfo.descContent,
link: window.wxinfo.lineLink,
imgUrl: window.wxinfo.imgUrl,
success: function () {
window.wxShare.methods.setShareRecord("onMenuShareQQ",0);
// 用户确认分享后执行的回调函数
},
cancel: function () {
window.wxShare.methods.setShareRecord("onMenuShareQQ",1);
// 用户取消分享后执行的回调函数
}
});
wx.onMenuShareWeibo({
title: window.wxinfo.shareTitle,
desc: window.wxinfo.descContent,
link: window.wxinfo.lineLink,
imgUrl: window.wxinfo.imgUrl,
success: function () {
window.wxShare.methods.setShareRecord("onMenuShareWeibo",0);
// 用户确认分享后执行的回调函数
},
cancel: function () {
window.wxShare.methods.setShareRecord("onMenuShareWeibo",1);
// 用户取消分享后执行的回调函数
}
});
wx.onMenuShareQZone({
title: window.wxinfo.shareTitle,
desc: window.wxinfo.descContent,
link: window.wxinfo.lineLink,
imgUrl: window.wxinfo.imgUrl,
success: function () {
window.wxShare.methods.setShareRecord("onMenuShareQZone",0);
// 用户确认分享后执行的回调函数
},
cancel: function () {
window.wxShare.methods.setShareRecord("onMenuShareQZone",1);
// 用户取消分享后执行的回调函数
}
});
});
},
setShareRecord: function setShareRecord(type,status) {
// 此分享记录回掉参数看自己业务需求改变
var data ={
title:window.wxinfo.shareTitle,
url:window.wxinfo.lineLink,
status:status,
type:type
};
$.post( window.wxinfo.setShareRecord,data);
}
}
};
window.wxShare.init();
使用方法: 在在每次改变分享内容的时候把window对象里面的分享设置也改变一下,然后执行:
//列如: window.wxinfo.lineLink = 'http:www.putdownon.com';
window.wxShare.methods.wxShareTo();