ServerChan.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Components;
  3. use App\Http\Models\EmailLog;
  4. use Log;
  5. class ServerChan
  6. {
  7. /**
  8. * 推送消息
  9. *
  10. * @param string $title 消息标题
  11. * @param string $content 消息内容
  12. *
  13. * @return mixed
  14. */
  15. public static function send($title, $content)
  16. {
  17. if (Helpers::systemConfig()['is_server_chan'] && Helpers::systemConfig()['server_chan_key']) {
  18. try {
  19. $url = 'https://sc.ftqq.com/' . Helpers::systemConfig()['server_chan_key'] . '.send?text=' . $title . '&desp=' . urlencode($content);
  20. $response = Curl::send($url);
  21. $result = json_decode($response);
  22. if (!$result->errno) {
  23. self::addLog($title, $content);
  24. } else {
  25. self::addLog($title, $content, 0, $result->errmsg);
  26. }
  27. } catch (\Exception $e) {
  28. Log::error($e);
  29. }
  30. }
  31. }
  32. /**
  33. * 添加serverChan投递日志
  34. *
  35. * @param string $title 标题
  36. * @param string $content 内容
  37. * @param int $status 投递状态
  38. * @param string $error 投递失败时记录的异常信息
  39. *
  40. * @return int
  41. */
  42. private static function addLog($title, $content, $status = 1, $error = '')
  43. {
  44. $log = new EmailLog();
  45. $log->type = 2;
  46. $log->address = 'admin';
  47. $log->title = $title;
  48. $log->content = $content;
  49. $log->status = $status;
  50. $log->error = $error;
  51. return $log->save();
  52. }
  53. }