@@ -10,20 +10,20 @@ |
||
10 | 10 | //引入配置文件 |
11 | 11 | $params = require('config/params.php'); |
12 | 12 | |
13 | -try{ |
|
14 | - $name="qq";//登录类型,例:qq / google |
|
13 | +try { |
|
14 | + $name = "qq"; //登录类型,例:qq / google |
|
15 | 15 | $oauth = new oauth2($params); |
16 | 16 | if (empty($_GET['code'])) { |
17 | 17 | /** 登录 */ |
18 | 18 | $result = $oauth->login($name); |
19 | 19 | // var_dump($result['url']);die; |
20 | 20 | //重定向到第三方登录页 |
21 | - header('Location: ' . $result['url']); |
|
22 | - }else{ |
|
21 | + header('Location: '.$result['url']); |
|
22 | + } else { |
|
23 | 23 | /** 登录回调 */ |
24 | 24 | $result = $this->callback($name); |
25 | - var_dump($result);die; |
|
25 | + var_dump($result); die; |
|
26 | 26 | } |
27 | -}catch(Exception $e){ |
|
27 | +} catch (Exception $e) { |
|
28 | 28 | echo 'Oauth登录失败!'.$e->getMessage(); |
29 | 29 | } |
30 | 30 | \ No newline at end of file |
@@ -12,12 +12,12 @@ discard block |
||
12 | 12 | /** |
13 | 13 | * @Author: TinyMeng <[email protected]> |
14 | 14 | */ |
15 | - public function run(){ |
|
15 | + public function run() { |
|
16 | 16 | if (isset($_GET['code'])) { |
17 | 17 | $state = isset($_GET['state']) ? $_GET['state'] : ""; |
18 | - header('Location: ' . $_COOKIE['redirect_uri'] . '?code=' . $_GET['code'] . '&state=' . $state); |
|
18 | + header('Location: '.$_COOKIE['redirect_uri'].'?code='.$_GET['code'].'&state='.$state); |
|
19 | 19 | } else { |
20 | - if(!isset($_GET['appid']) || !isset($_GET['response_type']) || !isset($_GET['scope'])){ |
|
20 | + if (!isset($_GET['appid']) || !isset($_GET['response_type']) || !isset($_GET['scope'])) { |
|
21 | 21 | echo "参数缺失"; |
22 | 22 | return; |
23 | 23 | } |
@@ -26,20 +26,20 @@ discard block |
||
26 | 26 | $protocol = $this->is_HTTPS() ? 'https://' : 'http://'; |
27 | 27 | $params = array( |
28 | 28 | 'appid' => $_GET['appid'], |
29 | - 'redirect_uri' => $protocol . $_SERVER['HTTP_HOST'] . $_SERVER['DOCUMENT_URI'], |
|
29 | + 'redirect_uri' => $protocol.$_SERVER['HTTP_HOST'].$_SERVER['DOCUMENT_URI'], |
|
30 | 30 | 'response_type' => $_GET['response_type'], |
31 | 31 | 'scope' => $_GET['scope'], |
32 | 32 | 'state' => $state, |
33 | 33 | ); |
34 | - if($_GET['scope'] == 'snsapi_login'){ |
|
34 | + if ($_GET['scope'] == 'snsapi_login') { |
|
35 | 35 | //扫码登录 |
36 | - $AuthorizeURL = $this->AuthorizeURL . '/connect/qrconnect'; |
|
37 | - }else{ |
|
38 | - $AuthorizeURL = $this->AuthorizeURL . '/connect/oauth2/authorize'; |
|
36 | + $AuthorizeURL = $this->AuthorizeURL.'/connect/qrconnect'; |
|
37 | + } else { |
|
38 | + $AuthorizeURL = $this->AuthorizeURL.'/connect/oauth2/authorize'; |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | setcookie('redirect_uri', urldecode($_GET['redirect_uri']), $_SERVER['REQUEST_TIME'] + 60, '/'); |
42 | - header('Location: ' . $AuthorizeURL . '?' . http_build_query($params) . '#wechat_redirect'); |
|
42 | + header('Location: '.$AuthorizeURL.'?'.http_build_query($params).'#wechat_redirect'); |
|
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
@@ -48,7 +48,7 @@ discard block |
||
48 | 48 | * @Author: TinyMeng <[email protected]> |
49 | 49 | * @return bool |
50 | 50 | */ |
51 | - protected function is_HTTPS(){ |
|
51 | + protected function is_HTTPS() { |
|
52 | 52 | if (!isset($_SERVER['HTTPS'])) { |
53 | 53 | return false; |
54 | 54 | } |
@@ -23,7 +23,7 @@ discard block |
||
23 | 23 | 'app_id' => 'wx52e2b2464*****', |
24 | 24 | 'app_secret' => 'd5dad705a1159d*********', |
25 | 25 | 'callback' => $callback, |
26 | - 'scope' => 'snsapi_login',//扫码登录 |
|
26 | + 'scope' => 'snsapi_login', //扫码登录 |
|
27 | 27 | //'proxy_url' => 'http://www.abc.com/wx_proxy.php',//如果不需要代理请注释此行 |
28 | 28 | //'proxy_url' => 'http://www.abc.com/weixin-authorize-proxy.html',//如果不需要代理请注释此行 |
29 | 29 | ], |
@@ -31,14 +31,14 @@ discard block |
||
31 | 31 | 'app_id' => 'wx6ca7410f8******', |
32 | 32 | 'app_secret' => '30a206b87b7689b19f11******', |
33 | 33 | 'callback' => $callback, |
34 | - 'scope' => 'snsapi_userinfo',//静默授权=>snsapi_base;获取用户信息=>snsapi_userinfo |
|
34 | + 'scope' => 'snsapi_userinfo', //静默授权=>snsapi_base;获取用户信息=>snsapi_userinfo |
|
35 | 35 | //'proxy_url' => 'http://www.abc.com/wx_proxy.php',//如果不需要代理请注释此行 |
36 | 36 | //'proxy_url' => 'http://www.abc.com/weixin-authorize-proxy.html',//如果不需要代理请注释此行 |
37 | 37 | ], |
38 | 38 | 'app'=>[ |
39 | 39 | 'app_id' => 'wx6ca7410f8******', |
40 | 40 | 'app_secret' => '30a206b87b7689b19f11******', |
41 | - 'type' => 'app',//登录类型app |
|
41 | + 'type' => 'app', //登录类型app |
|
42 | 42 | ], |
43 | 43 | /** |
44 | 44 | * 微信小程序只能获取到 openid session_key |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | 'applets'=>[ |
48 | 48 | 'app_id' => 'wx6ca7410f8******', |
49 | 49 | 'app_secret' => '30a206b87b7689b19f11******', |
50 | - 'type' => 'applets',//登录类型小程序 |
|
50 | + 'type' => 'applets', //登录类型小程序 |
|
51 | 51 | ], |
52 | 52 | ] |
53 | 53 | /** |
@@ -5,7 +5,7 @@ discard block |
||
5 | 5 | /** |
6 | 6 | * |
7 | 7 | */ |
8 | -class oauth2{ |
|
8 | +class oauth2 { |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * 全部配置文件 |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | /** |
27 | 27 | * @param $conf |
28 | 28 | */ |
29 | - public function __construct($conf){ |
|
29 | + public function __construct($conf) { |
|
30 | 30 | $this->configAll = $conf; |
31 | 31 | } |
32 | 32 | |
@@ -37,15 +37,15 @@ discard block |
||
37 | 37 | * Updater: |
38 | 38 | * @param $name |
39 | 39 | */ |
40 | - public function getConfig($name){ |
|
41 | - $this->config = $this->configAll[$name]??[]; |
|
42 | - if($name == 'wechat'){ |
|
43 | - if(!Tool::isMobile()){ |
|
44 | - $this->config = $this->config['pc'];//微信pc扫码登录 |
|
45 | - }elseif(Tool::isWeiXin()){ |
|
46 | - $this->config = $this->config['mobile'];//微信浏览器中打开 |
|
47 | - }else{ |
|
48 | - echo '请使用微信打开!';exit();//手机浏览器打开 |
|
40 | + public function getConfig($name) { |
|
41 | + $this->config = $this->configAll[$name] ?? []; |
|
42 | + if ($name == 'wechat') { |
|
43 | + if (!Tool::isMobile()) { |
|
44 | + $this->config = $this->config['pc']; //微信pc扫码登录 |
|
45 | + }elseif (Tool::isWeiXin()) { |
|
46 | + $this->config = $this->config['mobile']; //微信浏览器中打开 |
|
47 | + } else { |
|
48 | + echo '请使用微信打开!'; exit(); //手机浏览器打开 |
|
49 | 49 | } |
50 | 50 | } |
51 | 51 | //可以设置代理服务器,一般用于调试国外平台 |
@@ -57,15 +57,15 @@ discard block |
||
57 | 57 | * Author: JiaMeng <[email protected]> |
58 | 58 | * Updater: |
59 | 59 | */ |
60 | - public function login($name,$state=""){ |
|
60 | + public function login($name, $state = "") { |
|
61 | 61 | /** 获取配置 */ |
62 | - if($name == 'wx') $name = 'wechat'; |
|
62 | + if ($name == 'wx') $name = 'wechat'; |
|
63 | 63 | $this->getConfig($name); |
64 | 64 | $this->config['state'] = $state; |
65 | 65 | |
66 | 66 | /** 初始化实例类 */ |
67 | 67 | $oauth = OAuth::$name($this->config); |
68 | - if(Tool::isMobile() || Tool::isWeiXin()){ |
|
68 | + if (Tool::isMobile() || Tool::isWeiXin()) { |
|
69 | 69 | /** |
70 | 70 | * 对于微博,如果登录界面要适用于手机,则需要设定->setDisplay('mobile') |
71 | 71 | * 对于微信,如果是公众号登录,则需要设定->setDisplay('mobile'),否则是WEB网站扫码登录 |
@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | public function callback($name) |
95 | 95 | { |
96 | 96 | /** 获取配置 */ |
97 | - if($name == 'wx') $name = 'wechat'; |
|
97 | + if ($name == 'wx') $name = 'wechat'; |
|
98 | 98 | $this->getConfig($name); |
99 | 99 | |
100 | 100 | /** 初始化实例类 */ |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | 'state' => $this->config['state'], |
35 | 35 | 'scope' => $this->config['scope'], |
36 | 36 | ]; |
37 | - return $this->AuthorizeURL . '?' . http_build_query($params); |
|
37 | + return $this->AuthorizeURL.'?'.http_build_query($params); |
|
38 | 38 | } |
39 | 39 | |
40 | 40 | /** |
@@ -73,7 +73,7 @@ discard block |
||
73 | 73 | $params = [ |
74 | 74 | 'access_token'=>$this->token['access_token'], |
75 | 75 | ]; |
76 | - $data = $this->get($this->UserInfoURL,$params); |
|
76 | + $data = $this->get($this->UserInfoURL, $params); |
|
77 | 77 | return json_decode($data, true); |
78 | 78 | } |
79 | 79 | |
@@ -91,7 +91,7 @@ discard block |
||
91 | 91 | /** |
92 | 92 | * Description: 获取AccessToken |
93 | 93 | */ |
94 | - protected function getToken(){ |
|
94 | + protected function getToken() { |
|
95 | 95 | if (empty($this->token)) { |
96 | 96 | /** 验证state参数 */ |
97 | 97 | $this->CheckState(); |
@@ -100,8 +100,8 @@ discard block |
||
100 | 100 | $params = $this->accessTokenParams(); |
101 | 101 | |
102 | 102 | /** 获取access_token */ |
103 | - $this->AccessTokenURL = $this->AccessTokenURL . '?' . http_build_query($params); |
|
104 | - $token = $this->post($this->AccessTokenURL); |
|
103 | + $this->AccessTokenURL = $this->AccessTokenURL.'?'.http_build_query($params); |
|
104 | + $token = $this->post($this->AccessTokenURL); |
|
105 | 105 | /** 解析token值(子类实现此方法) */ |
106 | 106 | $this->token = $this->parseToken($token); |
107 | 107 | } |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | $this->AuthorizeURL = str_replace("alipay", "alipaydev", $this->AuthorizeURL); |
82 | 82 | } |
83 | 83 | |
84 | - return $this->AuthorizeURL . '?' . http_build_query($params); |
|
84 | + return $this->AuthorizeURL.'?'.http_build_query($params); |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | /** |
@@ -114,16 +114,16 @@ discard block |
||
114 | 114 | |
115 | 115 | $userInfo = [ |
116 | 116 | 'open_id' => $this->openid(), |
117 | - 'union_id' => $this->token['union_id']??'', |
|
117 | + 'union_id' => $this->token['union_id'] ?? '', |
|
118 | 118 | 'channel' => ConstCode::TYPE_ALIPAY, |
119 | 119 | 'nickname' => $result['nick_name'], |
120 | 120 | 'gender' => isset($result['gender']) ? $this->getGender($result['gender']) : ConstCode::GENDER, |
121 | 121 | 'avatar' => $result['avatar'], |
122 | 122 | // 拓展字段 |
123 | - 'access_token' => $this->token['access_token']??'', |
|
123 | + 'access_token' => $this->token['access_token'] ?? '', |
|
124 | 124 | 'user_id' => $this->token['user_id'], |
125 | 125 | ]; |
126 | - $userInfo['type'] = ConstCode::getTypeConst($userInfo['channel'],$this->type); |
|
126 | + $userInfo['type'] = ConstCode::getTypeConst($userInfo['channel'], $this->type); |
|
127 | 127 | return $userInfo; |
128 | 128 | } |
129 | 129 | |
@@ -136,12 +136,12 @@ discard block |
||
136 | 136 | */ |
137 | 137 | public function getUserInfo() |
138 | 138 | { |
139 | - if($this->type == 'app'){//App登录 |
|
140 | - if(!isset($_REQUEST['access_token']) ){ |
|
139 | + if ($this->type == 'app') {//App登录 |
|
140 | + if (!isset($_REQUEST['access_token'])) { |
|
141 | 141 | throw new \Exception("AliPay APP登录 需要传输access_token参数! "); |
142 | 142 | } |
143 | 143 | $this->token['access_token'] = $_REQUEST['access_token']; |
144 | - }else { |
|
144 | + } else { |
|
145 | 145 | /** 获取token信息 */ |
146 | 146 | $this->getToken(); |
147 | 147 | } |
@@ -159,7 +159,7 @@ discard block |
||
159 | 159 | |
160 | 160 | $data = $this->post(self::API_BASE, $params); |
161 | 161 | $data = mb_convert_encoding($data, 'utf-8', 'gbk'); |
162 | - $result = json_decode($data, true); |
|
162 | + $result = json_decode($data, true); |
|
163 | 163 | return $result['alipay_user_info_share_response']; |
164 | 164 | } |
165 | 165 | |
@@ -236,7 +236,7 @@ discard block |
||
236 | 236 | throw new \Exception('支付宝RSA密钥未配置'); |
237 | 237 | } |
238 | 238 | $rsa = str_replace([PHP_EOL, $header, $footer], '', $rsa); |
239 | - $rsaVal = $header . PHP_EOL . chunk_split($rsa, 64, PHP_EOL) . $footer; |
|
239 | + $rsaVal = $header.PHP_EOL.chunk_split($rsa, 64, PHP_EOL).$footer; |
|
240 | 240 | return $rsaVal; |
241 | 241 | } |
242 | 242 | |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | |
256 | 256 | if (isset($data['alipay_system_oauth_token_response'])) { |
257 | 257 | $data = $data['alipay_system_oauth_token_response']; |
258 | - $data['openid'] = $data['open_id']??$data['user_id']; |
|
258 | + $data['openid'] = $data['open_id'] ?? $data['user_id']; |
|
259 | 259 | return $data; |
260 | 260 | } else { |
261 | 261 | throw new \Exception("获取支付宝 ACCESS_TOKEN 出错:{$token}"); |
@@ -4,7 +4,7 @@ |
||
4 | 4 | */ |
5 | 5 | namespace tinymeng\OAuth2\Helper; |
6 | 6 | |
7 | -class Encryptor{ |
|
7 | +class Encryptor { |
|
8 | 8 | |
9 | 9 | /** |
10 | 10 | * 解密数据(微信小程序手机号) |