Passed
Push — master ( 30e113...d850f4 )
by ma
02:14
created

Aliyun::parseToken()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 7
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * 阿里云
4
 * api接口文档
5
 *      https://help.aliyun.com/zh/ram/user-guide/overview-of-oauth-applications
6
*/
7
namespace tinymeng\OAuth2\Gateways;
8
use tinymeng\OAuth2\Connector\Gateway;
9
use tinymeng\OAuth2\Helper\ConstCode;
10
11
/**
12
 * Class Aliyun
13
 * @package tinymeng\OAuth2\Gateways
14
 * @Author: TinyMeng <[email protected]>
15
 * @Created: 2023/07/09
16
 */
17
class Aliyun extends Gateway
18
{
19
    const API_BASE            = 'https://signin.aliyun.com/';
20
    protected $AuthorizeURL   = 'https://signin.aliyun.com/oauth2/v1/auth';
21
    protected $AccessTokenURL = 'https://oauth.aliyun.com/v1/token';
22
    protected $UserInfoURL = 'https://oauth.aliyun.com/v1/userinfo';
23
24
    /**
25
     * Description:  得到跳转地址
26
     * @author: JiaMeng <[email protected]>
27
     * Updater:
28
     * @return string
29
     */
30
    public function getRedirectUrl()
31
    {
32
        //存储state
33
        $this->saveState();
34
        //登录参数
35
        $params = [
36
            'response_type' => $this->config['response_type'],
37
            'client_id'     => $this->config['app_id'],
38
            'redirect_uri'  => $this->config['callback'],
39
            'state'         => $this->config['state'],
40
            'scope'         => $this->config['scope'],
41
        ];
42
        return $this->AuthorizeURL . '?' . http_build_query($params);
43
    }
44
45
    /**
46
     * Description:  获取格式化后的用户信息
47
     * @return array
48
     * @throws \Exception
49
     * @author: JiaMeng <[email protected]>
50
     * Updater:
51
     */
52
    public function userInfo()
53
    {
54
        $result = $this->getUserInfo();
55
        $userInfo = [
56
            'open_id' => isset($result['uid']) ? $result['uid'] : '',
57
            'union_id'=> isset($result['aid']) ? $result['aid'] : '',
58
            'channel' => ConstCode::TYPE_ALIYUN,
0 ignored issues
show
Bug introduced by
The constant tinymeng\OAuth2\Helper\ConstCode::TYPE_ALIYUN was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
59
            'nickname'=> $result['login_name'],
60
            'gender'  => ConstCode::GENDER,
61
            'avatar'  => '',
62
            'birthday'=> '',
63
            'access_token'=> $this->token['access_token'] ?? '',
64
            'native'=> $result,
65
        ];
66
        return $userInfo;
67
    }
68
69
    /**
70
     * Description:  获取原始接口返回的用户信息
71
     * @return array
72
     * @throws \Exception
73
     * @author: JiaMeng <[email protected]>
74
     * Updater:
75
     */
76
    public function getUserInfo()
77
    {
78
        /** 获取用户信息 */
79
        $this->openid();
80
81
        $headers[] = 'Authorization: Bearer '.$this->token['access_token'];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$headers was never initialized. Although not strictly required by PHP, it is generally a good practice to add $headers = array(); before regardless.
Loading history...
82
        $data = $this->get($this->UserInfoURL, [],$headers);
83
        return json_decode($data, true);
84
    }
85
86
    /**
87
     * Description:  获取当前授权用户的openid标识
88
     * @author: JiaMeng <[email protected]>
89
     * Updater:
90
     * @return mixed
91
     * @throws \Exception
92
     */
93
    public function openid()
94
    {
95
        $this->getToken();
96
    }
97
98
99
    /**
100
     * Description:  获取AccessToken
101
     * @author: JiaMeng <[email protected]>
102
     * Updater:
103
     */
104
    protected function getToken(){
105
        if (empty($this->token)) {
106
            /** 验证state参数 */
107
            $this->CheckState();
108
109
            /** 获取参数 */
110
            $params = $this->accessTokenParams();
111
112
            /** 获取access_token */
113
            $this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
114
            $token =  $this->post($this->AccessTokenURL);
115
            /** 解析token值(子类实现此方法) */
116
            $this->token = $this->parseToken($token);
117
        }
118
    }
119
120
    /**
121
     * Description:  解析access_token方法请求后的返回值
122
     * @author: JiaMeng <[email protected]>
123
     * Updater:
124
     * @param $token
125
     * @return mixed
126
     * @throws \Exception
127
     */
128
    protected function parseToken($token)
129
    {
130
        $data = json_decode($token, true);
131
        if (isset($data['access_token'])) {
132
            return $data;
133
        } else {
134
            throw new \Exception("获取Aliyun ACCESS_TOKEN出错:{$data['error']}");
135
        }
136
    }
137
138
    /**
139
     * Description:  通过接口获取openid
140
     * @author: JiaMeng <[email protected]>
141
     * Updater:
142
     * @return mixed|string
143
     * @throws \Exception
144
     */
145
    private function getOpenID(){
0 ignored issues
show
Unused Code introduced by
The method getOpenID() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
146
        $query = [
147
            'access_token' => $this->token['access_token']
148
        ];
149
        /** 如果要获取unionid,先去申请:http://wiki.connect.qq.com/开发者反馈 */
150
        if (isset($this->config['is_unioid']) && $this->config['is_unioid'] === true) {
151
            $query['unionid'] = 1;
152
        }
153
154
        $data = $this->get(self::API_BASE . 'oauth2.0/me',$query);
155
        $data     = json_decode(trim(substr($data, 9), " );\n"), true);
156
        if (isset($data['openid'])) {
157
            return $data;
158
        } else {
159
            throw new \Exception("获取用户openid出错:" . $data['error_description']);
160
        }
161
    }
162
163
    /**
164
     * 格式化性别参数
165
     * M代表男性,F代表女性
166
     * @param $gender
167
     */
168
    public function getGender($gender){
169
        return $gender == '男' ? ConstCode::GENDER_MAN : ConstCode::GENDER_WOMEN;
170
    }
171
}
172