TelegramRepository.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Repositories\Utils;
  3. use App\Repositories\ConfigRepoistory;
  4. use Hyperf\Guzzle\ClientFactory;
  5. use Psr\SimpleCache\CacheInterface;
  6. class TelegramRepository
  7. {
  8. /**
  9. * 缓存仓库
  10. * @var null|CacheInterface
  11. */
  12. private $cacheRepo = null;
  13. /**
  14. * http客户端仓库
  15. * @var null|\GuzzleHttp\Client
  16. */
  17. private $httpClient = null;
  18. private $configRepo = null;
  19. private $noticenterConfig = [];
  20. /**
  21. * 发送通用文本消息.
  22. * @param $content
  23. * @return bool
  24. * @throws \GuzzleHttp\Exception\GuzzleException
  25. */
  26. public function sendCommonText($content, $taskName = null)
  27. {
  28. $ntCofnig = $this->getNoticenterConfig();
  29. $uri = '/api/tg/send/common/text/message';
  30. $options = [
  31. 'query' => [
  32. 'AUTH-TOKEN' => $ntCofnig['token'],
  33. ],
  34. 'json' => [
  35. 'content' => $content,
  36. 'task_name' => $taskName,
  37. ],
  38. ];
  39. $ret = $this->getHttpClient()->post($uri, $options);
  40. $httpStatus = $ret->getStatusCode();
  41. $bodyRaw = $ret->getBody()->getContents();
  42. $result = [];
  43. if ($httpStatus == 200) {
  44. $arrResp = json_decode($bodyRaw, true);
  45. $result['code'] = $arrResp['code'];
  46. $result['message'] = $arrResp['message'];
  47. $result['data'] = $arrResp['data'];
  48. } else {
  49. logger('TelegramRepository-sendCommonText', ['http_status' => $httpStatus, 'body_raw' => $bodyRaw]);
  50. }
  51. return true;
  52. }
  53. /**
  54. * 获取消息中心配置.
  55. * @return array
  56. * @throws \Psr\SimpleCache\InvalidArgumentException
  57. */
  58. public function getNoticenterConfig()
  59. {
  60. if (empty($this->noticenterConfig)) {
  61. $baseUrl = $this->getConfigRepo()->get('tg_msg_server', 'sys', '');
  62. $token = $this->getConfigRepo()->get('tg_api_token', 'sys', '');
  63. $baseUrl = rtrim($baseUrl, '/'); // 干掉最后一个斜杆
  64. $this->noticenterConfig = [
  65. 'base_url' => $baseUrl,
  66. 'token' => $token,
  67. ];
  68. }
  69. return $this->noticenterConfig;
  70. }
  71. /**
  72. * @return \GuzzleHttp\Client|ClientFactory|null
  73. */
  74. private function getHttpClient()
  75. {
  76. if (!$this->httpClient instanceof ClientFactory) {
  77. $ntCofnig = $this->getNoticenterConfig();
  78. $baseUrl = rtrim($ntCofnig['base_url'], '/');
  79. $token = $ntCofnig['token'];
  80. $options = array(
  81. 'base_uri' => $baseUrl,
  82. 'timeout' => 15, // 超时,单位秒.
  83. 'verify' => false, // 是否校验SSL证书.
  84. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  85. 'debug' => false, // 是否启用调试模式.
  86. 'allow_redirects' => false, // 不跟随301/302跳转.
  87. 'headers' => [
  88. 'AUTH-TOKEN' => $token,
  89. ],
  90. );
  91. $this->httpClient = make(ClientFactory::class)->create($options);
  92. }
  93. return $this->httpClient;
  94. }
  95. /**
  96. * @return ConfigRepoistory|mixed
  97. */
  98. private function getConfigRepo()
  99. {
  100. if (!$this->configRepo instanceof ConfigRepoistory) {
  101. $this->configRepo = make(ConfigRepoistory::class);
  102. }
  103. return $this->configRepo;
  104. }
  105. }