Browse Source

feat: move admin/alive to admin/online

M1Screw 2 years ago
parent
commit
29a0042e23

+ 2 - 2
app/routes.php

@@ -187,9 +187,9 @@ return static function (Slim\App $app): void {
         $group->post('/coupon/{id}/disable', App\Controllers\Admin\CouponController::class . ':disable');
         // 登录日志 & 在线IP
         $group->get('/login', App\Controllers\Admin\IpController::class . ':login');
-        $group->get('/alive', App\Controllers\Admin\IpController::class . ':alive');
+        $group->get('/online', App\Controllers\Admin\IpController::class . ':online');
         $group->post('/login/ajax', App\Controllers\Admin\IpController::class . ':ajaxLogin');
-        $group->post('/alive/ajax', App\Controllers\Admin\IpController::class . ':ajaxAlive');
+        $group->post('/online/ajax', App\Controllers\Admin\IpController::class . ':ajaxOnline');
         // 订阅日志
         $group->get('/subscribe', App\Controllers\Admin\SubscribeLogController::class . ':index');
         $group->post('/subscribe/ajax', App\Controllers\Admin\SubscribeLogController::class . ':ajax');

+ 1 - 1
db/migrations/2023032600-online_log_per_user-ip.php

@@ -48,7 +48,7 @@ return new class() implements MigrationInterface {
 
         $pdo->exec('DROP TABLE alive_ip');
 
-        return 2023032500;
+        return 2023032600;
     }
 
     public function down(): int

+ 2 - 2
resources/views/tabler/admin/log/alive.tpl → resources/views/tabler/admin/log/online.tpl

@@ -43,9 +43,9 @@
             "searching": false,
             "ordering": false,
             ajax: {
-                url: '/admin/alive/ajax',
+                url: '/admin/online/ajax',
                 type: 'POST',
-                dataSrc: 'alives.data'
+                dataSrc: 'onlines'
             },
             "autoWidth":false,
             'iDisplayLength': 10,

+ 1 - 1
resources/views/tabler/admin/tabler_header.tpl

@@ -188,7 +188,7 @@
                                         <i class="ti ti-torii"></i>&nbsp;
                                         支付网关
                                     </a>
-                                    <a class="dropdown-item" href="/admin/alive">
+                                    <a class="dropdown-item" href="/admin/online">
                                         <i class="ti ti-router"></i>&nbsp;
                                         在线IP
                                     </a>

+ 23 - 10
src/Controllers/Admin/IpController.php

@@ -8,6 +8,7 @@ use App\Controllers\BaseController;
 use App\Models\LoginIp;
 use App\Services\DB;
 use App\Utils\Tools;
+use Exception;
 use Psr\Http\Message\ResponseInterface;
 use Slim\Http\Response;
 use Slim\Http\ServerRequest;
@@ -34,26 +35,28 @@ final class IpController extends BaseController
     [
         'field' => [
             'id' => '事件ID',
-            'userid' => '用户ID',
+            'user_id' => '用户ID',
             'user_name' => '用户名',
-            'nodeid' => '节点ID',
+            'node_id' => '节点ID',
             'node_name' => '节点名',
             'ip' => 'IP',
             'location' => 'IP归属地',
             'first_time' => '首次连接',
-            'first_time' => '最后连接',
+            'last_time' => '最后连接',
         ],
     ];
 
     /**
      * 后台登录记录页面
+     *
+     * @throws Exception
      */
     public function login(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
         return $response->write(
             $this->view()
                 ->assign('details', self::$login_details)
-                ->fetch('admin/ip/login.tpl')
+                ->fetch('admin/log/login.tpl')
         );
     }
 
@@ -86,20 +89,22 @@ final class IpController extends BaseController
 
     /**
      * 后台在线 IP 页面
+     *
+     * @throws Exception
      */
-    public function alive(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
+    public function online(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
         return $response->write(
             $this->view()
                 ->assign('details', self::$ip_details)
-                ->fetch('admin/ip/alive.tpl')
+                ->fetch('admin/log/online.tpl')
         );
     }
 
     /**
      * 后台在线 IP 页面 AJAX
      */
-    public function ajaxAlive(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
+    public function ajaxOnline(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
     {
         $data = $request->getParsedBody();
         $length = (int) ($data['length'] ?? 0);
@@ -108,23 +113,31 @@ final class IpController extends BaseController
 
         $logs = DB::select('
             SELECT
+                online_log.id,
+                online_log.user_id,
                 user.user_name,
-                online_log.ip,
+                online_log.node_id,
                 node.name AS node_name,
+                online_log.ip,
                 online_log.first_time,
                 online_log.last_time
             FROM
                 online_log
                 LEFT JOIN user ON user.id = online_log.user_id
                 LEFT JOIN node ON node.id = online_log.node_id
+            WHERE
+                last_time > UNIX_TIMESTAMP() - 90
         ');
 
         $count = count($logs);
         $data = array_map(static function ($val) {
             return [
+                'id' => $val->id,
+                'user_id' => $val->user_id,
                 'user_name' => $val->user_name,
-                'ip' => $val->ip,
+                'node_id' => $val->node_id,
                 'node_name' => $val->node_name,
+                'ip' => str_replace('::ffff:', '', $val->ip),
                 'location' => Tools::getIpLocation($val->ip),
                 'first_time' => Tools::toDateTime($val->first_time),
                 'last_time' => Tools::toDateTime($val->last_time),
@@ -135,7 +148,7 @@ final class IpController extends BaseController
             'draw' => $draw,
             'recordsTotal' => $count,
             'recordsFiltered' => $count,
-            'alives' => $data,
+            'onlines' => $data,
         ]);
     }
 }

+ 4 - 4
src/Controllers/WebAPI/UserController.php

@@ -75,7 +75,7 @@ final class UserController extends BaseController
                         last_time > UNIX_TIMESTAMP() - 90
                     GROUP BY
                         user_id
-                ) ON online_log.user_id = user.id
+                ) AS online_log ON online_log.user_id = user.id
             WHERE
                 user.is_banned = 0
                 AND user.expire_in > CURRENT_TIMESTAMP()
@@ -85,10 +85,10 @@ final class UserController extends BaseController
                         AND IF(? = 0, 1, user.node_group = ?)
                     ) OR user.is_admin = 1
                 )
-        ');
+        ', [$node->node_class, $node->node_group, $node->node_group]);
 
-        $keys_unset = match ($node->sort) {
-            11 || 14 => ['u', 'd', 'transfer_enable', 'method', 'port', 'passwd'],
+        $keys_unset = match ((int) $node->sort) {
+            14, 11 => ['u', 'd', 'transfer_enable', 'method', 'port', 'passwd'],
             default => ['u', 'd', 'transfer_enable', 'uuid']
         };
 

+ 0 - 8
src/Models/User.php

@@ -317,14 +317,6 @@ final class User extends Model
         Link::where('userid', $this->id)->delete();
     }
 
-    /**
-     * 获取用户的订阅链接
-     */
-    public function getSublink(): string
-    {
-        return Tools::generateSSRSubCode($this->id);
-    }
-
     /**
      * 删除用户的邀请码
      */

+ 0 - 88
src/Utils/Tools.php

@@ -265,79 +265,6 @@ final class Tools
         return true;
     }
 
