123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- namespace App\Console\Commands;
- use App\Http\Models\Site;
- use App\Http\Models\User;
- use App\Http\Services\YouMenGApiService;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- class Message extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'message:day';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- const MESSAGE_STENCIL = [
- 'siteExpires' => 1,//网站到期
- 'sslExpires' => 2,//证书到期
- 'cdnExpires' => 3,//cdn到期
- 'domainNameExpires' => 4,//域名到期
- 'servicePeriodExpires' => 5,//服务期到期
- ];
- const SEVEN_DAYS = 604800;//7天的时间戳
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- * 任务计划
- * @return mixed
- */
- public function handle()
- {
- $siteList = Site::query()->where('status', [2, 3, 9])->get();
- foreach ($siteList as $key => $value) {
- //网站到期 ,提前一个月
- if ($value->expired_at && $value->expired_at < date('Y-m-d', strtotime("-1 months"))) {
- $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['siteExpires'])->first();
- $info = [
- 'type' => $messageStencil->type,
- 'site_id' => $value->id,
- 'title' => $messageStencil->title,
- 'message' => $this->getMessageInfo($value->domain ?? '', $value->expired_at, $messageStencil->message),
- 'message_id' => $messageStencil->id,
- 'url' => $messageStencil->url,
- 'img' => $messageStencil->img,
- 'date' => strtotime(date('Y-m 00:00:00')),
- ];
- $this->insertMessageInfo($value->id, $messageStencil->type, $info);
- }
- //证书到期,提前一个月
- if ($value->cert_expired_date && $value->cert_expired_date < date('Y-m-d', strtotime("-1 months"))) {
- $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['sslExpires'])->first();
- $info = [
- 'type' => $messageStencil->type,
- 'site_id' => $value->id,
- 'title' => $messageStencil->title,
- 'message' => $this->getMessageInfo($value->domain ?? '', $value->cert_expired_date, $messageStencil->message),
- 'message_id' => $messageStencil->id,
- 'url' => $messageStencil->url,
- 'img' => $messageStencil->img,
- 'date' => strtotime(date('Y-m 00:00:00')),
- ];
- $this->insertMessageInfo($value->id, $messageStencil->type, $info);
- }
- //cdn到期,提前一个月
- if ($value->cdn_expired_date && $value->cdn_expired_date < date('Y-m-d', strtotime("-1 months"))) {
- $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['cdnExpires'])->first();
- $info = [
- 'type' => $messageStencil->type,
- 'site_id' => $value->id,
- 'title' => $messageStencil->title,
- 'message' => $this->getMessageInfo($value->domain ?? '', $value->cdn_expired_date, $messageStencil->message),
- 'message_id' => $messageStencil->id,
- 'url' => $messageStencil->url,
- 'img' => $messageStencil->img,
- 'date' => strtotime(date('Y-m 00:00:00')),
- ];
- $this->insertMessageInfo($value->id, $messageStencil->type, $info);
- }
- //域名到期,提前一个月
- if ($value->domain_expired_date && $value->domain_expired_date < date('Y-m-d', strtotime("-1 months"))) {
- $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['domainNameExpires'])->first();
- $info = [
- 'type' => $messageStencil->type,
- 'site_id' => $value->id,
- 'title' => $messageStencil->title,
- 'message' => $this->getMessageInfo($value->domain ?? '', $value->domain_expired_date, $messageStencil->message),
- 'message_id' => $messageStencil->id,
- 'url' => $messageStencil->url,
- 'img' => $messageStencil->img,
- 'date' => strtotime(date('Y-m 00:00:00')),
- ];
- $this->insertMessageInfo($value->id, $messageStencil->type, $info);
- }
- //服务期到期,提前两个月
- if ($value->renewal_end_at && $value->renewal_end_at < date('Y-m-d', strtotime("-2 months"))) {
- $messageStencil = DB::table('message')->where('id', self::MESSAGE_STENCIL['servicePeriodExpires'])->first();
- $info = [
- 'type' => $messageStencil->type,
- 'site_id' => $value->id,
- 'title' => $messageStencil->title,
- 'message' => $this->getMessageInfo($value->domain ?? '', $value->renewal_end_at, $messageStencil->message),
- 'message_id' => $messageStencil->id,
- 'url' => $messageStencil->url,
- 'img' => $messageStencil->img,
- 'date' => strtotime(date('Y-m 00:00:00')),
- ];
- $this->insertMessageInfo($value->id, $messageStencil->type, $info);
- }
- }
- $messageList = DB::table('message_status')
- ->whereIn('type', [1, 2, 3, 4, 5])
- ->where('status', 0)->get()->toArray();
- $youMenGApiService = new YouMenGApiService();
- foreach ($messageList as $key => $value) {
- $message = [
- 'title' => $value->title,
- 'subtitle' => $value->message,
- 'body' => ''
- ];
- $youMenGApiService->pushIosDevice($value->site_id, $message);
- $youMenGApiService->pushAndroidDevice($value->site_id, $message);
- DB::table('message_status')->where('id', $value->id)->update(['push_time' => date('Y-m-d H:i:s'), 'status' => 1]);
- }
- }
- /**
- * 替换模版中的变量
- * @param $website
- * @param $date
- * @param $message
- * @return mixed
- */
- public function getMessageInfo($website = '', $date = '', $message = '')
- {
- $message = str_replace('{$website}', $website, $message);
- $message = str_replace('{$date}', $date, $message);
- return $message;
- }
- /**
- * 插入任务
- * @param $siteId
- * @param $type
- * @param $info
- */
- public function insertMessageInfo($siteId, $type, $info)
- {
- $condition = [
- ['site_id', '=', $siteId],
- ['type', '=', $type],
- ];
- $messageStatus = DB::table('message_status');
- $result = $messageStatus->where($condition)->orderBy('id', 'desc')->first();
- $day = (int)strtotime(date('Y-m 00:00:00')) - self::SEVEN_DAYS;
- if (empty($result)) {
- $messageStatus->insert($info);
- } else {
- Log::info(json_encode($result));
- Log::info($result->date);
- if ($result->date < $day) {
- $messageStatus->insert($info);
- }
- }
- }
- }
|