1
0

DetectBanController.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\Admin;
  4. use App\Controllers\BaseController;
  5. use App\Models\DetectBanLog;
  6. use App\Utils\Tools;
  7. use Exception;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Slim\Http\Response;
  10. use Slim\Http\ServerRequest;
  11. final class DetectBanController extends BaseController
  12. {
  13. private static array $details =
  14. [
  15. 'field' => [
  16. 'id' => '事件ID',
  17. 'user_id' => '用户ID',
  18. 'user_name' => '用户名',
  19. 'detect_number' => '违规次数',
  20. 'ban_time' => '封禁时长(分钟)',
  21. 'start_time' => '统计开始时间',
  22. 'end_time' => '统计结束&封禁开始时间',
  23. 'ban_end_time' => '封禁结束时间',
  24. 'all_detect_number' => '累计违规次数',
  25. ],
  26. ];
  27. /**
  28. * @throws Exception
  29. */
  30. public function index(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  31. {
  32. return $response->write(
  33. $this->view()
  34. ->assign('details', self::$details)
  35. ->fetch('admin/log/detect_ban.tpl')
  36. );
  37. }
  38. public function ajax(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  39. {
  40. $length = $request->getParam('length');
  41. $page = $request->getParam('start') / $length + 1;
  42. $draw = $request->getParam('draw');
  43. $bans = (new DetectBanLog())->orderBy('id', 'desc')->paginate($length, '*', '', $page);
  44. $total = (new DetectBanLog())->count();
  45. foreach ($bans as $ban) {
  46. $ban->user_name = $ban->userName();
  47. $ban->start_time = Tools::toDateTime((int) $ban->start_time);
  48. $ban->end_time = Tools::toDateTime((int) $ban->end_time);
  49. $ban->ban_end_time = $ban->banEndTime();
  50. }
  51. return $response->withJson([
  52. 'draw' => $draw,
  53. 'recordsTotal' => $total,
  54. 'recordsFiltered' => $total,
  55. 'bans' => $bans,
  56. ]);
  57. }
  58. }