微信网页授权获取code

时间:2018-11-12 14:57:44   收藏:0   阅读:257

<script>
(function(){
var code = GetQueryString(‘code‘);

if(code){
alert(code)
return false;

}else{
shouquan();
}

function shouquan(){
var redirect_uri = window.location.origin + window.location.pathname;
console.log(redirect_uri)

var appId = "wx70c81e11981cab3d";
var wx_link = ‘https://open.weixin.qq.com/connect/oauth2/authorize?appid=‘ + appId + ‘&redirect_uri=‘ +
redirect_uri + ‘&response_type=code&scope=snsapi_base#wechat_redirect‘;
console.log(1)
window.location.href = wx_link;
code = GetQueryString(‘code‘);
}


function GetQueryString(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r!=null)return unescape(r[2]); return null;

}
})()

</script>

技术分享图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!