美文网首页
微信公众号授权获取code--兑换后端openid

微信公众号授权获取code--兑换后端openid

作者: 在下高姓 | 来源:发表于2020-03-26 13:02 被阅读0次
var AppId = "wxa**************7"; //公众号的appid
var code  = getUrlParam('code');//获取静默授权返回code
var local = encodeURIComponent(window.location.href);//获取当前页面路径,即回调地址

// 获取微信url中  的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;
}
function openid() {
    if(code == null || code == undefined || code == ''){
//静默授权
        window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='+AppId+'&redirect_uri='+local+'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';

    }
//授权弹窗
window.location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid="+ this.appid +"&redirect_uri="+local +"&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect"
  
}

相关文章

网友评论

      本文标题:微信公众号授权获取code--兑换后端openid

      本文链接:https://www.haomeiwen.com/subject/sstcuhtx.html