tp5 微信授权登录

 因为工作需要自己用tp5封装了微信授权登录功能,参考书籍为方倍的微信开发书。
本文章作为一个记录方式,防止自己忘记,如果能对哪个小伙伴儿一点帮助那就更好了。
授权登录在数据库有一张微信用户表,需要提前自建。
开始上代码:
//授权登录发起方式为:
$url= 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$this->User($url);
    /*
 +---------------------------------------------------------------
 |授权登录
 +---------------------------------------------------------------
 */
 public function User($callback){//传入参数为需要授权登录地址
    if(session('openid'))//判断用户是否登录过如果登陆过
    {
        $user=model('ld_member')->where('openid',session('openid'))->find();
        if(!$user){//判断用户在数据库中是否有记录如果没有记录就清空登录状态重新发起userinfo方式用户授权
            session(null);
            $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx207fa66c97a2db5c&redirect_uri=$callback&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
        }else{//如果用户登录过并且在数据库中找到了用户的信息就把用户信息写入到模板并设置全局变量备用
        //因为用户暱称有特殊字符数据库不能存储所以进行了格式转换需要使用时重新转换回来
            $mistdata=json_decode($user['nickname']);//将数据库中json数据还原
            $user['namep']=urldecode($mistdata);//将十六进制暱称还原成中文
            $this->user=$user;
            $this->assign('user',$user);
            return true;
        }
    }else{//如果没有登录执行这部分代码
        $get = input('param.');
        if($get){//这里判断是否有get数据 如果有说明是登录授权回调 用code获取用户openid
            $appid = config('weixin.appID');//此处加载APPid可以直接写上
            $appsecret = config('weixin.appsecret');
            if(isset($get['code'])){//判断是否有code参数
                $code=$get['code'];
                $urlp="https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$appsecret&code=$code&grant_type=authorization_code";
                $res = $this->https_request($urlp);//通过code获取openid
                $userinfo= json_decode($res, JSON_UNESCAPED_UNICODE);
                 session('openid',$userinfo['openid']);
                if($userinfo['scope']=='snsapi_base'){//判断获取方式是BASE还是userinfo
                    $user=model('ld_member')->where('openid',session('openid'))->find();
                    if(!isset($user['is_auth'])){//如果是base方式发起数据库没有用户信息就用userinfo方式重新发起
                        session(null);
                        $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx207fa66c97a2db5c&redirect_uri=$callback&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect");
                    }else{
                        $mistdata=json_decode($user['nickname']);//将数据库中json数据还原
                        $user['namep']=urldecode($mistdata);//将十六进制暱称还原成中文
                        $this->is_auth=$user['is_auth'];
                        $this->openid=$user['openid'];
                        $this->user=$user;
                        $this->assign('user',$user);
                        return true;
                    }       
                }else{//如果是userinfo 发起就接着调用接口获取用户详细信息
                    $token=$userinfo['access_token'];
                    $apenid=$userinfo['openid'];
                    $urln="https://api.weixin.qq.com/sns/userinfo?access_token=$token&openid=$apenid&lang=zh_CN";
                    $ress = $this->https_request($urln);//用opendi获取用户资料
                    $username= json_decode($ress, JSON_UNESCAPED_UNICODE);
                    $dataname=urlencode($username['nickname']);//将暱称中文转成16进制数字
                    $namedata=json_encode($dataname);
                    $userdata=array(
                       'openid'=>$username['openid'],
                       'nickname'=>$namedata,
                       'gender'=>$username['sex'],
                       'language'=>$username['language'],
                       'city'=>$username['city'],
                       'province'=>$username['province'],
                       'country'=>$username['country'],
                       'avatar'=>$username['headimgurl'],
                       'created_at'=>time()
                       );
                    $userstatus=model('ld_member')->allowField(true)->insert($userdata);//将数据写入到数据库
                    $user=model('ld_member')->where('openid',session('openid'))->find();//将用户信息从数据库查出来写入到模板和全局备用
                    $mistdata=json_decode($user['nickname']);//将数据库中json数据还原
                    $user['namep']=urldecode($mistdata);//将十六进制暱称还原成中文
                    $this->is_auth=$user['is_auth'];
                    $this->openid=$user['openid'];
                    $this->user=$user;
                    $this->assign('user',$user);
                    return true;
                }
            }else{//如果get数据没有code参数 发起base方式授权登录获取CODE
                $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid='你的appid'&redirect_uri=$callback&response_type=code&scope=snsapi_base&state=2#wechat_redirect"); 
            }
        }
        //如果没有get数据说明是后台调用方法 发起base方式授权登录获取CODE
        $this->redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx207fa66c97a2db5c&redirect_uri=$callback&response_type=code&scope=snsapi_base&state=2#wechat_redirect");
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章