Bark.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * Bark 推送
  4. *
  5. * @author mybsdc <[email protected]>
  6. * @date 2021/11/3
  7. * @time 11:18
  8. */
  9. namespace Luolongfei\Libs\MessageServices;
  10. use GuzzleHttp\Client;
  11. use Luolongfei\Libs\Connector\MessageGateway;
  12. class Bark extends MessageGateway
  13. {
  14. const TIMEOUT = 33;
  15. /**
  16. * @var string Bark Key
  17. */
  18. protected $barkKey;
  19. /**
  20. * @var string Bark 域名
  21. */
  22. protected $barkUrl;
  23. /**
  24. * @var integer|string 指定是否需要保存推送信息到历史记录,1 为保存,其他值为不保存。如果值为空字符串,则推送信息将按照 APP 内设置来决定是否保存
  25. */
  26. protected $isArchive;
  27. /**
  28. * @var string 指定推送消息分组,可在历史记录中按分组查看推送
  29. */
  30. protected $group;
  31. /**
  32. * 可选参数值
  33. * active:不设置时的默认值,系统会立即亮屏显示通知
  34. * timeSensitive:时效性通知,可在专注状态下显示通知
  35. * passive:仅将通知添加到通知列表,不会亮屏提醒
  36. *
  37. * @var string 时效性通知
  38. */
  39. protected $level;
  40. /**
  41. * @var string 指定推送消息图标 (仅 iOS15 或以上支持)http://day.app/assets/images/avatar.jpg
  42. */
  43. protected $icon;
  44. /**
  45. * @var string 点击推送将跳转到url的地址(发送时,URL参数需要编码),GuzzleHttp 库会自动编码
  46. */
  47. protected $jumpUrl;
  48. /**
  49. * IOS14.5 之后长按或下拉推送即可触发自动复制,IOS14.5 之前无需任何操作即可自动复制
  50. *
  51. * @var integer 携带参数 automaticallyCopy=1, 收到推送时,推送内容会自动复制到粘贴板(如发现不能自动复制,可尝试重启一下手机)
  52. */
  53. protected $automaticallyCopy = 1;
  54. /**
  55. * @var string 携带 copy 参数, 则上面两种复制操作,将只复制 copy 参数的值
  56. */
  57. protected $copy = 'https://my.freenom.com/domains.php?a=renewals';
  58. /**
  59. * @var string 通知铃声
  60. */
  61. protected $sound;
  62. /**
  63. * @var Client
  64. */
  65. protected $client;
  66. public function __construct()
  67. {
  68. $this->barkKey = $this->parseBarkKey(config('message.bark.bark_key'));
  69. $this->barkUrl = rtrim(config('message.bark.bark_url'), '/');
  70. $this->isArchive = config('message.bark.bark_is_archive');
  71. $this->group = config('message.bark.bark_group');
  72. $this->level = config('message.bark.bark_level');
  73. $this->icon = config('message.bark.bark_icon');
  74. $this->jumpUrl = config('message.bark.bark_jump_url');
  75. $this->sound = config('message.bark.bark_sound');
  76. $this->client = new Client([
  77. 'cookies' => false,
  78. 'timeout' => self::TIMEOUT,
  79. 'verify' => config('verify_ssl'),
  80. 'debug' => config('debug'),
  81. ]);
  82. }
  83. /**
  84. * 解析 Bark Key
  85. *
  86. * 支持从这类 url 地址中提取 Bark Key
  87. * https://api.day.app/xxx/这里改成你自己的推送内容
  88. *
  89. * @param string $barkKey
  90. *
  91. * @return string
  92. */
  93. public function parseBarkKey(string $barkKey)
  94. {
  95. if (preg_match('/^https?:\/\/[^\/]+?\/(?P<barkKey>.+?)\//iu', $barkKey, $m)) {
  96. return $m['barkKey'];
  97. }
  98. return $barkKey;
  99. }
  100. /**
  101. * 生成域名文本
  102. *
  103. * @param array $domains
  104. *
  105. * @return string
  106. */
  107. public function genDomainsText(array $domains)
  108. {
  109. $domainsText = '';
  110. foreach ($domains as $domain) {
  111. $domainsText .= sprintf('%s ', $domain);
  112. }
  113. $domainsText = trim($domainsText, ' ') . "\n";
  114. return $domainsText;
  115. }
  116. /**
  117. * 获取页脚
  118. *
  119. * @return string
  120. */
  121. public function getFooter()
  122. {
  123. $footer = '';
  124. $footer .= lang('100078');
  125. return $footer;
  126. }
  127. /**
  128. * 生成域名状态文本
  129. *
  130. * @param array $domainStatus
  131. *
  132. * @return string
  133. */
  134. public function genDomainStatusText(array $domainStatus)
  135. {
  136. if (empty($domainStatus)) {
  137. return lang('100080');
  138. }
  139. $domainStatusText = '';
  140. foreach ($domainStatus as $domain => $daysLeft) {
  141. $domainStatusText .= sprintf(lang('100081'), $domain, $daysLeft);
  142. }
  143. $domainStatusText = rtrim(rtrim($domainStatusText, ' '), ',,') . lang('100082');
  144. return $domainStatusText;
  145. }
  146. /**
  147. * 生成域名续期结果文本
  148. *
  149. * @param string $username
  150. * @param array $renewalSuccessArr
  151. * @param array $renewalFailuresArr
  152. * @param array $domainStatus
  153. *
  154. * @return string
  155. */
  156. public function genDomainRenewalResultsText(string $username, array $renewalSuccessArr, array $renewalFailuresArr, array $domainStatus)
  157. {
  158. $text = sprintf(lang('100083'), $username);
  159. if ($renewalSuccessArr) {
  160. $text .= lang('100084');
  161. $text .= $this->genDomainsText($renewalSuccessArr);
  162. }
  163. if ($renewalFailuresArr) {
  164. $text .= lang('100085');
  165. $text .= $this->genDomainsText($renewalFailuresArr);
  166. }
  167. $text .= lang('100086');
  168. $text .= $this->genDomainStatusText($domainStatus);
  169. $text .= $this->getFooter();
  170. return $text;
  171. }
  172. /**
  173. * 生成域名状态完整文本
  174. *
  175. * @param string $username
  176. * @param array $domainStatus
  177. *
  178. * @return string
  179. */
  180. public function genDomainStatusFullText(string $username, array $domainStatus)
  181. {
  182. $markDownText = sprintf(lang('100087'), $username);
  183. $markDownText .= $this->genDomainStatusText($domainStatus);
  184. $markDownText .= $this->getFooter();
  185. return $markDownText;
  186. }
  187. /**
  188. * 送信
  189. *
  190. * @param string $content
  191. * @param string $subject
  192. * @param int $type
  193. * @param array $data
  194. * @param string|null $recipient
  195. * @param mixed ...$params
  196. *
  197. * @return bool|mixed
  198. * @throws \Exception
  199. */
  200. public function send(string $content, string $subject = '', int $type = 1, array $data = [], ?string $recipient = null, ...$params)
  201. {
  202. $this->check($content, $data);
  203. $commonFooter = '';
  204. if ($type === 1 || $type === 4) {
  205. $this->setCommonFooter($commonFooter, "\n", false);
  206. } else if ($type === 2) {
  207. $this->setCommonFooter($commonFooter, "\n", false);
  208. $content = $this->genDomainRenewalResultsText($data['username'], $data['renewalSuccessArr'], $data['renewalFailuresArr'], $data['domainStatusArr']);
  209. } else if ($type === 3) {
  210. $this->setCommonFooter($commonFooter);
  211. $content = $this->genDomainStatusFullText($data['username'], $data['domainStatusArr']);
  212. } else {
  213. throw new \Exception(lang('100003'));
  214. }
  215. $content .= $commonFooter;
  216. $query = [
  217. 'level' => $this->level,
  218. 'automaticallyCopy' => $this->automaticallyCopy, // 携带参数 automaticallyCopy=1, 收到推送时,推送内容会自动复制到粘贴板(如发现不能自动复制,可尝试重启一下手机)
  219. 'copy' => isset($data['html_url']) ? $data['html_url'] : $this->copy, // 携带 copy 参数,则上面的复制操作,将只复制 copy 参数的值
  220. ];
  221. if ($this->isArchive !== null) {
  222. $query['isArchive'] = $this->isArchive;
  223. }
  224. if ($this->group !== null) {
  225. $query['group'] = $this->group;
  226. }
  227. if ($this->icon !== null) {
  228. $query['icon'] = $this->icon;
  229. }
  230. if ($this->jumpUrl !== null) {
  231. $query['url'] = $this->jumpUrl;
  232. }
  233. if ($this->sound !== null) {
  234. $query['sound'] = $this->sound;
  235. }
  236. if (isset($data['badge'])) { // 设置角标
  237. $query['badge'] = $data['badge'];
  238. }
  239. $formParams = [
  240. 'body' => $content, // 推送内容 换行请使用换行符 \n
  241. ];
  242. if ($subject !== '') {
  243. $formParams['title'] = $subject; // 推送标题 比 body 字号粗一点
  244. }
  245. try {
  246. $resp = $this->client->post(
  247. sprintf('%s/%s/', $this->barkUrl, $this->barkKey),
  248. [
  249. 'query' => $query,
  250. 'form_params' => $formParams,
  251. ]
  252. );
  253. $resp = json_decode($resp->getBody()->getContents(), true);
  254. if (isset($resp['code']) && $resp['code'] === 200) {
  255. return true;
  256. }
  257. throw new \Exception($resp['message'] ?? lang('100088'));
  258. } catch (\Exception $e) {
  259. system_log(sprintf(lang('100089'), $e->getMessage()));
  260. return false;
  261. }
  262. }
  263. }