├── README.md ├── .gitignore ├── composer.json └── src ├── AutoLoader.php └── Bean ├── QQ.php └── WeChat.php /README.md: -------------------------------------------------------------------------------- 1 | # swoft-oauth 2 | wechat、qq登陆组件 3 | 4 | ## app 使用 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .buildpath 2 | .settings/ 3 | .project 4 | *.patch 5 | .idea/ 6 | .git/ 7 | runtime/ 8 | vendor/ 9 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ctfang/swoft-oauth", 3 | "type": "library", 4 | "version": "v0.0.1", 5 | "keywords": [ 6 | "php", 7 | "swoole", 8 | "swoft", 9 | "oauth" 10 | ], 11 | "description": "swoft oauth component", 12 | "license": "Apache-2.0", 13 | "authors" : [ 14 | { 15 | "name": "ctfang", 16 | "email": "2206582181@qq.com" 17 | } 18 | ], 19 | "require": { 20 | "php": ">=7.1", 21 | "swlib/saber": "*" 22 | }, 23 | "autoload": { 24 | "classmap": [ 25 | ], 26 | "psr-4": { 27 | "Swoft\\OAuth\\": "src/" 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/AutoLoader.php: -------------------------------------------------------------------------------- 1 | dir path 18 | * ] 19 | */ 20 | public function getPrefixDirs(): array 21 | { 22 | return [ 23 | __NAMESPACE__ => __DIR__, 24 | ]; 25 | } 26 | 27 | /** 28 | * Metadata information for the component. 29 | * 30 | * Quick config: 31 | * 32 | * ```php 33 | * $jsonFile = \dirname(__DIR__) . '/composer.json'; 34 | * 35 | * return ComposerJSON::open($jsonFile)->getMetadata(); 36 | * ``` 37 | * 38 | * @return array 39 | * @see ComponentInterface::getMetadata() 40 | */ 41 | public function metadata(): array 42 | { 43 | $jsonFile = dirname(__DIR__) . '/composer.json'; 44 | 45 | return ComposerJSON::open($jsonFile)->getMetadata(); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Bean/QQ.php: -------------------------------------------------------------------------------- 1 | '', 19 | 'secret' => '', 20 | ]; 21 | 22 | 23 | protected $userInfoUri = 'https://graph.qq.com/user/get_user_info?'; 24 | 25 | /** 26 | * WeChat constructor. 27 | * @throws \ReflectionException 28 | * @throws \Swoft\Bean\Exception\ContainerException 29 | */ 30 | public function __construct() 31 | { 32 | $config = \config('oauth.qq'); 33 | if ($config) { 34 | $this->config = $config; 35 | } 36 | } 37 | 38 | /** 39 | * 获取用户信息 40 | * @param string $openid 41 | * @param $access_token 42 | * @return mixed 43 | * @throws Exception 44 | */ 45 | public function getUser(string $openid, string $access_token) 46 | { 47 | $param = [ 48 | 'access_token' => $access_token, 49 | 'oauth_consumer_key' => $this->getAppId(), 50 | 'openid' => $openid, 51 | 'format' => 'json' 52 | ]; 53 | $res = SaberGM::get($this->userInfoUri . http_build_query($param)); 54 | if (!$res->isSuccess()) throw new Exception("链接QQ平台网络异常"); 55 | 56 | $body = $res->getBody(); 57 | 58 | return json_decode($body, true); 59 | } 60 | 61 | /** 62 | * @return string 63 | * @throws Exception 64 | */ 65 | private function getAppId(): string 66 | { 67 | return $this->config["appid"]; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Bean/WeChat.php: -------------------------------------------------------------------------------- 1 | '', 20 | 'secret' => '', 21 | ]; 22 | 23 | /** 24 | * @var string 25 | */ 26 | protected $accessTokenURL = 'https://api.weixin.qq.com/sns/'; 27 | 28 | /** 29 | * WeChat constructor. 30 | * @throws \ReflectionException 31 | * @throws \Swoft\Bean\Exception\ContainerException 32 | */ 33 | public function __construct() 34 | { 35 | $config = \config('oauth.wx'); 36 | if ($config) { 37 | $this->config = $config; 38 | } 39 | } 40 | 41 | /** 42 | * @param string $code app授权的code 43 | * @return array 44 | * @throws Exception 45 | */ 46 | public function getAccessToken(string $code): array 47 | { 48 | $param = [ 49 | 'appid' => $this->getAppId(), 50 | 'secret' => $this->getSecret(), 51 | 'code' => $code, 52 | 'grant_type' => 'authorization_code' 53 | ]; 54 | $res = SaberGM::get($this->getAccessTokenUri() . http_build_query($param)); 55 | if (!$res->isSuccess()) throw new Exception("链接微信平台网络异常"); 56 | 57 | $body = $res->getBody(); 58 | 59 | return json_decode($body, true); 60 | } 61 | 62 | 63 | /** 64 | * @return string 65 | */ 66 | private function getAppId(): string 67 | { 68 | return $this->config["appid"]; 69 | } 70 | 71 | /** 72 | * @return string 73 | */ 74 | private function getSecret(): string 75 | { 76 | return $this->config["secret"]; 77 | } 78 | 79 | /** 80 | * @return string 81 | */ 82 | private function getAccessTokenUri(): string 83 | { 84 | return $this->accessTokenURL . 'oauth2/access_token?'; 85 | } 86 | } 87 | --------------------------------------------------------------------------------