首先,无论是基于微信做什么,分享也好,上传图片也罢,第一步都要先设置config参数
var appId = "";
var timeStamp = "";
var nonceStr = "";
var signature = "";
$.ajax({
type: \'post\',
url: \'/Control/GetWeiXinParm\',
async: false,
data: { url:window.location.href},
success: function (data) {
appId = data.appId;
timeStamp = data.timeStamp;
nonceStr = data.nonceStr;
signature = data.signaTure;
}
});
参数设置完成后,开始设置微信调用的方法
if (window.wx) {
wx.config({
debug: false,//debug为true时,打开微信调试
appId: appId,
timestamp: timeStamp,
nonceStr: nonceStr,
signature: signature,
jsApiList: [
\'checkJsApi\',
\'openLocation\',
\'getLocation\',
\'onMenuShareTimeline\',//微信朋友圈
\'onMenuShareAppMessage\'//微信好友
]
});
参数这是完成后,开始分享接口的调用
wx.ready(function () {
wx.checkJsApi({
jsApiList: [
\'getLocation\',
\'onMenuShareTimeline\',
\'onMenuShareAppMessage\'
],
success: function (res) {
alert(JSON.stringify(res));
}
});
wx.onMenuShareAppMessage({
title: \'这是title\',
desc: \'这是详情\',
link: \'这是分享的链接\',
imgUrl: \'这是分享的图标\',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
//alert(\'用户点击发送给朋友\');
},
success: function (res) {
//alert(\'已分享\');
},
cancel: function (res) {
//alert(\'已取消\');
},
fail: function (res) {
//alert(JSON.stringify(res));
}
});
wx.onMenuShareTimeline({
title: \'这是title\',
link: \'这是分享的链接地址\',
imgUrl: \'这是分享的图标\',
trigger: function (res) {
// 不要尝试在trigger中使用ajax异步请求修改本次分享的内容,因为客户端分享操作是一个同步操作,这时候使用ajax的回包会还没有返回
//alert(\'用户点击分享到朋友圈\');
},
success: function (res) {
//alert(\'已分享\');
},
cancel: function (res) {
//alert(\'已取消\');
},
fail: function (res) {
//alert(JSON.stringify(res));
}
});
wx.error(function (res) {
//alert(res.errMsg);
});
});
}
好了,分享到此结束~