1.微信分享使用jssdk调起分享功能
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
//分享
function jump2share(){
$.ajax({
type : 'GET',
url : 'xxx/jump2share',//后端提供的分享接口
data:{
urlwx:window.location.href
},
success : function(result) {
console.log(result)
// 0、准备
wx.config({
debug :false,
appId : result.appId, // 公众号的唯一标识
timestamp : result.timestamp , // 生成签名的时间戳
nonceStr : result.nonceStr , // 生成签名的随机串
signature : result.jsSignature ,// 签名
jsApiList : ['onMenuShareTimeline', 'onMenuShareAppMessage',
'onMenuShareWeibo', 'hideMenuItems']
// 需要使用的jsapi
});
// 1、验证通过ready接口
// 批量隐藏菜单项
wx.ready(function() {
wx.hideMenuItems({
menuList : [
'menuItem:copyUrl', // 复制链接
'menuItem:share:qq',// 分享到QQ:
'menuItem:openWithQQBrowser',// 在QQ浏览器中打开:
'menuItem:openWithSafari'// 在Safari中打开:
],
success : function(res) {
},
fail : function(res) {
}
});
// 分享到朋友圈
wx.onMenuShareTimeline({
title : result.shareTitle, // 分享标题
desc : result.shareDesc, // 分享描述
link : result.shareLink, // 分享链接
imgUrl : result.shareHeadImg, // 分享图标
success : function() {
//把分享次数加1
},
cancel : function() {
}
});
// 分享给朋友
wx.onMenuShareAppMessage({
title : result.shareTitle, // 分享标题
desc : result.shareDesc, // 分享描述
link : result.shareLink, // 分享链接
imgUrl : result.shareHeadImg, // 分享图标
type : '', // 分享类型,music、video或link,不填默认为link
dataUrl : '', // 如果type是music或video,则要提供数据链接,默认为空
success : function() {
},
cancel : function() {
}
});
// 分享到微博
wx.onMenuShareWeibo({
title : result.shareTitle, // 分享标题
desc : result.shareDesc, // 分享描述
link : result.shareLink, // 分享链接
imgUrl : result.shareHeadImg,// 分享图标
success : function() {
//把分享次数加1
},
cancel : function() {
}
});
// 批量隐藏功能按钮
wx.hideMenuItems({
menuList : ["menuItem:copyUrl"]
// “复制链接”功能
});
});
}
});
}
微信禁止分享
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
function onBridgeReady() {
WeixinJSBridge.call('hideOptionMenu');
}
if (typeof WeixinJSBridge == "undefined") {
if (document.addEventListener) {
document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
} else if (document.attachEvent) {
document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
}
} else {
onBridgeReady();
}
2.app分享
以10086app中的h5页面分享到微信为例
主要代码如下:
var appPlatform = new app86Platform();
/**
* 进行app页面分享
* @param imgUrl 分享图片的完整链接
* @param title 分享的标题
* @param url 分享的链接
* @param content 分享的内容
*/
function appShare(imgUrl, title, url, content) {
var json = {
"activityId": 711,
"type": 0,
"normaltem": {
"id": -1,
"img1": imgUrl,
"name": "10086",
"title": title,
"url": url,
"content": content
},
"specialList": []
};
appPlatform.dispatchShare([encodeURIComponent(JSON.stringify(json))]);
}
function share() {
$.ajax({
type: "post",
contentType: "application/json;charset=utf-8",
dataType: "json",
url: "xxx/getshare",
success: function (data) {
if (data.rtnCode==0) {
appShare(data.rtnurl , "分享标题", data.rtndata, "分享内容");
}
}
});
}