This commit is contained in:
root 2019-12-29 13:21:36 +08:00
parent 35c71be4a9
commit a3f59aac0a
6 changed files with 67 additions and 32 deletions

View File

@ -21,7 +21,11 @@ class ServerController extends Controller
$server[$i]['tags'] = json_decode($server[$i]['tags']); $server[$i]['tags'] = json_decode($server[$i]['tags']);
} }
$server[$i]['group_id'] = json_decode($server[$i]['group_id']); $server[$i]['group_id'] = json_decode($server[$i]['group_id']);
$server[$i]['last_check_at'] = Redis::get('server_last_check_at_' . $server[$i]['id']); if ($server[$i]['parent_id']) {
Redis::get('server_last_check_at_' . $server[$i]['parent_id']);
} else {
$server[$i]['last_check_at'] = Redis::get('server_last_check_at_' . $server[$i]['id']);
}
} }
return response([ return response([
'data' => $server 'data' => $server
@ -29,31 +33,51 @@ class ServerController extends Controller
} }
public function save (ServerSave $request) { public function save (ServerSave $request) {
if ($request->input('id')) { $params = $request->only([
$server = Server::find($request->input('id')); 'show',
if (!$server) { 'group_id',
abort(500, '服务器不存在'); 'parent_id',
} 'name',
} else { 'host',
$server = new Server(); 'port',
'server_port',
'tls',
'tags',
'rate',
'network',
'settings'
]);
$params['group_id'] = json_encode($params['group_id']);
if (isset($params['tags'])) {
$params['tags'] = json_encode($params['tags']);
} }
$server->group_id = json_encode($request->input('group_id'));
$server->name = $request->input('name'); if (isset($params['settings'])) {
$server->host = $request->input('host'); if (!is_object(json_decode($params['settings']))) {
$server->port = $request->input('port');
$server->server_port = $request->input('server_port');
$server->tls = $request->input('tls');
$server->tags = $request->input('tags') ? json_encode($request->input('tags')) : NULL;
$server->rate = $request->input('rate');
$server->network = $request->input('network');
if ($request->input('settings')) {
if (!is_object(json_decode($request->input('settings')))) {
abort(500, '传输协议配置格式不正确'); abort(500, '传输协议配置格式不正确');
} }
$server->settings = $request->input('settings'); $server->settings = $params['settings'];
} }
if ($request->input('id')) {
$server = Server::find($request->input('id'));
if (!$server) {
abort(500, '服务器不存在');
}
if (!$server->update($params)) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
if (!Server::create($params)) {
abort(500, '创建失败');
}
return response([ return response([
'data' => $server->save() 'data' => true
]); ]);
} }
@ -125,15 +149,16 @@ class ServerController extends Controller
} }
public function update (ServerUpdate $request) { public function update (ServerUpdate $request) {
$updateData = $request->only([ $params = $request->only([
'show', 'show',
]); ]);
$server = Server::find($request->input('id')); $server = Server::find($request->input('id'));
if (!$server) { if (!$server) {
abort(500, '该服务器不存在'); abort(500, '该服务器不存在');
} }
if (!$server->update($updateData)) { if (!$server->update($params)) {
abort(500, '保存失败'); abort(500, '保存失败');
} }

View File

@ -28,7 +28,11 @@ class ServerController extends Controller {
} }
for ($i = 0; $i < count($server); $i++) { for ($i = 0; $i < count($server); $i++) {
$server[$i]['link'] = Helper::buildVmessLink($server[$i], $user); $server[$i]['link'] = Helper::buildVmessLink($server[$i], $user);
$server[$i]['last_check_at'] = Redis::get('server_last_check_at_' . $server[$i]['id']); if ($server[$i]['parent_id']) {
Redis::get('server_last_check_at_' . $server[$i]['parent_id']);
} else {
$server[$i]['last_check_at'] = Redis::get('server_last_check_at_' . $server[$i]['id']);
}
} }
return response([ return response([
'data' => $server 'data' => $server

View File

@ -16,6 +16,7 @@ class ServerSave extends FormRequest
return [ return [
'name' => 'required', 'name' => 'required',
'group_id' => 'required|array', 'group_id' => 'required|array',
'parent_id' => 'integer',
'host' => 'required', 'host' => 'required',
'port' => 'required', 'port' => 'required',
'server_port' => 'required', 'server_port' => 'required',
@ -32,6 +33,7 @@ class ServerSave extends FormRequest
'name.required' => '节点名称不能为空', 'name.required' => '节点名称不能为空',
'group_id.required' => '权限组不能为空', 'group_id.required' => '权限组不能为空',
'group_id.array' => '权限组格式不正确', 'group_id.array' => '权限组格式不正确',
'parent_id.integer' => '父ID格式不正确',
'host.required' => '节点地址不能为空', 'host.required' => '节点地址不能为空',
'port.required' => '连接端口不能为空', 'port.required' => '连接端口不能为空',
'server_port.required' => '后端服务端口不能为空', 'server_port.required' => '后端服务端口不能为空',

View File

@ -58,10 +58,10 @@ CREATE TABLE `v2_plan` (
`show` tinyint(1) NOT NULL DEFAULT '0', `show` tinyint(1) NOT NULL DEFAULT '0',
`renew` tinyint(1) NOT NULL DEFAULT '1', `renew` tinyint(1) NOT NULL DEFAULT '1',
`content` text, `content` text,
`month_price` int(11) NOT NULL DEFAULT '0', `month_price` int(11) DEFAULT '0',
`quarter_price` int(11) NOT NULL DEFAULT '0', `quarter_price` int(11) DEFAULT '0',
`half_year_price` int(11) NOT NULL DEFAULT '0', `half_year_price` int(11) DEFAULT '0',
`year_price` int(11) NOT NULL DEFAULT '0', `year_price` int(11) DEFAULT '0',
`created_at` int(11) NOT NULL, `created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL, `updated_at` int(11) NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
@ -74,6 +74,7 @@ DROP TABLE IF EXISTS `v2_server`;
CREATE TABLE `v2_server` ( CREATE TABLE `v2_server` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`group_id` varchar(255) NOT NULL, `group_id` varchar(255) NOT NULL,
`parent_id` int(11) DEFAULT NULL,
`name` varchar(255) CHARACTER SET utf8mb4 NOT NULL, `name` varchar(255) CHARACTER SET utf8mb4 NOT NULL,
`host` varchar(255) NOT NULL, `host` varchar(255) NOT NULL,
`port` int(11) NOT NULL, `port` int(11) NOT NULL,
@ -172,4 +173,4 @@ CREATE TABLE `v2_user` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 2019-12-27 07:14:40 -- 2019-12-29 04:58:01

File diff suppressed because one or more lines are too long

View File

@ -77,3 +77,6 @@ CHANGE `month_price` `month_price` int(11) NULL DEFAULT '0' AFTER `content`,
CHANGE `quarter_price` `quarter_price` int(11) NULL DEFAULT '0' AFTER `month_price`, CHANGE `quarter_price` `quarter_price` int(11) NULL DEFAULT '0' AFTER `month_price`,
CHANGE `half_year_price` `half_year_price` int(11) NULL DEFAULT '0' AFTER `quarter_price`, CHANGE `half_year_price` `half_year_price` int(11) NULL DEFAULT '0' AFTER `quarter_price`,
CHANGE `year_price` `year_price` int(11) NULL DEFAULT '0' AFTER `half_year_price`; CHANGE `year_price` `year_price` int(11) NULL DEFAULT '0' AFTER `half_year_price`;
ALTER TABLE `v2_server`
ADD `parent_id` int(11) NULL AFTER `group_id`;