作者 李美松

服务方法修改 | 域名信息添加

... ... @@ -22,9 +22,25 @@ class ServerInformationController extends BaseController
*/
public function lists($deleted = ServerInformation::DELETED_NORMAL)
{
$search = [];
$ip = response()->input('ip');
if ($ip) {
$search['ip'] = $ip;
}
$remark = response()->input('title');
if ($remark) {
$search['title'] = $remark;
}
if (empty($search)) {
return $this->response('请输入搜索内容', Code::USER_ERROR);
}
$size = request()->input('size', $this->row);
$data = ServerInformation::query()->select(['id', 'title', 'ip'])
->where('deleted', $deleted)
$query = ServerInformation::query()->select(['id', 'title', 'ip']);
foreach ($search as $key => $item) {
$query = $query->Where("{$key}", 'like', "%{$item}%");
}
$data = $query->where('deleted', $deleted)
->orderBy('id', 'desc')
->paginate($size);
return $this->response('success', Code::SUCCESS, $data);
... ... @@ -110,7 +126,7 @@ class ServerInformationController extends BaseController
public function search(Request $request)
{
$search = [];
$ip = $request->input('ip');
$ip = $request->input('ip');
if ($ip) {
$search['ip'] = $ip;
}
... ... @@ -125,7 +141,7 @@ class ServerInformationController extends BaseController
foreach ($search as $key => $item) {
$query = $query->Where("{$key}", 'like', "%{$item}%");
}
$size = $request->input('size', $this->row);
$size = $request->input('size', $this->row);
$query = $query->orderBy('id', 'desc')->paginate($size);
return $this->response('success', Code::SUCCESS, $query);
}
... ... @@ -142,7 +158,7 @@ class ServerInformationController extends BaseController
public function getServerInfo(int $deleted = ServerInformation::DELETED_NORMAL)
{
$serverInformationLogic = new ServerInformationLogic();
$data = $serverInformationLogic->serverInfo($deleted);
$data = $serverInformationLogic->serverInfo($deleted);
if (!$data) {
return $this->response('服务器信息不存在', Code::USER_ERROR);
}
... ... @@ -150,6 +166,7 @@ class ServerInformationController extends BaseController
}
/**
* 获取软删除的服务器信息
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
... ...
<?php
namespace App\Http\Controllers\Aside\Domain;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Domain\DomainInfoLogic;
use App\Http\Requests\Aside\Domain\DomainInfoRequest;
use App\Models\Aside\Domain\DomainInfo;
use App\Models\Aside\Domain\DomainInfoLog;
use Illuminate\Http\JsonResponse;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
/**
* Class DomainInfoController
* @package App\Http\Controllers\Aside 域名管理
*/
class DomainInfoController extends BaseController
{
/**
* 域名列表
* @param int $deleted
* @return JsonResponse
*/
public function lists(int $deleted = DomainInfo::DELETED_NORMAL)
{
$size = request()->input('size', $this->row);
$data = DomainInfo::query()->select(['id', 'domain', 'belong_to'])
->where('deleted', $deleted)
->orderBy('id', 'desc')
->paginate($size);
return $this->response('success', Code::SUCCESS, $data);
}
/**
* 获取软删除的数据
* @return JsonResponse
*/
public function delete_list()
{
return $this->lists(DomainInfo::DELETED_DELETE);
}
/**
* 添加域名
* @param DomainInfoRequest $domainInfoRequest
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function add(DomainInfoRequest $domainInfoRequest, DomainInfoLogic $domainInfoLogic)
{
$domainInfoRequest->validated();
$domainInfoLogic->create();
$this->response('域名添加成功!');
}
/**
* 编辑域名
* @param DomainInfoRequest $domainInfoRequest
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function edit(DomainInfoRequest $domainInfoRequest, DomainInfoLogic $domainInfoLogic)
{
$domainInfoRequest->validate([
'id' => 'required|integer'
], [
'id.required' => 'id不能为空',
'id.integer' => 'id参数错误'
]);
$domainInfoLogic->update();
$this->response('域名修改成功!');
}
/**
* 删除
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function delete(DomainInfoLogic $domainInfoLogic)
{
$domainInfoLogic->get_batch_update();
$this->response('服务器删除成功!');
}
/**
* 恢复数据
* @param DomainInfoLogic $domainInfoLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function restore(DomainInfoLogic $domainInfoLogic)
{
$domainInfoLogic->get_batch_update(DomainInfoLog::ACTION_RECOVER, DomainInfo::DELETED_DELETE);
$this->response('服务器恢复成功!');
}
/**
* 域名信息
* @param int $deleted
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getDomainInfo(int $deleted = DomainInfo::DELETED_NORMAL)
{
$domainInfoLogic = new DomainInfoLogic();
$data = $domainInfoLogic->domainInfo($deleted);
if (!$data) {
return $this->response('服务器信息不存在', Code::USER_ERROR);
}
return $this->success($data->toArray());
}
/**
* 获取软删除域名信息
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getDeleteDomainInfo()
{
return $this->getDomainInfo(DomainInfo::DELETED_DELETE);
}
}
... ...
... ... @@ -152,7 +152,7 @@ class ServerInformationLogic extends BaseLogic
if (!$id) {
return $this->fail('参数错误');
}
$data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'sshpass', 'ports', 'create_at', 'update_at']);
$data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'ports', 'create_at', 'update_at']);
if (!$data) {
return $this->fail('数据不存在!');
}
... ...
<?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);
}
}
}
... ...
<?php
namespace App\Http\Requests\Aside\Domain;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class DomainInfoRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'domain' => 'required|max:200',
'belong_to' => [
'required',
Rule::in([1, 2])
],
'status' => [
'required',
Rule::in([0, 1])
]
];
}
public function messages()
{
return [
'domain.required' => '域名不能为空',
'domain.max' => '域名长度不能超过200个字符',
'belong_to.required' => '所属不能为空',
'belong_to.in' => '所属参数错误',
'status.required' => '状态不能为空',
'status.in' => '状态参数错误',
];
}
}
... ...
<?php
namespace App\Models\Aside\Domain;
use App\Models\Base;
/**
* Class DomainInfo
* @package App\Models\Aside\Domain
* @Author YiYuan-LIn
* @Date: 2019/5/16
* 域名信息模型
*/
class DomainInfo extends Base
{
protected $table = 'gl_domain_info';
// 软删除 0:正常 1:删除
/** @var int 软删除 - 正常 */
const DELETED_NORMAL = 0;
/** @var int 软删除 - 删除 */
const DELETED_DELETE = 1;
protected $hidden = [
'created_at',
'updated_at'
];
/**
* 字段信息
* @return array
*/
public function FieldsArray()
{
return [
'domain' => '域名',
'belong_to' => '域名归属',
'status' => '域名状态',
'domain_start_time' => '域名开始时间',
'domain_end_time' => '域名结束时间',
'certificate_start_time' => '证书开始时间',
'certificate_end_time' => '证书结束时间',
];
}
/**
* 域名归属信息
* @return array
*/
public function BelongToArray()
{
return [
1 => '公司',
2 => '客户',
];
}
public function BelongTo($num)
{
return $this->BelongToArray()[$num];
}
}
... ...
<?php
namespace App\Models\Aside\Domain;
use Illuminate\Database\Eloquent\Model;
class DomainInfoLog extends Model
{
protected $table = 'gl_domain_info_log';
const CREATED_AT = 'create_at';
const UPDATED_AT = 'update_at';
public function getOriginalAttribute($value)
{
return json_decode($value, true);
}
public function getRevisedAttribute($value)
{
return json_decode($value, true);
}
/** @var int 日志添加 */
const ACTION_ADD = 1;
/** @var int 日志修改 */
const ACTION_UPDATE = 2;
/** @var int 日志删除 */
const ACTION_DELETE = 3;
/** @var int 日志恢复 */
const ACTION_RECOVER = 4;
/**
* @return string[]
*/
public static function actionArr()
{
return [
1 => '添加',
2 => '修改',
3 => '删除',
4 => '恢复',
];
}
}
... ...
... ... @@ -127,7 +127,7 @@ Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上w
// 服务器添加|修改|删除
Route::prefix('server')->group(function () {
Route::get('/', [Aside\Devops\ServerInformationController::class, 'lists'])->name('admin.devops.bt'); // 列表
Route::get('/', [Aside\Devops\ServerInformationController::class, 'lists'])->name('admin.devops.bt'); // 列表 | 搜索
Route::get('/info', [Aside\Devops\ServerInformationController::class, 'getServerInfo'])->name('admin.devops.bt_info'); // 获取信息
Route::get('/delete_info', [Aside\Devops\ServerInformationController::class, 'getDeleteServerInfo'])->name('admin.devops.bt_delete_info'); // 删除信息
Route::post('/add', [Aside\Devops\ServerInformationController::class, 'add'])->name('admin.devops.bt_add'); // 添加
... ... @@ -135,7 +135,6 @@ Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上w
Route::get('/delete', [Aside\Devops\ServerInformationController::class, 'delete'])->name('admin.devops.bt_delete'); // 删除
Route::get('/delete_list', [Aside\Devops\ServerInformationController::class, 'delete_list'])->name('admin.devops.bt_delete_list'); // 删除列表
Route::get('/restore', [Aside\Devops\ServerInformationController::class, 'restore'])->name('admin.devops.bt_restore'); //恢复数据
Route::get('/search', [Aside\Devops\ServerInformationController::class, 'search'])->name('admin.devops.bt_search'); //搜索
Route::get('/log', [Aside\Devops\ServerInformationLogController::class, 'lists'])->name('admin.devops.bt_log_lists'); //日志列表
});
... ...