-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from wwenling/master
开放平台代小程序实现登录
- Loading branch information
Showing
8 changed files
with
294 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: wwl | ||
* Date: 2019/08/13 | ||
* Time: 12:19 AM | ||
*/ | ||
|
||
namespace EasySwoole\WeChat\OpenPlatform; | ||
|
||
use EasySwoole\HttpClient\Exception\InvalidUrl; | ||
use EasySwoole\WeChat\Exception\RequestError; | ||
use EasySwoole\WeChat\Utility\NetWork; | ||
use EasySwoole\WeChat\Exception\OpenPlatformError; | ||
|
||
/** | ||
* 访问令牌管理 | ||
* Class AccessToken | ||
* @package EasySwoole\WeChat\MiniProgram | ||
*/ | ||
class AccessToken extends OpenPlatformBase | ||
{ | ||
|
||
/** | ||
* 获取访问令牌 默认刷新一次 | ||
* @param int $refreshTimes 获取失败的重试次数 | ||
* @return string|null | ||
* @throws OpenPlatformError | ||
* @throws RequestError | ||
* @throws InvalidUrl | ||
*/ | ||
public function getComponentAccessToken($refreshTimes = 1): ?string | ||
{ | ||
if ($refreshTimes < 0) { | ||
return null; | ||
} | ||
$data = $this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: wwl | ||
* Date: 2019/082/13 | ||
* Time: 11:39 AM | ||
*/ | ||
|
||
namespace EasySwoole\WeChat\OpenPlatform; | ||
|
||
|
||
class Encryptor extends OpenPlatformBase | ||
{ | ||
/** | ||
* decryptData | ||
* | ||
* @param string $sessionKey | ||
* @param string $iv | ||
* @param string $encryptedData | ||
* @return array | ||
*/ | ||
public function decryptData(string $sessionKey, string $iv, string $encryptedData): array | ||
{ | ||
$jsonString = openssl_decrypt(base64_decode($encryptedData), "aes-128-cbc", base64_decode($sessionKey), OPENSSL_RAW_DATA, base64_decode($iv)); | ||
return json_decode($jsonString, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: wwl | ||
* Date: 2019/082/13 | ||
* Time: 11:39 AM | ||
*/ | ||
|
||
namespace EasySwoole\WeChat\OpenPlatform; | ||
|
||
use EasySwoole\WeChat\Exception\Exception; | ||
|
||
/** | ||
* component_verify_ticket 微信服务器 每隔10分钟会向第三方的消息接收地址推送一次component_verify_ticket,用于获取第三方平台接口调用凭据 | ||
* Class AccessToken | ||
* @package EasySwoole\WeChat\OpenPlatform | ||
*/ | ||
|
||
class VerifyTicket extends OpenPlatformBase | ||
{ | ||
|
||
/** | ||
* set component_verify_ticket | ||
*/ | ||
public function setTicket(string $ticket) | ||
{ | ||
$cacheKey = $this->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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters