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

Baidu::openid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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