1.获取微信公众号的AppID和AppSecret
2. 配置授权回调的页面
3. 用户授权并获取code
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect
3.1 重定向到该地址,其中appid为开发者id(AppID),redirect_uri为回调地址
3.2 scope 参数为snsapi_base时: 静默授权,没有弹窗,只能获取用户的openId;
scope 参数为snsapi_userinfo时:非静默授权,有弹框弹(近期登录过,弹框不会弹出)出需要用户手动点击确认授权。可以获取openId,用户的头像、昵称等
3.3截取code(下面的方法可以截取code)
function getUrlParam(name){
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]);
return null;
};
特别注意:重定向到微信的页面后需要跳回到自己的页面,否则会导致页面获取code死循环的bug,导致code失效