Explorar o código

修复Validation使用不规范问题

- 移除不存在的ValidationMessage trait的使用
- 将throwMessage方法替换为addError方法
- 改进错误处理逻辑,使用规范的addError方式
- 添加详细的中文注释和文档
- 修复的文件:
  * UCore/Validator/ModelRelatedValidator.php
  * UCore/Validator/DataValidation.php
  * UCore/Validator/ForeachValidation.php
  * UCore/Validator/ReverseValidator.php
  * UCore/Validator/ReverseValidatorDataValidator.php
  * UCore/Validator/ModelUnidValidator.php
notfff hai 7 meses
pai
achega
396f62f473

+ 3 - 2
AiWork/WORK.md

@@ -10,8 +10,9 @@
 本系统没有任何Api,你写的Api是干嘛的;
 shop_items 的 $max_buy  最大购买数量(0表示无限制)是否已经被完全替代,期望是被新的限购替代;
 shop_items 的 $max_buy 确认被替代后移除,使用mcp执行sql
-请求  request_1749197896922 报错,看日志,修复,使用下面命令进行验证
- php artisan debug:reproduce-error 6846ac9e1110f
+
+请求  68479bba328da 报错,看日志,修复,使用下面命令进行验证
+ php artisan debug:reproduce-error 68479bba328da
 请求  request_1749207804951 
 
 ## 待处理任务

+ 1 - 20
AiWork/WORK2.md

@@ -2,24 +2,5 @@
 1. 目前代码中没有 is_random /random_count 的使用,代表该功能未实现 (查实,无需修改)
 2. 这个和奖励项中的数量有什么关系
 
-后台 奖励组管理 列表
-http://kku_laravel.local.gd/admin/game-reward-groups
-增加行操作:单次随即奖励 / 10次随即奖励 / 100次随即奖励 按钮;单次直接提示,多次使用弹窗表格展示每次的奖励内容(异步渲染)
 
-
-宠物,petuser表增加软删除
-
-
-消耗钻石 5(币种消耗:2,消耗组:16,来源:shop_buy,ID:7) ;这是错误的,这对用户来说有很大的理解难度,正常应该是“购买 xx 消耗 5 钻石”
-
-
-后台 物品分解规则管理 列表;
-后台 合成配方管理 列表
-消耗组	奖励组	条件组 ,不仅显示名字,还要显示内容,还要可点击;
-参考 宝箱配置 列表 
-
-
-ConsumeService 增加 获取消耗组;返回 Dto
-ConditionService 增加 获取条件组;返回 Dto
-RewardService 添加 获取奖励组;返回 Dto
-增加 消耗组Dto转Protobuf 对象 Deduct 、奖励组Dto转 Protobuf 对象 Reward
+作物成熟期,产出固定数

+ 18 - 25
UCore/Validator/DataValidation.php

@@ -5,45 +5,38 @@ namespace UCore\Validator;
 use UCore\ValidationCore;
 use UCore\Validator;
 
