SiteSslRenewal.php 1.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2022/11/05
 * Time: 09:09
 */
namespace App\Console\Commands;

use App\Models\BtEvents;
use App\Models\BtSites;
use App\Repositories\BtRepository;
use Illuminate\Console\Command;

/**
 * Class SiteSslRenewal
 * @package App\Console\Commands
 */
class SiteSslRenewal extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'site:ssl_renewal';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检测ssl续签';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return bool
     */
    public function handle()
    {
        $sites = BtSites::where(['is_del' => 0])->get();

        if ($sites->isEmpty())
            return true;

        foreach ($sites as $val) {
            // 未开启ssl 抛弃
            if ($val->ssl_open == 0)
                continue;

            // 未生成ssl 申请ssl
            if ($val->ssl_status == 0) {
                $result = app(BtRepository::class)->applySsl($sites->domain);
                if (empty($result))
                    BtEvents::createBtEvent(BtEvents::TYPE_CREATE_SSL, json_encode(['domain' => $sites->domain]));
                continue;
            }

            // 开启ssl续签 检查是否到期进行续签
            if ($val->ssl_auto) {
               $result = app(BtRepository::class)->renewalSsl($sites->domain);
               if (empty($result))
                   BtEvents::createBtEvent(BtEvents::TYPE_RENEWAL_SSL, json_encode(['domain' => $sites->domain]));
               continue;
            }

        }
        return true;
    }
}