Pushplus.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * Pushplus
  4. *
  5. * @author thund1r <[email protected]>
  6. * @date 2022/10/13
  7. * @time 21:00
  8. */
  9. namespace Luolongfei\Libs\MessageServices;
  10. use GuzzleHttp\Client;
  11. use Luolongfei\Libs\Connector\MessageGateway;
  12. class Pushplus extends MessageGateway
  13. {
  14. const TIMEOUT = 33;
  15. const API_URL = 'https://www.pushplus.plus/send';
  16. /**
  17. * @var string SendKey
  18. */
  19. protected $sendKey;
  20. /**
  21. * @var Client
  22. */
  23. protected $client;
  24. public function __construct()
  25. {
  26. $this->sendKey = config('message.pushplus.pushplus_key');
  27. $this->client = new Client([
  28. 'cookies' => false,
  29. 'timeout' => self::TIMEOUT,
  30. 'verify' => config('verify_ssl'),
  31. 'debug' => config('debug'),
  32. ]);
  33. }
  34. /**
  35. * 生成域名状态 MarkDown 完整文本
  36. *
  37. * @param string $username
  38. * @param array $domainStatus
  39. *
  40. * @return string
  41. */
  42. public function genDomainStatusFullMarkDownText(string $username, array $domainStatus)
  43. {
  44. $markDownText = sprintf(lang('100090'), $username);
  45. $markDownText .= $this->genDomainStatusMarkDownText($domainStatus);
  46. $markDownText .= $this->getMarkDownFooter();
  47. return $markDownText;
  48. }
  49. /**
  50. * 获取 MarkDown 页脚
  51. *
  52. * @return string
  53. */
  54. public function getMarkDownFooter()
  55. {
  56. $footer = '';
  57. $footer .= lang('100091');
  58. return $footer;
  59. }
  60. /**
  61. * 生成域名状态 MarkDown 文本
  62. *
  63. * @param array $domainStatus
  64. *
  65. * @return string
  66. */
  67. public function genDomainStatusMarkDownText(array $domainStatus)
  68. {
  69. if (empty($domainStatus)) {
  70. return lang('100093');
  71. }
  72. $domainStatusMarkDownText = '';
  73. foreach ($domainStatus as $domain => $daysLeft) {
  74. $domainStatusMarkDownText .= sprintf(lang('100094'), $domain, $domain, $daysLeft);
  75. }
  76. $domainStatusMarkDownText = rtrim(rtrim($domainStatusMarkDownText, ' '), ',,') . lang('100095');
  77. return $domainStatusMarkDownText;
  78. }
  79. /**
  80. * 生成域名续期结果 MarkDown 文本
  81. *
  82. * @param string $username
  83. * @param array $renewalSuccessArr
  84. * @param array $renewalFailuresArr
  85. * @param array $domainStatus
  86. *
  87. * @return string
  88. */
  89. public function genDomainRenewalResultsMarkDownText(string $username, array $renewalSuccessArr, array $renewalFailuresArr, array $domainStatus)
  90. {
  91. $text = sprintf(lang('100096'), $username);
  92. if ($renewalSuccessArr) {
  93. $text .= lang('100097');
  94. $text .= $this->genDomainsMarkDownText($renewalSuccessArr);
  95. }
  96. if ($renewalFailuresArr) {
  97. $text .= lang('100098');
  98. $text .= $this->genDomainsMarkDownText($renewalFailuresArr);
  99. }
  100. $text .= lang('100099');
  101. $text .= $this->genDomainStatusMarkDownText($domainStatus);
  102. $text .= $this->getMarkDownFooter();
  103. return $text;
  104. }
  105. /**
  106. * 生成域名 MarkDown 文本
  107. *
  108. * @param array $domains
  109. *
  110. * @return string
  111. */
  112. public function genDomainsMarkDownText(array $domains)
  113. {
  114. $domainsMarkDownText = '';
  115. foreach ($domains as $domain) {
  116. $domainsMarkDownText .= sprintf("[%s](http://%s) ", $domain, $domain);
  117. }
  118. $domainsMarkDownText = trim($domainsMarkDownText, ' ') . "\n";
  119. return $domainsMarkDownText;
  120. }
  121. /**
  122. * 送信
  123. *
  124. * @param string $content
  125. * @param string $subject
  126. * @param int $type
  127. * @param array $data
  128. * @param string|null $recipient
  129. * @param mixed ...$params
  130. *
  131. * @return bool
  132. * @throws \Exception
  133. */
  134. public function send(string $content, string $subject = '', int $type = 1, array $data = [], ?string $recipient = null, ...$params)
  135. {
  136. $this->check($content, $data);
  137. $commonFooter = '';
  138. if ($type === 1 || $type === 4) {
  139. $this->setCommonFooter($commonFooter, "\n", false);
  140. } else if ($type === 2) {
  141. $this->setCommonFooter($commonFooter, "\n", false);
  142. $content = $this->genDomainRenewalResultsMarkDownText($data['username'], $data['renewalSuccessArr'], $data['renewalFailuresArr'], $data['domainStatusArr']);
  143. } else if ($type === 3) {
  144. $this->setCommonFooter($commonFooter);
  145. $content = $this->genDomainStatusFullMarkDownText($data['username'], $data['domainStatusArr']);
  146. } else {
  147. throw new \Exception(lang('100003'));
  148. }
  149. $content .= $commonFooter;
  150. $subject = $subject === '' ? mb_substr($content, 0, 12) . '...' : $subject;
  151. try {
  152. $resp = $this->client->post(
  153. self::API_URL,
  154. [
  155. 'form_params' => [
  156. 'token' => $this->sendKey,
  157. 'template' => "markdown",
  158. 'title' => $subject,
  159. 'content' => $content,
  160. ],
  161. ]
  162. );
  163. $resp = json_decode((string)$resp->getBody(), true);
  164. if (isset($resp['code']) && $resp['code'] === 200) {
  165. return true;
  166. }
  167. throw new \Exception($resp['msg'] ?? lang('100100'));
  168. } catch (\Exception $e) {
  169. system_log(sprintf(lang('100137'), $e->getMessage()));
  170. return false;
  171. }
  172. }
  173. }