微信授权登陆的坑

 //获取code
    public function get_code(){
        $this->redirect_uri='http://www.e-hl.cc/phone/phone.php?r=WxLogin/wx_login_index';
//        $this->redirect_uri='https://www.baidu.com';
        $this->redirect_uri=urlencode($this->redirect_uri);
        $this->url = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appId.'&redirect_uri='.$this->redirect_uri.'&response_type=code&scope=snsapi_userinfo&state=123&connect_redirect=1#wechat_redirect';
//        $this->url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$this->appId.'&redirect_uri='.$this->redirect_uri.'&response_type=code&scope=snsapi_base&state=123#wechat_redirect';
        header("location:{$this->url}");
    }
 

    //获取token返回
    public function wx_login_index(){
//       $wx = new Lib\WXOfficeAccount();

        $this->get_openId();  //通过code换取网页授权access_token ,openId
//        $this->get_token();   //获取access_token
        $this->get_one_user_msg_code(); //获取用户信息.
        $this->client = 'WX_OA';
//        var_dump($this->user_msg);
        $this->union_id = $this->user_msg->unionid;
        $this->nick_name = $this->user_msg->nickname;
        $this->headimgurl = $this->user_msg->headimgurl;
//        var_dump($this->union_id, $this->nick_name,$this->headimgurl);
        $this->wx_login();
        $_SESSION['token']=empty($this->token)?output_error('微信登陆错误!'):$this->token;
//        output_data($_SESSION['token']);
        setcookie('token',$_SESSION['token']);
        setcookie('key',$_SESSION['token']);
        header("location:http://www.e-hl.cc/wap/templates/E_index.html?a=".$_SESSION['token']);

}

#########################js代码#################################

window.onload=function () {
    if (!getCookie('key')) {
        // document.getElementById("alink").click();
        if (isWeiXin()) {    //是微信端
            window.location.href='http://www.e-hl.cc/phone/phone.php?r=WxLogin/get_code/';
        }
    }
}

var k=getQueryString('a')?addCookie('key',getQueryString('a')):'';



先说说我遇到的问题把。首先授权的流程是可以了,但问题是,如果前端使用AJAX的话就没有反应,所以就只能使用window.location.href来请求我写的code接口,从而持行下面的代码,自动登陆注册,及返回token值,微信苹果端会一直死循环,所以我就直接授权获取完我设置的token后就把值带在地址中,然后前端就拿到KEY值,实现登陆

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值