EditCustomDomainBt.php 3.7 KB
<?php

namespace App\Jobs;

use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Project\CountryCustom;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class EditCustomDomainBt implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public $tries = 1; // 可配置任务重试次数

    protected $domain_id;

    /**
     * Create a new job instance.
     *
     * @param  $domain_id
     */
    public function __construct($domain_id)
    {
        $this->domain_id = $domain_id;
    }

    /**
     * Execute the job.
     *
     * @return bool
     */
    public function handle()
    {
        //获取域名数据
        $domain_model = new CountryCustom();
        $domain_info = $domain_model->read(['id' => $this->domain_id]);
        if ($domain_info === false) {
            return $this->output($domain_info['custom_domain'] . ':获取域名数据失败');
        }

        //获取项目数据
        $project_model = new Project();
        $project_info = $project_model->read(['id' => $domain_info['project_id']], 'serve_id');
        if ($project_info === false) {
            return $this->output($domain_info['custom_domain'] . ':获取项目数据失败');
        }

        //获取服务器数据
        $serverIpModel = new ServersIp();
        $serversIpInfo = $serverIpModel->read(['id' => $project_info['serve_id']], ['servers_id']);
        if ($serversIpInfo === false) {
            return $this->output($domain_info['custom_domain'] . ':获取服务器数据失败');
        }
        $serverModel = new Servers();
        $serverInfo = $serverModel->read(['id' => $serversIpInfo['servers_id']], ['init_domain']);

        //编辑站点
        if ($domain_info['type'] == 2) {
            $api_url = 'http://' . $serverInfo['init_domain'] . '/api/setSsl';
            $api_param = [
                'domain' => $domain_info['custom_domain'],
                'private_key' => $domain_info['private_key'],
                'cert' => $domain_info['private_cert'],
                'rewrite' => [],
                'other_domain' => [],
                'is_https' => 1
            ];
        } else {
            $api_url = 'http://' . $serverInfo['init_domain'] . '/api/applySsl';
            $api_param = [
                'domain' => $domain_info['custom_domain'],
                'rewrite' => [],
                'other_domain' => [],
                'is_https' => 1
            ];
        }
        try {
            $rs = HttpUtils::get($api_url, $api_param);
            $rs = json_decode($rs, true);
            if (isset($rs['status']) && $rs['status'] == 200) {
                $this->output($domain_info['custom_domain'] . ':站点编辑成功');
            } else {
                $this->output($domain_info['custom_domain'] . ':站点编辑失败,原因:' . ($rs['message'] ?? ''));
            }
        } catch (\Exception | GuzzleException $e) {
            $this->output($domain_info['custom_domain'] . ':站点编辑失败,原因:' . $e->getMessage());
        }

        return true;
    }

    /**
     * 输出处理日志
     * @param $message
     * @return bool
     */
    public function output($message)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
        return true;
    }

    public function failed(\Exception $exception)
    {
        return $this->output($exception->getMessage());
    }
}