|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Logic\Aside\Devops;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Enums\Common\Code;
|
|
|
|
use App\Exceptions\AsideGlobalException;
|
|
|
|
use App\Exceptions\BsideGlobalException;
|
|
|
|
use App\Http\Logic\Aside\BaseLogic;
|
|
|
|
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\Http\JsonResponse;
|
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
|
|
class ServerInformationLogic 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;
|
|
|
|
$service = new ServerInformation();
|
|
|
|
$this->extracted($request, $service);
|
|
|
|
if ($this->checkIp($service->ip)) {
|
|
|
|
return $this->fail('服务器信息添加失败,ip已存在');
|
|
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
|
|
|
|
if ($service->save()) {
|
|
|
|
$original = [
|
|
|
|
'id' => $service->id,
|
|
|
|
'type' => $service->type,
|
|
|
|
'ip' => $service->ip,
|
|
|
|
'title' => $service->title,
|
|
|
|
'belong_to' => $service->belong_to,
|
|
|
|
'sshpass' => $service->sshpass,
|
|
|
|
'ports' => $service->ports,
|
|
|
|
];
|
|
|
|
// 添加日志
|
|
|
|
$this->server_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id);
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
}
|
|
|
|
DB::rollBack();
|
|
|
|
return $this->fail('服务器信息添加失败');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 修改数据
|
|
|
|
* @return array
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function update()
|
|
|
|
{
|
|
|
|
|
|
|
|
$service = new ServerInformation();
|
|
|
|
$fields_array = $service->FieldsArray();
|
|
|
|
$request = $this->param;
|
|
|
|
$service = $this->ServerInfo();
|
|
|
|
$original = $service->toArray();
|
|
|
|
$this->extracted($request, $service);
|
|
|
|
|
|
|
|
// 检查ip是否存在
|
|
|
|
if ($service->ip != $request['ip']) {
|
|
|
|
if ($this->checkIp($request['ip'])) {
|
|
|
|
$this->fail('服务器信息修改失败,ip已存在', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
if ($service->save()) {
|
|
|
|
$revised = [
|
|
|
|
'id' => $service->id,
|
|
|
|
'type' => $service->type,
|
|
|
|
'ip' => $service->ip,
|
|
|
|
'title' => $service->title,
|
|
|
|
'belong_to' => $service->belong_to,
|
|
|
|
'sshpass' => $service->sshpass,
|
|
|
|
'ports' => $service->ports,
|
|
|
|
'other' => $service->other,
|
|
|
|
'delete' => $service->delete,
|
|
|
|
];
|
|
|
|
$diff = array_diff_assoc($original, $revised);
|
|
|
|
unset($diff['create_at']);
|
|
|
|
unset($diff['update_at']);
|
|
|
|
unset($diff['deleted']);
|
|
|
|
$remarks = '';
|
|
|
|
if ($diff) {
|
|
|
|
$remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,修改内容为:';
|
|
|
|
foreach ($diff as $key => $value) {
|
|
|
|
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 添加日志
|
|
|
|
$this->server_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);
|
|
|
|
DB::commit();
|
|
|
|
return $this->success();
|
|
|
|
}
|
|
|
|
DB::rollBack();
|
|
|
|
return $this->fail('服务器信息修改失败');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 检查ip是否存在
|
|
|
|
* @param $ip
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function checkIp($ip)
|
|
|
|
{
|
|
|
|
$usIp = ServerInformation::query()->where('ip', $ip)->first();
|
|
|
|
if ($usIp) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 详情
|
|
|
|
* @param int $deleted
|
|
|
|
* @return array|Builder|Collection|Model
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function serverInfo(int $deleted = ServerInformation::DELETED_NORMAL)
|
|
|
|
{
|
|
|
|
$id = \request()->input('id');
|
|
|
|
if (!$id) {
|
|
|
|
return $this->fail('参数错误');
|
|
|
|
}
|
|
|
|
$data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'sshpass', 'ports', '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 server_action_log(int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
|
|
|
|
{
|
|
|
|
// $action 1:添加 2:修改 3:删除 4:恢复
|
|
|
|
$actionArr = ServerInformationLog::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 $service
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function extracted(array $request, $service)
|
|
|
|
{
|
|
|
|
$service->type = trim($request['type']); // 服务器类型
|
|
|
|
$service->ip = trim($request['ip']); // 服务器ip
|
|
|
|
$service->title = trim($request['title']); // 服务器标题
|
|
|
|
$service->belong_to = trim($request['belong_to']); // 服务器归属
|
|
|
|
$service->sshpass = trim($request['sshpass']); // ssh密码
|
|
|
|
$service->ports = trim($request['ports']); // ssh端口
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 批量获取数据删除
|
|
|
|
* @return array
|
|
|
|
* @throws AsideGlobalException
|
|
|
|
* @throws BsideGlobalException
|
|
|
|
*/
|
|
|
|
public function get_batch_update($action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::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 = ServerInformation::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
|
|
|
|
$restore_ids = $data->pluck('id')->toArray();
|
|
|
|
$actionArr = ServerInformationLog::actionArr();
|
|
|
|
$actionStr = $actionArr[$action];
|
|
|
|
if (empty($restore_ids)) {
|
|
|
|
$this->fail($actionStr . '服务器信息不存在!', Code::USER_ERROR);
|
|
|
|
}
|
|
|
|
$original = $data->toArray();
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
|
|
$update = $deleted == ServerInformation::DELETED_NORMAL ? ServerInformation::DELETED_DELETE : ServerInformation::DELETED_NORMAL;
|
|
|
|
ServerInformation::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
|
|
|
|
$this->server_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|