查看: 61|回复: 0

gitee三方登录

[复制链接]

2

主题

0

回帖

0

积分

热心网友

金币
0
阅读权限
220
精华
0
威望
0
贡献
0
在线时间
0 小时
注册时间
2011-8-27
发表于 2021-11-23 10:39:00 | 显示全部楼层 |阅读模式
gitee三方登录

步骤

1、先在gitee设置的第三方应用上创建应用,编写回调地址,和网址主页,生成Client ID 和 Client Secret

2、在点击用户授权之后,会在回调地址栏上出现一个code获取到code,请求 https://gitee.com/oauth/token 地址,post请求, 'https://gitee.com/oauth/token?grant_type=authorization_code&' . $code . '&client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret; 拼接url请求可以获得一个token。

3、通过token请求https://gitee.com/api/v5/user地址,get请求,https://gitee.com/api/v5/user?access_token=' . $token,获得用户的基本信息$userinfo.

4、创建一个三方关联表,将用户信息和三方关联的信息存在表中,用来判断该三方账号是否已经绑定用户账号,没有则新增一个用户,返回用户信息,有的话就返回用户信息。

CREATE TABLE `trilateral_login` (
 `id` int unsigned NOT NULL AUTO_INCREMENT,
 `type` int DEFAULT NULL COMMENT '三方服务 1:gitee',
 `user_id` int DEFAULT NULL COMMENT '用户的id',
 `auth_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin DEFAULT NULL COMMENT '验证的三方登录的唯一标识',
 `init_time` int DEFAULT NULL,
 `last_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_bin COMMENT='三方关联表';
配置文件config.php新增的配置
$config['gitee_get_token'] = 'https://gitee.com/oauth/token';
$config['gitee_get_info'] = 'https://gitee.com/api/v5/user';
$config['gitee_client_id'] = '70fe5fdf4ed079a2998a*********1e0af6a61bb80ae92b691e025eef2607***';
$config['gitee_client_secret'] = '7c02b14f9ea47f67*********fbd3f2b0a914b89f1f42ab76febdc12cf******';
$config['gitee_redirect_uri'] = 'http://ci.com/gitCallback';
controller代码
<?php
/**
* Created by PhpStorm.
* User: wyq
* Date: 2021/10/15
* Time: 11:20
*/

class Callback extends Base_Controller
{
   public function __construct()
  {
       parent::__construct();
       $this->load->model('Users_model');
  }
   //回调地址
   public function gitCallback()
  {
       //获取当前用户返回的code
       if (empty($_SERVER['QUERY_STRING'])) {
           fail(400, '参数错误');
      }
       //获取到返回的code
       $code = $_SERVER['QUERY_STRING'];
       //配置文件获取配置信息
       $get_token_url = config_item('gitee_get_token');//获取token的url
       $client_id = config_item('gitee_client_id');//获取client_id
       $redirect_uri = config_item('gitee_redirect_uri');//获取回调地址
       $client_secret = config_item('gitee_client_secret');//获取client_secret
       $get_info_url = config_item('gitee_get_info');//通过token获取用户信息的url
       //发送curl获取当前用户的token
       $url1 = $get_token_url . '?grant_type=authorization_code&' . $code . '&client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret;
       $res = curl_request($url1);
       $res = json_decode($res, true);
       $token = $res['access_token'];
       //通过token获取用户基本信息
       $url2 = $get_info_url . '?access_token=' . $token;
       $info = curl_request($url2, false);
       $info = json_decode($info, true);
       //通过gitee的id判断该用户gitee账号是否已经注册
       $count = $this->Users_model->checkTrilateralLogin(1, $info['id']);
       $count = $count[0]['num'];
       //使用过gitee三方账号登录
       if (!empty($info['id'])){
           if ($count > 0) {
               $userInfo = $this->Users_model->TrilateralLogin($info['id']);
               success($userInfo);
          } else {
               $userInfo = $this->Users_model->TrilateralRegister($info['name'], encrypt(123), 1, $info['id']);
               success($userInfo);
          }
      }
       fail(400,'信息有误');
  }
}
model代码
    
//判断三方登录表中是否有该id用户,即是否该三方账号已经绑定用户
   public function checkTrilateralLogin($type, $id)
  {
       return $this->commonQuery('count(id) num', ['type' => $type, 'auth_id' => $id], 'trilateral_login');
  }

   //使用三方返回的用户信息注册用户,返回用户信息
   public function TrilateralRegister($nickname, $password, $type, $auth_id)
  {
       $this->db->insert('users', ['nickname' => $nickname, 'password' => $password, 'username' => $nickname, 'create_time' => time()]);
       $user_id = $this->db->insert_id('users');
       $this->commonInsert('trilateral_login', ['type' => $type, 'user_id' => $user_id, 'auth_id' => $auth_id, 'init_time' => time()]);
       return $this->commonQuery('*', ['id' => $user_id], 'users');
  }

//在三方绑定表中,通过用户id获取用户信息
   public function TrilateralLogin($auth_id)
  {
       $user_id = $this->commonQuery('user_id', ['auth_id' => $auth_id], 'trilateral_login');
       $user_id = $user_id[0]['user_id'];
       return $this->commonQuery('*', ['id' => $user_id], 'users');
  }

 



来源:https://www.cnblogs.com/wyqgg/p/15592098.html
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

相关侵权、举报、投诉及建议等,请发 E-mail:qiongdian@foxmail.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖返回顶部