您的位置:首页 > 移动开发 > 微信开发

微信自定义分享功能

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>

  通过上述的配置,就可以实现自定义分享的配置了。
  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  微信