Passed
Push — master ( 67c0a2...6d4ce9 )
by ma
02:20
created

Gitee::getRedirectUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * Gitee
4
 * api接口文档
5
 */
6
namespace tinymeng\OAuth2\Gateways;
7
use tinymeng\OAuth2\Connector\Gateway;
8
use tinymeng\OAuth2\Helper\ConstCode;
9
10
/**
11
 * Class Gitee
12
 */
13
class Gitee extends Gateway
14
{
15
    protected $AuthorizeURL   = 'https://gitee.com/oauth/authorize';
16
    protected $AccessTokenURL = 'https://gitee.com/oauth/token/';
17
    protected $UserInfoURL = 'https://gitee.com/api/v5/user';
18
19
    /**
20
     * Description:  得到跳转地址
21
     * Updater:
22
     * @return string
23
     */
24
    public function getRedirectUrl()
25
    {
26
        //存储state
27
        $this->saveState();
28
        //登录参数
29
        $params = [
30
            'response_type' => $this->config['response_type'],
31
            'client_id'     => $this->config['app_id'],
32
            'redirect_uri'  => $this->config['callback'],
33
            'state'         => $this->config['state'],
34
            'scope'         => $this->config['scope'],
35
        ];
36
        return $this->AuthorizeURL . '?' . http_build_query($params);
37
    }
38
39
    /**
40
     * Description:  获取格式化后的用户信息
41
     * @return array
42
     * @throws \Exception
43
     */
44
    public function userInfo()
45
    {
46
        $result = $this->getUserInfo();
47
        $userInfo = [
48
            'open_id' => isset($result['id']) ? $result['id'] : '',
49
            'union_id'=> isset($result['login']) ? $result['login'] : '',
50
            'channel' => ConstCode::TYPE_GITEE,
51
            'nickname'=> $result['name'],
52
            'gender'  => ConstCode::GENDER,
53
            'avatar'  => $result['avatar_url'],
54
            'birthday'=> '',
55
            'access_token'=> $this->token['access_token'] ?? '',
56
            'native'=> $result,
57
        ];
58
        return $userInfo;
59
    }
60
61
    /**
62
     * Description:  获取原始接口返回的用户信息
63
     * @return array
64
     * @throws \Exception
65
     */
66
    public function getUserInfo()
67
    {
68
        /** 获取用户信息 */
69
        $this->openid();
70
71
        $params = [
72
            'access_token'=>$this->token['access_token'],
73
        ];
74
        $data = $this->get($this->UserInfoURL,$params);
75
        return json_decode($data, true);
76
    }
77
78
    /**
79
     * Description:  获取当前授权用户的openid标识
80
     * @return mixed
81
     * @throws \Exception
82
     */
83
    public function openid()
84
    {
85
        $this->getToken();
86
    }
87
88
89
    /**
90
     * Description:  获取AccessToken
91
     */
92
    protected function getToken(){
93
        if (empty($this->token)) {
94
            /** 验证state参数 */
95
            $this->CheckState();
96
97
            /** 获取参数 */
98
            $params = $this->accessTokenParams();
99
100
            /** 获取access_token */
101
            $this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
102
            $token =  $this->post($this->AccessTokenURL);
103
            /** 解析token值(子类实现此方法) */
104
            $this->token = $this->parseToken($token);
105
        }
106
    }
107
108
    /**
109
     * Description:  解析access_token方法请求后的返回值
110
     * @param $token
111
     * @return mixed
112
     * @throws \Exception
113
     */
114
    protected function parseToken($token)
115
    {
116
        $data = json_decode($token, true);
117
        if (isset($data['access_token'])) {
118
            return $data;
119
        } else {
120
            throw new \Exception("获取Gitee ACCESS_TOKEN出错:{$data['error']}");
121
        }
122
    }
123
124
}