注意:
1. 所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
2. 如果发现在 Android 不能分享自定义内容,请到官网下载最新的包覆盖安装,Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
3. 常见问题及完整 JS-SDK 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
<div class&#61;"lbox_close wxapi_form"><h3 id&#61;"menu-basic">基础接口h3><span class&#61;"desc">判断当前客户端是否支持指定JS接口span><button class&#61;"btn btn_primary" id&#61;"checkJsApi">checkJsApibutton><h3 id&#61;"menu-share">分享接口h3><span class&#61;"desc">获取“分享到朋友圈”按钮点击状态及自定义分享内容接口span><button class&#61;"btn btn_primary" id&#61;"onMenuShareTimeline">onMenuShareTimelinebutton><span class&#61;"desc">获取“分享给朋友”按钮点击状态及自定义分享内容接口span><button class&#61;"btn btn_primary" id&#61;"onMenuShareAppMessage">onMenuShareAppMessagebutton><span class&#61;"desc">获取“分享到QQ”按钮点击状态及自定义分享内容接口span><button class&#61;"btn btn_primary" id&#61;"onMenuShareQQ">onMenuShareQQbutton><span class&#61;"desc">获取“分享到腾讯微博”按钮点击状态及自定义分享内容接口span><button class&#61;"btn btn_primary" id&#61;"onMenuShareWeibo">onMenuShareWeibobutton><h3 id&#61;"menu-image">图像接口h3><span class&#61;"desc">拍照或从手机相册中选图接口span><button class&#61;"btn btn_primary" id&#61;"chooseImage">chooseImagebutton><span class&#61;"desc">预览图片接口span><button class&#61;"btn btn_primary" id&#61;"previewImage">previewImagebutton><span class&#61;"desc">上传图片接口span><button class&#61;"btn btn_primary" id&#61;"uploadImage">uploadImagebutton><span class&#61;"desc">下载图片接口span><button class&#61;"btn btn_primary" id&#61;"downloadImage">downloadImagebutton><h3 id&#61;"menu-voice">音频接口h3><span class&#61;"desc">开始录音接口span><button class&#61;"btn btn_primary" id&#61;"startRecord">startRecordbutton><span class&#61;"desc">停止录音接口span><button class&#61;"btn btn_primary" id&#61;"stopRecord">stopRecordbutton><span class&#61;"desc">播放语音接口span><button class&#61;"btn btn_primary" id&#61;"playVoice">playVoicebutton><span class&#61;"desc">暂停播放接口span><button class&#61;"btn btn_primary" id&#61;"pauseVoice">pauseVoicebutton><span class&#61;"desc">停止播放接口span><button class&#61;"btn btn_primary" id&#61;"stopVoice">stopVoicebutton><span class&#61;"desc">上传语音接口span><button class&#61;"btn btn_primary" id&#61;"uploadVoice">uploadVoicebutton><span class&#61;"desc">下载语音接口span><button class&#61;"btn btn_primary" id&#61;"downloadVoice">downloadVoicebutton><h3 id&#61;"menu-smart">智能接口h3><span class&#61;"desc">识别音频并返回识别结果接口span><button class&#61;"btn btn_primary" id&#61;"translateVoice">translateVoicebutton><h3 id&#61;"menu-device">设备信息接口h3><span class&#61;"desc">获取网络状态接口span><button class&#61;"btn btn_primary" id&#61;"getNetworkType">getNetworkTypebutton><h3 id&#61;"menu-location">地理位置接口h3><span class&#61;"desc">使用微信内置地图查看位置接口span><button class&#61;"btn btn_primary" id&#61;"openLocation">openLocationbutton><span class&#61;"desc">获取地理位置接口span><button class&#61;"btn btn_primary" id&#61;"getLocation">getLocationbutton><h3 id&#61;"menu-webview">界面操作接口h3><span class&#61;"desc">隐藏右上角菜单接口span><button class&#61;"btn btn_primary" id&#61;"hideOptionMenu">hideOptionMenubutton><span class&#61;"desc">显示右上角菜单接口span><button class&#61;"btn btn_primary" id&#61;"showOptionMenu">showOptionMenubutton><span class&#61;"desc">关闭当前网页窗口接口span><button class&#61;"btn btn_primary" id&#61;"closeWindow">closeWindowbutton><span class&#61;"desc">批量隐藏功能按钮接口span><button class&#61;"btn btn_primary" id&#61;"hideMenuItems">hideMenuItemsbutton><span class&#61;"desc">批量显示功能按钮接口span><button class&#61;"btn btn_primary" id&#61;"showMenuItems">showMenuItemsbutton><span class&#61;"desc">隐藏所有非基础按钮接口span><button class&#61;"btn btn_primary" id&#61;"hideAllNonBaseMenuItem">hideAllNonBaseMenuItembutton><span class&#61;"desc">显示所有功能按钮接口span><button class&#61;"btn btn_primary" id&#61;"showAllNonBaseMenuItem">showAllNonBaseMenuItembutton><h3 id&#61;"menu-scan">微信扫一扫h3><span class&#61;"desc">调起微信扫一扫接口span><button class&#61;"btn btn_primary" id&#61;"scanQRCode0">scanQRCode(微信处理结果)button><button class&#61;"btn btn_primary" id&#61;"scanQRCode1">scanQRCode(直接返回结果)button><h3 id&#61;"menu-shopping">微信小店接口h3><span class&#61;"desc">跳转微信商品页接口span><button class&#61;"btn btn_primary" id&#61;"openProductSpecificView">openProductSpecificViewbutton><h3 id&#61;"menu-card">微信卡券接口h3><span class&#61;"desc">批量添加卡券接口span><button class&#61;"btn btn_primary" id&#61;"addCard">addCardbutton><span class&#61;"desc">调起适用于门店的卡券列表并获取用户选择列表span><button class&#61;"btn btn_primary" id&#61;"chooseCard">chooseCardbutton><span class&#61;"desc">查看微信卡包中的卡券接口span><button class&#61;"btn btn_primary" id&#61;"openCard">openCardbutton><h3 id&#61;"menu-pay">微信支付接口h3><span class&#61;"desc">发起一个微信支付请求span><button class&#61;"btn btn_primary" id&#61;"chooseWXPay">chooseWXPaybutton>div>
/** 注意&#xff1a;* 1. 所有的JS接口只能在公众号绑定的域名下调用&#xff0c;公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。* 2. 如果发现在 Android 不能分享自定义内容&#xff0c;请到官网下载最新的包覆盖安装&#xff0c;Android 自定义分享接口需升级至 6.0.2.58 版本及以上。* 3. 完整 JS-SDK 文档地址&#xff1a;http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html*/
wx.ready(function () {// 1 判断当前版本是否支持指定 JS 接口&#xff0c;支持批量判断document.querySelector(&#39;#checkJsApi&#39;).onclick &#61; function () {wx.checkJsApi({jsApiList: [&#39;getNetworkType&#39;,&#39;previewImage&#39;],success: function (res) {alert(JSON.stringify(res));}});};// 2. 分享接口// 2.1 监听“分享给朋友”&#xff0c;按钮点击、自定义分享内容及分享结果接口document.querySelector(&#39;#onMenuShareAppMessage&#39;).onclick &#61; function () {wx.onMenuShareAppMessage({title: &#39;互联网之子&#39;,desc: &#39;在长大的过程中&#xff0c;我才慢慢发现&#xff0c;我身边的所有事&#xff0c;别人跟我说的所有事&#xff0c;那些所谓本来如此&#xff0c;注定如此的事&#xff0c;它们其实没有非得如此&#xff0c;事情是可以改变的。更重要的是&#xff0c;有些事既然错了&#xff0c;那就该做出改变。&#39;,link: &#39;http://movie.douban.com/subject/25785114/&#39;,imgUrl: &#39;http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg&#39;,trigger: function (res) {// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容&#xff0c;因为客户端分享操作是一个同步操作&#xff0c;这时候使用ajax的回包会还没有返回alert(&#39;用户点击发送给朋友&#39;);},success: function (res) {alert(&#39;已分享&#39;);},cancel: function (res) {alert(&#39;已取消&#39;);},fail: function (res) {alert(JSON.stringify(res));}});alert(&#39;已注册获取“发送给朋友”状态事件&#39;);};// 2.2 监听“分享到朋友圈”按钮点击、自定义分享内容及分享结果接口document.querySelector(&#39;#onMenuShareTimeline&#39;).onclick &#61; function () {wx.onMenuShareTimeline({title: &#39;互联网之子&#39;,link: &#39;http://movie.douban.com/subject/25785114/&#39;,imgUrl: &#39;http://demo.open.weixin.qq.com/jssdk/images/p2166127561.jpg&#39;,trigger: function (res) {// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容&#xff0c;因为客户端分享操作是一个同步操作&#xff0c;这时候使用ajax的回包会还没有返回alert(&#39;用户点击分享到朋友圈&#39;);},success: function (res) {alert(&#39;已分享&#39;);},cancel: function (res) {alert(&#39;已取消&#39;);},fail: function (res) {alert(JSON.stringify(res));}});alert(&#39;已注册获取“分享到朋友圈”状态事件&#39;);};// 2.3 监听“分享到QQ”按钮点击、自定义分享内容及分享结果接口document.querySelector(&#39;#onMenuShareQQ&#39;).onclick &#61; function () {wx.onMenuShareQQ({title: &#39;互联网之子&#39;,desc: &#39;在长大的过程中&#xff0c;我才慢慢发现&#xff0c;我身边的所有事&#xff0c;别人跟我说的所有事&#xff0c;那些所谓本来如此&#xff0c;注定如此的事&#xff0c;它们其实没有非得如此&#xff0c;事情是可以改变的。更重要的是&#xff0c;有些事既然错了&#xff0c;那就该做出改变。&#39;,link: &#39;http://movie.douban.com/subject/25785114/&#39;,imgUrl: &#39;http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg&#39;,trigger: function (res) {alert(&#39;用户点击分享到QQ&#39;);},complete: function (res) {alert(JSON.stringify(res));},success: function (res) {alert(&#39;已分享&#39;);},cancel: function (res) {alert(&#39;已取消&#39;);},fail: function (res) {alert(JSON.stringify(res));}});alert(&#39;已注册获取“分享到 QQ”状态事件&#39;);};// 2.4 监听“分享到微博”按钮点击、自定义分享内容及分享结果接口document.querySelector(&#39;#onMenuShareWeibo&#39;).onclick &#61; function () {wx.onMenuShareWeibo({title: &#39;互联网之子&#39;,desc: &#39;在长大的过程中&#xff0c;我才慢慢发现&#xff0c;我身边的所有事&#xff0c;别人跟我说的所有事&#xff0c;那些所谓本来如此&#xff0c;注定如此的事&#xff0c;它们其实没有非得如此&#xff0c;事情是可以改变的。更重要的是&#xff0c;有些事既然错了&#xff0c;那就该做出改变。&#39;,link: &#39;http://movie.douban.com/subject/25785114/&#39;,imgUrl: &#39;http://img3.douban.com/view/movie_poster_cover/spst/public/p2166127561.jpg&#39;,trigger: function (res) {alert(&#39;用户点击分享到微博&#39;);},complete: function (res) {alert(JSON.stringify(res));},success: function (res) {alert(&#39;已分享&#39;);},cancel: function (res) {alert(&#39;已取消&#39;);},fail: function (res) {alert(JSON.stringify(res));}});alert(&#39;已注册获取“分享到微博”状态事件&#39;);};// 3 智能接口var voice &#61; {localId: &#39;&#39;,serverId: &#39;&#39;};// 3.1 识别音频并返回识别结果document.querySelector(&#39;#translateVoice&#39;).onclick &#61; function () {if (voice.localId &#61;&#61; &#39;&#39;) {alert(&#39;请先使用 startRecord 接口录制一段声音&#39;);return;}wx.translateVoice({localId: voice.localId,complete: function (res) {if (res.hasOwnProperty(&#39;translateResult&#39;)) {alert(&#39;识别结果&#xff1a;&#39; &#43; res.translateResult);} else {alert(&#39;无法识别&#39;);}}});};// 4 音频接口// 4.2 开始录音document.querySelector(&#39;#startRecord&#39;).onclick &#61; function () {wx.startRecord({cancel: function () {alert(&#39;用户拒绝授权录音&#39;);}});};// 4.3 停止录音document.querySelector(&#39;#stopRecord&#39;).onclick &#61; function () {wx.stopRecord({success: function (res) {voice.localId &#61; res.localId;},fail: function (res) {alert(JSON.stringify(res));}});};// 4.4 监听录音自动停止
wx.onVoiceRecordEnd({complete: function (res) {voice.localId &#61; res.localId;alert(&#39;录音时间已超过一分钟&#39;);}});// 4.5 播放音频document.querySelector(&#39;#playVoice&#39;).onclick &#61; function () {if (voice.localId &#61;&#61; &#39;&#39;) {alert(&#39;请先使用 startRecord 接口录制一段声音&#39;);return;}wx.playVoice({localId: voice.localId});};// 4.6 暂停播放音频document.querySelector(&#39;#pauseVoice&#39;).onclick &#61; function () {wx.pauseVoice({localId: voice.localId});};// 4.7 停止播放音频document.querySelector(&#39;#stopVoice&#39;).onclick &#61; function () {wx.stopVoice({localId: voice.localId});};// 4.8 监听录音播放停止
wx.onVoicePlayEnd({complete: function (res) {alert(&#39;录音&#xff08;&#39; &#43; res.localId &#43; &#39;&#xff09;播放结束&#39;);}});// 4.8 上传语音document.querySelector(&#39;#uploadVoice&#39;).onclick &#61; function () {if (voice.localId &#61;&#61; &#39;&#39;) {alert(&#39;请先使用 startRecord 接口录制一段声音&#39;);return;}wx.uploadVoice({localId: voice.localId,success: function (res) {alert(&#39;上传语音成功&#xff0c;serverId 为&#39; &#43; res.serverId);voice.serverId &#61; res.serverId;}});};// 4.9 下载语音document.querySelector(&#39;#downloadVoice&#39;).onclick &#61; function () {if (voice.serverId &#61;&#61; &#39;&#39;) {alert(&#39;请先使用 uploadVoice 上传声音&#39;);return;}wx.downloadVoice({serverId: voice.serverId,success: function (res) {alert(&#39;下载语音成功&#xff0c;localId 为&#39; &#43; res.localId);voice.localId &#61; res.localId;}});};// 5 图片接口// 5.1 拍照、本地选图var images &#61; {localId: [],serverId: []};document.querySelector(&#39;#chooseImage&#39;).onclick &#61; function () {wx.chooseImage({success: function (res) {images.localId &#61; res.localIds;alert(&#39;已选择 &#39; &#43; res.localIds.length &#43; &#39; 张图片&#39;);}});};// 5.2 图片预览document.querySelector(&#39;#previewImage&#39;).onclick &#61; function () {wx.previewImage({current: &#39;http://img5.douban.com/view/photo/photo/public/p1353993776.jpg&#39;,urls: [&#39;http://img3.douban.com/view/photo/photo/public/p2152117150.jpg&#39;,&#39;http://img5.douban.com/view/photo/photo/public/p1353993776.jpg&#39;,&#39;http://img3.douban.com/view/photo/photo/public/p2152134700.jpg&#39;]});};// 5.3 上传图片document.querySelector(&#39;#uploadImage&#39;).onclick &#61; function () {if (images.localId.length &#61;&#61; 0) {alert(&#39;请先使用 chooseImage 接口选择图片&#39;);return;}var i &#61; 0, length &#61; images.localId.length;images.serverId &#61; [];function upload() {wx.uploadImage({localId: images.localId[i],success: function (res) {i&#43;&#43;;alert(&#39;已上传&#xff1a;&#39; &#43; i &#43; &#39;/&#39; &#43; length);images.serverId.push(res.serverId);if (i < length) {upload();}},fail: function (res) {alert(JSON.stringify(res));}});}upload();};// 5.4 下载图片document.querySelector(&#39;#downloadImage&#39;).onclick &#61; function () {if (images.serverId.length &#61;&#61;&#61; 0) {alert(&#39;请先使用 uploadImage 上传图片&#39;);return;}var i &#61; 0, length &#61; images.serverId.length;images.localId &#61; [];function download() {wx.downloadImage({serverId: images.serverId[i],success: function (res) {i&#43;&#43;;alert(&#39;已下载&#xff1a;&#39; &#43; i &#43; &#39;/&#39; &#43; length);images.localId.push(res.localId);if (i < length) {download();}}});}download();};// 6 设备信息接口// 6.1 获取当前网络状态document.querySelector(&#39;#getNetworkType&#39;).onclick &#61; function () {wx.getNetworkType({success: function (res) {alert(res.networkType);},fail: function (res) {alert(JSON.stringify(res));}});};// 7 地理位置接口// 7.1 查看地理位置document.querySelector(&#39;#openLocation&#39;).onclick &#61; function () {wx.openLocation({latitude: 23.099994,longitude: 113.324520,name: &#39;TIT 创意园&#39;,address: &#39;广州市海珠区新港中路 397 号&#39;,scale: 14,infoUrl: &#39;http://weixin.qq.com&#39;});};// 7.2 获取当前地理位置document.querySelector(&#39;#getLocation&#39;).onclick &#61; function () {wx.getLocation({success: function (res) {alert(JSON.stringify(res));},cancel: function (res) {alert(&#39;用户拒绝授权获取地理位置&#39;);}});};// 8 界面操作接口// 8.1 隐藏右上角菜单document.querySelector(&#39;#hideOptionMenu&#39;).onclick &#61; function () {wx.hideOptionMenu();};// 8.2 显示右上角菜单document.querySelector(&#39;#showOptionMenu&#39;).onclick &#61; function () {wx.showOptionMenu();};// 8.3 批量隐藏菜单项document.querySelector(&#39;#hideMenuItems&#39;).onclick &#61; function () {wx.hideMenuItems({menuList: [&#39;menuItem:readMode&#39;, // 阅读模式&#39;menuItem:share:timeline&#39;, // 分享到朋友圈&#39;menuItem:copyUrl&#39; // 复制链接
],success: function (res) {alert(&#39;已隐藏“阅读模式”&#xff0c;“分享到朋友圈”&#xff0c;“复制链接”等按钮&#39;);},fail: function (res) {alert(JSON.stringify(res));}});};// 8.4 批量显示菜单项document.querySelector(&#39;#showMenuItems&#39;).onclick &#61; function () {wx.showMenuItems({menuList: [&#39;menuItem:readMode&#39;, // 阅读模式&#39;menuItem:share:timeline&#39;, // 分享到朋友圈&#39;menuItem:copyUrl&#39; // 复制链接
],success: function (res) {alert(&#39;已显示“阅读模式”&#xff0c;“分享到朋友圈”&#xff0c;“复制链接”等按钮&#39;);},fail: function (res) {alert(JSON.stringify(res));}});};// 8.5 隐藏所有非基本菜单项document.querySelector(&#39;#hideAllNonBaseMenuItem&#39;).onclick &#61; function () {wx.hideAllNonBaseMenuItem({success: function () {alert(&#39;已隐藏所有非基本菜单项&#39;);}});};// 8.6 显示所有被隐藏的非基本菜单项document.querySelector(&#39;#showAllNonBaseMenuItem&#39;).onclick &#61; function () {wx.showAllNonBaseMenuItem({success: function () {alert(&#39;已显示所有非基本菜单项&#39;);}});};// 8.7 关闭当前窗口document.querySelector(&#39;#closeWindow&#39;).onclick &#61; function () {wx.closeWindow();};// 9 微信原生接口// 9.1.1 扫描二维码并返回结果document.querySelector(&#39;#scanQRCode0&#39;).onclick &#61; function () {wx.scanQRCode();};// 9.1.2 扫描二维码并返回结果document.querySelector(&#39;#scanQRCode1&#39;).onclick &#61; function () {wx.scanQRCode({needResult: 1,desc: &#39;scanQRCode desc&#39;,success: function (res) {alert(JSON.stringify(res));}});};// 10 微信支付接口// 10.1 发起一个支付请求document.querySelector(&#39;#chooseWXPay&#39;).onclick &#61; function () {// 注意&#xff1a;此 Demo 使用 2.7 版本支付接口实现&#xff0c;建议使用此接口时参考微信支付相关最新文档。
wx.chooseWXPay({timestamp: 1414723227,nonceStr: &#39;noncestr&#39;,package: &#39;addition&#61;action_id%3dgaby1234%26limit_pay%3d&bank_type&#61;WX&body&#61;innertest&fee_type&#61;1&input_charset&#61;GBK¬ify_url&#61;http%3A%2F%2F120.204.206.246%2Fcgi-bin%2Fmmsupport-bin%2Fnotifypay&out_trade_no&#61;1414723227818375338&partner&#61;1900000109&spbill_create_ip&#61;127.0.0.1&total_fee&#61;1&sign&#61;432B647FE95C7BF73BCD177CEECBEF8D&#39;,signType: &#39;SHA1&#39;, // 注意&#xff1a;新版支付接口使用 MD5 加密paySign: &#39;bd5b1933cda6e9548862944836a9b52e8c9a2b69&#39;});};// 11.3 跳转微信商品页document.querySelector(&#39;#openProductSpecificView&#39;).onclick &#61; function () {wx.openProductSpecificView({productId: &#39;pDF3iY_m2M7EQ5EKKKWd95kAxfNw&#39;});};// 12 微信卡券接口// 12.1 添加卡券document.querySelector(&#39;#addCard&#39;).onclick &#61; function () {wx.addCard({cardList: [{cardId: &#39;pDF3iY9tv9zCGCj4jTXFOo1DxHdo&#39;,cardExt: &#39;{"code": "", "openid": "", "timestamp": "1418301401", "signature":"64e6a7cc85c6e84b726f2d1cbef1b36e9b0f9750"}&#39;},{cardId: &#39;pDF3iY9tv9zCGCj4jTXFOo1DxHdo&#39;,cardExt: &#39;{"code": "", "openid": "", "timestamp": "1418301401", "signature":"64e6a7cc85c6e84b726f2d1cbef1b36e9b0f9750"}&#39;}],success: function (res) {alert(&#39;已添加卡券&#xff1a;&#39; &#43; JSON.stringify(res.cardList));}});};// 12.2 选择卡券document.querySelector(&#39;#chooseCard&#39;).onclick &#61; function () {wx.chooseCard({cardSign: &#39;97e9c5e58aab3bdf6fd6150e599d7e5806e5cb91&#39;,timestamp: 1417504553,nonceStr: &#39;k0hGdSXKZEj3Min5&#39;,success: function (res) {alert(&#39;已选择卡券&#xff1a;&#39; &#43; JSON.stringify(res.cardList));}});};// 12.3 查看卡券document.querySelector(&#39;#openCard&#39;).onclick &#61; function () {alert(&#39;您没有该公众号的卡券无法打开卡券。&#39;);wx.openCard({cardList: []});};var shareData &#61; {title: &#39;微信JS-SDK Demo&#39;,desc: &#39;微信JS-SDK,帮助第三方为用户提供更优质的移动web服务&#39;,link: &#39;http://demo.open.weixin.qq.com/jssdk/&#39;,imgUrl: &#39;http://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRt8Qia4lv7k3M9J1SKqKCImxJCt7j9rHYicKDI45jRPBxdzdyREWnk0ia0N5TMnMfth7SdxtzMvVgXg/0&#39;};wx.onMenuShareAppMessage(shareData);wx.onMenuShareTimeline(shareData);
});wx.error(function (res) {alert(res.errMsg);
});