Sina::userInfo()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace tinymeng\OAuth2\Gateways;
4
5
use tinymeng\OAuth2\Connector\Gateway;
6
use tinymeng\OAuth2\Exception\OAuthException;
7
use tinymeng\OAuth2\Helper\ConstCode;
8
9
/**
10
 * Class Sina
11
 * @package tinymeng\OAuth2\Gateways
12
 * @Author: TinyMeng <[email protected]>
13
 * @Created: 2018/11/9
14
 */
15
class Sina extends Gateway
16
{
17
18
    const API_BASE            = 'https://api.weibo.com/2/';
19
    protected $AuthorizeURL   = 'https://api.weibo.com/oauth2/authorize';
20
    protected $AccessTokenURL = 'https://api.weibo.com/oauth2/access_token';
21
22
    /**
23
     * Description:  得到跳转地址
24
     * @author: JiaMeng <[email protected]>
25
     * Updater:
26
     * @return string
27
     */
28
    public function getRedirectUrl()
29
    {
30
        //存储state
31
        $this->saveState();
32
        //登录参数
33
        $this->switchAccessTokenURL();
34
        $params = [
35
            'client_id'    => $this->config['app_id'],
36
            'redirect_uri' => $this->config['callback'],
37
            'scope'        => $this->config['scope'],
38
            'state'        => $this->config['state'],
39
            'display'      => $this->display,
40
        ];
41
        return $this->AuthorizeURL . '?' . http_build_query($params);
42
    }
43
44
    /**
45
     * Description:  获取当前授权用户的openid标识
46
     * @author: JiaMeng <[email protected]>
47
     * Updater:
48
     * @return mixed
49
     * @throws OAuthException
50
     */
51
    public function openid()
52
    {
53
        $this->getToken();
54
55
        if (isset($this->token['openid'])) {
56
            return $this->token['openid'];
57
        } else {
58
            throw new OAuthException('没有获取到新浪微博用户ID!');
59
        }
60
    }
61
62
    /**
63
     * Description:  获取格式化后的用户信息
64
     * @author: JiaMeng <[email protected]>
65
     * Updater:
66
     * @return array
67
     */
68
    public function userInfo()
69
    {
70
        $result = $this->getUserInfo();
71
72
        $userInfo = [
73
            'open_id'  => $this->openid(),
74
            'access_token'=> $this->token['access_token'] ?? '',
75
            'union_id'  => $this->openid(),
76
            'channel' => ConstCode::TYPE_SINA,
77
            'nickname'    => $result['screen_name'],
78
            'gender'  => $this->getGender($result['gender']),
79
            'avatar'  => $result['avatar_hd'],
80
            'native'   => $result,
81
        ];
82
        return $userInfo;
83
    }
84
85
    /**
86
     * Description:  获取原始接口返回的用户信息
87
     * @author: JiaMeng <[email protected]>
88
     * Updater:
89
     * @return array
90
     * @throws OAuthException
91
     */
92
    public function getUserInfo()
93
    {
94
        if($this->type == 'app'){//App登录
95
            if(!isset($_REQUEST['access_token']) || !isset($_REQUEST['uid'])){
96
                throw new OAuthException("Sina APP登录 需要传输access_token和uid参数! ");
97
            }
98
            $this->token['access_token'] = $_REQUEST['access_token'];
99
            $this->token['openid'] = $_REQUEST['uid'];
100
        }else{
101
            /** 获取参数 */
102
            $params = $this->accessTokenParams();
103
            $this->AccessTokenURL = $this->AccessTokenURL. '?' . http_build_query($params);//get传参
104
            $this->getToken();
105
        }
106
107
        return $this->call('users/show.json', ['uid' => $this->openid()]);
108
    }
109
110
    /**
111
     * Description:  发起请求
112
     * @author: JiaMeng <[email protected]>
113
     * Updater:
114
     * @param $api
115
     * @param array $params
116
     * @param string $method
117
     * @return mixed
118
     */
119
    private function call($api, $params = [], $method = 'GET')
120
    {
121
        $method = strtoupper($method);
122
123
        if(isset($this->token['access_token'])){
124
            $params['access_token'] = $this->token['access_token'];
125
        }
126
127
        $data = $this->$method(self::API_BASE . $api, $params);
128
        return json_decode($data, true);
129
    }
130
131
    /**
132
     * Description:  根据第三方授权页面样式切换跳转地址
133
     * @author: JiaMeng <[email protected]>
134
     * Updater:
135
     */
136
    private function switchAccessTokenURL()
137
    {
138
        if ($this->display == 'mobile') {
139
            $this->AuthorizeURL = 'https://open.weibo.cn/oauth2/authorize';
140
        }
141
    }
142
143
    /**
144
     * Description:  解析access_token方法请求后的返回值
145
     * @author: JiaMeng <[email protected]>
146
     * Updater:
147
     * @param $token
148
     * @return mixed
149
     * @throws OAuthException
150
     */
151
    protected function parseToken($token)
152
    {
153
        $data = json_decode($token, true);
154
        if (isset($data['access_token'])) {
155
            $data['openid'] = $data['uid'];
156
            unset($data['uid']);
157
            return $data;
158
        } else {
159
            throw new OAuthException("获取新浪微博ACCESS_TOKEN出错:{$data['error']}");
160
        }
161
    }
162
163
    /**
164
     * 第三方分享到微博
165
     * @param $data
166
     * @return mixed
167
     */
168
    public function statusesShare($data){
169
        return $this->call('statuses/share.json', json_encode($data),'POST');
0 ignored issues
show
Bug introduced by
json_encode($data) of type string is incompatible with the type array expected by parameter $params of tinymeng\OAuth2\Gateways\Sina::call(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
        return $this->call('statuses/share.json', /** @scrutinizer ignore-type */ json_encode($data),'POST');
Loading history...
170
    }
171
172
}
173