Passed
Push — master ( 4ee940...caedb6 )
by ma
01:46
created

Coding::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
 * Coding
4
 * api接口文档
5
*/
6
namespace tinymeng\OAuth2\Gateways;
7
use tinymeng\OAuth2\Connector\Gateway;
8
use tinymeng\OAuth2\Helper\ConstCode;
9
10
/**
11
 * Class Coding
12
 * @package tinymeng\OAuth2\Gateways
13
 * @Author: TinyMeng <[email protected]>
14
 * @Created: 2023/07/09
15
 */
16
class Coding extends Gateway
17
{
18
    const API_BASE            = 'https://openapi.baidu.com/';
19
    protected $AuthorizeURL   = 'https://openapi.baidu.com/oauth/2.0/authorize';
20
    protected $AccessTokenURL = 'https://openapi.baidu.com/';
21
    protected $UserInfoURL = 'https://openapi.baidu.com/';
22
23
    /**
24
     * Description:  得到跳转地址
25
     * @author: JiaMeng <[email protected]>
26
     * Updater:
27
     * @return string
28
     */
29
    public function getRedirectUrl()
30
    {
31
        //存储state
32
        $this->saveState();
33
        //登录参数
34
        $params = [
35
            'response_type' => $this->config['response_type'],
36
            'client_id'     => $this->config['app_id'],
37
            'redirect_uri'  => $this->config['callback'],
38
            'state'         => $this->config['state'],
39
            'scope'         => $this->config['scope'],
40
        ];
41
        return $this->AuthorizeURL . '?' . http_build_query($params);
42
    }
43
44
    /**
45
     * Description:  获取格式化后的用户信息
46
     * @return array
47
     * @throws \Exception
48
     * @author: JiaMeng <[email protected]>
49
     * Updater:
50
     */
51
    public function userInfo()
52
    {
53
        $result = $this->getUserInfo();
54
        $userInfo = [
55
            'open_id' => isset($result['uid']) ? $result['uid'] : '',
56
            'union_id'=> isset($result['aid']) ? $result['aid'] : '',
57
            'channel' => ConstCode::TYPE_CODING,
58
            'nickname'=> $result['login_name'],
59
            'gender'  => ConstCode::GENDER,
60
            'avatar'  => '',
61
            'birthday'=> '',
62
            'access_token'=> $this->token['access_token'] ?? '',
63
            'native'=> $result,
64
        ];
65
        return $userInfo;
66
    }
67
68
    /**
69
     * Description:  获取原始接口返回的用户信息
70
     * @return array
71
     * @throws \Exception
72
     * @author: JiaMeng <[email protected]>
73
     * Updater:
74
     */
75
    public function getUserInfo()
76
    {
77
        /** 获取用户信息 */
78
        $this->openid();
79
80
        $headers = ['Authorization: Bearer '.$this->token['access_token']];
81
        $data = $this->get($this->UserInfoURL, [],$headers);
82
        return json_decode($data, true);
83
    }
84
85
    /**
86
     * Description:  获取当前授权用户的openid标识
87
     * @author: JiaMeng <[email protected]>
88
     * Updater:
89
     * @return mixed
90
     * @throws \Exception
91
     */
92
    public function openid()
93
    {
94
        $this->getToken();
95
    }
96
97
98
    /**
99
     * Description:  获取AccessToken
100
     * @author: JiaMeng <[email protected]>
101
     * Updater:
102
     */
103
    protected function getToken(){
104
        if (empty($this->token)) {
105
            /** 验证state参数 */
106
            $this->CheckState();
107
108
            /** 获取参数 */
109
            $params = $this->accessTokenParams();
110
111
            /** 获取access_token */
112
            $this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params);
113
            $token =  $this->post($this->AccessTokenURL);
114
            /** 解析token值(子类实现此方法) */
115
            $this->token = $this->parseToken($token);
116
        }
117
    }
118
119
    /**
120
     * Description:  解析access_token方法请求后的返回值
121
     * @author: JiaMeng <[email protected]>
122
     * Updater:
123
     * @param $token
124
     * @return mixed
125
     * @throws \Exception
126
     */
127
    protected function parseToken($token)
128
    {
129
        $data = json_decode($token, true);
130
        if (isset($data['access_token'])) {
131
            return $data;
132
        } else {
133
            throw new \Exception("获取Coding ACCESS_TOKEN出错:{$data['error']}");
134
        }
135
    }
136
137
}
138