微信自定义分享功能
2015-12-31 14:20
561 查看
微信给我们提供了自定义的分享平台。
一、绑定域名:
登录微信公众平台,进入“公众号设置”的“功能设置”里填写“JS接口安全域名”
二、引入JS文件
在需要调用JS接口的页面引入JS文件:http://res.wx.qq.com/open/js/jweixin-1.0.0.js
三、通过config接口注入权限验证配置
在需要自定义分享的页面必须注入配置信息:
例如:
<span style="font-size:18px;"> wx.config({
debug: false,
appId: '${map.appId}',
timestamp: '${map.timestamp}',
nonceStr: '${map.nonceStr}',
signature: '${map.signature}',
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'translateVoice',
'startRecord',
'stopRecord',
'onRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType',
'openLocation',
'getLocation',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode',
'chooseWXPay',
'openProductSpecificView',
'addCard',
'chooseCard',
'openCard'
]
});
</span>
其中:appId 必填,是公众号的唯一标识;timestamp必填,生成签名的时间戳;nonceStr必填,生成签名的随机串(例如:UUID);signature必填,需要签名机制来生成;jsApiList:[]必填,需要使用的JS接口列表。
四、通过ready接口处理成功验证:
<span style="font-size:18px;"> wx.ready(function () {
var shareData = {
title: '${newsItem.title}',//分享标题
desc: '${newsItem.description}',//分享描述
link: '${url}',//分享地址
imgUrl:'${shareImgUrl}'//分享图标
};
wx.onMenuShareAppMessage({
title: '${newsItem.title}',
desc: '${newsItem.description}',
link: '${url}',
imgUrl:'${shareImgUrl}',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
// alert('用户点击发送给朋友');
},
success: function (res) {
//alert('已分享');用户确认分享后成功回调
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
wx.onMenuShareTimeline(shareData);
// 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮
wx.hideMenuItems({
menuList: [
'menuItem:copyUrl'
]
});
});</span>五、通过error接口处理失败验证:
<span style="font-size:18px;"> wx.error(function (res) {
alert("error: "+ res.errMsg);
});
</span>
通过上述的配置,就可以实现自定义分享的配置了。
一、绑定域名:
登录微信公众平台,进入“公众号设置”的“功能设置”里填写“JS接口安全域名”
二、引入JS文件
在需要调用JS接口的页面引入JS文件:http://res.wx.qq.com/open/js/jweixin-1.0.0.js
三、通过config接口注入权限验证配置
在需要自定义分享的页面必须注入配置信息:
例如:
<span style="font-size:18px;"> wx.config({
debug: false,
appId: '${map.appId}',
timestamp: '${map.timestamp}',
nonceStr: '${map.nonceStr}',
signature: '${map.signature}',
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'translateVoice',
'startRecord',
'stopRecord',
'onRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType',
'openLocation',
'getLocation',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode',
'chooseWXPay',
'openProductSpecificView',
'addCard',
'chooseCard',
'openCard'
]
});
</span>
其中:appId 必填,是公众号的唯一标识;timestamp必填,生成签名的时间戳;nonceStr必填,生成签名的随机串(例如:UUID);signature必填,需要签名机制来生成;jsApiList:[]必填,需要使用的JS接口列表。
四、通过ready接口处理成功验证:
<span style="font-size:18px;"> wx.ready(function () {
var shareData = {
title: '${newsItem.title}',//分享标题
desc: '${newsItem.description}',//分享描述
link: '${url}',//分享地址
imgUrl:'${shareImgUrl}'//分享图标
};
wx.onMenuShareAppMessage({
title: '${newsItem.title}',
desc: '${newsItem.description}',
link: '${url}',
imgUrl:'${shareImgUrl}',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
// alert('用户点击发送给朋友');
},
success: function (res) {
//alert('已分享');用户确认分享后成功回调
},
cancel: function (res) {
//alert('已取消');
},
fail: function (res) {
alert(JSON.stringify(res));
}
});
wx.onMenuShareTimeline(shareData);
// 要隐藏的菜单项,只能隐藏“传播类”和“保护类”按钮
wx.hideMenuItems({
menuList: [
'menuItem:copyUrl'
]
});
});</span>五、通过error接口处理失败验证:
<span style="font-size:18px;"> wx.error(function (res) {
alert("error: "+ res.errMsg);
});
</span>
通过上述的配置,就可以实现自定义分享的配置了。
相关文章推荐
- 社交巨头三国杀:微信、WhatsApp、Line到底有啥区别?
- 微信悄悄升级群聊功能:个人微信营销号的福音
- 突击部队拼多多
- 我是运营,我没有假期
- 如何做到日消息量100万的微信公众号?
- 论微信取消推送功能的可能性(原创)
- 微信的成功,靠的是QQ导流吗?
- 微信服务号推送模板消息接口
- PHP限制页面只能在微信自带浏览器访问的代码
- HTML5实现微信拍摄上传照片功能
- 微信小程序去哪里找 小程序到底如何使用(附小程序名单)
- 使用jQuery.wechat构建微信WEB应用
- nw.js实现类似微信的聊天软件
- jquery1.8版本使用ajax实现微信调用出现的问题分析及解决办法
- JS版微信6.0分享接口用法分析
- 大家在抢红包,程序员在研究红包算法
- js如何判断用户是否是用微信浏览器