OfficialService.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Master\Framework\Library\Easywechat;
  4. use App\Master\Framework\Library\Extend\Module;
  5. use EasyWeChat\OfficialAccount\Application;
  6. /**
  7. * 微信小程序开发组
  8. * class MiniAppService
  9. */
  10. class OfficialService extends EasyModule
  11. {
  12. /**
  13. * @var Application
  14. */
  15. private Application $app;
  16. private array $config;
  17. /**
  18. * 实例化
  19. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  20. */
  21. public function __construct()
  22. {
  23. parent::__construct();
  24. // 微信开发配置
  25. $_table = "EasywechatConfig";
  26. $config = Cache::remember("OFFICIAL_SERVICE_{$_table}", $this->ttl, function () use ($_table) {
  27. $module = Module::_SetupModule($_table);
  28. return $module['official'] ?? [];
  29. });
  30. $config = [
  31. 'app_id' => $config['app_id'] ?? '',
  32. 'secret' => $config['app_secret'] ?? '',
  33. 'token' => $config['token'] ?? '',
  34. 'aes_key' => $config['aes_key'] ?? '',
  35. /**
  36. * 接口请求相关配置,超时时间等,具体可用参数请参考:
  37. * https://github.com/symfony/symfony/blob/5.3/src/Symfony/Contracts/HttpClient/HttpClientInterface.php
  38. */
  39. 'http' => [
  40. 'throw' => true, // 状态码非 200、300 时是否抛出异常,默认为开启
  41. 'timeout' => 5.0,
  42. 'retry' => true, // 使用默认重试配置
  43. ],
  44. ];
  45. $this->config = $config;
  46. $this->app = new Application($config);
  47. }
  48. }