作者 lyh

Merge branch 'develop' of http://47.244.231.31:8099/zhl/globalso-v6 into develop

<?php
namespace App\Http\Controllers\Aside\Devops;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Devops\ServerInformationLogic;
use App\Http\Requests\Aside\Devops\ServerInformationRequest;
use App\Models\Devops\ServerInformation;
use App\Models\Devops\ServerInformationLog;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class ServerInformationController extends BaseController
{
/**
* @return JsonResponse
*/
public function lists($deleted = ServerInformation::DELETED_NORMAL)
{
$size = request()->input('size', $this->row);
$data = ServerInformation::query()->select(['id', 'title', 'ip'])
->where('deleted', $deleted)
->orderBy('id', 'desc')
->paginate($size);
return $this->response('success', Code::SUCCESS, $data);
}
/**
* 添加
* @param ServerInformationRequest $serverInformationRequest
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function add(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic)
{
$serverInformationRequest->validated();
$serverInformationLogic->create();
$this->response('服务器添加成功!');
}
/**
* 编辑
* @param ServerInformationRequest $serverInformationRequest
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function edit(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic)
{
$serverInformationRequest->validate([
'id' => ['required'],
], [
'id.required' => 'ID不能为空',
]);
$serverInformationLogic->update();
$this->response('服务器修改成功!');
}
/**
* 删除
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function delete(ServerInformationLogic $serverInformationLogic)
{
$serverInformationLogic->get_batch_update();
$this->response('服务器删除成功!');
}
/**
* 获取软删除的数据
* @return JsonResponse
*/
public function delete_list()
{
return $this->lists(ServerInformation::DELETED_DELETE);
}
/**
* 恢复数据
* @param ServerInformationLogic $serverInformationLogic
* @return void
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function restore(ServerInformationLogic $serverInformationLogic)
{
$serverInformationLogic->get_batch_update(ServerInformationLog::ACTION_RECOVER, ServerInformation::DELETED_DELETE);
$this->response('服务器恢复成功!');
}
/**
* 搜索
* @param Request $request
* @return JsonResponse
*/
public function search(Request $request)
{
$search = [];
$ip = $request->input('ip');
if ($ip) {
$search['ip'] = $ip;
}
$remark = $request->input('title');
if ($remark) {
$search['title'] = $remark;
}
if (empty($search)) {
return $this->response('请输入搜索内容', Code::USER_ERROR);
}
$query = ServerInformation::query()->select(['id', 'title', 'ip']);
foreach ($search as $key => $item) {
$query = $query->Where("{$key}", 'like', "%{$item}%");
}
$size = $request->input('size', $this->row);
$query = $query->orderBy('id', 'desc')->paginate($size);
return $this->response('success', Code::SUCCESS, $query);
}
/**
* 服务器信息
* @param int $deleted
* @return JsonResponse
* @throws AsideGlobalException
* @throws BsideGlobalException
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function getServerInfo(int $deleted = ServerInformation::DELETED_NORMAL)
{
$serverInformationLogic = new ServerInformationLogic();
$data = $serverInformationLogic->serverInfo($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 getDeleteServerInfo()
{
return $this->getServerInfo(ServerInformation::DELETED_DELETE);
}
}
... ...
<?php
namespace App\Http\Controllers\Aside\Devops;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
use App\Http\Controllers\Aside\BaseController;
use App\Http\Logic\Aside\Devops\ServerInformationLogic;
use App\Http\Requests\Aside\Devops\ServerInformationRequest;
use App\Models\Devops\ServerInformation;
use App\Models\Devops\ServerInformationLog;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class ServerInformationLogController extends BaseController
{
public function lists(Request $request)
{
$size = $request->input('size', $this->row);
$data = ServerInformationLog::query()
->orderBy('id', 'desc')
->paginate($size);
return $this->response('success', Code::SUCCESS, $data);
}
}
... ...
<?php
namespace App\Http\Controllers\Aside\Template;
use App\Http\Controllers\Aside\BaseController;
/**
* 模板header footer
* @author:dc
* @time 2023/4/26 11:10
* Class HeaderFooterController
* @package App\Http\Controllers\Aside\Template
*/
class HeaderFooterController extends BaseController
{
}
... ...
<?php
namespace App\Http\Controllers\Bside\Template;
use App\Http\Controllers\Bside\BaseController;
/**
* 模板header footer
* @author:dc
* @time 2023/4/26 11:10
* Class HeaderFooterController
* @package App\Http\Controllers\Bside\Template
*/
class HeaderFooterController extends BaseController
{
}
... ...
<?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);
}
}
}
... ...
... ... @@ -96,6 +96,7 @@ class RankDataLogic extends BaseLogic
];
//外链引荐域名
$recomm_domain = $recomm_domain ? $recomm_domain->toArray() : [];
$data['external_links_domain_chat'] = [
'labels' => array_map(function ($item) {
return Str::substrReplace($item, '***', 2, 3);
... ... @@ -120,8 +121,8 @@ class RankDataLogic extends BaseLogic
}
//关键词排名分析图
$data['rank_chat'] = [
'data' => $rank_week['data'],
'labels' => $rank_week['date'],
'data' => $rank_week['data'] ?? [],
'labels' => $rank_week['date'] ?? [],
];
return $data;
... ...
<?php
namespace App\Http\Requests\Aside\Devops;
use App\Models\Devops\ServerInformation;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class ServerInformationRequest 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 [
'type' => [ 'required', Rule::in( array_keys( ( new ServerInformation )->ServiceArray() ) ) ],
'ip' => 'required|ip',
'title' => 'required|max:200',
'belong_to' => 'required|integer|in:1,2', // 1:公司 2:客户
'sshpass' => 'required|max:100',
'ports' => 'required|integer|min:1|max:65535',
];
}
public function messages()
{
$service_array = ( new ServerInformation )->ServiceArray();
$serviceStr = implode( ', ', $service_array );
return [
'type.required' => '服务器类型不能为空',
'type.integer' => '服务器类型格式不正确',
'type.between' => '服务器类型 ' . $serviceStr,
'ip.required' => 'ip不能为空',
'ip.ip' => 'ip格式不正确',
'title.required' => '服务器标题不能为空',
'title.max' => '服务器标题不能超过200个字符',
'belong_to.required' => '服务器归属不能为空',
'belong_to.integer' => '服务器归属格式不正确',
'belong_to.in' => '服务器归属只能是公司或者客户',
'sshpass.required' => 'ssh密码不能为空',
'sshpass.max' => 'ssh密码不能超过100个字符',
'ports.required' => 'ssh端口不能为空',
'ports.integer' => 'ssh端口格式不正确',
'ports.min' => 'ssh端口不能小于1',
'ports.max' => 'ssh端口不能大于65535',
];
}
}
... ...
<?php
namespace App\Models\Aside;
use App\Models\Base;
/**
* 模板 头部底部 对所有 客户
* @author:dc
* @time 2023/4/26 11:21
* Class TemplateHeaderFooter
* @package App\Models\Aside
*/
class TemplateHeaderFooter extends Base
{
protected $table = 'gl_aside_template_header_footer';
}
... ...
<?php
namespace App\Models\Bside;
use App\Models\Base;
/**
* 模板 头部底部 客户自己的
* @author:dc
* @time 2023/4/26 11:21
* Class TemplateHeaderFooter
* @package App\Models\Bside
*/
class TemplateHeaderFooter extends Base
{
protected $table = 'gl_bside_template_header_footer';
}
... ...
<?php
namespace App\Models\Devops;
use Illuminate\Database\Eloquent\Model;
class ServerInformation extends Model
{
protected $table = 'gl_server_information';
const CREATED_AT = 'create_at';
const UPDATED_AT = 'update_at';
// 软删除 0:正常 1:删除
const DELETED_NORMAL = 0;
const DELETED_DELETE = 1;
/**
* @param $num
*
* @return string
*/
public function Service($num)
{
return $this->ServiceArray()[$num];
}
/**
* @return array
*/
public function ServiceArray()
{
return [
1 => '阿里云',
2 => '腾讯云',
3 => 'linode',
4 => '尊云',
5 => '互联',
6 => '其他',
7 => 'Ramnode',
8 => 'CN2-SSD美国',
9 => '国内测试服务器',
];
}
/**
* 字段信息
* @return array
*/
public function FieldsArray()
{
return [
'type' => '服务器类型',
'ip' => '服务器IP',
'title' => '服务器标题',
'belong_to' => '服务器归属',
'sshpass' => 'SSH 密码',
'ports' => 'SSH 端口',
'other' => '其他信息 json格式',
];
}
/**
* 服务器归属信息
* @return array
*/
public function BelongToArray()
{
return [
1 => '公司',
2 => '客户',
];
}
public function BelongTo($num)
{
return $this->BelongToArray()[$num];
}
/**
* 返回服务器类型
* @param $value
*
* @return string
*/
public function getTypeAttribute($value)
{
return $this->Service($value);
}
/**
* 返回服务器归属
* @param $value
*
* @return string
*/
public function getBelongToAttribute($value)
{
return $this->BelongTo($value);
}
}
... ...
<?php
namespace App\Models\Devops;
use Illuminate\Database\Eloquent\Model;
class ServerInformationLog extends Model
{
protected $table = 'gl_server_information_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 => '恢复',
];
}
}
... ...
此 diff 太大无法显示。
... ... @@ -4,6 +4,7 @@
*/
use \Illuminate\Support\Facades\Route;
use \App\Http\Controllers\Aside;
//必须登录验证的路由组
Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上web的中间件
Route::middleware(['aloginauth'])->group(function () {
... ... @@ -123,6 +124,21 @@ Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上w
Route::post('/update_database', [Aside\Devops\ServerConfigController::class, 'updateDatabase'])->name('admin.devops.server_config.update_database');
Route::post('/update_code', [Aside\Devops\ServerConfigController::class, 'updateCode'])->name('admin.devops.server_config.update_code');
});
// 服务器添加|修改|删除
Route::prefix('server')->group(function () {
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'); // 添加
Route::post('/edit', [Aside\Devops\ServerInformationController::class, 'edit'])->name('admin.devops.bt_edit'); // 修改
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'); //日志列表
});
});
// 自定义页面 模板,头部底部
... ...