如果不處理,很丑。
加上下面的代碼,就會好看多了。
<script>
// 初始化
wx.config({
debug: false,
appId: '{$weixin.appId}',
timestamp: '{$weixin.timestamp}',
nonceStr: '{$weixin.nonceStr}',
signature: '{$weixin.signature}',
jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline']
});
// 處理分享
wx.ready(function () {
// 在頁面加載時就調用相關接口,如:分享到朋友圈、獲取位置信息等接口
wx.checkJsApi({
jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline'],
success: function (res) {
if (res.errMsg == 'checkJsApi:ok') {
// 分享給朋友
wx.onMenuShareAppMessage({
title: '{$vote_info.title}', // 分享標題
desc: '{$vote_info.sub_title}', // 分享描述
link: '{$share_url}', // 分享鏈接,該鏈接域名或路徑必須與當前頁面對應的公眾號JS安全域名一致
imgUrl: '{$vote_info.share_img}', // 分享圖標
type: '', // 分享類型,music、video或link,不填默認為link
dataUrl: '', // 如果type是music或video,則要提供數據鏈接,默認為空
// 用戶取消已經監聽不到了,不管取消與否,都是成功的
success: function () {
alert("恭喜你,分享成功");
},
// 用戶取消分享后執行的回調函數
cancel: function () {
alert("取消分享");
}
});
// 分享到朋友圈
wx.onMenuShareTimeline({
title: '{$vote_info.title}', // 分享時的標題
link: '{$share_url}', // 分享時的鏈接,該鏈接域名或路徑必須與當前頁面對應的公眾號JS安全域名一致
imgUrl: '{$vote_info.share_img}', // 分享時顯示的圖標
// 用戶取消已經監聽不到了,不管取消與否,都是成功的
success: function () {
alert("恭喜你,分享成功");
},
// 用戶取消分享后執行的回調函數
cancel: function () {
alert("取消分享");
}
});
} else {
alert("失敗了.......");
}
}
});
});
// 初始化錯誤監聽
wx.error(function (res) {
alert("error");
});
</script>