作者 lyh

gx

1 -<?php  
2 -  
3 -namespace App\Http\Logic\Aside\Devops;  
4 -  
5 -  
6 -use App\Enums\Common\Code;  
7 -use App\Exceptions\AsideGlobalException;  
8 -use App\Exceptions\BsideGlobalException;  
9 -use App\Http\Logic\Aside\BaseLogic;  
10 -use App\Models\Devops\ServerInformation;  
11 -use App\Models\Devops\ServerInformationLog;  
12 -use Illuminate\Database\Eloquent\Builder;  
13 -use Illuminate\Database\Eloquent\Collection;  
14 -use Illuminate\Database\Eloquent\Model;  
15 -use Illuminate\Support\Facades\DB;  
16 -  
17 -class ServerInformationLogic extends BaseLogic  
18 -{  
19 - /**  
20 - * @var array  
21 - */  
22 - private $param;  
23 -  
24 - public function __construct()  
25 - {  
26 - parent::__construct();  
27 -  
28 - $this->param = $this->requestAll;  
29 -  
30 - }  
31 -  
32 - /**  
33 - * 添加数据  
34 - * @return array  
35 - * @throws AsideGlobalException  
36 - * @throws BsideGlobalException  
37 - */  
38 - public function create()  
39 - {  
40 - $request = $this->param ?? [];  
41 - $service = new ServerInformation();  
42 - $this->extracted($request, $service, $service->FieldsArray());  
43 - if ($this->checkIp($request['ip'])) {  
44 - return $this->fail('服务器信息添加失败,ip已存在');  
45 - }  
46 - DB::beginTransaction();  
47 - if ($service->save()) {  
48 - $original = $service->getOriginal();  
49 - $original['type'] = $request['type'];  
50 - $original['belong_to'] = $request['belong_to'];  
51 - // 添加日志  
52 - $this->server_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id);  
53 - DB::commit();  
54 - return $this->success();  
55 - }  
56 - DB::rollBack();  
57 - return $this->fail('服务器信息添加失败');  
58 -  
59 - }  
60 -  
61 - /**  
62 - * 修改数据  
63 - * @return array  
64 - * @throws AsideGlobalException  
65 - * @throws BsideGlobalException  
66 - */  
67 - public function update()  
68 - {  
69 - $service = $this->getService();  
70 - $fields_array = $service->FieldsArray();  
71 - $request = $this->param ?? [];  
72 - $original = $service->getOriginal();  
73 - $original['type'] = $service->ServiceStr($original['type']);  
74 - $original['belong_to'] = $service->BelongToStr($original['belong_to']);  
75 - $this->extracted($request, $service, $original);  
76 - // 检查ip是否存在  
77 - if ($service->ip != $request['ip']) {  
78 - if ($this->checkIp($request['ip'])) {  
79 - $this->fail('服务器信息修改失败,ip已存在', Code::USER_ERROR);  
80 - }  
81 - }  
82 - DB::beginTransaction();  
83 - if ($service->save()) {  
84 - $revised = $service->getAttributes();  
85 - $diff = array_diff_assoc($original, $revised);  
86 - unset($diff['created_at']);  
87 - unset($diff['updated_at']);  
88 - unset($diff['deleted']);  
89 - $remarks = '';  
90 - if ($diff) {  
91 - $remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,修改内容为:';  
92 - foreach ($diff as $key => $value) {  
93 - $remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';  
94 - }  
95 - } else {  
96 - $remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,无修改';  
97 - }  
98 - // 添加日志  
99 - $this->server_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);  
100 - DB::commit();  
101 - return $this->success();  
102 - }  
103 - DB::rollBack();  
104 - return $this->fail('服务器信息修改失败');  
105 - }  
106 -  
107 - public function getService(int $deleted = ServerInformation::DELETED_NORMAL)  
108 - {  
109 - $id = $this->param['id'] ?? 0;  
110 - if (!$id) {  
111 - return $this->fail('ID不能为空');  
112 - }  
113 - $data = ServerInformation::query()->where('deleted', $deleted)->find($id);  
114 - if (!$data) {  
115 - return $this->fail('数据不存在!');  
116 - }  
117 - return $data;  
118 - }  
119 -  
120 - /**  
121 - * 检查ip是否存在  
122 - * @param $ip  
123 - * @return bool  
124 - */  
125 - public function checkIp($ip)  
126 - {  
127 - $usIp = ServerInformation::query()->where('ip', $ip)->first();  
128 - if ($usIp) {  
129 - return true;  
130 - } else {  
131 - return false;  
132 - }  
133 - }  
134 -  
135 - /**  
136 - * 详情  
137 - * @param int $deleted  
138 - * @return array|Builder|Collection|Model  
139 - * @throws AsideGlobalException  
140 - * @throws BsideGlobalException  
141 - */  
142 - public function serverInfo(int $deleted = ServerInformation::DELETED_NORMAL)  
143 - {  
144 - $id = $this->param['id'] ?? 0;  
145 - if (!$id) {  
146 - return $this->fail('ID不能为空');  
147 - }  
148 - $data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'ports', 'created_at', 'updated_at']);  
149 - if (!$data) {  
150 - return $this->fail('数据不存在!');  
151 - }  
152 - return $data;  
153 - }  
154 -  
155 - /**  
156 - * 服务器操作日志  
157 - * @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表  
158 - * @param array $original 原始数据  
159 - * @param array $revised 修改后数据  
160 - */  
161 - public function server_action_log(int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')  
162 - {  
163 - $log = new ServerInformationLog();  
164 - $this->log($log, $action, $original, $revised, $remarks);  
165 - }  
166 -  
167 - /**  
168 - * 提取数据  
169 - * @param array $request  
170 - * @param $service  
171 - * @param array $original  
172 - * @return void  
173 - */  
174 - public function extracted(array $request, $service, array $original)  
175 - {  
176 - $request = array_intersect_key($request, $original);  
177 - foreach ($request as $key => $value) {  
178 - $service->$key = trim($value) ?? $original[$key];  
179 - }  
180 - }  
181 -  
182 - /**  
183 - * 批量获取数据删除  
184 - * @return array  
185 - * @throws AsideGlobalException  
186 - * @throws BsideGlobalException  
187 - */  
188 - public function get_batch_update($action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL)  
189 - {  
190 - $ids = $this->getIds();  
191 - $data = ServerInformation::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();  
192 - $restore_ids = $data->pluck('id')->toArray();  
193 - $actionArr = ServerInformationLog::actionArr();  
194 - $actionStr = $actionArr[$action];  
195 - if (empty($restore_ids)) {  
196 - $this->fail($actionStr . '服务器信息不存在!', Code::USER_ERROR);  
197 - }  
198 - $original = $data->toArray();  
199 - DB::beginTransaction();  
200 - try {  
201 - $update = $deleted == ServerInformation::DELETED_NORMAL ? ServerInformation::DELETED_DELETE : ServerInformation::DELETED_NORMAL;  
202 - ServerInformation::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);  
203 - $this->server_action_log($action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode(', ', $restore_ids));  
204 - DB::commit();  
205 - return $this->success();  
206 - } catch (\Exception $e) {  
207 - DB::rollBack();  
208 - return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR);  
209 - }  
210 - }  
211 -  
212 - /**  
213 - * 批量获取数据恢复  
214 - * @return array  
215 - * @throws AsideGlobalException  
216 - * @throws BsideGlobalException  
217 - */  
218 - public function getIds()  
219 - {  
220 - $id = $this->param['id'] ?? 0;  
221 - if (!$id) {  
222 - return $this->fail('参数错误');  
223 - }  
224 - $ids = [];  
225 - if (!is_array($id)) {  
226 - $ids = explode(',', $id);  
227 - }  
228 - $ids = array_filter($ids, 'intval');  
229 - if (empty($ids)) {  
230 - return $this->fail('参数错误');  
231 - }  
232 - return $ids;  
233 - }  
234 -}  
@@ -100,75 +100,6 @@ class BaseLogic extends Logic @@ -100,75 +100,6 @@ class BaseLogic extends Logic
100 } 100 }
101 101
102 /** 102 /**
103 - * @name :上传图片返回hash  
104 - * @return void  
105 - * @author :liyuhang  
106 - * @method  
107 - */  
108 - public function upload(){  
109 - $files = $this->request->file('image');  
110 - $hash = hash_file('md5', $files->getPathname());  
111 - //查看文件是否存在  
112 - $imageModel = new ImageModel();  
113 - $image_hash = $imageModel->read(['hash'=>$hash]);  
114 - if($image_hash !== false){  
115 - return $hash;  
116 - }  
117 - $this->config = config('filesystems.disks.upload');  
118 - $this->uploads = config('upload.default_image');  
119 - $this->path = $this->config['root'].$this->uploads['path'].'/';  
120 - $url = $this->path;  
121 - $fileName = uniqid().rand(10000,99999).'.'.$files->getClientOriginalExtension();  
122 - $res = $files->move($url,$fileName);  
123 - if ($res === false) {  
124 - return false;  
125 - }  
126 - $data = [  
127 - 'path' => $url.$fileName,  
128 - 'created_at' => date('Y-m-d H:i:s',time()),  
129 - 'size' => $res->getSize(),  
130 - 'hash' => $hash,  
131 - 'type'=>$files->getClientOriginalExtension(),  
132 - ];  
133 - $rs = $imageModel->add($data);  
134 - if ($rs === false) {  
135 - return false;  
136 - }  
137 - return $hash;  
138 - }  
139 -  
140 - /**  
141 - * @name :自增或自减  
142 - * @return bool  
143 - * @author :liyuhang  
144 - * @method  
145 - */  
146 - public function set_num($model,$data,$type = 'add',$num = 1){  
147 - if(is_array($data)){  
148 - foreach ($data as $v){  
149 - $this->set_num($model,$v,$type,$num);  
150 - }  
151 - }else{  
152 - if($type == 'del'){  
153 - $rs = $model::where('id',$data)->decrement('num',$num);  
154 - }else{  
155 - $rs = $model::where('id',$data)->increment('num',$num);  
156 - }  
157 - }  
158 - return $rs;  
159 - }  
160 -  
161 - public function getProjectDomain(){  
162 - if(!empty($this->project['deploy_optimize']['domain'])){  
163 - return $this->project['deploy_optimize']['domain'];  
164 - }  
165 - if(!empty($this->project['deploy_build']['test_domain'])){  
166 - return $this->project['deploy_build']['test_domain'];  
167 - }  
168 - return '';  
169 - }  
170 -  
171 - /**  
172 * @name :(通知更新)projectUrl 103 * @name :(通知更新)projectUrl
173 * @author :lyh 104 * @author :lyh
174 * @method :post 105 * @method :post
@@ -31,7 +31,7 @@ class CategoryLogic extends BaseLogic @@ -31,7 +31,7 @@ class CategoryLogic extends BaseLogic
31 { 31 {
32 $data = parent::getList($map, $sort, $columns, $limit); 32 $data = parent::getList($map, $sort, $columns, $limit);
33 foreach ($data as &$v){ 33 foreach ($data as &$v){
34 - $v['url'] = $this->getProjectDomain() . $v['route'] ; 34 + $v['url'] = $this->user['domain'] . $v['route'] ;
35 $v['product_num'] = $this->getProductNum($v['id']); 35 $v['product_num'] = $this->getProductNum($v['id']);
36 $v['image_link'] = getImageUrl($v['image']); 36 $v['image_link'] = getImageUrl($v['image']);
37 } 37 }
@@ -44,7 +44,7 @@ class CategoryLogic extends BaseLogic @@ -44,7 +44,7 @@ class CategoryLogic extends BaseLogic
44 public function getInfo($id) 44 public function getInfo($id)
45 { 45 {
46 $info = $this->model->read(['id'=>$id]); 46 $info = $this->model->read(['id'=>$id]);
47 - $info['url'] = $this->getProjectDomain() . $info['route'] ; 47 + $info['url'] = $this->user['domain'] . $info['route'] ;
48 $info['image_link'] = getImageUrl($info['image']); 48 $info['image_link'] = getImageUrl($info['image']);
49 return $this->success($info); 49 return $this->success($info);
50 } 50 }
@@ -31,7 +31,7 @@ class KeywordLogic extends BaseLogic @@ -31,7 +31,7 @@ class KeywordLogic extends BaseLogic
31 foreach ($data['list'] as &$v){ 31 foreach ($data['list'] as &$v){
32 $v['product_num'] = $this->getProductNum($v['id']); 32 $v['product_num'] = $this->getProductNum($v['id']);
33 $v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']); 33 $v['tdk'] = boolval($v['seo_title']) * boolval($v['seo_keywords']) * boolval($v['seo_description']);
34 - $v['url'] = $this->getProjectDomain() . $v['route']; 34 + $v['url'] = $this->user['domain'] . $v['route'];
35 } 35 }
36 return $this->success($data); 36 return $this->success($data);
37 } 37 }
@@ -39,7 +39,7 @@ class KeywordLogic extends BaseLogic @@ -39,7 +39,7 @@ class KeywordLogic extends BaseLogic
39 public function getInfo($id) 39 public function getInfo($id)
40 { 40 {
41 $info = parent::getInfo($id); 41 $info = parent::getInfo($id);
42 - $info['url'] = $this->getProjectDomain() . $info['route']; 42 + $info['url'] = $this->user['domain'] . $info['route'];
43 return $this->success($info); 43 return $this->success($info);
44 } 44 }
45 45
@@ -54,7 +54,7 @@ class ProductLogic extends BaseLogic @@ -54,7 +54,7 @@ class ProductLogic extends BaseLogic
54 $info['keyword_id_text'] = Arr::arrToSet($info['keyword_id_text'], 'trim'); 54 $info['keyword_id_text'] = Arr::arrToSet($info['keyword_id_text'], 'trim');
55 $info['status_text'] = Product::statusMap()[$info['status']] ?? ''; 55 $info['status_text'] = Product::statusMap()[$info['status']] ?? '';
56 $info['created_uid_text'] = (new UserLogic())->getCacheInfo($info['created_uid'])['name'] ?? ''; 56 $info['created_uid_text'] = (new UserLogic())->getCacheInfo($info['created_uid'])['name'] ?? '';
57 - $info['url'] = $this->getProjectDomain() . $info['route'] ; 57 + $info['url'] = $this->user['domain'] . $info['route'] ;
58 return $info; 58 return $info;
59 } 59 }
60 60
@@ -393,39 +393,4 @@ class Logic @@ -393,39 +393,4 @@ class Logic
393 return new static(...$params); 393 return new static(...$params);
394 } 394 }
395 395
396 - /**  
397 - * 添加日志  
398 - * @param $log  
399 - * @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表  
400 - * @param array $original 原始数据  
401 - * @param array $revised 修改后数据  
402 - * @param string $remarks 备注  
403 - * @return bool  
404 - */  
405 - public function log($log, int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], string $remarks = ''): bool  
406 - {  
407 - // $action 1:添加 2:修改 3:删除 4:恢复  
408 - $actionArr = $log->actionArr();  
409 - $actionStr = $actionArr[$action];  
410 - $ip = request()->getClientIp();  
411 - $url = request()->getRequestUri();  
412 - $method = request()->getMethod();  
413 - $userId = $this->uid ?? 0;  
414 - $log->user_id = $userId;  
415 - $log->action = $actionStr;  
416 - $log->original = json_encode($original);  
417 - $log->revised = json_encode($revised);  
418 - $log->ip = $ip;  
419 - $log->url = $url;  
420 - $log->method = $method;  
421 - $log->remarks = $remarks;  
422 - DB::beginTransaction();  
423 - if ($log->save()) {  
424 - DB::commit();  
425 - return true;  
426 - }  
427 - DB::rollBack();  
428 - Log::error('日志添加失败');  
429 - return false;  
430 - }  
431 } 396 }