Instantly share code, notes, and snippets.
Created
September 25, 2017 09:47
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save awesee/178676fbaced92f38b15bef3833ed1c0 to your computer and use it in GitHub Desktop.
微信分享js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function ($) { | |
$(function () { | |
//用户自定义配置,可传入配置参数覆盖默认设置 | |
wechatShare({ | |
apiUrl: '/', //获取签名api | |
title: '分享标题', // 分享标题 | |
desc: '分享描述', // 分享描述 | |
link: window.location.href, // 分享链接 | |
imgUrl: window.location.origin + '/public/images/wechat_share.png' // 分享图标 | |
}); | |
}); | |
/** | |
* 以下代码请不要擅自修改 | |
* 可根据需求添加新功能 | |
* @param _config 用户自定义参数 | |
* @auth Sandy | |
* @version v1.0.0 | |
*/ | |
function wechatShare(_config) { | |
window.config = $.extend({ | |
debug: false, // 调试模式 | |
link: window.location.href, // 分享链接 | |
currentUrl: window.location.href.split('#')[0], // 当前url | |
jsApiList: [ | |
"onMenuShareTimeline", | |
"onMenuShareAppMessage", | |
"onMenuShareQQ", | |
"onMenuShareWeibo", | |
"onMenuShareQZone", | |
"startRecord", | |
"stopRecord", | |
"onVoiceRecordEnd", | |
"playVoice", | |
"pauseVoice", | |
"stopVoice", | |
"onVoicePlayEnd", | |
"uploadVoice", | |
"downloadVoice", | |
"chooseImage", | |
"previewImage", | |
"uploadImage", | |
"downloadImage", | |
"translateVoice", | |
"getNetworkType", | |
"openLocation", | |
"getLocation", | |
"hideOptionMenu", | |
"showOptionMenu", | |
"hideMenuItems", | |
"showMenuItems", | |
"hideAllNonBaseMenuItem", | |
"showAllNonBaseMenuItem", | |
"closeWindow", | |
"scanQRCode", | |
"chooseWXPay", | |
"openProductSpecificView", | |
"addCard", | |
"chooseCard", | |
"openCard" | |
], | |
onMenuShareTimeline: { | |
title: _config.title, // 分享标题 | |
link: _config.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 | |
imgUrl: _config.imgUrl, // 分享图标 | |
success: function () { | |
// 用户确认分享后执行的回调函数 | |
}, | |
cancel: function () { | |
// 用户取消分享后执行的回调函数 | |
} | |
}, | |
onMenuShareAppMessage: { | |
title: _config.title, // 分享标题 | |
desc: _config.desc, // 分享描述 | |
link: _config.link, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 | |
imgUrl: _config.imgUrl, // 分享图标 | |
type: '', // 分享类型,music、video或link,不填默认为link | |
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空 | |
success: function () { | |
// 用户确认分享后执行的回调函数 | |
}, | |
cancel: function () { | |
// 用户取消分享后执行的回调函数 | |
} | |
}, | |
onMenuShareQQ: { | |
title: _config.title, // 分享标题 | |
desc: _config.desc, // 分享描述 | |
link: _config.link, // 分享链接 | |
imgUrl: _config.imgUrl, // 分享图标 | |
success: function () { | |
// 用户确认分享后执行的回调函数 | |
}, | |
cancel: function () { | |
// 用户取消分享后执行的回调函数 | |
} | |
}, | |
onMenuShareWeibo: { | |
title: _config.title, // 分享标题 | |
desc: _config.desc, // 分享描述 | |
link: _config.link, // 分享链接 | |
imgUrl: _config.imgUrl, // 分享图标 | |
success: function () { | |
// 用户确认分享后执行的回调函数 | |
}, | |
cancel: function () { | |
// 用户取消分享后执行的回调函数 | |
} | |
}, | |
onMenuShareQZone: { | |
title: _config.title, // 分享标题 | |
desc: _config.desc, // 分享描述 | |
link: _config.link, // 分享链接 | |
imgUrl: _config.imgUrl, // 分享图标 | |
success: function () { | |
// 用户确认分享后执行的回调函数 | |
}, | |
cancel: function () { | |
// 用户取消分享后执行的回调函数 | |
} | |
} | |
}, _config); | |
$.ajax({ | |
url: config.apiUrl, | |
dataType: 'json', | |
type: 'GET', | |
data: { | |
url: config.currentUrl | |
}, | |
success: function (res) { | |
if (res.error_code == 200) { | |
wx.config({ | |
debug: config.debug, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 | |
appId: res.data.appId, // 必填,公众号的唯一标识 | |
timestamp: res.data.timestamp, // 必填,生成签名的时间戳 | |
nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 | |
signature: res.data.signature,// 必填,签名,见附录1 | |
jsApiList: config.jsApiList// 必填,需要使用的JS接口列表,所有JS接口列表见附录2 | |
}); | |
//config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。 | |
wx.ready(function () { | |
//获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 | |
wx.onMenuShareTimeline(config.onMenuShareTimeline); | |
//获取“分享给朋友”按钮点击状态及自定义分享内容接口 | |
wx.onMenuShareAppMessage(config.onMenuShareAppMessage); | |
//获取“分享到QQ”按钮点击状态及自定义分享内容接口 | |
wx.onMenuShareQQ(config.onMenuShareQQ); | |
//获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口 | |
wx.onMenuShareWeibo(config.onMenuShareWeibo); | |
//获取“分享到QQ空间”按钮点击状态及自定义分享内容接口 | |
wx.onMenuShareQZone(config.onMenuShareQZone); | |
}); | |
//通过error接口处理失败验证 | |
wx.error(function (res) { | |
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 | |
console.log(res); | |
}); | |
} | |
} | |
}); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment