From 8238bf43df2fbf931c461d47f226afddef47d0f8 Mon Sep 17 00:00:00 2001 From: wangwenling Date: Thu, 15 Aug 2019 14:48:31 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E4=BF=AE=E6=94=B9post=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E8=AF=B7=E6=B1=82=E9=97=AE=E9=A2=98=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9getForJson=E5=8F=AF=E4=BC=A0=E5=85=A5?= =?UTF-8?q?=E6=95=B0=E7=BB=84=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Utility/NetWork.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Utility/NetWork.php b/src/Utility/NetWork.php index 3b9386b..57c6d7d 100644 --- a/src/Utility/NetWork.php +++ b/src/Utility/NetWork.php @@ -42,6 +42,17 @@ static function get(string $url): Response return (new HttpClient($url))->setTimeout(self::$TIMEOUT)->get(); } + /** + * 发起一次Get请求并获得返回 + * @param string $url + * @param array $data + * @return Response + * @throws InvalidUrl + */ + static function getByArray(string $url, array $data): Response + { + return (new HttpClient($url))->setTimeout(self::$TIMEOUT)->setQuery($data)->get(); + } /** * 返回内容进行Json解析 @@ -50,9 +61,13 @@ static function get(string $url): Response * @throws InvalidUrl * @throws RequestError */ - static function getForJson(string $url): array + static function getForJson(string $url, array $data = []): array { - return self::parserJson(self::get($url)); + if ($data) { + return self::parserJson(self::getByArray($url, $data)); + } else { + return self::parserJson(self::get($url)); + } } /** @@ -65,9 +80,7 @@ static function getForJson(string $url): array */ static function post(string $url, $data = null) { - $client = new HttpClient($url); - $client->setTimeout(self::$TIMEOUT); - return $client->postJson($data); + return self::postJson($url, $data); } /** From 1c62cf2b5608142a0d013fdc6e13651cde41e3e9 Mon Sep 17 00:00:00 2001 From: wangwenling Date: Thu, 15 Aug 2019 14:49:03 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=AC=AC=E4=B8=89?= =?UTF-8?q?=E6=96=B9=E5=B9=B3=E5=8F=B0component=5Faccess=5Ftoken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OpenPlatform/AccessToken.php | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/OpenPlatform/AccessToken.php diff --git a/src/OpenPlatform/AccessToken.php b/src/OpenPlatform/AccessToken.php new file mode 100644 index 0000000..c700b1e --- /dev/null +++ b/src/OpenPlatform/AccessToken.php @@ -0,0 +1,71 @@ +getOpenPlatform()->getConfig()->getStorage()->get('component_access_token'); + if (!empty($data)) { + return $data; + } else { + $this->refresh(); + return $this->getComponentAccessToken($refreshTimes - 1); + } + } + + /** + * 刷新访问令牌 + * @return string + * @throws OpenPlatformError + * @throws RequestError + * @throws InvalidUrl + */ + public function refresh(): string + { + $verifyTicket = $this->getOpenPlatform()->verifyTicket()->getTicket(); + $config = $this->getOpenPlatform()->getConfig(); + $data = [ + 'component_appid' => $config->getComponentAppId(), + 'component_appsecret' => $config->getComponentAppSecret(), + 'component_verify_ticket' => $verifyTicket + ]; + $responseArray = NetWork::postForJson(ApiUrl::API_COMPONENT_TOKEN, $data); + $ex = OpenPlatformError::hasException($responseArray); + if ($ex) { + throw $ex; + } + $token = $responseArray['component_access_token']; + $config->getStorage()->set('component_access_token', $token, time() + 7180); + return $token; + } +} \ No newline at end of file From 61bd05d1a1b9ea296e422a99a2cb324e0db2011c Mon Sep 17 00:00:00 2001 From: wangwenling Date: Thu, 15 Aug 2019 14:50:45 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96compone?= =?UTF-8?q?nt=5Ftoken=20=E5=92=8C=20jscode2session=20=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OpenPlatform/ApiUrl.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/OpenPlatform/ApiUrl.php b/src/OpenPlatform/ApiUrl.php index 82553ca..904ff18 100644 --- a/src/OpenPlatform/ApiUrl.php +++ b/src/OpenPlatform/ApiUrl.php @@ -31,6 +31,15 @@ class ApiUrl // 获取用户信息 const GET_USER_INFO = 'https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID'; + /* + * 三方平台代替小程序 code 换取 session_key + * @see https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1492585163_FtTNA&token=&lang= + */ + const COMPONENT_AUTH_CODE2SESSION = 'https://api.weixin.qq.com/sns/component/jscode2session'; + + //获取第三方平台component_access_token + const API_COMPONENT_TOKEN = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token'; + public static function generateURL(string $baseUrl, array $data): string { foreach ($data as $key => $item) { From db34a3cb2e9b2f42caad3b3b13322f8d83a4e92a Mon Sep 17 00:00:00 2001 From: wangwenling Date: Thu, 15 Aug 2019 14:51:35 +0800 Subject: [PATCH 4/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BC=80=E5=8F=91?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0=E5=BE=85=E5=B0=8F=E7=A8=8B=E5=BA=8F=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0session2code=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OpenPlatform/Auth.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/OpenPlatform/Auth.php b/src/OpenPlatform/Auth.php index 6f24bb6..908e655 100644 --- a/src/OpenPlatform/Auth.php +++ b/src/OpenPlatform/Auth.php @@ -70,4 +70,33 @@ function authCheck(SnsAuthBean $authBean) { } + + /** + * session2code + * + * @param string $code + * @return array + * @throws OpenPlatformError + * @throws RequestError + * @throws \EasySwoole\HttpClient\Exception\InvalidUrl + */ + public function session(string $code): array + { + $accessToken = $this->getOpenPlatform()->accessToken()->getComponentAccessToken(); + $config = $this->getOpenPlatform()->getConfig(); + $data = [ + 'appid' => $config->getAppId(), + 'js_code' => $code, + 'grant_type' => 'authorization_code', + 'component_appid' => $config->getComponentAppId(), + 'component_access_token' => $accessToken, + ]; + $responseArray = NetWork::getForJson(ApiUrl::COMPONENT_AUTH_CODE2SESSION, $data); + $ex = OpenPlatformError::hasException($responseArray); + if ($ex) { + throw $ex; + } + + return $responseArray; + } } \ No newline at end of file From 44ec828433d108107435a56ca41dbda0630bc909 Mon Sep 17 00:00:00 2001 From: wangwenling Date: Thu, 15 Aug 2019 14:51:45 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E8=A7=A3=E5=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OpenPlatform/Encryptor.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/OpenPlatform/Encryptor.php diff --git a/src/OpenPlatform/Encryptor.php b/src/OpenPlatform/Encryptor.php new file mode 100644 index 0000000..3299fb7 --- /dev/null +++ b/src/OpenPlatform/Encryptor.php @@ -0,0 +1,28 @@ + Date: Thu, 15 Aug 2019 14:52:08 +0800 Subject: [PATCH 6/7] component_verify_ticke --- src/OpenPlatform/VerifyTicket.php | 57 +++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/OpenPlatform/VerifyTicket.php diff --git a/src/OpenPlatform/VerifyTicket.php b/src/OpenPlatform/VerifyTicket.php new file mode 100644 index 0000000..09ba960 --- /dev/null +++ b/src/OpenPlatform/VerifyTicket.php @@ -0,0 +1,57 @@ +getCacheKey(); + $this->getOpenPlatform()->getConfig()->getStorage()->set($cacheKey, $ticket, time() + 580); + + if (!$this->getOpenPlatform()->getConfig()->getStorage()->get($this->getCacheKey())) { + throw new Exception('Failed to cache verify ticket'); + } + + return $this; + + } + + /** + * get component_verify_ticket + * @return string + */ + public function getTicket(): string + { + if ($cached = $this->getOpenPlatform()->getConfig()->getStorage()->get($this->getCacheKey())) { + return $cached; + } + } + + /** + * 获取 cache key + * @return string + */ + protected function getCacheKey(): string + { + return 'wechat.open_platform.verify_ticket.'.$this->getOpenPlatform()->getConfig()->getAppId(); + } +} \ No newline at end of file From 7f57e7dc8298137aec3c98d532a1137f62f9b604 Mon Sep 17 00:00:00 2001 From: wangwenling Date: Thu, 15 Aug 2019 14:53:08 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OpenPlatform/OpenPlatform.php | 42 +++++++++++++++++++++++++ src/OpenPlatform/OpenPlatformConfig.php | 40 +++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/OpenPlatform/OpenPlatform.php b/src/OpenPlatform/OpenPlatform.php index f7876e3..ff44711 100644 --- a/src/OpenPlatform/OpenPlatform.php +++ b/src/OpenPlatform/OpenPlatform.php @@ -42,4 +42,46 @@ public function getConfig(): OpenPlatformConfig { return $this->config; } + + /** + * accessToken + * + * @return AccessToken + */ + public function accessToken(): AccessToken + { + if (!isset($this->accessToken)) { + $this->accessToken = new AccessToken($this); + } + + return $this->accessToken; + } + + /** + * VerifyTicket + * + * @return VerifyTicket + */ + public function verifyTicket(): VerifyTicket + { + if (!isset($this->verifyTicket)) { + $this->verifyTicket = new VerifyTicket($this); + } + + return $this->verifyTicket; + } + + /** + * encryptor + * + * @return Encryptor + */ + public function encryptor(): Encryptor + { + if (!isset($this->encryptor)) { + $this->encryptor = new Encryptor($this); + } + + return $this->encryptor; + } } \ No newline at end of file diff --git a/src/OpenPlatform/OpenPlatformConfig.php b/src/OpenPlatform/OpenPlatformConfig.php index 1b77035..ee21973 100644 --- a/src/OpenPlatform/OpenPlatformConfig.php +++ b/src/OpenPlatform/OpenPlatformConfig.php @@ -122,4 +122,44 @@ public function setTempDir($tempDir): OpenPlatformConfig $this->tempDir = $tempDir; return $this; } + + /** + * 获取第三方平台appid + * @return mixed + */ + public function getComponentAppId() + { + return $this->componentAppId; + } + + /** + * 设置第三方平台appid + * @param mixed + * @return OpenPlatformConfig + */ + public function setComponentAppId($componentAppId): OpenPlatformConfig + { + $this->componentAppId = $componentAppId; + return $this; + } + + /** + * 获取第三方平台appsecret + * @return mixed + */ + public function getComponentAppSecret() + { + return $this->componentAppSecret; + } + + /** + * 设置第三方平台appsecret + * @param mixed + * @return OpenPlatformConfig + */ + public function setComponentAppSecret($componentAppSecret): OpenPlatformConfig + { + $this->componentAppSecret = $componentAppSecret; + return $this; + } } \ No newline at end of file