-    /**
-     * Eloquent 分页链接渲染
-     *
-     * @param mixed $data
-     *
-     * @return string
-     */
-    public static function paginateRender(mixed $data): string
-    {
-        $totalPage = $data->lastPage();
-        $currentPage = $data->currentPage();
-        $html = '<ul class="pagination pagination-primary justify-content-end">';
-        for ($i = 1; $i <= $totalPage; $i++) {
-            $active = '<li class="page-item active"><span class="page-link">' . $i . '</span></li>';
-            $page = '<li class="page-item"><a class="page-link" href="' . $data->url($i) . '">' . $i . '</a></li>';
-            if ($i === 1) {
-                // 当前为第一页
-                if ($currentPage === $i) {
-                    $html .= '<li class="page-item disabled"><a class="page-link">上一页</a></li>';
-                    $html .= $active;
-                    if ($i === $totalPage) {
-                        $html .= '<li class="page-item disabled"><a class="page-link">下一页</a></li>';
-                        continue;
-                    }
-                } else {
-                    $html .= '<li class="page-item"><a class="page-link" href="' . $data->url($currentPage - 1) . '">上一页</a></li>';
-                    if ($currentPage > 4) {
-                        $html .= '<li class="page-item disabled"><a class="page-link">...</a></li>';
-                    } else {
-                        $html .= $page;
-                    }
-                }
-            }
-            if ($i === $totalPage) {
-                // 当前为最后一页
-                if ($currentPage === $i) {
-                    $html .= $active;
-                    $html .= '<li class="page-item disabled"><a class="page-link">下一页</a></li>';
-                } else {
-                    if ($totalPage - $currentPage > 3) {
-                        $html .= '<li class="page-item disabled"><a class="page-link">...</a></li>';
-                    } else {
-                        $html .= $page;
-                    }
-                    $html .= '<li class="page-item"><a class="page-link" href="' . $data->url($currentPage + 1) . '">下一页</a></li>';
-                }
-            }
-            if ($i > 1 && $i < $totalPage) {
-                // 其他页
-                if ($currentPage === $i) {
-                    $html .= $active;
-                } else {
-                    if ($totalPage > 10) {
-                        if (
-                            ($currentPage > 4 && $i < $currentPage && $i > $currentPage - 3)
-                            ||
-                            ($totalPage - $currentPage > 4 && $i > $currentPage && $i < $currentPage + 4)
-                            ||
-                            ($currentPage <= 4 && $i <= 4)
-                            ||
-                            ($totalPage - $currentPage <= 4 && $i > $currentPage)
-                        ) {
-                            $html .= $page;
-                        }
-                        continue;
-                    }
-                    $html .= $page;
-                }
-            }
-        }
-        return $html . '</ul>';
-    }
-
     public static function genSubToken(): string
     {
         for ($i = 0; $i < 10; $i++) {
@@ -351,21 +278,6 @@ final class Tools
         return "couldn't alloc token";
     }
 
-    public static function generateSSRSubCode(int $userid): string
-    {
-        $Elink = Link::where('userid', $userid)->first();
-        if ($Elink !== null) {
-            return $Elink->token;
-        }
-
-        $NLink = new Link();
-        $NLink->userid = $userid;
-        $NLink->token = self::genSubToken();
-        $NLink->save();
-
-        return $NLink->token;
-    }
-
     public static function searchEnvName($name): int|string|null
     {
         global $_ENV;