OPApplication.php 925 B

12345678910111213141516171819202122232425
  1. <?php
  2. namespace Modules\Common\Support\Wechat\OpenPlatform;
  3. use EasyWeChat\OpenPlatform\Application;
  4. class OPApplication extends Application
  5. {
  6. public function getAuthorizerAccessToken(string $appId, string $refreshToken): string
  7. {
  8. $component_appid = $this->getAccount()->getAppId();
  9. $cacheKey = sprintf('open-platform.authorizer_access_token.%s.%s.%s', $component_appid, $appId, md5($refreshToken));
  10. /** @phpstan-ignore-next-line */
  11. $authorizerAccessToken = (string) $this->getCache()->get($cacheKey);
  12. if (! $authorizerAccessToken) {
  13. $response = $this->refreshAuthorizerToken($appId, $refreshToken);
  14. $authorizerAccessToken = (string) $response['authorizer_access_token'];
  15. $this->getCache()->set($cacheKey, $authorizerAccessToken, intval($response['expires_in'] ?? 7200) - 500);
  16. }
  17. return $authorizerAccessToken;
  18. }
  19. }