@@ -6,16 +6,16 @@ discard block |
||
| 6 | 6 | |
| 7 | 7 | use tinymeng\tools\Tool; |
| 8 | 8 | |
| 9 | -class ConstCode{ |
|
| 9 | +class ConstCode { |
|
| 10 | 10 | |
| 11 | 11 | /** 公共状态 */ |
| 12 | - const STATUS_DELETE = 0;//删除 |
|
| 13 | - const STATUS_NORMAL = 1;//正常 |
|
| 12 | + const STATUS_DELETE = 0; //删除 |
|
| 13 | + const STATUS_NORMAL = 1; //正常 |
|
| 14 | 14 | |
| 15 | 15 | /** 性别 */ |
| 16 | - const GENDER = 0;//未知 |
|
| 17 | - const GENDER_MAN = 1;//男 |
|
| 18 | - const GENDER_WOMEN = 2;//女 |
|
| 16 | + const GENDER = 0; //未知 |
|
| 17 | + const GENDER_MAN = 1; //男 |
|
| 18 | + const GENDER_WOMEN = 2; //女 |
|
| 19 | 19 | |
| 20 | 20 | /** 登录类型 */ |
| 21 | 21 | const TYPE_QQ = 1; //QQ |
@@ -27,8 +27,8 @@ discard block |
||
| 27 | 27 | const TYPE_FACEBOOK = 7; //faceBook |
| 28 | 28 | const TYPE_GOOGLE = 8; //google |
| 29 | 29 | const TYPE_TWITTER = 9; //飞鸽 |
| 30 | - const TYPE_LINE = 10;//line |
|
| 31 | - const TYPE_NAVER = 11;//naver |
|
| 30 | + const TYPE_LINE = 10; //line |
|
| 31 | + const TYPE_NAVER = 11; //naver |
|
| 32 | 32 | const TYPE_ALIYUN = 12; //阿里云 |
| 33 | 33 | const TYPE_BAIDU = 13; //百度 |
| 34 | 34 | const TYPE_CODING = 14; //Coding |
@@ -53,24 +53,24 @@ discard block |
||
| 53 | 53 | * @param bool $type 类型:app applets |
| 54 | 54 | * @return int |
| 55 | 55 | */ |
| 56 | - static public function getTypeConst($channel,$type="") |
|
| 56 | + static public function getTypeConst($channel, $type = "") |
|
| 57 | 57 | { |
| 58 | - switch ($channel){ |
|
| 58 | + switch ($channel) { |
|
| 59 | 59 | case self::TYPE_QQ: |
| 60 | - if($type == 'app'){ |
|
| 61 | - $typeConst = self::TYPE_QQ_APP;//qqApp |
|
| 62 | - }else{ |
|
| 60 | + if ($type == 'app') { |
|
| 61 | + $typeConst = self::TYPE_QQ_APP; //qqApp |
|
| 62 | + } else { |
|
| 63 | 63 | $typeConst = $channel; |
| 64 | 64 | } |
| 65 | 65 | break; |
| 66 | 66 | case self::TYPE_WECHAT: |
| 67 | - if($type == 'app'){ |
|
| 68 | - $typeConst = self::TYPE_WECHAT_APP;//微信App |
|
| 69 | - }else if($type == 'applets'){ |
|
| 70 | - $typeConst = self::TYPE_WECHAT_APPLETS;//微信小程序 |
|
| 71 | - }else if(Tool::isWeiXin()){ |
|
| 72 | - $typeConst = self::TYPE_WECHAT_MOBILE;//微信mobile |
|
| 73 | - }else{ |
|
| 67 | + if ($type == 'app') { |
|
| 68 | + $typeConst = self::TYPE_WECHAT_APP; //微信App |
|
| 69 | + } else if ($type == 'applets') { |
|
| 70 | + $typeConst = self::TYPE_WECHAT_APPLETS; //微信小程序 |
|
| 71 | + } else if (Tool::isWeiXin()) { |
|
| 72 | + $typeConst = self::TYPE_WECHAT_MOBILE; //微信mobile |
|
| 73 | + } else { |
|
| 74 | 74 | $typeConst = $channel; |
| 75 | 75 | } |
| 76 | 76 | break; |
@@ -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 | /** 初始化实例类 */ |
@@ -41,7 +41,7 @@ discard block |
||
| 41 | 41 | 'scope' => $this->config['scope'], |
| 42 | 42 | 'state' => $this->config['state'], |
| 43 | 43 | ]; |
| 44 | - return self::AUTHORIZE_URL . '?' . http_build_query($params); |
|
| 44 | + return self::AUTHORIZE_URL.'?'.http_build_query($params); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | 'gender' => isset($result['gender']) ? $this->getGender($result['gender']) : ConstCode::GENDER, |
| 70 | 70 | 'avatar' => $result['picture'], |
| 71 | 71 | ); |
| 72 | - if(isset($result['email'])){ |
|
| 72 | + if (isset($result['email'])) { |
|
| 73 | 73 | $userInfo['email'] = $result['email']; |
| 74 | 74 | } |
| 75 | 75 | return $userInfo; |
@@ -80,15 +80,15 @@ discard block |
||
| 80 | 80 | */ |
| 81 | 81 | public function getUserInfo() |
| 82 | 82 | { |
| 83 | - if($this->type == 'app'){//App登录 |
|
| 84 | - if(!isset($_REQUEST['code']) ){ |
|
| 83 | + if ($this->type == 'app') {//App登录 |
|
| 84 | + if (!isset($_REQUEST['code'])) { |
|
| 85 | 85 | throw new \Exception("Google APP登录 需要传输code参数! "); |
| 86 | 86 | } |
| 87 | 87 | } |
| 88 | 88 | $this->getToken(); |
| 89 | 89 | |
| 90 | 90 | $headers = array( |
| 91 | - 'Authorization: Bearer ' . $this->token['access_token'], |
|
| 91 | + 'Authorization: Bearer '.$this->token['access_token'], |
|
| 92 | 92 | ); |
| 93 | 93 | |
| 94 | 94 | // 创建请求上下文 |
@@ -99,15 +99,15 @@ discard block |
||
| 99 | 99 | )); |
| 100 | 100 | |
| 101 | 101 | try { |
| 102 | - $data = file_get_contents(self::API_BASE . 'oauth2/v2/userinfo', false, $context); |
|
| 102 | + $data = file_get_contents(self::API_BASE.'oauth2/v2/userinfo', false, $context); |
|
| 103 | 103 | // 检查响应是否成功 |
| 104 | 104 | if ($data !== false) { |
| 105 | 105 | return json_decode($data, true); |
| 106 | 106 | } else { |
| 107 | - throw new \Exception("请求失败 " . $data); |
|
| 107 | + throw new \Exception("请求失败 ".$data); |
|
| 108 | 108 | } |
| 109 | - }catch (Exception $exception){ |
|
| 110 | - throw new \Exception("异常 " . $exception); |
|
| 109 | + } catch (Exception $exception) { |
|
| 110 | + throw new \Exception("异常 ".$exception); |
|
| 111 | 111 | } |
| 112 | 112 | } |
| 113 | 113 | |