|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
use App\Models\Devops\ServerConfig;
|
|
|
|
use App\Models\Domain\DomainInfo;
|
|
|
|
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 EditDomainBt implements ShouldQueue
|
|
|
|
{
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
public $tries = 3; // 可配置任务重试次数
|
|
|
|
|
|
|
|
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 DomainInfo();
|
|
|
|
$domain_info = $domain_model->read(['id' => $this->domain_id]);
|
|
|
|
if ($domain_info === false) {
|
|
|
|
return $this->output($domain_info['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['domain'] . ':获取项目数据失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
//获取服务器数据
|
|
|
|
$server_model = new ServerConfig();
|
|
|
|
$server_info = $server_model->read(['id' => $project_info['serve_id']], ['init_domain', 'host']);
|
|
|
|
if ($server_info === false) {
|
|
|
|
return $this->output($domain_info['domain'] . ':获取服务器数据失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
//编辑主站
|
|
|
|
$rewrite = $domain_info['extend_config'] ? json_decode($domain_info['extend_config'], true) : [];
|
|
|
|
$other_domain = $domain_info['other_domain'] ? json_decode($domain_info['other_domain'], true) : [];
|
|
|
|
if ($domain_info['type'] == 2) {
|
|
|
|
$api_url = 'http://' . $server_info['init_domain'] . '/api/setSsl';
|
|
|
|
$api_param = [
|
|
|
|
'domain' => $domain_info['domain'],
|
|
|
|
'private_key' => $domain_info['private_key'],
|
|
|
|
'cert' => $domain_info['private_cert'],
|
|
|
|
'rewrite' => $rewrite,
|
|
|
|
'other_domain' => $other_domain,
|
|
|
|
'is_https' => $domain_info['is_https']
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
$api_url = 'http://' . $server_info['init_domain'] . '/api/applySsl';
|
|
|
|
$api_param = [
|
|
|
|
'domain' => $domain_info['domain'],
|
|
|
|
'rewrite' => $rewrite,
|
|
|
|
'other_domain' => $other_domain,
|
|
|
|
'is_https' => $domain_info['is_https']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$rs = HttpUtils::get($api_url, $api_param);
|
|
|
|
$rs = json_decode($rs, true);
|
|
|
|
if (isset($rs['status']) && $rs['status'] == 200) {
|
|
|
|
$this->output($domain_info['domain'] . ':主站编辑成功');
|
|
|
|
} else {
|
|
|
|
$this->output($domain_info['domain'] . ':主站编辑失败,原因:' . ($rs['message'] ?? ''));
|
|
|
|
}
|
|
|
|
} catch (\Exception | GuzzleException $e) {
|
|
|
|
$this->output($domain_info['domain'] . ':主站编辑失败,原因:' . $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
//编辑amp站
|
|
|
|
if ($domain_info['amp_status']) {
|
|
|
|
$api_url_amp = 'http://' . $server_info['init_domain'] . '/api/createSiteAmp';
|
|
|
|
$api_param_amp = [
|
|
|
|
'domain' => $domain_info['domain'],
|
|
|
|
'private_key' => $domain_info['amp_private_key'],
|
|
|
|
'cert' => $domain_info['amp_private_cert']
|
|
|
|
];
|
|
|
|
|
|
|
|
try {
|
|
|
|
$rs_amp = HttpUtils::get($api_url_amp, $api_param_amp);
|
|
|
|
$rs_amp = json_decode($rs_amp, true);
|
|
|
|
if (isset($rs_amp['status']) && $rs_amp['status'] == 200) {
|
|
|
|
$this->output($domain_info['domain'] . ':amp站编辑成功');
|
|
|
|
} else {
|
|
|
|
$this->output($domain_info['domain'] . ':amp站编辑失败,原因:' . ($rs_amp['message'] ?? ''));
|
|
|
|
}
|
|
|
|
} catch (\Exception | GuzzleException $e) {
|
|
|
|
$this->output($domain_info['domain'] . ':amp站编辑失败,原因:' . $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;
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|