From 379de8a3e80d57a1e75f8e03540d0c7e5fe4c7d5 Mon Sep 17 00:00:00 2001 From: ljyljy0211 Date: Fri, 30 Dec 2022 14:52:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BC=81=E4=B8=9A=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E8=87=AA=E5=BB=BA=E5=BA=94=E7=94=A8=E5=92=8C=E4=BB=A3?= =?UTF-8?q?=E5=BC=80=E5=8F=91=E5=BA=94=E7=94=A8oauth=E6=8E=88=E6=9D=83?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=8E=9F=E6=9C=89?= =?UTF-8?q?=E7=9A=84oauth=E6=98=AF=E5=80=9F=E7=94=A8socialite=E7=BB=84?= =?UTF-8?q?=E4=BB=B6=E5=8C=85=EF=BC=8C=E9=82=A3=E4=B8=AA=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E5=AF=B9=E5=BA=943.x=EF=BC=8C=E9=87=8C?= =?UTF-8?q?=E9=9D=A2=E7=9A=84=E9=93=BE=E6=8E=A5=E5=92=8C=E4=BC=81=E5=BE=AE?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E9=93=BE=E6=8E=A5=E4=B8=8D=E4=B8=80=E8=87=B4?= =?UTF-8?q?=E4=BA=86=EF=BC=8C=E5=B9=B6=E4=B8=94=E6=B2=A1=E6=9C=89angentid?= =?UTF-8?q?=E7=9A=84=E8=AE=BE=E7=BD=AE=E3=80=82=20(#2640)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: lujunyi --- src/Work/Auth/Client.php | 94 +++++++++++++++++++++++++++++++ src/Work/Auth/ServiceProvider.php | 3 + 2 files changed, 97 insertions(+) create mode 100644 src/Work/Auth/Client.php diff --git a/src/Work/Auth/Client.php b/src/Work/Auth/Client.php new file mode 100644 index 000000000..4584eb8da --- /dev/null +++ b/src/Work/Auth/Client.php @@ -0,0 +1,94 @@ + + * + * This source file is subject to the MIT license that is bundled + * with this source code in the file LICENSE. + */ + +namespace EasyWeChat\Work\Auth; + +use EasyWeChat\Kernel\BaseClient; + +/** + * Class Client. + * + * @author lujunyi + */ +class Client extends BaseClient +{ + /** + * 获取网页授权登录url,适用于自建应用与代开发应用。 + * + * @see https://developer.work.weixin.qq.com/document/path/91022 + * + * @param string $redirectUri + * @param string $scope + * @param string $agentId + * @param string|null $state + * + * @return string + * @throws \Exception + */ + public function getOAuthRedirectUrl(string $redirectUri = '', string $scope = 'snsapi_privateinfo', string $agentId = '', string $state = null) + { + $redirectUri || $redirectUri = $this->app->config['redirect_uri_oauth']; + $state || $state = random_bytes(64); + $params = [ + 'appid' => $this->app['config']['suite_id'], + 'redirect_uri' => $redirectUri, + 'response_type' => 'code', + 'scope' => $scope, + 'state' => $state, + 'agentid' => $agentId, + ]; + + return 'https://open.weixin.qq.com/connect/oauth2/authorize?' . http_build_query($params) . '#wechat_redirect'; + } + + + /** + * 获取访问用户身份。根据code获取成员信息,适用于自建应用与代开发应用。 + * + * @see https://developer.work.weixin.qq.com/document/path/91023 + * + * @param string $code + * + * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string + * + * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function getUserByCode(string $code) + { + $params = [ + 'code' => $code, + ]; + + return $this->httpGet('cgi-bin/auth/getuserinfo', $params); + } + + /** + * 获取访问用户敏感信息。自建应用与代开发应用可通过该接口获取成员授权的敏感字段。 + * + * @see https://developer.work.weixin.qq.com/document/path/95833 + * + * @param string $userTicket + * + * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string + * + * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function getUserByTicket(string $userTicket) + { + $params = [ + 'user_ticket' => $userTicket, + ]; + + return $this->httpPostJson('cgi-bin/auth/getuserdetail', $params); + } +} diff --git a/src/Work/Auth/ServiceProvider.php b/src/Work/Auth/ServiceProvider.php index 5f0dba9db..7e0ee9528 100644 --- a/src/Work/Auth/ServiceProvider.php +++ b/src/Work/Auth/ServiceProvider.php @@ -29,5 +29,8 @@ public function register(Container $app) isset($app['access_token']) || $app['access_token'] = function ($app) { return new AccessToken($app); }; + !isset($app['auth']) && $app['auth'] = function ($app) { + return new Client($app); + }; } }