js 微信分享代码
微信分享是要在页面中引入 <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
var xAjax = function(attr, callback, error) {
$.ajax({
type: attr.type || "get",
url: attr.url,
data: attr.data || '',
dataType: 'json',
success: function(msg) {
if (msg) {
callback(msg);
}
},
error: function() {
if (error) {
error();
}
}
});
}
// shareContent();
var href = '';
var imgurl ='';
var title = '';
var uese_name = '';
var classify = '';
var bewrite ='';
var cur_title = '';
function shareContentForAndroid()
{
// alert("111111");
var result = colleague;
window.stub.jsMethod(result);//用接口stub, 通过调用内部类中的方法jsMethod给java传回result。
}
$(function(){
url = window.location.href;
function orderAdel(ops){
var url=window.location.href;
var obj=QueryString.parse(url.split('?')[1]);
for(var i=0;i<ops.length;i++){
delete obj[ops[i]];
}
//obj=objKeySort(obj)
var urlQuery=QueryString.stringify(obj);
return url.split('?')[0]+'?'+urlQuery;
}
imgurl = ‘图片url’;
title = 标题;
classify = 内容;
var getConfig = {
url:'后台定义的接口',
type: 'post',
datatype:"json",
data: {
'url':link
}
}
xAjax(getConfig, function(data) {
//console.log(data);
//$('#code').html(data.data.code);
var wx = data;
var shareConfig = {
shareTitle:标题,
descContent:内容,
link:链接 ,
imgUrl:图片
}
wxFn(wx, shareConfig);
})
function wxFn(wxConfig, attr) {
if (wxConfig) {
wx.config({
// debug:true,
appId: wxConfig.appId, //appid
timestamp: wxConfig.timestamp, //时间戳
nonceStr: wxConfig.nonceStr, //生成签名的随机串
signature: wxConfig.signature, // 必填,签名,见附录1
jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", 'previewImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
//console.log(wx.config.appId)
wx.ready(function() {
//console.log(attr.imgUrl)
var imgUrl = attr.imgUrl;
var lineLink = attr.link;
var descContent = attr.descContent;
var shareTitle = attr.shareTitle;
// 分享到朋友
wx.onMenuShareAppMessage({
title: shareTitle,
desc: descContent,
link: lineLink,
imgUrl: imgUrl,
type: 'link',
dataUrl: '',
success: function() {
// alert("chenggong");
},
cancel: function() {
//alert("shibai");
}
});
// 分享到朋友圈
wx.onMenuShareTimeline({
title: shareTitle,
link: lineLink,
imgUrl: imgUrl,
desc: descContent,
success: function() {},
cancel: function() {}
});
// 分享到qq
wx.onMenuShareQQ({
title: shareTitle,
link: lineLink,
imgUrl: imgUrl,
desc: descContent,
success: function() {},
cancel: function() {}
});
});
};
}
var xAjax = function(attr, callback, error) {
$.ajax({
type: attr.type || "get",
url: attr.url,
data: attr.data || '',
dataType: 'json',
success: function(msg) {
if (msg) {
callback(msg);
}
},
error: function() {
if (error) {
error();
}
}
});
}
// shareContent();
var href = '';
var imgurl ='';
var title = '';
var uese_name = '';
var classify = '';
var bewrite ='';
var cur_title = '';
function shareContentForAndroid()
{
// alert("111111");
var result = colleague;
window.stub.jsMethod(result);//用接口stub, 通过调用内部类中的方法jsMethod给java传回result。
}
$(function(){
url = window.location.href;
function orderAdel(ops){
var url=window.location.href;
var obj=QueryString.parse(url.split('?')[1]);
for(var i=0;i<ops.length;i++){
delete obj[ops[i]];
}
//obj=objKeySort(obj)
var urlQuery=QueryString.stringify(obj);
return url.split('?')[0]+'?'+urlQuery;
}
imgurl = ‘图片url’;
title = 标题;
classify = 内容;
var getConfig = {
url:'后台定义的接口',
type: 'post',
datatype:"json",
data: {
'url':link
}
}
xAjax(getConfig, function(data) {
//console.log(data);
//$('#code').html(data.data.code);
var wx = data;
var shareConfig = {
shareTitle:标题,
descContent:内容,
link:链接 ,
imgUrl:图片
}
wxFn(wx, shareConfig);
})
function wxFn(wxConfig, attr) {
if (wxConfig) {
wx.config({
// debug:true,
appId: wxConfig.appId, //appid
timestamp: wxConfig.timestamp, //时间戳
nonceStr: wxConfig.nonceStr, //生成签名的随机串
signature: wxConfig.signature, // 必填,签名,见附录1
jsApiList: ["onMenuShareTimeline", "onMenuShareAppMessage", "onMenuShareQQ", 'previewImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
//console.log(wx.config.appId)
wx.ready(function() {
//console.log(attr.imgUrl)
var imgUrl = attr.imgUrl;
var lineLink = attr.link;
var descContent = attr.descContent;
var shareTitle = attr.shareTitle;
// 分享到朋友
wx.onMenuShareAppMessage({
title: shareTitle,
desc: descContent,
link: lineLink,
imgUrl: imgUrl,
type: 'link',
dataUrl: '',
success: function() {
// alert("chenggong");
},
cancel: function() {
//alert("shibai");
}
});
// 分享到朋友圈
wx.onMenuShareTimeline({
title: shareTitle,
link: lineLink,
imgUrl: imgUrl,
desc: descContent,
success: function() {},
cancel: function() {}
});
// 分享到qq
wx.onMenuShareQQ({
title: shareTitle,
link: lineLink,
imgUrl: imgUrl,
desc: descContent,
success: function() {},
cancel: function() {}
});
});
};
}