作者 李美松

更新 | 定时任务修改

<?php
namespace App\Console\Commands\Domain;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Helper\AyrShare as AyrShareHelper;
use App\Http\Logic\Aside\Domain\DomainInfoLogic;
use App\Models\AyrShare\AyrRelease as AyrReleaseModel;
use Carbon\Carbon;
use App\Models\AyrShare\AyrShare as AyrShareModel;
use Illuminate\Console\Command;
class DomainTime extends Command
{
public $error = 0;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'domain_time';
/**
* The console command description.
*
* @var string
*/
protected $description = '域名定时任务 更新域名|证书到期时间';
/**
* @name :(定时执行)handle
* @author :lyh
* @method :post
* @time :2023/5/12 14:48
*/
public function handle()
{
echo $this->update_domain_time();
}
/**
* 更新域名|证书到期时间
* @return int|mixed|void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
protected function update_domain_time()
{
$domainCon = new DomainInfoLogic();
$all = $domainCon->getAllDomain();
$all = $all->toArray();
foreach ($all as $item) {
$domain = $item['domain'];
// 域名到期时间
$domainT = $domainCon->getDomainTime($domain);
if ($domainT) {
$domain_time = $item['domain_end_time'];
$domainValidFrom = $domainT['validFrom'];
$domainValidTo = $domainT['validTo'];
if (strtotime($domain_time) < strtotime($domainValidTo)) {
$this->info($domain . '域名到期时间更新成功');
$domainCon->updateDomain($item['id'], ['domain_end_time' => $domainValidTo]);
}
} else {
$this->error++;
$this->info($domain . '域名到期时间获取失败');
}
// 证书到期时间
$certificateT = $domainCon->getDomainCertificateTime($domain);
if ($certificateT) {
$certificate_time = $item['certificate_end_time'];
$certificateValidFrom = $certificateT['validFrom'];
$certificateValidTo = $certificateT['validTo'];
if (strtotime($certificate_time) < strtotime($certificateValidTo)) {
$this->info($domain . '证书到期时间更新成功');
$domainCon->updateDomain($item['id'], ['certificate_end_time' => $certificateValidTo]);
}
} else {
$this->error++;
$this->info($domain . '证书到期时间获取失败');
}
return $this->error;
}
}
}
... ...
... ... @@ -27,6 +27,8 @@ class DomainInfoController extends BaseController
*/
public function lists(int $deleted = DomainInfo::DELETED_NORMAL)
{
$dd = new DomainInfoLogic();
dd($dd->getAllDomain()->toArray());
$request = $this->param;
$search = [];
$search_array = [
... ...
... ... @@ -226,10 +226,10 @@ class DomainInfoLogic extends BaseLogic
}
/**
* 域名到期时间
* 证书到期时间
* @return array
*/
public function domainCertificateTime($domain)
public function getDomainCertificateTime($domain)
{
$domain = trim($domain);
$data = [];
... ... @@ -249,6 +249,33 @@ class DomainInfoLogic extends BaseLogic
}
/**
* 获取所有正常域名信息
* @return array
*/
public function getAllDomain()
{
return DomainInfo::query()->where('status', 1)->where('deleted', DomainInfo::DELETED_NORMAL)->get();
}
/**
* 域名到期时间
* @return array
*/
public function getDomainTime($domain)
{
$conJson = file_get_contents("http://openai.waimaoq.com/v1/whois_api?domain={$domain}");
$conArr = json_decode($conJson, true);
$data = [];
if ($conArr['code'] == 200) {
$con = $conArr['text'];
$data['domain'] = $domain;
$data['validFrom'] = $con['creation_date'];
$data['validTo'] = $con['expiration_date'];
}
return $data;
}
/**
* 验证给定的值是否是有效的域名。
*
* @param mixed $value
... ... @@ -277,4 +304,22 @@ class DomainInfoLogic extends BaseLogic
}
return true;
}
/**
* 根据域名更新证书到期时间
* @param $id
* @param $updata
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function updateDomain($id, $updata)
{
$isRes = DomainInfo::query()->where('id', $id)->where('deleted', DomainInfo::DELETED_NORMAL)->update($updata);
if ($isRes) {
return $this->success();
} else {
return $this->fail('更新域名到期时间失败');
}
}
}
... ...