Message.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Site;
  4. use App\Http\Models\User;
  5. use App\Http\Services\YouMenGApiService;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Facades\DB;
  8. use Illuminate\Support\Facades\Log;
  9. class Message extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'message:day';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. const MESSAGE_STENCIL = [
  24. 'siteExpires' => 1,//网站到期
  25. 'sslExpires' => 2,//证书到期
  26. 'cdnExpires' => 3,//cdn到期
  27. 'domainNameExpires' => 4,//域名到期
  28. 'servicePeriodExpires' => 5,//服务期到期
  29. ];
  30. const SEVEN_DAYS = 604800;//7天的时间戳
  31. /**
  32. * Create a new command instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct()
  37. {
  38. parent::__construct();
  39. }
  40. /**
  41. * Execute the console command.
  42. * 任务计划
  43. * @return mixed
  44. */
  45. public function handle()
  46. {
  47. $siteList = Site::query()->where('status', [2, 3, 9])->get();
  48. foreach ($siteList as $key => $value) {
  49. //网站到期 ,提前一个月
  50. if ($value->expired_at && $value->expired_at < date('Y-m-d', strtotime("-1 months"))) {
  51. $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['siteExpires'])->first();
  52. $info = [
  53. 'type' => $messageStencil->type,
  54. 'site_id' => $value->id,
  55. 'title' => $messageStencil->title,
  56. 'message' => $this->getMessageInfo($value->domain ?? '', $value->expired_at, $messageStencil->message),
  57. 'message_id' => $messageStencil->id,
  58. 'url' => $messageStencil->url,
  59. 'img' => $messageStencil->img,
  60. 'date' => strtotime(date('Y-m 00:00:00')),
  61. ];
  62. $this->insertMessageInfo($value->id, $messageStencil->type, $info);
  63. }
  64. //证书到期,提前一个月
  65. if ($value->cert_expired_date && $value->cert_expired_date < date('Y-m-d', strtotime("-1 months"))) {
  66. $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['sslExpires'])->first();
  67. $info = [
  68. 'type' => $messageStencil->type,
  69. 'site_id' => $value->id,
  70. 'title' => $messageStencil->title,
  71. 'message' => $this->getMessageInfo($value->domain ?? '', $value->cert_expired_date, $messageStencil->message),
  72. 'message_id' => $messageStencil->id,
  73. 'url' => $messageStencil->url,
  74. 'img' => $messageStencil->img,
  75. 'date' => strtotime(date('Y-m 00:00:00')),
  76. ];
  77. $this->insertMessageInfo($value->id, $messageStencil->type, $info);
  78. }
  79. //cdn到期,提前一个月
  80. if ($value->cdn_expired_date && $value->cdn_expired_date < date('Y-m-d', strtotime("-1 months"))) {
  81. $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['cdnExpires'])->first();
  82. $info = [
  83. 'type' => $messageStencil->type,
  84. 'site_id' => $value->id,
  85. 'title' => $messageStencil->title,
  86. 'message' => $this->getMessageInfo($value->domain ?? '', $value->cdn_expired_date, $messageStencil->message),
  87. 'message_id' => $messageStencil->id,
  88. 'url' => $messageStencil->url,
  89. 'img' => $messageStencil->img,
  90. 'date' => strtotime(date('Y-m 00:00:00')),
  91. ];
  92. $this->insertMessageInfo($value->id, $messageStencil->type, $info);
  93. }
  94. //域名到期,提前一个月
  95. if ($value->domain_expired_date && $value->domain_expired_date < date('Y-m-d', strtotime("-1 months"))) {
  96. $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['domainNameExpires'])->first();
  97. $info = [
  98. 'type' => $messageStencil->type,
  99. 'site_id' => $value->id,
  100. 'title' => $messageStencil->title,
  101. 'message' => $this->getMessageInfo($value->domain ?? '', $value->domain_expired_date, $messageStencil->message),
  102. 'message_id' => $messageStencil->id,
  103. 'url' => $messageStencil->url,
  104. 'img' => $messageStencil->img,
  105. 'date' => strtotime(date('Y-m 00:00:00')),
  106. ];
  107. $this->insertMessageInfo($value->id, $messageStencil->type, $info);
  108. }
  109. //服务期到期,提前两个月
  110. if ($value->renewal_end_at && $value->renewal_end_at < date('Y-m-d', strtotime("-2 months"))) {
  111. $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['servicePeriodExpires'])->first();
  112. $info = [
  113. 'type' => $messageStencil->type,
  114. 'site_id' => $value->id,
  115. 'title' => $messageStencil->title,
  116. 'message' => $this->getMessageInfo($value->domain ?? '', $value->renewal_end_at, $messageStencil->message),
  117. 'message_id' => $messageStencil->id,
  118. 'url' => $messageStencil->url,
  119. 'img' => $messageStencil->img,
  120. 'date' => strtotime(date('Y-m 00:00:00')),
  121. ];
  122. $this->insertMessageInfo($value->id, $messageStencil->type, $info);
  123. }
  124. }
  125. $messageList = DB::table('message_status')
  126. ->whereIn('type', [1, 2, 3, 4, 5])
  127. ->where('status', 0)->get()->toArray();
  128. $youMenGApiService = new YouMenGApiService();
  129. foreach ($messageList as $key => $value) {
  130. $message = [
  131. 'title' => $value->title,
  132. 'subtitle' => $value->message,
  133. 'body' => ''
  134. ];
  135. $youMenGApiService->pushIosDevice($value->site_id, $message);
  136. $youMenGApiService->pushAndroidDevice($value->site_id, $message);
  137. DB::table('message_status')->where('id', $value->id)->update(['push_time' => date('Y-m-d H:i:s'), 'status' => 1]);
  138. }
  139. }
  140. /**
  141. * 替换模版中的变量
  142. * @param $website
  143. * @param $date
  144. * @param $message
  145. * @return mixed
  146. */
  147. public function getMessageInfo($website = '', $date = '', $message = '')
  148. {
  149. $message = str_replace('{$website}', $website, $message);
  150. $message = str_replace('{$date}', $date, $message);
  151. return $message;
  152. }
  153. /**
  154. * 插入任务
  155. * @param $siteId
  156. * @param $type
  157. * @param $info
  158. */
  159. public function insertMessageInfo($siteId, $type, $info)
  160. {
  161. $condition = [
  162. ['site_id', '=', $siteId],
  163. ['type', '=', $type],
  164. ];
  165. $messageStatus = DB::table('message_status');
  166. $result = $messageStatus->where($condition)->orderBy('id', 'desc')->first();
  167. $day = (int)strtotime(date('Y-m 00:00:00')) - self::SEVEN_DAYS;
  168. if (empty($result)) {
  169. $messageStatus->insert($info);
  170. } else {
  171. Log::info(json_encode($result));
  172. Log::info($result->date);
  173. if ($result->date < $day) {
  174. $messageStatus->insert($info);
  175. }
  176. }
  177. }
  178. }