前端开发:
首先不能用ajax去请求微信授权接口(https://****/test/api/wx/wxLogin)
需要通过条件判断来进行跳转
微信授权:
$.ajax({
type: "get",
url: "https://****/test/api/wx/getUserInfo", //用户信息
data: {},
dataType: "json",
success: function (result) {
if(result.errno!=0){
window.location.href = "https://****/test/api/wx/wxLogin" //授权登录
}else{
id=result.data.id
}
},
error: function (result) {
// alert(result);
}
});
微信分享:
// 分享
var appid;
var timestamp;
var noncestr;
var signature;
var shareData = {
title:'1111' , 标题
desc: '2323', //描述
link : "https://*******/test/friend.html", //跳转的链接
imgUrl: '', //缩略图
success: function(){
}
};
$.ajax({
type:"get",
url:"https://****/test/api/wx/share",
data:{},
dataType:"json",
success:function(result){
//console.log(result);
appid = result.appId;
timestamp = result.timestamp;
noncestr = result.nonceStr;
signature = result.signature;
wx.config({
debug: false,
appId: appid,
timestamp: timestamp,
nonceStr: noncestr,
signature: 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'
]
});
wx.ready(function () {
wx.onMenuShareAppMessage(shareData);
wx.onMenuShareTimeline(shareData);
});
wx.error(function (res) {
alert(res.errMsg);
});
}
});
用户同意授权后
如果用户同意授权,页面将跳转至 redirect_uri/?code=CODE&state=STATE。若用户禁止授权,则重定向后不会带上code参数,
仅会带上state参数redirect_uri?state=STATE
code说明 : code作为换取access_token的票据,每次用户授权带上的code将不一样,code只能使用一次,5分钟未被使用自动过期。
网友评论