v2board/app/Http/Controllers/User/ServerController.php

72 lines
2.4 KiB
PHP
Raw Normal View History

2019-12-22 23:59:27 +08:00
<?php
2020-01-29 16:08:50 +08:00
namespace App\Http\Controllers\User;
2019-12-22 23:59:27 +08:00
2020-01-29 16:08:50 +08:00
use App\Http\Controllers\Controller;
2020-02-28 01:12:55 +08:00
use App\Services\UserService;
2019-12-22 23:59:27 +08:00
use Illuminate\Http\Request;
2020-01-12 01:27:36 +08:00
use Illuminate\Support\Facades\Cache;
2019-12-22 23:59:27 +08:00
use App\Models\Server;
use App\Models\ServerLog;
2019-12-23 15:26:08 +08:00
use App\Models\User;
use App\Utils\Helper;
2019-12-22 23:59:27 +08:00
2020-01-11 13:36:52 +08:00
class ServerController extends Controller
{
public function fetch(Request $request)
{
2019-12-23 15:38:50 +08:00
$user = User::find($request->session()->get('id'));
$server = [];
2020-02-28 17:16:05 +08:00
$userService = new UserService();
if ($userService->isAvailable($user)) {
2019-12-23 15:38:50 +08:00
$servers = Server::where('show', 1)
2020-04-21 00:22:05 +08:00
->orderBy('sort', 'ASC')
2019-12-23 15:38:50 +08:00
->get();
foreach ($servers as $item) {
$groupId = json_decode($item['group_id']);
if (in_array($user->group_id, $groupId)) {
array_push($server, $item);
}
}
}
for ($i = 0; $i < count($server); $i++) {
$server[$i]['link'] = Helper::buildVmessLink($server[$i], $user);
2019-12-29 13:21:36 +08:00
if ($server[$i]['parent_id']) {
$server[$i]['last_check_at'] = Cache::get('server_last_check_at_' . $server[$i]['parent_id']);
2019-12-29 13:21:36 +08:00
} else {
$server[$i]['last_check_at'] = Cache::get('server_last_check_at_' . $server[$i]['id']);
2019-12-29 13:21:36 +08:00
}
2019-12-23 15:38:50 +08:00
}
return response([
'data' => $server
]);
}
2020-01-11 13:36:52 +08:00
public function logFetch(Request $request)
{
$type = $request->input('type') ? $request->input('type') : 0;
2019-12-23 01:06:15 +08:00
$current = $request->input('current') ? $request->input('current') : 1;
$pageSize = $request->input('pageSize') >= 10 ? $request->input('pageSize') : 10;
$serverLogModel = ServerLog::where('user_id', $request->session()->get('id'))
->orderBy('created_at', 'DESC');
2020-01-11 13:36:52 +08:00
switch ($type) {
case 0:
$serverLogModel->where('created_at', '>=', strtotime(date('Y-m-d')));
break;
case 1:
$serverLogModel->where('created_at', '>=', strtotime(date('Y-m-d')) - 604800);
break;
case 2:
$serverLogModel->where('created_at', '>=', strtotime(date('Y-m-1')));
}
2019-12-23 01:06:15 +08:00
$total = $serverLogModel->count();
$res = $serverLogModel->forPage($current, $pageSize)
->get();
return response([
'data' => $res,
2020-04-26 19:19:31 +08:00
'total' => $total
2019-12-23 01:06:15 +08:00
]);
2019-12-22 23:59:27 +08:00
}
2020-01-11 13:36:52 +08:00
}