作者 lyh

gx

<?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\Support\Facades\DB;
class ServerInformationLogic extends BaseLogic
{
/**
* @var array
*/
private $param;
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
}
/**
* 添加数据
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function create()
{
$request = $this->param ?? [];
$service = new ServerInformation();
$this->extracted($request, $service, $service->FieldsArray());
if ($this->checkIp($request['ip'])) {
return $this->fail('服务器信息添加失败,ip已存在');
}
DB::beginTransaction();
if ($service->save()) {
$original = $service->getOriginal();
$original['type'] = $request['type'];
$original['belong_to'] = $request['belong_to'];
// 添加日志
$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 = $this->getService();
$fields_array = $service->FieldsArray();
$request = $this->param ?? [];
$original = $service->getOriginal();
$original['type'] = $service->ServiceStr($original['type']);
$original['belong_to'] = $service->BelongToStr($original['belong_to']);
$this->extracted($request, $service, $original);
// 检查ip是否存在
if ($service->ip != $request['ip']) {
if ($this->checkIp($request['ip'])) {
$this->fail('服务器信息修改失败,ip已存在', Code::USER_ERROR);
}
}
DB::beginTransaction();
if ($service->save()) {
$revised = $service->getAttributes();
$diff = array_diff_assoc($original, $revised);
unset($diff['created_at']);
unset($diff['updated_at']);
unset($diff['deleted']);
$remarks = '';
if ($diff) {
$remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,修改内容为:';
foreach ($diff as $key => $value) {
$remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
}
} else {
$remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,无修改';
}
// 添加日志
$this->server_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);
DB::commit();
return $this->success();
}
DB::rollBack();
return $this->fail('服务器信息修改失败');
}
public function getService(int $deleted = ServerInformation::DELETED_NORMAL)
{
$id = $this->param['id'] ?? 0;
if (!$id) {
return $this->fail('ID不能为空');
}
$data = ServerInformation::query()->where('deleted', $deleted)->find($id);
if (!$data) {
return $this->fail('数据不存在!');
}
return $data;
}
/**
* 检查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 = $this->param['id'] ?? 0;
if (!$id) {
return $this->fail('ID不能为空');
}
$data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'ports', 'created_at', 'updated_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 = '')
{
$log = new ServerInformationLog();
$this->log($log, $action, $original, $revised, $remarks);
}
/**
* 提取数据
* @param array $request
* @param $service
* @param array $original
* @return void
*/
public function extracted(array $request, $service, array $original)
{
$request = array_intersect_key($request, $original);
foreach ($request as $key => $value) {
$service->$key = trim($value) ?? $original[$key];
}
}
/**
* 批量获取数据删除
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function get_batch_update($action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL)
{
$ids = $this->getIds();
$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);
}
}
/**
* 批量获取数据恢复
* @return array
* @throws AsideGlobalException
* @throws BsideGlobalException
*/
public function getIds()
{
$id = $this->param['id'] ?? 0;
if (!$id) {
return $this->fail('参数错误');
}
$ids = [];
if (!is_array($id)) {
$ids = explode(',', $id);
}
$ids = array_filter($ids, 'intval');
if (empty($ids)) {
return $this->fail('参数错误');
}
return $ids;
}
}
... ... @@ -100,75 +100,6 @@ class BaseLogic extends Logic
}
/**
* @name :上传图片返回hash
* @return void
* @author :liyuhang
* @method
*/
public function upload(){
$files = $this->request->file('image');
$hash = hash_file('md5', $files->getPathname());
//查看文件是否存在
$imageModel = new ImageModel();
$image_hash = $imageModel->read(['hash'=>$hash]);
if($image_hash !== false){
return $hash;
}
$this->config = config('filesystems.disks.upload');
$this->uploads = config('upload.default_image');
$this->path = $this->config['root'].$this->uploads['path'].'/';
$url = $this->path;
$fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();
$res = $files->move($url,$fileName);
if ($res === false) {
return false;
}
$data = [
'path' => $url.$fileName,
'created_at' => date('Y-m-d H:i:s',time()),
'size' => $res->getSize(),
'hash' => $hash,
'type'=>$files->getClientOriginalExtension(),
];
$rs = $imageModel->add($data);
if ($rs === false) {
return false;
}
return $hash;
}
/**
* @name :自增或自减
* @return bool
* @author :liyuhang
* @method
*/
public function set_num($model,$data,$type = 'add',$num = 1){
if(is_array($data)){
foreach ($data as $v){
$this->set_num($model,$v,$type,$num);
}
}else{
if($type == 'del'){
$rs = $model::where('id',$data)->decrement('num',$num);
}else{
$rs = $model::where('id',$data)->increment('num',$num);
}
}
return $rs;
}
public function getProjectDomain(){
if(!empty($this->project['deploy_optimize']['domain'])){
return $this->project['deploy_optimize']['domain'];
}
if(!empty($this->project['deploy_build']['test_domain'])){
return $this->project['deploy_build']['test_domain'];
}
return '';
}
/**
* @name :(通知更新)projectUrl
* @author :lyh
* @method :post
... ...
... ... @@ -31,7 +31,7 @@ class CategoryLogic extends BaseLogic
{
$data = parent::getList($map, $sort, $columns, $limit);
foreach ($data as &$v){
$v['url'] = $this->getProjectDomain() . $v['route'] ;
$v['url'] = $this->user['domain'] . $v['route'] ;
$v['product_num'] = $this->getProductNum($v['id']);
$v['image_link'] = getImageUrl($v['image']);
}
... ... @@ -44,7 +44,7 @@ class CategoryLogic extends BaseLogic
public function getInfo($id)
{
$info = $this->model->read(['id'=>$id]);
$info['url'] = $this->getProjectDomain() . $info['route'] ;
$info['url'] = $this->user['domain'] . $info['route'] ;
$info['image_link'] = getImageUrl($info['image']);
return $this->success($info);
}
... ...
... ... @@ -31,7 +31,7 @@ class KeywordLogic extends BaseLogic
foreach ($data['list'] as &$v){
$v['product_num'] = $this->getProductNum($v['id']);
$v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']);
$v['url'] = $this->getProjectDomain() . $v['route'];
$v['url'] = $this->user['domain'] . $v['route'];
}
return $this->success($data);
}
... ... @@ -39,7 +39,7 @@ class KeywordLogic extends BaseLogic
public function getInfo($id)
{
$info = parent::getInfo($id);
$info['url'] = $this->getProjectDomain() . $info['route'];
$info['url'] = $this->user['domain'] . $info['route'];
return $this->success($info);
}
... ...
... ... @@ -54,7 +54,7 @@ class ProductLogic extends BaseLogic
$info['keyword_id_text'] = Arr::arrToSet($info['keyword_id_text'], 'trim');
$info['status_text'] = Product::statusMap()[$info['status']] ?? '';
$info['created_uid_text'] = (new UserLogic())->getCacheInfo($info['created_uid'])['name'] ?? '';
$info['url'] = $this->getProjectDomain() . $info['route'] ;
$info['url'] = $this->user['domain'] . $info['route'] ;
return $info;
}
... ...
... ... @@ -393,39 +393,4 @@ class Logic
return new static(...$params);
}
/**
* 添加日志
* @param $log
* @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表
* @param array $original 原始数据
* @param array $revised 修改后数据
* @param string $remarks 备注
* @return bool
*/
public function log($log, int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], string $remarks = ''): bool
{
// $action 1:添加 2:修改 3:删除 4:恢复
$actionArr = $log->actionArr();
$actionStr = $actionArr[$action];
$ip = request()->getClientIp();
$url = request()->getRequestUri();
$method = request()->getMethod();
$userId = $this->uid ?? 0;
$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();
if ($log->save()) {
DB::commit();
return true;
}
DB::rollBack();
Log::error('日志添加失败');
return false;
}
}
... ...