GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Weixin   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 70
c 4
b 0
f 0
dl 0
loc 168
rs 10
wmc 18

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getGender() 0 14 3
A userinfoRaw() 0 5 1
A switchAccessTokenURL() 0 7 2
A parseToken() 0 7 2
A openid() 0 8 2
A call() 0 10 1
A accessTokenParams() 0 9 2
A getProxyURL() 0 10 1
A userinfo() 0 18 3
A getRedirectUrl() 0 11 1
1
<?php
2
3
namespace anerg\OAuth2\Gateways;
4
5
use anerg\OAuth2\Connector\Gateway;
6
7
class Weixin extends Gateway
8
{
9
    const API_BASE            = 'https://api.weixin.qq.com/sns/';
10
    protected $AuthorizeURL   = 'https://open.weixin.qq.com/connect/qrconnect';
11
    protected $AccessTokenURL = 'https://api.weixin.qq.com/sns/oauth2/access_token';
12
    /**
13
     * 得到跳转地址
14
     */
15
    public function getRedirectUrl()
16
    {
17
        $this->switchAccessTokenURL();
18
        $params = [
19
            'appid'         => $this->config['app_id'],
20
            'redirect_uri'  => $this->config['callback'],
21
            'response_type' => $this->config['response_type'],
22
            'scope'         => $this->config['scope'],
23
            'state'         => $this->config['state'],
24
        ];
25
        return $this->AuthorizeURL . '?' . http_build_query($params) . '#wechat_redirect';
26
    }
27
28
    /**
29
     * 获取中转代理地址
30
     */
31
    public function getProxyURL()
32
    {
33
        $params = [
34
            'appid'         => $this->config['app_id'],
35
            'response_type' => $this->config['response_type'],
36
            'scope'         => $this->config['scope'],
37
            'state'         => $this->config['state'],
38
            'return_uri'    => $this->config['callback'],
39
        ];
40
        return $this->config['proxy_url'] . '?' . http_build_query($params);
41
    }
42
43
    /**
44
     * 获取当前授权用户的openid标识
45
     */
46
    public function openid()
47
    {
48
        $this->getToken();
49
50
        if (isset($this->token['openid'])) {
51
            return $this->token['openid'];
52
        } else {
53
            throw new \Exception('没有获取到微信用户ID!');
54
        }
55
    }
56
57
    /**
58
     * 获取格式化后的用户信息
59
     */
60
    public function userinfo()
61
    {
62
        $rsp = $this->userinfoRaw();
63
64
        $avatar = $rsp['headimgurl'];
65
        if ($avatar) {
66
            $avatar = \preg_replace('~\/\d+$~', '/0', $avatar);
67
        }
68
69
        $userinfo = [
70
            'openid'  => $this->openid(),
71
            'unionid' => isset($this->token['unionid']) ? $this->token['unionid'] : '',
72
            'channel' => 'weixin',
73
            'nick'    => $rsp['nickname'],
74
            'gender'  => $this->getGender($rsp['sex']),
75
            'avatar'  => $avatar,
76
        ];
77
        return $userinfo;
78
    }
79
80
    /**
81
     * 获取原始接口返回的用户信息
82
     */
83
    public function userinfoRaw()
84
    {
85
        $this->getToken();
86
87
        return $this->call('userinfo');
88
    }
89
90
    /**
91
     * 发起请求
92
     *
93
     * @param string $api
94
     * @param array $params
95
     * @param string $method
96
     * @return array
97
     */
98
    private function call($api, $params = [], $method = 'GET')
99
    {
100
        $method = strtoupper($method);
101
102
        $params['access_token'] = $this->token['access_token'];
103
        $params['openid']       = $this->openid();
104
        $params['lang']         = 'zh_CN';
105
106
        $data = $this->$method(self::API_BASE . $api, $params);
107
        return json_decode($data, true);
108
    }
109
110
    /**
111
     * 根据第三方授权页面样式切换跳转地址
112
     *
113
     * @return void
114
     */
115
    private function switchAccessTokenURL()
116
    {
117
        if ($this->display == 'mobile') {
118
            $this->AuthorizeURL = 'https://open.weixin.qq.com/connect/oauth2/authorize';
119
        } else {
120
            //微信扫码网页登录,只支持此scope
121
            $this->config['scope'] = 'snsapi_login';
122
        }
123
    }
124
125
    /**
126
     * 默认的AccessToken请求参数
127
     *
128
     * @return array
129
     */
130
    protected function accessTokenParams()
131
    {
132
        $params = [
133
            'appid'      => $this->config['app_id'],
134
            'secret'     => $this->config['app_secret'],
135
            'grant_type' => $this->config['grant_type'],
136
            'code'       => isset($_REQUEST['code']) ? $_REQUEST['code'] : '',
137
        ];
138
        return $params;
139
    }
140
141
    /**
142
     * 解析access_token方法请求后的返回值
143
     * @param string $token 获取access_token的方法的返回值
144
     */
145
    protected function parseToken($token)
146
    {
147
        $data = json_decode($token, true);
148
        if (isset($data['access_token'])) {
149
            return $data;
150
        } else {
151
            throw new \Exception("获取微信 ACCESS_TOKEN 出错:{$token}");
152
        }
153
    }
154
155
    /**
156
     * 格式化性别
157
     *
158
     * @param string $gender
159
     * @return string
160
     */
161
    private function getGender($gender)
162
    {
163
        $return = null;
164
        switch ($gender) {
165
            case 1:
166
                $return = 'm';
167
                break;
168
            case 2:
169
                $return = 'f';
170
                break;
171
            default:
172
                $return = 'n';
173
        }
174
        return $return;
175
    }
176
}
177