作者 李美松

域名方法完成 | 服务器方法修改

@@ -22,24 +22,24 @@ class ServerInformationController extends BaseController @@ -22,24 +22,24 @@ class ServerInformationController extends BaseController
22 */ 22 */
23 public function lists($deleted = ServerInformation::DELETED_NORMAL) 23 public function lists($deleted = ServerInformation::DELETED_NORMAL)
24 { 24 {
25 - 25 + $request = $this->param;
26 $search = []; 26 $search = [];
27 - $ip = response()->input('ip');  
28 - if ($ip) {  
29 - $search['ip'] = $ip; 27 + $search_array = [
  28 + 'ip' => 'ip',
  29 + 'title' => 'title'
  30 + ];
  31 + foreach ($search_array as $key => $item) {
  32 + if (isset($request[$key]) && $request[$key]) {
  33 + $search[$item] = $request[$key];
30 } 34 }
31 - $remark = response()->input('title');  
32 - if ($remark) {  
33 - $search['title'] = $remark;  
34 - }  
35 - if (empty($search)) {  
36 - return $this->response('请输入搜索内容', Code::USER_ERROR);  
37 } 35 }
38 $size = request()->input('size', $this->row); 36 $size = request()->input('size', $this->row);
39 $query = ServerInformation::query()->select(['id', 'title', 'ip']); 37 $query = ServerInformation::query()->select(['id', 'title', 'ip']);
  38 + if ($search) {
40 foreach ($search as $key => $item) { 39 foreach ($search as $key => $item) {
41 $query = $query->Where("{$key}", 'like', "%{$item}%"); 40 $query = $query->Where("{$key}", 'like', "%{$item}%");
42 } 41 }
  42 + }
43 $data = $query->where('deleted', $deleted) 43 $data = $query->where('deleted', $deleted)
44 ->orderBy('id', 'desc') 44 ->orderBy('id', 'desc')
45 ->paginate($size); 45 ->paginate($size);
@@ -74,9 +74,10 @@ class ServerInformationController extends BaseController @@ -74,9 +74,10 @@ class ServerInformationController extends BaseController
74 public function edit(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic) 74 public function edit(ServerInformationRequest $serverInformationRequest, ServerInformationLogic $serverInformationLogic)
75 { 75 {
76 $serverInformationRequest->validate([ 76 $serverInformationRequest->validate([
77 - 'id' => ['required'], 77 + 'id' => 'required|integer',
78 ], [ 78 ], [
79 'id.required' => 'ID不能为空', 79 'id.required' => 'ID不能为空',
  80 + 'id.integer' => 'ID必须为整数',
80 ]); 81 ]);
81 $serverInformationLogic->update(); 82 $serverInformationLogic->update();
82 $this->response('服务器修改成功!'); 83 $this->response('服务器修改成功!');
@@ -27,9 +27,28 @@ class DomainInfoController extends BaseController @@ -27,9 +27,28 @@ class DomainInfoController extends BaseController
27 */ 27 */
28 public function lists(int $deleted = DomainInfo::DELETED_NORMAL) 28 public function lists(int $deleted = DomainInfo::DELETED_NORMAL)
29 { 29 {
  30 + $request = $this->param;
  31 + $search = [];
  32 + $search_array = [
  33 + 'domain' => 'domain',
  34 + 'belong' => 'belong_to',
  35 + 'domain_time' => 'domain_end_time',
  36 + 'certificate_time' => 'certificate_end_time'
  37 + ];
  38 + foreach ($search_array as $key => $item) {
  39 + if (isset($request[$key]) && $request[$key]) {
  40 + $search[$item] = $request[$key];
  41 + }
  42 + }
  43 + // 每页条数
30 $size = request()->input('size', $this->row); 44 $size = request()->input('size', $this->row);
31 - $data = DomainInfo::query()->select(['id', 'domain', 'belong_to'])  
32 - ->where('deleted', $deleted) 45 + $query = DomainInfo::query()->select(['id', 'domain', 'belong_to']);
  46 + if ($search) {
  47 + foreach ($search as $key => $item) {
  48 + $query = $query->Where("{$key}", 'like', "%{$item}%");
  49 + }
  50 + }
  51 + $data = $query->where('deleted', $deleted)
33 ->orderBy('id', 'desc') 52 ->orderBy('id', 'desc')
34 ->paginate($size); 53 ->paginate($size);
35 return $this->response('success', Code::SUCCESS, $data); 54 return $this->response('success', Code::SUCCESS, $data);
@@ -89,7 +108,7 @@ class DomainInfoController extends BaseController @@ -89,7 +108,7 @@ class DomainInfoController extends BaseController
89 public function delete(DomainInfoLogic $domainInfoLogic) 108 public function delete(DomainInfoLogic $domainInfoLogic)
90 { 109 {
91 $domainInfoLogic->get_batch_update(); 110 $domainInfoLogic->get_batch_update();
92 - $this->response('服务器删除成功!'); 111 + $this->response('域名删除成功!');
93 } 112 }
94 113
95 /** 114 /**
@@ -102,7 +121,7 @@ class DomainInfoController extends BaseController @@ -102,7 +121,7 @@ class DomainInfoController extends BaseController
102 public function restore(DomainInfoLogic $domainInfoLogic) 121 public function restore(DomainInfoLogic $domainInfoLogic)
103 { 122 {
104 $domainInfoLogic->get_batch_update(DomainInfoLog::ACTION_RECOVER, DomainInfo::DELETED_DELETE); 123 $domainInfoLogic->get_batch_update(DomainInfoLog::ACTION_RECOVER, DomainInfo::DELETED_DELETE);
105 - $this->response('服务器恢复成功!'); 124 + $this->response('域名恢复成功!');
106 } 125 }
107 126
108 /** 127 /**
@@ -114,12 +133,12 @@ class DomainInfoController extends BaseController @@ -114,12 +133,12 @@ class DomainInfoController extends BaseController
114 * @throws ContainerExceptionInterface 133 * @throws ContainerExceptionInterface
115 * @throws NotFoundExceptionInterface 134 * @throws NotFoundExceptionInterface
116 */ 135 */
117 - public function getDomainInfo(int $deleted = DomainInfo::DELETED_NORMAL) 136 + public function info(int $deleted = DomainInfo::DELETED_NORMAL)
118 { 137 {
119 $domainInfoLogic = new DomainInfoLogic(); 138 $domainInfoLogic = new DomainInfoLogic();
120 $data = $domainInfoLogic->domainInfo($deleted); 139 $data = $domainInfoLogic->domainInfo($deleted);
121 if (!$data) { 140 if (!$data) {
122 - return $this->response('服务器信息不存在', Code::USER_ERROR); 141 + return $this->response('域名信息不存在', Code::USER_ERROR);
123 } 142 }
124 return $this->success($data->toArray()); 143 return $this->success($data->toArray());
125 } 144 }
@@ -134,6 +153,6 @@ class DomainInfoController extends BaseController @@ -134,6 +153,6 @@ class DomainInfoController extends BaseController
134 */ 153 */
135 public function getDeleteDomainInfo() 154 public function getDeleteDomainInfo()
136 { 155 {
137 - return $this->getDomainInfo(DomainInfo::DELETED_DELETE); 156 + return $this->info(DomainInfo::DELETED_DELETE);
138 } 157 }
139 } 158 }
  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Aside\Domain;
  4 +
  5 +use App\Enums\Common\Code;
  6 +use App\Http\Controllers\Aside\BaseController;
  7 +use App\Models\Aside\Domain\DomainInfoLog;
  8 +use Illuminate\Http\Request;
  9 +
  10 +class DomainInfoLogController extends BaseController
  11 +{
  12 + public function lists(Request $request)
  13 + {
  14 + $size = $request->input('size', $this->row);
  15 + $data = DomainInfoLog::query()
  16 + ->orderBy('id', 'desc')
  17 + ->paginate($size);
  18 + return $this->response('success', Code::SUCCESS, $data);
  19 +
  20 + }
  21 +}
@@ -12,9 +12,7 @@ use App\Models\Devops\ServerInformationLog; @@ -12,9 +12,7 @@ use App\Models\Devops\ServerInformationLog;
12 use Illuminate\Database\Eloquent\Builder; 12 use Illuminate\Database\Eloquent\Builder;
13 use Illuminate\Database\Eloquent\Collection; 13 use Illuminate\Database\Eloquent\Collection;
14 use Illuminate\Database\Eloquent\Model; 14 use Illuminate\Database\Eloquent\Model;
15 -use Illuminate\Http\JsonResponse;  
16 use Illuminate\Support\Facades\DB; 15 use Illuminate\Support\Facades\DB;
17 -use Illuminate\Support\Facades\Log;  
18 16
19 class ServerInformationLogic extends BaseLogic 17 class ServerInformationLogic extends BaseLogic
20 { 18 {
@@ -41,7 +39,7 @@ class ServerInformationLogic extends BaseLogic @@ -41,7 +39,7 @@ class ServerInformationLogic extends BaseLogic
41 */ 39 */
42 public function create() 40 public function create()
43 { 41 {
44 - $request = $this->param; 42 + $request = $this->param ?? [];
45 $service = new ServerInformation(); 43 $service = new ServerInformation();
46 $this->extracted($request, $service); 44 $this->extracted($request, $service);
47 if ($this->checkIp($service->ip)) { 45 if ($this->checkIp($service->ip)) {
@@ -50,15 +48,9 @@ class ServerInformationLogic extends BaseLogic @@ -50,15 +48,9 @@ class ServerInformationLogic extends BaseLogic
50 DB::beginTransaction(); 48 DB::beginTransaction();
51 49
52 if ($service->save()) { 50 if ($service->save()) {
53 - $original = [  
54 - 'id' => $service->id,  
55 - 'type' => $service->type,  
56 - 'ip' => $service->ip,  
57 - 'title' => $service->title,  
58 - 'belong_to' => $service->belong_to,  
59 - 'sshpass' => $service->sshpass,  
60 - 'ports' => $service->ports,  
61 - ]; 51 + $original = $service->getOriginal();
  52 + $original['type'] = $request['type'];
  53 + $original['belong_to'] = $request['belong_to'];
62 // 添加日志 54 // 添加日志
63 $this->server_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id); 55 $this->server_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $service->id);
64 DB::commit(); 56 DB::commit();
@@ -77,14 +69,13 @@ class ServerInformationLogic extends BaseLogic @@ -77,14 +69,13 @@ class ServerInformationLogic extends BaseLogic
77 */ 69 */
78 public function update() 70 public function update()
79 { 71 {
80 -  
81 - $service = new ServerInformation(); 72 + $service = $this->getService();
82 $fields_array = $service->FieldsArray(); 73 $fields_array = $service->FieldsArray();
83 - $request = $this->param;  
84 - $service = $this->ServerInfo();  
85 - $original = $service->toArray(); 74 + $request = $this->param ?? [];
  75 + $original = $service->getOriginal();
  76 + $original['type'] = $service->ServiceStr($original['type']);
  77 + $original['belong_to'] = $service->BelongToStr($original['belong_to']);
86 $this->extracted($request, $service); 78 $this->extracted($request, $service);
87 -  
88 // 检查ip是否存在 79 // 检查ip是否存在
89 if ($service->ip != $request['ip']) { 80 if ($service->ip != $request['ip']) {
90 if ($this->checkIp($request['ip'])) { 81 if ($this->checkIp($request['ip'])) {
@@ -93,20 +84,10 @@ class ServerInformationLogic extends BaseLogic @@ -93,20 +84,10 @@ class ServerInformationLogic extends BaseLogic
93 } 84 }
94 DB::beginTransaction(); 85 DB::beginTransaction();
95 if ($service->save()) { 86 if ($service->save()) {
96 - $revised = [  
97 - 'id' => $service->id,  
98 - 'type' => $service->type,  
99 - 'ip' => $service->ip,  
100 - 'title' => $service->title,  
101 - 'belong_to' => $service->belong_to,  
102 - 'sshpass' => $service->sshpass,  
103 - 'ports' => $service->ports,  
104 - 'other' => $service->other,  
105 - 'delete' => $service->delete,  
106 - ]; 87 + $revised = $service->getAttributes();
107 $diff = array_diff_assoc($original, $revised); 88 $diff = array_diff_assoc($original, $revised);
108 - unset($diff['create_at']);  
109 - unset($diff['update_at']); 89 + unset($diff['created_at']);
  90 + unset($diff['updated_at']);
110 unset($diff['deleted']); 91 unset($diff['deleted']);
111 $remarks = ''; 92 $remarks = '';
112 if ($diff) { 93 if ($diff) {
@@ -114,6 +95,8 @@ class ServerInformationLogic extends BaseLogic @@ -114,6 +95,8 @@ class ServerInformationLogic extends BaseLogic
114 foreach ($diff as $key => $value) { 95 foreach ($diff as $key => $value) {
115 $remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; '; 96 $remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
116 } 97 }
  98 + } else {
  99 + $remarks .= '修改ID为 ' . $service->id . ' 的服务器信息,无修改';
117 } 100 }
118 // 添加日志 101 // 添加日志
119 $this->server_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks); 102 $this->server_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks);
@@ -124,6 +107,19 @@ class ServerInformationLogic extends BaseLogic @@ -124,6 +107,19 @@ class ServerInformationLogic extends BaseLogic
124 return $this->fail('服务器信息修改失败'); 107 return $this->fail('服务器信息修改失败');
125 } 108 }
126 109
  110 + public function getService(int $deleted = ServerInformation::DELETED_NORMAL)
  111 + {
  112 + $id = $this->param['id'] ?? 0;
  113 + if (!$id) {
  114 + return $this->fail('ID不能为空');
  115 + }
  116 + $data = ServerInformation::query()->where('deleted', $deleted)->find($id);
  117 + if (!$data) {
  118 + return $this->fail('数据不存在!');
  119 + }
  120 + return $data;
  121 + }
  122 +
127 /** 123 /**
128 * 检查ip是否存在 124 * 检查ip是否存在
129 * @param $ip 125 * @param $ip
@@ -148,11 +144,11 @@ class ServerInformationLogic extends BaseLogic @@ -148,11 +144,11 @@ class ServerInformationLogic extends BaseLogic
148 */ 144 */
149 public function serverInfo(int $deleted = ServerInformation::DELETED_NORMAL) 145 public function serverInfo(int $deleted = ServerInformation::DELETED_NORMAL)
150 { 146 {
151 - $id = \request()->input('id'); 147 + $id = $this->param['id'] ?? 0;
152 if (!$id) { 148 if (!$id) {
153 - return $this->fail('参数错误'); 149 + return $this->fail('ID不能为空');
154 } 150 }
155 - $data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'ports', 'create_at', 'update_at']); 151 + $data = ServerInformation::query()->where('deleted', $deleted)->find($id, ['type', 'ip', 'title', 'belong_to', 'ports', 'created_at', 'updated_at']);
156 if (!$data) { 152 if (!$data) {
157 return $this->fail('数据不存在!'); 153 return $this->fail('数据不存在!');
158 } 154 }
@@ -167,30 +163,8 @@ class ServerInformationLogic extends BaseLogic @@ -167,30 +163,8 @@ class ServerInformationLogic extends BaseLogic
167 */ 163 */
168 public function server_action_log(int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '') 164 public function server_action_log(int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
169 { 165 {
170 - // $action 1:添加 2:修改 3:删除 4:恢复  
171 - $actionArr = ServerInformationLog::actionArr();  
172 - $actionStr = $actionArr[$action];  
173 - $ip = request()->getClientIp();  
174 - $url = request()->getRequestUri();  
175 - $method = request()->getMethod();  
176 - $userId = $this->uid ?? 0;  
177 $log = new ServerInformationLog(); 166 $log = new ServerInformationLog();
178 - $log->user_id = $userId;  
179 - $log->action = $actionStr;  
180 - $log->original = json_encode($original);  
181 - $log->revised = json_encode($revised);  
182 - $log->ip = $ip;  
183 - $log->url = $url;  
184 - $log->method = $method;  
185 - $log->remarks = $remarks;  
186 - DB::beginTransaction();  
187 - try {  
188 - $log->save();  
189 - DB::commit();  
190 - } catch (\Exception $e) {  
191 - DB::rollBack();  
192 - Log::error('服务器信息日志添加失败');  
193 - } 167 + $this->log($log, $action, $original, $revised, $remarks);
194 } 168 }
195 169
196 /** 170 /**
@@ -216,18 +190,7 @@ class ServerInformationLogic extends BaseLogic @@ -216,18 +190,7 @@ class ServerInformationLogic extends BaseLogic
216 */ 190 */
217 public function get_batch_update($action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL) 191 public function get_batch_update($action = ServerInformationLog::ACTION_DELETE, $deleted = ServerInformation::DELETED_NORMAL)
218 { 192 {
219 - $id = request()->input('id');  
220 - if (!$id) {  
221 - return $this->fail('参数错误');  
222 - }  
223 - $ids = [];  
224 - if (!is_array($id)) {  
225 - $ids = explode(',', $id);  
226 - }  
227 - $ids = array_filter($ids, 'intval');  
228 - if (empty($ids)) {  
229 - return $this->fail('参数错误');  
230 - } 193 + $ids = $this->getIds();
231 $data = ServerInformation::query()->whereIn('id', $ids)->where('deleted', $deleted)->get(); 194 $data = ServerInformation::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
232 $restore_ids = $data->pluck('id')->toArray(); 195 $restore_ids = $data->pluck('id')->toArray();
233 $actionArr = ServerInformationLog::actionArr(); 196 $actionArr = ServerInformationLog::actionArr();
@@ -248,4 +211,27 @@ class ServerInformationLogic extends BaseLogic @@ -248,4 +211,27 @@ class ServerInformationLogic extends BaseLogic
248 return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR); 211 return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR);
249 } 212 }
250 } 213 }
  214 +
  215 + /**
  216 + * 批量获取数据恢复
  217 + * @return array
  218 + * @throws AsideGlobalException
  219 + * @throws BsideGlobalException
  220 + */
  221 + public function getIds()
  222 + {
  223 + $id = $this->param['id'] ?? 0;
  224 + if (!$id) {
  225 + return $this->fail('参数错误');
  226 + }
  227 + $ids = [];
  228 + if (!is_array($id)) {
  229 + $ids = explode(',', $id);
  230 + }
  231 + $ids = array_filter($ids, 'intval');
  232 + if (empty($ids)) {
  233 + return $this->fail('参数错误');
  234 + }
  235 + return $ids;
  236 + }
251 } 237 }
@@ -7,15 +7,14 @@ use App\Enums\Common\Code; @@ -7,15 +7,14 @@ use App\Enums\Common\Code;
7 use App\Exceptions\AsideGlobalException; 7 use App\Exceptions\AsideGlobalException;
8 use App\Exceptions\BsideGlobalException; 8 use App\Exceptions\BsideGlobalException;
9 use App\Http\Logic\Aside\BaseLogic; 9 use App\Http\Logic\Aside\BaseLogic;
  10 +use App\Http\Logic\Aside\Devops\ServerInformationLogic;
10 use App\Models\Aside\Domain\DomainInfo; 11 use App\Models\Aside\Domain\DomainInfo;
11 use App\Models\Aside\Domain\DomainInfoLog; 12 use App\Models\Aside\Domain\DomainInfoLog;
12 use App\Models\Devops\ServerInformation; 13 use App\Models\Devops\ServerInformation;
13 -use App\Models\Devops\ServerInformationLog;  
14 use Illuminate\Database\Eloquent\Builder; 14 use Illuminate\Database\Eloquent\Builder;
15 use Illuminate\Database\Eloquent\Collection; 15 use Illuminate\Database\Eloquent\Collection;
16 use Illuminate\Database\Eloquent\Model; 16 use Illuminate\Database\Eloquent\Model;
17 use Illuminate\Support\Facades\DB; 17 use Illuminate\Support\Facades\DB;
18 -use Illuminate\Support\Facades\Log;  
19 18
20 class DomainInfoLogic extends BaseLogic 19 class DomainInfoLogic extends BaseLogic
21 { 20 {
@@ -42,23 +41,19 @@ class DomainInfoLogic extends BaseLogic @@ -42,23 +41,19 @@ class DomainInfoLogic extends BaseLogic
42 */ 41 */
43 public function create() 42 public function create()
44 { 43 {
45 - $request = $this->param; 44 + $request = $this->param ?? [];
  45 + if ($this->checkDomain($request['domain'])) {
  46 + return $this->fail('域名已存在!');
  47 + }
46 $domain = new DomainInfo(); 48 $domain = new DomainInfo();
47 $this->extracted($request, $domain); 49 $this->extracted($request, $domain);
48 DB::beginTransaction(); 50 DB::beginTransaction();
49 if ($domain->save()) { 51 if ($domain->save()) {
50 - $original = [  
51 - 'id' => $domain->id,  
52 - 'domain' => $domain->domain,  
53 - 'belong_to' => $domain->belong_to,  
54 - 'status' => $domain->status,  
55 - 'domain_start_time' => $domain->domain_start_time,  
56 - 'domain_end_time' => $domain->domain_end_time,  
57 - 'certificate_start_time' => $domain->certificate_start_time,  
58 - 'certificate_end_time' => $domain->certificate_end_time,  
59 - ]; 52 + $original = $domain->getOriginal();
  53 + $original['belong_to'] = $request['belong_to'];
  54 + $original['status'] = $request['status'];
60 // 添加日志 55 // 添加日志
61 - $this->domain_action_log(ServerInformationLog::ACTION_ADD, $original, $original, '添加服务器信息成功 - ID : ' . $domain->id); 56 + $this->domain_action_log(DomainInfoLog::ACTION_ADD, $original, $original, '添加域名信息成功 - ID : ' . $domain->id);
62 DB::commit(); 57 DB::commit();
63 return $this->success(); 58 return $this->success();
64 } 59 }
@@ -75,25 +70,25 @@ class DomainInfoLogic extends BaseLogic @@ -75,25 +70,25 @@ class DomainInfoLogic extends BaseLogic
75 */ 70 */
76 public function update() 71 public function update()
77 { 72 {
78 - $domain = new DomainInfo(); 73 + $domain = $this->getDomain();
  74 + $original = $domain->getOriginal();
  75 + $original['belong_to'] = $domain->BelongToStr($original['belong_to']);
  76 + $original['status'] = $domain->StatusToStr($original['status']);
79 $request = $this->param; 77 $request = $this->param;
80 $this->extracted($request, $domain); 78 $this->extracted($request, $domain);
  79 + // 检查ip是否存在
  80 + if ($domain->domain != $request['domain']) {
  81 + if ($this->checkDomain($request['domain'])) {
  82 + $this->fail('域名信息修改失败,域名已存在', Code::USER_ERROR);
  83 + }
  84 + }
81 DB::beginTransaction(); 85 DB::beginTransaction();
82 if ($domain->save()) { 86 if ($domain->save()) {
83 $fields_array = $domain->FieldsArray(); 87 $fields_array = $domain->FieldsArray();
84 - $original = $domain->getOriginal();  
85 - $revised = [  
86 - 'id' => $domain->id,  
87 - 'domain' => $domain->domain,  
88 - 'belong_to' => $domain->belong_to,  
89 - 'status' => $domain->status,  
90 - 'domain_start_time' => $domain->domain_start_time,  
91 - 'domain_end_time' => $domain->domain_end_time,  
92 - 'certificate_start_time' => $domain->certificate_start_time,  
93 - 'certificate_end_time' => $domain->certificate_end_time,  
94 - 'delete' => $domain->delete,  
95 - ]; 88 + $revised = $domain->getAttributes();
96 $diff = array_diff_assoc($original, $revised); 89 $diff = array_diff_assoc($original, $revised);
  90 + unset($diff['created_at']);
  91 + unset($diff['updated_at']);
97 unset($diff['deleted']); 92 unset($diff['deleted']);
98 $remarks = ''; 93 $remarks = '';
99 if ($diff) { 94 if ($diff) {
@@ -101,9 +96,11 @@ class DomainInfoLogic extends BaseLogic @@ -101,9 +96,11 @@ class DomainInfoLogic extends BaseLogic
101 foreach ($diff as $key => $value) { 96 foreach ($diff as $key => $value) {
102 $remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; '; 97 $remarks .= $fields_array[$key] . ' 由 ' . $value . ' 修改为 ' . $revised[$key] . '; ';
103 } 98 }
  99 + } else {
  100 + $remarks .= '修改ID为 ' . $domain->id . ' 的域名信息,无修改';
104 } 101 }
105 // 添加日志 102 // 添加日志
106 - $this->domain_action_log(ServerInformationLog::ACTION_UPDATE, $original, $revised, $remarks); 103 + $this->domain_action_log(DomainInfoLog::ACTION_UPDATE, $original, $revised, $remarks);
107 DB::commit(); 104 DB::commit();
108 return $this->success(); 105 return $this->success();
109 } 106 }
@@ -112,6 +109,25 @@ class DomainInfoLogic extends BaseLogic @@ -112,6 +109,25 @@ class DomainInfoLogic extends BaseLogic
112 } 109 }
113 110
114 /** 111 /**
  112 + * 根据ID获取数据
  113 + * @return array
  114 + * @throws AsideGlobalException
  115 + * @throws BsideGlobalException
  116 + */
  117 + public function getDomain(int $deleted = DomainInfo::DELETED_NORMAL)
  118 + {
  119 + $id = $this->param['id'] ?? 0;
  120 + if (!$id) {
  121 + return $this->fail('ID不能为空');
  122 + }
  123 + $data = DomainInfo::query()->where('deleted', $deleted)->find($id);
  124 + if (!$data) {
  125 + return $this->fail('数据不存在!');
  126 + }
  127 + return $data;
  128 + }
  129 +
  130 + /**
115 * 检查域名是否存在 131 * 检查域名是否存在
116 * @param $domain 132 * @param $domain
117 * @return bool 133 * @return bool
@@ -135,11 +151,11 @@ class DomainInfoLogic extends BaseLogic @@ -135,11 +151,11 @@ class DomainInfoLogic extends BaseLogic
135 */ 151 */
136 public function domainInfo(int $deleted = DomainInfo::DELETED_NORMAL) 152 public function domainInfo(int $deleted = DomainInfo::DELETED_NORMAL)
137 { 153 {
138 - $id = request()->input('id'); 154 + $id = $this->param['id'] ?? 0;
139 if (!$id) { 155 if (!$id) {
140 - return $this->fail('参数错误'); 156 + return $this->fail('ID不能为空');
141 } 157 }
142 - $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']); 158 + $data = DomainInfo::query()->where('deleted', $deleted)->find($id, ['domain', 'belong_to', 'status', 'domain_start_time', 'domain_end_time', 'certificate_start_time', 'certificate_end_time', 'created_at', 'updated_at']);
143 if (!$data) { 159 if (!$data) {
144 return $this->fail('数据不存在!'); 160 return $this->fail('数据不存在!');
145 } 161 }
@@ -154,44 +170,27 @@ class DomainInfoLogic extends BaseLogic @@ -154,44 +170,27 @@ class DomainInfoLogic extends BaseLogic
154 */ 170 */
155 public function domain_action_log(int $action = DomainInfoLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '') 171 public function domain_action_log(int $action = DomainInfoLog::ACTION_ADD, array $original = [], array $revised = [], $remarks = '')
156 { 172 {
157 - // $action 1:添加 2:修改 3:删除 4:恢复  
158 - $actionArr = DomainInfoLog::actionArr();  
159 - $actionStr = $actionArr[$action];  
160 - $ip = request()->getClientIp();  
161 - $url = request()->getRequestUri();  
162 - $method = request()->getMethod();  
163 - $userId = $this->uid ?? 0;  
164 - $log = new ServerInformationLog();  
165 - $log->user_id = $userId;  
166 - $log->action = $actionStr;  
167 - $log->original = json_encode($original);  
168 - $log->revised = json_encode($revised);  
169 - $log->ip = $ip;  
170 - $log->url = $url;  
171 - $log->method = $method;  
172 - $log->remarks = $remarks;  
173 - DB::beginTransaction();  
174 - try {  
175 - $log->save();  
176 - DB::commit();  
177 - } catch (\Exception $e) {  
178 - DB::rollBack();  
179 - Log::error('服务器信息日志添加失败');  
180 - } 173 + $log = new DomainInfoLog();
  174 + $this->log($log, $action, $original, $revised, $remarks);
181 } 175 }
182 176
183 /** 177 /**
184 * @param array $request 178 * @param array $request
185 * @param $domain 179 * @param $domain
186 * @return void 180 * @return void
  181 + * @throws AsideGlobalException
  182 + * @throws BsideGlobalException
187 */ 183 */
188 public function extracted(array $request, $domain) 184 public function extracted(array $request, $domain)
189 { 185 {
190 - $domain->domain = trim($request['domain']); // 域名 186 + $domain->domain = trim($request['domain']);
  187 + if (!$this->validateDomain($domain->domain)) {
  188 + $this->fail('域名格式错误');
  189 + }
191 $domain->belong_to = trim($request['belong_to']); // 域名归属 1 - 公司 2 - 客户 190 $domain->belong_to = trim($request['belong_to']); // 域名归属 1 - 公司 2 - 客户
192 $domain->status = trim($request['status']); // 域名状态 0 - 正常 1 - 关闭 191 $domain->status = trim($request['status']); // 域名状态 0 - 正常 1 - 关闭
193 $domain->domain_start_time = trim($request['domain_start_time']); // 域名开始时间 192 $domain->domain_start_time = trim($request['domain_start_time']); // 域名开始时间
194 - $domain->domain_end_time = trim($request['domain_end_time']); // 域名结束时间 193 + $domain->domain_end_time = trim($request['domain_end_time']);
195 $domain->certificate_start_time = trim($request['certificate_start_time']); // 证书开始时间 194 $domain->certificate_start_time = trim($request['certificate_start_time']); // 证书开始时间
196 $domain->certificate_end_time = trim($request['certificate_end_time']); // 证书结束时间 195 $domain->certificate_end_time = trim($request['certificate_end_time']); // 证书结束时间
197 } 196 }
@@ -204,36 +203,78 @@ class DomainInfoLogic extends BaseLogic @@ -204,36 +203,78 @@ class DomainInfoLogic extends BaseLogic
204 */ 203 */
205 public function get_batch_update($action = DomainInfoLog::ACTION_DELETE, $deleted = DomainInfo::DELETED_NORMAL) 204 public function get_batch_update($action = DomainInfoLog::ACTION_DELETE, $deleted = DomainInfo::DELETED_NORMAL)
206 { 205 {
207 - $id = request()->input('id');  
208 - if (!$id) {  
209 - return $this->fail('参数错误');  
210 - }  
211 - $ids = [];  
212 - if (!is_array($id)) {  
213 - $ids = explode(',', $id);  
214 - }  
215 - $ids = array_filter($ids, 'intval');  
216 - if (empty($ids)) {  
217 - return $this->fail('参数错误');  
218 - } 206 + $ids = (new ServerInformationLogic())->getIds();
219 $data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get(); 207 $data = DomainInfo::query()->whereIn('id', $ids)->where('deleted', $deleted)->get();
220 $restore_ids = $data->pluck('id')->toArray(); 208 $restore_ids = $data->pluck('id')->toArray();
221 $actionArr = DomainInfoLog::actionArr(); 209 $actionArr = DomainInfoLog::actionArr();
222 $actionStr = $actionArr[$action]; 210 $actionStr = $actionArr[$action];
223 if (empty($restore_ids)) { 211 if (empty($restore_ids)) {
224 - $this->fail($actionStr . '服务器信息不存在!', Code::USER_ERROR); 212 + $this->fail($actionStr . '域名信息不存在!', Code::USER_ERROR);
225 } 213 }
226 $original = $data->toArray(); 214 $original = $data->toArray();
227 DB::beginTransaction(); 215 DB::beginTransaction();
228 try { 216 try {
229 $update = $deleted == DomainInfo::DELETED_NORMAL ? DomainInfo::DELETED_DELETE : DomainInfo::DELETED_NORMAL; 217 $update = $deleted == DomainInfo::DELETED_NORMAL ? DomainInfo::DELETED_DELETE : DomainInfo::DELETED_NORMAL;
230 DomainInfo::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]); 218 DomainInfo::query()->whereIn('id', $restore_ids)->update(['deleted' => $update]);
231 - $this->domain_action_log($action, $original, $original, $actionStr . '服务器信息 - ID : ' . implode(', ', $restore_ids)); 219 + $this->domain_action_log($action, $original, $original, $actionStr . '域名信息 - ID : ' . implode(', ', $restore_ids));
232 DB::commit(); 220 DB::commit();
233 return $this->success(); 221 return $this->success();
234 } catch (\Exception $e) { 222 } catch (\Exception $e) {
235 DB::rollBack(); 223 DB::rollBack();
236 - return $this->fail('服务器信息' . $actionStr . '失败', Code::USER_ERROR); 224 + return $this->fail('域名信息' . $actionStr . '失败', Code::USER_ERROR);
  225 + }
237 } 226 }
  227 +
  228 + /**
  229 + * 域名到期时间
  230 + * @return array
  231 + */
  232 + public function domainCertificateTime($domain)
  233 + {
  234 + $domain = trim($domain);
  235 + $data = [];
  236 + $context = stream_context_create(['ssl' => ['capture_peer_cert' => true]]); // Notice: only 7.0.7+ supports this
  237 + $stream = stream_socket_client("ssl://$domain:443", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
  238 + if ($stream) {
  239 + $params = stream_context_get_params($stream);
  240 + $peerCertificate = openssl_x509_parse($params['options']['ssl']['peer_certificate']);
  241 + if ($peerCertificate) {
  242 + $validFrom = date_create_from_format('U', $peerCertificate['validFrom_time_t']); // 有效期开始时间
  243 + $validTo = date_create_from_format('U', $peerCertificate['validTo_time_t']); // 有效期结束时间
  244 + $data['validFrom'] = $validFrom->format('Y-m-d H:i:s');
  245 + $data['validTo'] = $validTo->format('Y-m-d H:i:s');
  246 + }
  247 + }
  248 + return $data;
  249 + }
  250 +
  251 + /**
  252 + * 验证给定的值是否是有效的域名。
  253 + *
  254 + * @param mixed $value
  255 + * @return bool
  256 + */
  257 + public function validateDomain($value)
  258 + {
  259 + // 从域中删除任何空间或路径。
  260 + $domain = preg_replace('/\s|\/.*$/', '', $value);
  261 + // 验证域是否至少包含一个句点。
  262 + if (strpos($domain, '.') === false) {
  263 + return false;
  264 + }
  265 + // 验证域是否以句点开始或结束。
  266 + if (strpos($domain, '.') === 0 || strrpos($domain, '.') === strlen($domain) - 1) {
  267 + return false;
  268 + }
  269 + // 验证域是否不包含无效字符。
  270 + if (!preg_match('/^[a-zA-Z0-9\-\.]+$/', $domain)) {
  271 + return false;
  272 + }
  273 + // 验证域是否具有有效的顶级域。
  274 + $tld = substr($domain, strrpos($domain, '.') + 1);
  275 + if (!in_array($tld, ['com', 'net', 'org'])) { // 如有必要,添加更多TLD。
  276 + return false;
  277 + }
  278 + return true;
238 } 279 }
239 } 280 }
@@ -8,7 +8,10 @@ use \App\Helper\Common as CommonHelper; @@ -8,7 +8,10 @@ use \App\Helper\Common as CommonHelper;
8 use App\Exceptions\AsideGlobalException; 8 use App\Exceptions\AsideGlobalException;
9 use App\Exceptions\BsideGlobalException; 9 use App\Exceptions\BsideGlobalException;
10 use App\Helper\Arr; 10 use App\Helper\Arr;
  11 +use App\Models\Devops\ServerInformationLog;
11 use Illuminate\Support\Facades\Cache; 12 use Illuminate\Support\Facades\Cache;
  13 +use Illuminate\Support\Facades\DB;
  14 +use Illuminate\Support\Facades\Log;
12 use Illuminate\Support\Facades\Schema; 15 use Illuminate\Support\Facades\Schema;
13 16
14 /** 17 /**
@@ -393,4 +396,40 @@ class Logic @@ -393,4 +396,40 @@ class Logic
393 public static function instance(...$params){ 396 public static function instance(...$params){
394 return new static(...$params); 397 return new static(...$params);
395 } 398 }
  399 +
  400 + /**
  401 + * 添加日志
  402 + * @param $log
  403 + * @param int $action 1:添加 2:修改 3:删除 4:搜索 5:详情 6:列表
  404 + * @param array $original 原始数据
  405 + * @param array $revised 修改后数据
  406 + * @param string $remarks 备注
  407 + * @return bool
  408 + */
  409 + public function log($log, int $action = ServerInformationLog::ACTION_ADD, array $original = [], array $revised = [], string $remarks = ''): bool
  410 + {
  411 + // $action 1:添加 2:修改 3:删除 4:恢复
  412 + $actionArr = $log->actionArr();
  413 + $actionStr = $actionArr[$action];
  414 + $ip = request()->getClientIp();
  415 + $url = request()->getRequestUri();
  416 + $method = request()->getMethod();
  417 + $userId = $this->uid ?? 0;
  418 + $log->user_id = $userId;
  419 + $log->action = $actionStr;
  420 + $log->original = json_encode($original);
  421 + $log->revised = json_encode($revised);
  422 + $log->ip = $ip;
  423 + $log->url = $url;
  424 + $log->method = $method;
  425 + $log->remarks = $remarks;
  426 + DB::beginTransaction();
  427 + if($log->save()){
  428 + DB::commit();
  429 + return true;
  430 + }
  431 + DB::rollBack();
  432 + Log::error('日志添加失败');
  433 + return false;
  434 + }
396 } 435 }
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace App\Http\Requests\Aside\Domain; 3 namespace App\Http\Requests\Aside\Domain;
4 4
  5 +use App\Models\Aside\Domain\DomainInfo;
5 use Illuminate\Foundation\Http\FormRequest; 6 use Illuminate\Foundation\Http\FormRequest;
6 use Illuminate\Validation\Rule; 7 use Illuminate\Validation\Rule;
7 8
@@ -24,15 +25,18 @@ class DomainInfoRequest extends FormRequest @@ -24,15 +25,18 @@ class DomainInfoRequest extends FormRequest
24 */ 25 */
25 public function rules() 26 public function rules()
26 { 27 {
  28 + $domain = new DomainInfo();
  29 + $status_tot_keys = array_keys($domain->StatusToArray());
  30 + $belong_to_keys = array_keys($domain->BelongToArray());
27 return [ 31 return [
28 'domain' => 'required|max:200', 32 'domain' => 'required|max:200',
29 'belong_to' => [ 33 'belong_to' => [
30 'required', 34 'required',
31 - Rule::in([1, 2]) 35 + Rule::in($belong_to_keys)
32 ], 36 ],
33 'status' => [ 37 'status' => [
34 'required', 38 'required',
35 - Rule::in([0, 1]) 39 + Rule::in($status_tot_keys)
36 ] 40 ]
37 ]; 41 ];
38 } 42 }
@@ -42,10 +46,10 @@ class DomainInfoRequest extends FormRequest @@ -42,10 +46,10 @@ class DomainInfoRequest extends FormRequest
42 return [ 46 return [
43 'domain.required' => '域名不能为空', 47 'domain.required' => '域名不能为空',
44 'domain.max' => '域名长度不能超过200个字符', 48 'domain.max' => '域名长度不能超过200个字符',
45 - 'belong_to.required' => '所属不能为空',  
46 - 'belong_to.in' => '所属参数错误',  
47 - 'status.required' => '状态不能为空',  
48 - 'status.in' => '状态参数错误', 49 + 'belong_to.required' => '域名所属不能为空',
  50 + 'belong_to.in' => '域名所属参数错误',
  51 + 'status.required' => '域名状态不能为空',
  52 + 'status.in' => '域名状态参数错误',
49 ]; 53 ];
50 } 54 }
51 55
@@ -55,8 +55,63 @@ class DomainInfo extends Base @@ -55,8 +55,63 @@ class DomainInfo extends Base
55 ]; 55 ];
56 } 56 }
57 57
  58 + public function BelongToStr($num)
  59 + {
  60 + return array_flip($this->BelongToArray())[$num];
  61 + }
  62 +
58 public function BelongTo($num) 63 public function BelongTo($num)
59 { 64 {
60 return $this->BelongToArray()[$num]; 65 return $this->BelongToArray()[$num];
61 } 66 }
  67 +
  68 + /**
  69 + * 域名状态信息
  70 + * @return array
  71 + */
  72 + public function StatusToArray()
  73 + {
  74 + return [
  75 + 0 => '未使用',
  76 + 1 => '使用中',
  77 + ];
  78 + }
  79 +
  80 + public function StatusToStr($num)
  81 + {
  82 + return array_flip($this->StatusToArray())[$num];
  83 + }
  84 +
  85 + /**
  86 + * 返回域名状态
  87 + * @param $num
  88 + *
  89 + * @return string
  90 + */
  91 + public function StatusTo($num)
  92 + {
  93 + return $this->StatusToArray()[$num];
  94 + }
  95 +
  96 + /**
  97 + * 返回服务器归属
  98 + * @param $value
  99 + *
  100 + * @return string
  101 + */
  102 + public function getBelongToAttribute($value)
  103 + {
  104 + return $this->BelongTo($value);
  105 + }
  106 +
  107 + /**
  108 + * 返回服务器状态
  109 + * @param $value
  110 + *
  111 + * @return string
  112 + */
  113 + public function getStatusAttribute($value)
  114 + {
  115 + return $this->StatusTo($value);
  116 + }
62 } 117 }
@@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Model; @@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Model;
7 class DomainInfoLog extends Model 7 class DomainInfoLog extends Model
8 { 8 {
9 protected $table = 'gl_domain_info_log'; 9 protected $table = 'gl_domain_info_log';
10 - const CREATED_AT = 'create_at';  
11 - const UPDATED_AT = 'update_at';  
12 10
13 public function getOriginalAttribute($value) 11 public function getOriginalAttribute($value)
14 { 12 {
@@ -8,8 +8,6 @@ class ServerInformation extends Model @@ -8,8 +8,6 @@ class ServerInformation extends Model
8 { 8 {
9 9
10 protected $table = 'gl_server_information'; 10 protected $table = 'gl_server_information';
11 - const CREATED_AT = 'create_at';  
12 - const UPDATED_AT = 'update_at';  
13 11
14 // 软删除 0:正常 1:删除 12 // 软删除 0:正常 1:删除
15 const DELETED_NORMAL = 0; 13 const DELETED_NORMAL = 0;
@@ -44,6 +42,11 @@ class ServerInformation extends Model @@ -44,6 +42,11 @@ class ServerInformation extends Model
44 ]; 42 ];
45 } 43 }
46 44
  45 + public function ServiceStr($num)
  46 + {
  47 + return array_flip($this->ServiceArray())[$num];
  48 + }
  49 +
47 /** 50 /**
48 * 字段信息 51 * 字段信息
49 * @return array 52 * @return array
@@ -73,6 +76,11 @@ class ServerInformation extends Model @@ -73,6 +76,11 @@ class ServerInformation extends Model
73 ]; 76 ];
74 } 77 }
75 78
  79 + public function BelongToStr($num)
  80 + {
  81 + return array_flip($this->BelongToArray())[$num];
  82 + }
  83 +
76 public function BelongTo($num) 84 public function BelongTo($num)
77 { 85 {
78 return $this->BelongToArray()[$num]; 86 return $this->BelongToArray()[$num];
@@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Model; @@ -7,8 +7,6 @@ use Illuminate\Database\Eloquent\Model;
7 class ServerInformationLog extends Model 7 class ServerInformationLog extends Model
8 { 8 {
9 protected $table = 'gl_server_information_log'; 9 protected $table = 'gl_server_information_log';
10 - const CREATED_AT = 'create_at';  
11 - const UPDATED_AT = 'update_at';  
12 10
13 public function getOriginalAttribute($value) 11 public function getOriginalAttribute($value)
14 { 12 {
@@ -116,6 +116,19 @@ Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上w @@ -116,6 +116,19 @@ Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上w
116 Route::post('/save_follow', [Aside\Task\TaskController::class, 'save_follow'])->name('admin.task_save_follow'); 116 Route::post('/save_follow', [Aside\Task\TaskController::class, 'save_follow'])->name('admin.task_save_follow');
117 }); 117 });
118 118
  119 + // 域名
  120 + Route::prefix('domain')->group(function () {
  121 + Route::get('/', [Aside\Domain\DomainInfoController::class, 'lists'])->name('admin.domain'); // 列表 | 搜索
  122 + Route::get('/info', [Aside\Domain\DomainInfoController::class, 'info'])->name('admin.domain_info'); // 详情
  123 + Route::get('/delete_info', [Aside\Domain\DomainInfoController::class, 'getDeleteDomainInfo'])->name('admin.domain_delete_info'); // 删除信息
  124 + Route::post('/add', [Aside\Domain\DomainInfoController::class, 'add'])->name('admin.domain_save'); // 添加
  125 + Route::post('/edit', [Aside\Domain\DomainInfoController::class, 'edit'])->name('admin.domain_edit'); // 编辑
  126 + Route::any('/delete', [Aside\Domain\DomainInfoController::class, 'delete'])->name('admin.domain_delete'); // 删除
  127 + Route::get('/delete_list', [Aside\Domain\DomainInfoController::class, 'delete_list'])->name('admin.domain_delete_list'); // 删除列表
  128 + Route::any('/restore', [Aside\Domain\DomainInfoController::class, 'restore'])->name('admin.domain_restore'); // 恢复
  129 + Route::get('/log', [Aside\Domain\DomainInfoLogController::class, 'lists'])->name('admin.domain_log_lists'); // 日志
  130 + });
  131 +
119 //运维 132 //运维
120 Route::prefix('devops')->group(function () { 133 Route::prefix('devops')->group(function () {
121 //服务器配置 134 //服务器配置
@@ -137,7 +150,6 @@ Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上w @@ -137,7 +150,6 @@ Route::middleware(['web'])->group(function (){ //admin用渲染默认要加上w
137 Route::get('/restore', [Aside\Devops\ServerInformationController::class, 'restore'])->name('admin.devops.bt_restore'); //恢复数据 150 Route::get('/restore', [Aside\Devops\ServerInformationController::class, 'restore'])->name('admin.devops.bt_restore'); //恢复数据
138 Route::get('/log', [Aside\Devops\ServerInformationLogController::class, 'lists'])->name('admin.devops.bt_log_lists'); //日志列表 151 Route::get('/log', [Aside\Devops\ServerInformationLogController::class, 'lists'])->name('admin.devops.bt_log_lists'); //日志列表
139 }); 152 });
140 -  
141 }); 153 });
142 154
143 // 自定义页面 模板,头部底部 155 // 自定义页面 模板,头部底部