【踩坑】TP5 微信授权登录

<?php 

//授权登录发起方式为:

$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");
    }
}

【转载地址】:https://blog.csdn.net/qq_38013755/article/details/78598811

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值