-
 /**
- * 整个数据,使用Validation
+ * 数据验证器
  *
+ * 使用指定的Validation类验证整个数据
  */
 class DataValidation extends Validator
 {
-
-// args[0] 验证器
-// args[1] 索引
-    use ValidationMessage;
-
+    /**
+     * 验证数据
+     *
+     * args[0] 验证器类名
+     *
+     * @param mixed $values 要验证的值
+     * @param array $data 所有验证数据
+     * @return bool 验证是否通过
+     */
     public function validate(mixed $values, array $data): bool
     {
-        $vaClass = $this->args[0];
-
-        // "数值{index}{value}没有通过验证,消息 {message}"
-        $message = $this->message ?? "";
-//        dd($values);
-
+        $validationClass = $this->args[0];
 
         /**
-         * @var ValidationCore $va
+         * @var ValidationCore $validation
          */
-        $va = new $vaClass($data);
-        $va->validate();
-        if ($va->isFail()) {
-            $p = [
-                '{message}' => $va->firstError()
-            ];
-
-            $this->throwMessage($p, $message);
+        $validation = new $validationClass($data);
+        $validation->validate();
 
+        if ($validation->isFail()) {
+            $errorMessage = $validation->firstError();
+            $this->addError("数据验证失败: {$errorMessage}");
             return false;
         }
 
-
         return true;
-
     }
-
 }

+ 26 - 31
UCore/Validator/ForeachValidation.php

@@ -5,55 +5,50 @@ namespace UCore\Validator;
 use UCore\ValidationCore;
 use UCore\Validator;
 
-
 /**
- * 遍历数组,使用Validation
+ * 数组遍历验证器
  *
+ * 遍历数组中的每个元素,使用指定的Validation类进行验证
  */
 class ForeachValidation extends Validator
 {
-
-// args[0] 验证器
-// args[1] 索引
-    use ValidationMessage;
-
+    /**
+     * 验证数组中的每个元素
+     *
+     * args[0] 验证器类名
+     * args[1] 索引字段名(可选)
+     *
+     * @param mixed $values 要验证的数组
+     * @param array $data 所有验证数据
+     * @return bool 验证是否通过
+     */
     public function validate(mixed $values, array $data): bool
     {
-        $vaClass = $this->args[0];
-        $index   = $this->args[1]??'';
-        // "数值{index}{value}没有通过验证,消息 {message}"
-        $message = $this->message ?? "";
-//        dd($values);
-        foreach ($values as $k =>$value) {
-            if ($index) {
-                $data2 = [
-                    $index => $value
+        $validationClass = $this->args[0];
+        $indexField = $this->args[1] ?? '';
+
+        foreach ($values as $index => $value) {
+            if ($indexField) {
+                $validationData = [
+                    $indexField => $value
                 ];
             } else {
-                $data2 = $value;
+                $validationData = $value;
             }
 
             /**
-             * @var ValidationCore $va
+             * @var ValidationCore $validation
              */
-            $va = new $vaClass($data2);
-            $va->validate();
-            if ($va->isFail()) {
-                $p = [
-                    '{index}'   => $k,
-                    '{value}'   => $value,
-                    '{message}' => $va->firstError()
-                ];
-
-                $this->throwMessage($p, $message);
+            $validation = new $validationClass($validationData);
+            $validation->validate();
 
+            if ($validation->isFail()) {
+                $errorMessage = $validation->firstError();
+                $this->addError("索引 {$index} 的数据验证失败: {$errorMessage}");
                 return false;
             }
-
         }
 
         return true;
-
     }
-
 }

+ 20 - 16
UCore/Validator/ModelRelatedValidator.php

@@ -3,35 +3,39 @@
 namespace UCore\Validator;
 
 use UCore\Validator;
-use App\Models\Hp\User;
-use Inhere\Validate\ValidationTrait;
 
+/**
+ * 模型关联验证器
+ *
+ * 用于验证指定值在模型中是否存在关联记录
+ */
 class ModelRelatedValidator extends Validator
 {
-    use ValidationMessage;
-
+    /**
+     * 验证模型关联
+     *
+     * @param mixed $value 要验证的值
+     * @param array $data 所有验证数据
+     * @return bool 验证是否通过
+     */
     public function validate(mixed $value, array $data): bool
     {
-
         $list = $this->args;
 
-        foreach ($list as $modelClass => $field){
+        foreach ($list as $modelClass => $field) {
             /**
              * @var \Illuminate\Database\Eloquent\Builder $query
              */
             $query = $modelClass::query();
-            $re = $query->where($field,'=',$value)->first();
-//            dd($field,$modelClass,$value);
-            if($re){
-                $p = [
-                    'value'=>$value,
-                    'message'=>$modelClass::$NAME
-                ];
-                $this->throwMessage($p,$this->message);
+            $record = $query->where($field, '=', $value)->first();
+
+            if ($record) {
+                $modelName = $modelClass::$NAME ?? $modelClass;
+                $this->addError("值 {$value} 在 {$modelName} 中已存在关联记录");
                 return false;
             }
         }
-        return  true;
-    }
 
+        return true;
+    }
 }

+ 15 - 9
UCore/Validator/ModelUnidValidator.php

@@ -2,30 +2,36 @@
 
 namespace UCore\Validator;
 
-
-
 use UCore\Validator;
 
 /**
- * 使用模型进行唯一验证,不存在true
+ * 模型唯一性验证器
  *
+ * 验证指定字段的值在模型中是否唯一(不存在则验证通过)
  */
 class ModelUnidValidator extends Validator
 {
-
-    use ValidationMessage;
-
+    /**
+     * 验证模型字段唯一性
+     *
+     * @param mixed $value 要验证的值
+     * @param array $data 所有验证数据
+     * @return bool 验证是否通过
+     */
     public function validate(mixed $value, array $data): bool
     {
         $modelClass = $this->args[0];
-        $filed = $this->args[1];
+        $field = $this->args[1];
+
         $model = $modelClass::query()->where([
-            $filed => $value
+            $field => $value
         ])->first();
+
         if ($model) {
+            $this->addError("值 '{$value}' 在字段 '{$field}' 中已存在,不满足唯一性要求");
             return false;
         }
+
         return true;
     }
-
 }

+ 20 - 16
UCore/Validator/ReverseValidator.php

@@ -2,35 +2,39 @@
 
 namespace UCore\Validator;
 
-
-
 use UCore\Validator;
 
 /**
- * 使用验证器 验证,反向
+ * 反向验证器
  *
+ * 使用指定的验证器进行验证,但返回相反的结果
  */
 class ReverseValidator extends Validator
 {
-
-    use ValidationMessage;
-
+    /**
+     * 反向验证
+     *
+     * @param mixed $value 要验证的值
+     * @param array $data 所有验证数据
+     * @return bool 验证是否通过(与原验证器结果相反)
+     */
     public function validate(mixed $value, array $data): bool
     {
-        $vaClass = $this->args;
-        $data = [
-            $value
-        ];
+        $validatorClass = $this->args[0];
+        $validationData = [$value];
+
         /**
-         * @var Validator $va
+         * @var Validator $validator
          */
-        $va  = new $vaClass($this->validation);
-        $res = $va->validate($value, $data);
-        if ($res) {
+        $validator = new $validatorClass($this->validation);
+        $result = $validator->validate($value, $validationData);
+
+        // 返回相反的结果
+        if ($result) {
+            $this->addError("反向验证失败:原验证器验证通过");
             return false;
         }
-        return true;
 
+        return true;
     }
-
 }

+ 18 - 15
UCore/Validator/ReverseValidatorDataValidator.php

@@ -2,35 +2,38 @@
 
 namespace UCore\Validator;
 
-
-
-
-
 use UCore\Validator;
 
 /**
- * 使用验证器 验证,反向 (data全部使用的)
+ * 反向数据验证器
  *
+ * 使用指定的验证器进行验证,但返回相反的结果(使用完整的数据)
  */
 class ReverseValidatorDataValidator extends Validator
 {
-
-    use ValidationMessage;
-
+    /**
+     * 反向验证(使用完整数据)
+     *
+     * @param mixed $value 要验证的值
+     * @param array $data 所有验证数据
+     * @return bool 验证是否通过(与原验证器结果相反)
+     */
     public function validate(mixed $value, array $data): bool
     {
-        $vaClass = $this->args;
+        $validatorClass = $this->args[0];
 
         /**
-         * @var Validator $va
+         * @var Validator $validator
          */
-        $va  = new $vaClass($this->validation);
-        $res = $va->validate($value, $data);
-        if ($res) {
+        $validator = new $validatorClass($this->validation);
+        $result = $validator->validate($value, $data);
+
+        // 返回相反的结果
+        if ($result) {
+            $this->addError("反向验证失败:原验证器验证通过");
             return false;
         }
-        return true;
 
+        return true;
     }
-
 }