|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Logic\Aside\Domain;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Exceptions\AsideGlobalException;
|
|
|
|
use App\Exceptions\BsideGlobalException;
|
|
|
|
use App\Http\Logic\Aside\BaseLogic;
|
|
|
|
use App\Models\Aside\Domain\DomainInfo;
|
|
|
|
use App\Models\Aside\Domain\DomainInfoLog;
|
|
|
|
use App\Models\Devops\ServerInformation;
|
|
|
|
use App\Models\Devops\ServerInformationLog;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
class DomainInfoLogic extends BaseLogic
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $param;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->model = new ServerInformation();
|
|
|
|
|
|
|
|
$this->param = $this->requestAll;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 添加数据
|
|
|
|
* @return array
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
|
|
|
$request = $this->param;
|
|
|
|
$domain = new DomainInfo();
|
|
|
|
$this->extracted($request, $domain);
|
|
|
|
DB::beginTransaction();
|
|
|
|
if ($domain->save()) {
|
|
|
|
$original = [
|
|
|
|
'id' => $domain->id,
|
|
|
|
'domain' => $domain->domain,
|
|
|
|
'belong_to' => $domain->belong_to,
|
|
|
|
'status' => $domain->status,
|
|
|
|
'domain_start_time' => $domain->domain_start_time,
|
|
|
|
'domain_end_time' => $domain->domain_end_time,
|
|
|
|
'certificate_start_time' => $domain->certificate_start_time,
|
|
|
|
'certificate_end_time' => $domain->certificate_end_time,
|
|
|
|
];
|
|
|
|
// 添加日志
|
|
|
|
$this->domain_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $domain->id);
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
}
|
|
|
|
DB::rollBack();
|
|
|
|
return $this->fail('域名信息添加失败');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 修改数据
|
|
|
|
* @return array
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
$domain = new DomainInfo();
|
|
|
|
$request = $this->param;
|
|
|
|
$this->extracted($request, $domain);
|
|
|
|
DB::beginTransaction();
|
|
|
|
if ($domain->save()) {
|
|
|
|
$fields_array = $domain->FieldsArray();
|
|
|
|
$original = $domain->getOriginal();
|
|
|
|
$revised = [
|
|
|
|
'id' => $domain->id,
|
|
|
|
'domain' => $domain->domain,
|
|
|
|
'belong_to' => $domain->belong_to,
|
|
|
|
'status' => $domain->status,
|
|
|
|
'domain_start_time' => $domain->domain_start_time,
|
|
|
|
'domain_end_time' => $domain->domain_end_time,
|
|
|
|
'certificate_start_time' => $domain->certificate_start_time,
|
|
|
|
'certificate_end_time' => $domain->certificate_end_time,
|
|
|
|
'delete' => $domain->delete,
|
|
|
|
];
|
|
|
|
$diff = array_diff_assoc($original, $revised);
|
|
|
|
unset($diff['deleted']);
|
|
|
|
$remarks = '';
|
|
|
|
if ($diff) {
|
|
|
|
$remarks .= '修改ID为 ' . $domain->id . ' 的服务器信息,修改内容为:';
|
|
|
|
foreach ($diff as $key => $value) {
|
|
|
|
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 添加日志
|
|
|
|
$this->domain_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
}
|
|
|
|
DB::rollBack();
|
|
|
|
return $this->fail('域名信息修改失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 检查域名是否存在
|
|
|
|
* @param $domain
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkDomain($domain)
|
|
|
|
{
|
|
|
|
$usIp = DomainInfo::query()->where('domain', $domain)->first();
|
|
|
|
if ($usIp) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 详情
|
|
|
|
* @param int $deleted
|
|
|
|
* @return array|Builder|Collection|Model
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function domainInfo(int $deleted = DomainInfo::DELETED_NORMAL)
|
|
|
|
{
|
|
|
|
$id = request()->input('id');
|
|
|
|
if (!$id) {
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
}
|
|
|
|
$data = DomainInfo::query()->where('deleted', $deleted)->find($id, ['domain', 'belong_to', 'title', 'domain_start_time', 'domain_end_time', 'certificate_start_time', 'certificate_end_time', 'create_at', 'update_at']);
|
|
|
|
if (!$data) {
|
|
|
|
return $this->fail('数据不存在!');
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 服务器操作日志
|
|
|
|
* @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表
|
|
|
|
* @param array $original 原始数据
|
|
|
|
* @param array $revised 修改后数据
|
|
|
|
*/
|
|
|
|
public function domain_action_log(int $action = DomainInfoLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
|
|
|
|
{
|
|
|
|
// $action 1:添加 2:修改 3:删除 4:恢复
|
|
|
|
$actionArr = DomainInfoLog::actionArr();
|
|
|
|
$actionStr = $actionArr[$action];
|
|
|
|
$ip = request()->getClientIp();
|
|
|
|
$url = request()->getRequestUri();
|
|
|
|
$method = request()->getMethod();
|
|
|
|
$userId = $this->uid ?? 0;
|
|
|
|
$log = new ServerInformationLog();
|
|
|
|
$log->user_id = $userId;
|
|
|
|
$log->action = $actionStr;
|
|
|
|
$log->original = json_encode($original);
|
|
|
|
$log->revised = json_encode($revised);
|
|
|
|
$log->ip = $ip;
|
|
|
|
$log->url = $url;
|
|
|
|
$log->method = $method;
|
|
|
|
$log->remarks = $remarks;
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$log->save();
|
|
|
|
DB::commit();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
Log::error('服务器信息日志添加失败');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $request
|
|
|
|
* @param $domain
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function extracted(array $request, $domain)
|
|
|
|
{
|
|
|
|
$domain->domain = trim($request['domain']); // 域名
|
|
|
|
$domain->belong_to = trim($request['belong_to']); // 域名归属 1 - 公司 2 - 客户
|
|
|
|
$domain->status = trim($request['status']); // 域名状态 0 - 正常 1 - 关闭
|
|
|
|
$domain->domain_start_time = trim($request['domain_start_time']); // 域名开始时间
|
|
|
|
$domain->domain_end_time = trim($request['domain_end_time']); // 域名结束时间
|
|
|
|
$domain->certificate_start_time = trim($request['certificate_start_time']); // 证书开始时间
|
|
|
|
$domain->certificate_end_time = trim($request['certificate_end_time']); // 证书结束时间
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 批量获取数据删除
|
|
|
|
* @return array
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function get_batch_update($action = DomainInfoLog::ACTION_DELETE, $deleted = DomainInfo::DELETED_NORMAL)
|
|
|
|
{
|
|
|
|
$id = request()->input('id');
|
|
|
|
if (!$id) {
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
}
|
|
|
|
$ids = [];
|
|
|
|
if (!is_array($id)) {
|
|
|
|
$ids = explode(',', $id);
|
|
|
|
}
|
|
|
|
$ids = array_filter($ids, 'intval');
|
|
|
|
if (empty($ids)) {
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
}
|
|
|
|
$data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
|
|
|
|
$restore_ids = $data->pluck('id')->toArray();
|
|
|
|
$actionArr = DomainInfoLog::actionArr();
|
|
|
|
$actionStr = $actionArr[$action];
|
|
|
|
if (empty($restore_ids)) {
|
|
|
|
$this->fail($actionStr . '服务器信息不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$original = $data->toArray();
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$update = $deleted == DomainInfo::DELETED_NORMAL ? DomainInfo::DELETED_DELETE : DomainInfo::DELETED_NORMAL;
|
|
|
|
DomainInfo::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
|
|
|
|
$this->domain_action_log($action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode(', ', $restore_ids));
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
DB::rollBack();
|
|
|
|
return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|