| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- declare(strict_types=1);
- namespace App\Controllers\Admin;
- use App\Controllers\BaseController;
- use App\Models\OnlineLog;
- use App\Utils\Tools;
- use Exception;
- use MaxMind\Db\Reader\InvalidDatabaseException;
- use Psr\Http\Message\ResponseInterface;
- use Slim\Http\Response;
- use Slim\Http\ServerRequest;
- use function time;
- final class OnlineLogController extends BaseController
- {
- private static array $details =
- [
- 'field' => [
- 'id' => '事件ID',
- 'user_id' => '用户ID',
- 'user_name' => '用户名',
- 'node_id' => '节点ID',
- 'node_name' => '节点名',
- 'ip' => 'IP',
- 'location' => 'IP归属地',
- 'first_time' => '首次连接',
- 'last_time' => '最后连接',
- ],
- ];
- /**
- * 后台在线 IP 页面
- *
- * @throws Exception
- */
- public function index(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
- {
- return $response->write(
- $this->view()
- ->assign('details', self::$details)
- ->fetch('admin/log/online.tpl')
- );
- }
- /**
- * 后台在线 IP 页面 AJAX
- *
- * @throws InvalidDatabaseException
- */
- public function ajax(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
- {
- $length = $request->getParam('length');
- $page = $request->getParam('start') / $length + 1;
- $draw = $request->getParam('draw');
- $onlines = OnlineLog::where('last_time', '>', time() - 90)->orderByDesc('last_time')->paginate($length, '*', '', $page);
- $total = OnlineLog::where('last_time', '>', time() - 90)->count();
- foreach ($onlines as $online) {
- $online->user_name = $online->userName();
- $online->node_name = $online->nodeName();
- $online->ip = $online->ip();
- $online->location = Tools::getIpLocation($online->ip);
- $online->first_time = Tools::toDateTime($online->first_time);
- $online->last_time = Tools::toDateTime($online->last_time);
- }
- return $response->withJson([
- 'draw' => $draw,
- 'recordsTotal' => $total,
- 'recordsFiltered' => $total,
- 'onlines' => $onlines,
- ]);
- }
- }
|