关于微信扫码登录的2种解决办法thinkphp5

关于微信扫码登录的2种解决办法

1 因为之前写过微信的扫码登录 所以朋友有这个需求的时候 我直接让他去 微信开放平台去注册

https://open.weixin.qq.com/   当然是这里了, 因为是网站上的需求 所以

https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html

就是这里了。

2 不过朋友没有申请这个,只有公众号的相关资料,公众号没有明文的扫码登录  但是有授权登录这种接口。

无奈百度找找,结果还真有,就是用公众号的授权登录生成网址,然后用户使用微信扫码,实现登录的过程。

直接上代码吧  公众号

//页面授权,获取用户的code 第一步 访问地址
    public function getWechatCode()
    {
        //这里直接校验登陆码
        $logincode=input('logincode');
        

        $map['code']=$logincode;
        if(empty($logincode)) {
            $this->error("logincode参数错误");exit;
        }
        $info=db('client_logincode')->where($map)->find();
        if(!empty($info)) {
            if($info['expire_time']<time()) {
                $data=array();
                $data['status']=3;
                $data['update_time']=time();
                db('client_logincode')->where($map)->update($data);
                
                $this->error("logincode已过期");exit;
            } else {
                $data=array();
                $data['status']=2;
                $data['update_time']=time();
                db('client_logincode')->where($map)->update($data);
            }
        } else {
            $this->error("logincode不存在");exit;
        }
        
        
        $appid = '111111';//改成你的APPID
        
        $redirect_uri=urlencode("http://www.***.net/index.php/api/user/getWechatAccessToken/");//改成你的域名地址
        $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appid."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
 
        $this->redirect($url);
    }

    //网页授权,获取用户的openId 第二步
    public function getWechatAccessToken()
    {
        
        $appid = '1111';//改成你的APPID
        $appsecret='2222';//改成你的 SECRET
 
        $result=0;
        
        $request=request();
        $code=$request->param('code');
        $state=$request->param('state');
        if($code && $state=='STATE')
        {
            $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
            $response = file_get_contents($url);
            $arr = json_decode($response, true);
 
            if(!empty($arr)) {
                $accessToken=$arr['access_token'];
                $openId=$arr['openid'];
                $result=$this->getWechatUserInfo($accessToken,$openId);
            }

        }
        
        
           if($result) {
            $this->success("操作成功",$result);
        } else {
            $this->error("操作失败");
        }

    }

    //网页授权,获取微信用户信息
    public function getWechatUserInfo($accessToken,$openId)
    {
        $url="https://api.weixin.qq.com/sns/userinfo?access_token=".$accessToken."&openid=".$openId."&lang=zh_CN";
        $response = file_get_contents($url);
        $arr = json_decode($response, true);
 
        $res['status']=0;
        
        if(!empty($arr)) {
            //这里可以数据处理
            //根据openid 查询用户ID
            $map['openid']=$arr['openid'];
            $info=db('client_users')->where($map)->find();
   
            if($info['user_type']==2) {
                $res['status']=1;
                $res['userinfo']=$info;
                $res['usertoken']=db('user_token')->where('user_id='.$info['user_id'])->find();
            }
        }
        
        return $res;
    }
   
 
   
    /**
     *
     * 前端请求这个接口 获取登陆网址和code
     */
    public function getClientUserLoginCode()
    {
        $code=$this->getLoginCode();//code获取
        //组装访问首页面的网址
        $url='http://www.***.net/index.php/Api/User/getWechatCode/logincode/'.$code;
 
        $this->success("操作成功",$url);
    }
   
    //创建登陆码
    public function getLoginCode()
    {
         
        $logincode='';
        
        //校验下是否有未到期 且可用的
        $map['status']=1;
        $map['expire_time']=['>',time()];
        $info=db('client_logincode')->where($map)->order('create_time desc')->find();
        if(empty($info)) {
        
            //产生
            $logincode=md5($this->createLoginCode());
            
            //存入库中
            $data=array();
            $data['code']=$logincode;
            $data['create_time']=time();
            $data['expire_time']=time()+300;//5分钟
            db('client_logincode')->insert($data);
        } else {
            $logincode=$info['code'];
        }
        
        return $logincode;
    }
   
    //登陆码生成函数
    public function createLoginCode()
    {
   
        $str='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $randStr = str_shuffle($str);//打乱字符串
        $rands= substr($randStr,0,6);//substr(string,start,length);返回字符串的一部分
        return $rands;
 
    }

整体介绍下

1 因为是扫码  所以第一步是前端请求 一个函数  生成扫码使用的网址

在我的这里就是 http://www.***.net/index.php/Api/User/getClientUserLoginCode

http://www.***.net/index.php/Api/User/getWechatCode/logincode/1c5932c3b89b8a71db88f5a727b27419

这个是生成的网址  就是扫码访问的网址   会提示需要授权登录  然后获取token和openid  和表中的数据进行对比

使用了登录码做验证  

猜你喜欢

转载自www.cnblogs.com/baker95935/p/12736734.html