gitee三方登录

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=’ . t o k e n , 获 得 用 户 的 基 本 信 息 token,获得用户的基本信息 tokenuserinfo.

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']);
                //通过用户id生成Jwt token发送给客户端
                $userInfo[0]['token'] = $this->Jwt_service->getToken($userInfo['id']);
                success($userInfo);
            } else {
                $userInfo = $this->Users_model->TrilateralRegister($info['name'], encrypt(123), 1, $info['id']);
                //通过用户id生成Jwt token发送给客户端
                $userInfo[0]['token'] = $this->Jwt_service->getToken($userInfo['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');
    }
{"code":200,"msg":"success","data":[{
    "id":"51",                              
    "username":"wyqgg",
    "password":"408d2b2b5cc43be7a34822c28a2ec3a7",
    "phone":null,
    "nickname":"wyqgg",
    "follow":"0",
    "fans":"0",
    "coin":null,
    "score":"0",
    "sign":null,
    "email":null,
    "aire":null,
    "true_name":null,
    "image":null,
    "sex":"1",
    "brith_time":null,
    "create_time":"1637586682",
    "update_time":null, 								                        "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiIsImp0aSI6IjNmMmc1N2E5MmFhIn0.eyJpc3MiOiJodHRwOlwvXC9jaS5jb20iLCJhdWQiOiJodHRwOlwvXC93d3cucHlnLmNvbSIsImp0aSI6IjNmMmc1N2E5MmFhIiwiaWF0IjoxNjM3NjM1NTM4LCJuYmYiOjE2Mzc2MzU1MzcsImV4cCI6MTYzNzcyMTkzOCwidXNlcl9pZCI6bnVsbH0.ufR3FPosgWnB6PfWrFZYSqShoeO7yJHWx2uvaeEg5DE"
}]}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值