作者 李美松

更新 | 定时任务修改

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Domain;
  4 +
  5 +use App\Exceptions\AsideGlobalException;
  6 +use App\Exceptions\BsideGlobalException;
  7 +use App\Helper\AyrShare as AyrShareHelper;
  8 +use App\Http\Logic\Aside\Domain\DomainInfoLogic;
  9 +use App\Models\AyrShare\AyrRelease as AyrReleaseModel;
  10 +use Carbon\Carbon;
  11 +use App\Models\AyrShare\AyrShare as AyrShareModel;
  12 +use Illuminate\Console\Command;
  13 +
  14 +class DomainTime extends Command
  15 +{
  16 + public $error = 0;
  17 + /**
  18 + * The name and signature of the console command.
  19 + *
  20 + * @var string
  21 + */
  22 + protected $signature = 'domain_time';
  23 +
  24 + /**
  25 + * The console command description.
  26 + *
  27 + * @var string
  28 + */
  29 + protected $description = '域名定时任务 更新域名|证书到期时间';
  30 +
  31 + /**
  32 + * @name :(定时执行)handle
  33 + * @author :lyh
  34 + * @method :post
  35 + * @time :2023/5/12 14:48
  36 + */
  37 + public function handle()
  38 + {
  39 + echo $this->update_domain_time();
  40 + }
  41 +
  42 + /**
  43 + * 更新域名|证书到期时间
  44 + * @return int|mixed|void
  45 + * @throws AsideGlobalException
  46 + * @throws BsideGlobalException
  47 + */
  48 + protected function update_domain_time()
  49 + {
  50 + $domainCon = new DomainInfoLogic();
  51 + $all = $domainCon->getAllDomain();
  52 + $all = $all->toArray();
  53 +
  54 + foreach ($all as $item) {
  55 + $domain = $item['domain'];
  56 + // 域名到期时间
  57 + $domainT = $domainCon->getDomainTime($domain);
  58 + if ($domainT) {
  59 + $domain_time = $item['domain_end_time'];
  60 + $domainValidFrom = $domainT['validFrom'];
  61 + $domainValidTo = $domainT['validTo'];
  62 + if (strtotime($domain_time) < strtotime($domainValidTo)) {
  63 + $this->info($domain . '域名到期时间更新成功');
  64 + $domainCon->updateDomain($item['id'], ['domain_end_time' => $domainValidTo]);
  65 + }
  66 + } else {
  67 + $this->error++;
  68 + $this->info($domain . '域名到期时间获取失败');
  69 + }
  70 + // 证书到期时间
  71 + $certificateT = $domainCon->getDomainCertificateTime($domain);
  72 + if ($certificateT) {
  73 + $certificate_time = $item['certificate_end_time'];
  74 + $certificateValidFrom = $certificateT['validFrom'];
  75 + $certificateValidTo = $certificateT['validTo'];
  76 + if (strtotime($certificate_time) < strtotime($certificateValidTo)) {
  77 + $this->info($domain . '证书到期时间更新成功');
  78 + $domainCon->updateDomain($item['id'], ['certificate_end_time' => $certificateValidTo]);
  79 + }
  80 + } else {
  81 + $this->error++;
  82 + $this->info($domain . '证书到期时间获取失败');
  83 + }
  84 + return $this->error;
  85 + }
  86 +
  87 + }
  88 +}
@@ -27,6 +27,8 @@ class DomainInfoController extends BaseController @@ -27,6 +27,8 @@ class DomainInfoController extends BaseController
27 */ 27 */
28 public function lists(int $deleted = DomainInfo::DELETED_NORMAL) 28 public function lists(int $deleted = DomainInfo::DELETED_NORMAL)
29 { 29 {
  30 + $dd = new DomainInfoLogic();
  31 + dd($dd->getAllDomain()->toArray());
30 $request = $this->param; 32 $request = $this->param;
31 $search = []; 33 $search = [];
32 $search_array = [ 34 $search_array = [
@@ -226,10 +226,10 @@ class DomainInfoLogic extends BaseLogic @@ -226,10 +226,10 @@ class DomainInfoLogic extends BaseLogic
226 } 226 }
227 227
228 /** 228 /**
229 - * 域名到期时间 229 + * 证书到期时间
230 * @return array 230 * @return array
231 */ 231 */
232 - public function domainCertificateTime($domain) 232 + public function getDomainCertificateTime($domain)
233 { 233 {
234 $domain = trim($domain); 234 $domain = trim($domain);
235 $data = []; 235 $data = [];
@@ -249,6 +249,33 @@ class DomainInfoLogic extends BaseLogic @@ -249,6 +249,33 @@ class DomainInfoLogic extends BaseLogic
249 } 249 }
250 250
251 /** 251 /**
  252 + * 获取所有正常域名信息
  253 + * @return array
  254 + */
  255 + public function getAllDomain()
  256 + {
  257 + return DomainInfo::query()->where('status', 1)->where('deleted', DomainInfo::DELETED_NORMAL)->get();
  258 + }
  259 +
  260 + /**
  261 + * 域名到期时间
  262 + * @return array
  263 + */
  264 + public function getDomainTime($domain)
  265 + {
  266 + $conJson = file_get_contents("http://openai.waimaoq.com/v1/whois_api?domain={$domain}");
  267 + $conArr = json_decode($conJson, true);
  268 + $data = [];
  269 + if ($conArr['code'] == 200) {
  270 + $con = $conArr['text'];
  271 + $data['domain'] = $domain;
  272 + $data['validFrom'] = $con['creation_date'];
  273 + $data['validTo'] = $con['expiration_date'];
  274 + }
  275 + return $data;
  276 + }
  277 +
  278 + /**
252 * 验证给定的值是否是有效的域名。 279 * 验证给定的值是否是有效的域名。
253 * 280 *
254 * @param mixed $value 281 * @param mixed $value
@@ -277,4 +304,22 @@ class DomainInfoLogic extends BaseLogic @@ -277,4 +304,22 @@ class DomainInfoLogic extends BaseLogic
277 } 304 }
278 return true; 305 return true;
279 } 306 }
  307 +
  308 + /**
  309 + * 根据域名更新证书到期时间
  310 + * @param $id
  311 + * @param $updata
  312 + * @return array
  313 + * @throws AsideGlobalException
  314 + * @throws BsideGlobalException
  315 + */
  316 + public function updateDomain($id, $updata)
  317 + {
  318 + $isRes = DomainInfo::query()->where('id', $id)->where('deleted', DomainInfo::DELETED_NORMAL)->update($updata);
  319 + if ($isRes) {
  320 + return $this->success();
  321 + } else {
  322 + return $this->fail('更新域名到期时间失败');
  323 + }
  324 + }
280 } 325 }