瀏覽代碼

feat: drop deprecated detect_ban_log values

M1Screw 1 年之前
父節點
當前提交
b1533c5d25

+ 0 - 2
db/migrations/2023020100-init.php

@@ -30,9 +30,7 @@ return new class() implements MigrationInterface {
 
             CREATE TABLE `detect_ban_log` (
                 `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '封禁记录ID',
-                `user_name` varchar(255) NOT NULL DEFAULT '' COMMENT '用户名',
                 `user_id` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT '用户ID',
-                `email` varchar(255) NOT NULL DEFAULT '' COMMENT '用户邮箱',
                 `detect_number` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '本次违规次数',
                 `ban_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '封禁时长',
                 `start_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '封禁开始时间',

+ 0 - 2
db/migrations/2023061800-update_new_shop_data_type.php

@@ -14,9 +14,7 @@ return new class() implements MigrationInterface {
         ALTER TABLE announcement MODIFY COLUMN `content` text NOT NULL DEFAULT '' COMMENT '公告内容';
         ALTER TABLE config MODIFY COLUMN `value` varchar(2048) DEFAULT NULL COMMENT '值';
         ALTER TABLE detect_ban_log MODIFY COLUMN `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '记录ID';
-        ALTER TABLE detect_ban_log MODIFY COLUMN `user_name` varchar(255) NOT NULL DEFAULT '' COMMENT '用户名';
         ALTER TABLE detect_ban_log MODIFY COLUMN `user_id` bigint(20) unsigned NOT NULL DEFAULT 0 COMMENT '用户ID';
-        ALTER TABLE detect_ban_log MODIFY COLUMN `email` varchar(255) NOT NULL DEFAULT '' COMMENT '用户邮箱';
         ALTER TABLE detect_ban_log MODIFY COLUMN `detect_number` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '本次违规次数';
         ALTER TABLE detect_ban_log MODIFY COLUMN `ban_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '本次封禁时长';
         ALTER TABLE detect_ban_log MODIFY COLUMN `start_time` int(11) unsigned NOT NULL DEFAULT 0 COMMENT '统计开始时间';

+ 28 - 0
db/migrations/2023111801-remove_detect_ban_log_user_info.php

@@ -0,0 +1,28 @@
+<?php
+
+declare(strict_types=1);
+
+use App\Interfaces\MigrationInterface;
+use App\Services\DB;
+
+return new class() implements MigrationInterface {
+    public function up(): int
+    {
+        DB::getPdo()->exec('
+            ALTER TABLE detect_ban_log DROP COLUMN IF EXISTS `user_name`;
+            ALTER TABLE detect_ban_log DROP COLUMN IF EXISTS `email`;
+        ');
+
+        return 2023111801;
+    }
+
+    public function down(): int
+    {
+        DB::getPdo()->exec("
+            ALTER TABLE detect_ban_log ADD COLUMN IF NOT EXISTS `user_name` varchar(255) NOT NULL DEFAULT '' COMMENT '用户名';
+            ALTER TABLE detect_ban_log ADD COLUMN IF NOT EXISTS `email` varchar(255) NOT NULL DEFAULT '' COMMENT '用户邮箱';
+        ");
+
+        return 2023111800;
+    }
+};

+ 2 - 2
src/Controllers/Admin/DetectBanController.php

@@ -18,9 +18,8 @@ final class DetectBanController extends BaseController
         [
             'field' => [
                 'id' => '事件ID',
-                'user_name' => '用户名',
                 'user_id' => '用户ID',
-                'email' => '用户邮箱',
+                'user_name' => '用户名',
                 'detect_number' => '违规次数',
                 'ban_time' => '封禁时长(分钟)',
                 'start_time' => '统计开始时间',
@@ -52,6 +51,7 @@ final class DetectBanController extends BaseController
         $total = DetectBanLog::count();
 
         foreach ($bans as $ban) {
+            $ban->user_name = $ban->userName();
             $ban->start_time = Tools::toDateTime((int) $ban->start_time);
             $ban->end_time = Tools::toDateTime((int) $ban->end_time);
             $ban->ban_end_time = $ban->banEndTime();

+ 8 - 4
src/Models/DetectBanLog.php

@@ -9,11 +9,7 @@ use Illuminate\Database\Query\Builder;
 
 /**
  * @property int    $id                封禁记录ID
- * todo: delete this
- * @property string $user_name         用户名
  * @property int    $user_id           用户ID
- * todo: delete this as well
- * @property string $email             用户邮箱
  * @property int    $detect_number     本次违规次数
  * @property int    $ban_time          封禁时长
  * @property int    $start_time        封禁开始时间
@@ -35,6 +31,14 @@ final class DetectBanLog extends Model
         return User::find($this->user_id);
     }
 
+    /**
+     * 用户名
+     */
+    public function userName(): string
+    {
+        return $this->user() === null ? '用户不存在' : $this->user()->user_name;
+    }
+
     /**
      * 封禁结束时间
      */

+ 0 - 2
src/Services/Detect.php

@@ -145,9 +145,7 @@ final class Detect
                 $user->last_detect_ban_time = Tools::toDateTime(time());
                 $user->save();
                 $DetectBanLog = new DetectBanLog();
-                $DetectBanLog->user_name = $user->user_name;
                 $DetectBanLog->user_id = $user->id;
-                $DetectBanLog->email = $user->email;
                 $DetectBanLog->detect_number = $detect_number;
                 $DetectBanLog->ban_time = $_ENV['auto_detect_ban_time'];
                 $DetectBanLog->start_time = strtotime($last_detect_ban_time);