zoukankan      html  css  js  c++  java
  • TP6|TP5.1 QQ登录

    <?php
    /**
     * Created by PhpStorm.
     * User: 张永峰
     * Date: 2021/3/15
     * Time: 11:11
     * ━━━━━━━━━神兽出没━━━━━━━━━
     *        ┏┓   ┏┓+ +
     *       ┏┛┻━━━┛┻┓ + +
     *       ┃       ┃  
     *       ┃   ━   ┃ ++ + + +
     *       ████━████ ┃+
     *       ┃       ┃ +
     *       ┃   ┻   ┃
     *       ┃       ┃ + +
     *       ┗━┓   ┏━┛
     *         ┃   ┃           
     *         ┃   ┃ + + + +
     *         ┃   ┃    Code is far away from bug with the animal protecting       
     *         ┃   ┃ +     神兽保佑,代码无bug  
     *         ┃   ┃
     *         ┃   ┃  +         
     *         ┃    ┗━━━┓ + +
     *         ┃        ┣┓
     *         ┃        ┏┛
     *         ┗┓┓┏━┳┓┏┛ + + + +
     *          ┃┫┫ ┃┫┫
     *          ┗┻┛ ┗┻┛+ + + +
     * ━━━━━━━━━感觉萌萌哒━━━━━━━━━
     */
    
    
    namespace zyf;
    
    /**
     * @title   类标题
     * @desc    类说明
     * Class QQ
     * @package zyf
     * @Date    : 2021/3/15
     * @Time    : 11:11
     */
    class QQ
    {
    
        /**
         * QQ登录接口
         * @return mixed
         * @author zyf <1322816443@qq.com>
         * */
        public function login()
        {
            $app_id = config('qq.main.app_id');
            //回调地址
            $redirect_uri = config('qq.main.redirect_uri');
            $url = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=".
                    $app_id."&redirect_uri=".
                    $redirect_uri."&scope=get_user_info&state=text";
            redirect($url);
        }
    
        /**
         * QQ登录
         * @param string code 返回码
         * @return mixed
         * @author zyf <1322816443@qq.com>
         * */
        public function token()
        {
            // Step1:定义参数
            // appid
            $app_id = config('qq.main.app_id');
            // appkey
            $app_secret = config('qq.main.app_secret');
            // 成功授权后的回调地址
            $my_url = urlencode(config('qq.main.redirect_uri'));
            // 获取code
            $code = $_GET['code'];
    
            // Step2:通过Authorization Code获取Access Token
            $token_url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=".
                        $app_id."&redirect_uri=". $my_url."&client_secret=". $app_secret."&code=".$code;
            // file_get_contents() 把整个文件读入一个字符串中。
            $response = file_get_contents($token_url);
    
            // Step3:在上一步获取的Access Token,得到对应用户身份的OpenID。
            $params = array();
            // parse_str() 函数把查询字符串('a=x&b=y')解析到变量中。
            parse_str($response, $params);
            $graph_url = "https://graph.qq.com/oauth2.0/me?access_token=".$params['access_token'];
            $str = file_get_contents($graph_url);
            // strpos() 函数查找字符串在另一字符串中第一次出现的位置,从0开始
            if (strpos($str, "callback") !== false) {
                $lpos = strpos($str, "(");
                // strrpos() 函数查找字符串在另一字符串中最后一次出现的位置。
                $rpos = strrpos($str, ")");
                // substr(string,start,length) 截取字符串某一位置
                $str = substr($str, $lpos+1, $rpos-$lpos-1);
            }
            // json_decode() 函数用于对 JSON 格式-->{"a":1,"b":2,"c":3,"d":4,"e":5}<--的字符串进行解码,并转换为 PHP 变量,默认返回对象
            $user = json_decode($str);
            $openid = $user->openid;
    
            // Step4: 调用OpenAPI接口,得到json数据,要转换为数组
            $arr = "https://graph.qq.com/user/get_user_info?access_token=".
                    $params['access_token']."&oauth_consumer_key=".$app_id."&openid=".$user->openid;
            // 加上true,得到数组,否则默认得到对象
            $result = json_decode(file_get_contents($arr), true);
    
            $memMod = new appcommonmodelMember();
            $res = $memMod->getOneWhereCache([], [['open_id', '=', $openid]]);
            // 如果没有找到,进行注册
            // 把 id 存session
            if (!$res) {
                $data = [
                    'open_id' => $openid,
                    'sex' => $res['gender'],
                    'nickname' => $res['nickname'],
                    'headimg' => $res['figureurl_qq_2'],
                    'province' => $res['province'],
                    'city' => $res['city'],
                ];
                $is_add = $memMod->edit($data);
                if ($is_add) {
                    session('indexId', $is_add['id']);
                }
            } else {
                session('indexId', $res['id']);
            }
        }
    
    }
  • 相关阅读:
    hibernate 使用hibernate 的注解做多对一双向映射
    JBPM学习笔记
    在测试Hibernate的一对多双向关联映射时
    js定时三秒后自动跳转页面
    struts2 的验证框架
    hibernate 双向一多对关联 删除一端时级联删除多端
    JPA 一些常用的东西
    Python 基础语法
    设计一个带有getmin功能的栈,保证时间复杂度在O(1)
    数据结构栈模拟队列
  • 原文地址:https://www.cnblogs.com/zyfeng/p/14607519.html
Copyright © 2011-2022 走看看