浏览代码

refactor(pet): 重构宠物验证器

- 移除了所有宠物验证器中的 ValidationMessage 特性
- 将 throwMessage 方法替换为 addError 方法
-优化了错误消息的处理方式,不再使用占位符替换
- 统一了异常处理逻辑
Your Name 8 月之前
父节点
当前提交
249326eb29

+ 11 - 13
app/Module/Pet/Validators/PetCreateValidator.php

@@ -4,20 +4,18 @@ namespace App\Module\Pet\Validators;
 
 use App\Module\Pet\Models\PetUser;
 use UCore\Validator;
-use UCore\Validator\ValidationMessage;
 
 /**
  * 宠物创建验证器
- * 
+ *
  * 用于验证宠物创建参数的合法性
  */
 class PetCreateValidator extends Validator
 {
-    use ValidationMessage;
-    
+
     /**
      * 验证方法
-     * 
+     *
      * @param mixed $value 宠物名称
      * @param array $data 所有数据
      * @return bool 验证是否通过
@@ -26,27 +24,27 @@ class PetCreateValidator extends Validator
     {
         $name = $value;
         $userId = $data['userId'] ?? 0;
-        
+
         // 验证宠物名称
         if (empty($name)) {
-            $this->throwMessage([], '宠物名称不能为空');
+            $this->addError('宠物名称不能为空');
             return false;
         }
-        
+
         if (mb_strlen($name) > 20) {
-            $this->throwMessage([], '宠物名称不能超过20个字符');
+            $this->addError('宠物名称不能超过20个字符');
             return false;
         }
-        
+
         // 检查用户宠物数量限制
         $petCount = PetUser::where('user_id', $userId)->count();
         $maxPets = config('pet.max_pets_per_user', 3);
-        
+
         if ($petCount >= $maxPets) {
-            $this->throwMessage(['max' => $maxPets], "已达到最大宠物数量限制: {max}");
+            $this->addError("已达到最大宠物数量限制: " . $maxPets);
             return false;
         }
-        
+
         return true;
     }
 }

+ 0 - 2
app/Module/Pet/Validators/PetFoodValidator.php

@@ -6,7 +6,6 @@ use App\Module\GameItems\Services\ItemService;
 use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\Log;
 use UCore\Validator;
-use UCore\Validator\ValidationMessage;
 
 /**
  * 宠物口粮验证器
@@ -16,7 +15,6 @@ use UCore\Validator\ValidationMessage;
  */
 class PetFoodValidator extends Validator
 {
-    use ValidationMessage;
     /**
      * 缓存键前缀
      *

+ 16 - 21
app/Module/Pet/Validators/PetLevelUpValidator.php

@@ -6,20 +6,18 @@ use App\Module\Pet\Enums\PetStatus;
 use App\Module\Pet\Models\PetLevelConfig;
 use App\Module\Pet\Models\PetUser;
 use UCore\Validator;
-use UCore\Validator\ValidationMessage;
 
 /**
  * 宠物升级验证器
- * 
+ *
  * 用于验证宠物升级条件的合法性
  */
 class PetLevelUpValidator extends Validator
 {
-    use ValidationMessage;
-    
+
     /**
      * 验证方法
-     * 
+     *
      * @param mixed $value 宠物ID
      * @param array $data 所有数据
      * @return bool 验证是否通过
@@ -27,48 +25,45 @@ class PetLevelUpValidator extends Validator
     public function validate(mixed $value, array $data): bool
     {
         $petId = $value;
-        
+
         try {
             // 获取宠物信息
             $pet = PetUser::find($petId);
-            
+
             if (!$pet) {
-                $this->throwMessage([], '宠物不存在');
+                $this->addError('宠物不存在');
                 return false;
             }
-            
+
             // 检查宠物状态
             if ($pet->status !== PetStatus::NORMAL) {
-                $this->throwMessage(['status' => $pet->status->value], '宠物当前状态({status})不允许升级');
+                $this->addError('宠物当前状态(' . $pet->status->name . ')不允许升级');
                 return false;
             }
-            
+
             // 获取当前等级配置
             $currentLevelConfig = PetLevelConfig::where('level', $pet->level)->first();
             if (!$currentLevelConfig) {
-                $this->throwMessage([], '宠物等级配置不存在');
+                $this->addError('宠物等级配置不存在');
                 return false;
             }
-            
+
             // 获取下一级配置
             $nextLevelConfig = PetLevelConfig::where('level', $pet->level + 1)->first();
             if (!$nextLevelConfig) {
-                $this->throwMessage(['level' => $pet->level], '宠物已达到最大等级({level})');
+                $this->addError('宠物已达到最大等级(' . $pet->level . ')');
                 return false;
             }
-            
+
             // 检查经验值是否足够
             if ($pet->experience < $nextLevelConfig->exp_required) {
-                $this->throwMessage([
-                    'current' => $pet->experience,
-                    'required' => $nextLevelConfig->exp_required
-                ], '经验值不足,无法升级,当前({current}),需要({required})');
+                $this->addError('经验值不足,无法升级,当前(' . $pet->experience . '),需要(' . $nextLevelConfig->exp_required . ')');
                 return false;
             }
-            
+
             return true;
         } catch (\Exception $e) {
-            $this->throwMessage(['error' => $e->getMessage()], '验证过程发生错误: {error}');
+            $this->addError('验证过程发生错误: ' . $e->getMessage());
             return false;
         }
     }

+ 12 - 14
app/Module/Pet/Validators/PetRemouldValidator.php

@@ -5,20 +5,18 @@ namespace App\Module\Pet\Validators;
 use App\Module\Pet\Enums\PetStatus;
 use App\Module\Pet\Models\PetUser;
 use UCore\Validator;
-use UCore\Validator\ValidationMessage;
 
 /**
  * 宠物洗髓验证器
- * 
+ *
  * 用于验证宠物洗髓条件的合法性
  */
 class PetRemouldValidator extends Validator
 {
-    use ValidationMessage;
-    
+
     /**
      * 验证方法
-     * 
+     *
      * @param mixed $value 宠物ID
      * @param array $data 所有数据
      * @return bool 验证是否通过
@@ -27,35 +25,35 @@ class PetRemouldValidator extends Validator
     {
         $petId = $value;
         $itemId = $data['itemId'] ?? 0;
-        
+
         try {
             // 获取宠物信息
             $pet = PetUser::find($petId);
-            
+
             if (!$pet) {
-                $this->throwMessage([], '宠物不存在');
+                $this->addError('宠物不存在');
                 return false;
             }
-            
+
             // 检查宠物状态
             if ($pet->status !== PetStatus::NORMAL) {
-                $this->throwMessage(['status' => $pet->status->value], '宠物当前状态({status})不允许洗髓');
+                $this->addError('宠物当前状态(' . $pet->status->name . ')不允许洗髓');
                 return false;
             }
-            
+
             // 如果使用道具
             if ($itemId > 0) {
                 // 验证物品是否为洗髓道具
                 $remouldItemId = config('pet.remould_cost.item_id');
                 if ($itemId != $remouldItemId) {
-                    $this->throwMessage(['itemId' => $itemId], '物品({itemId})不是洗髓道具');
+                    $this->addError('物品(' . $itemId . ')不是洗髓道具');
                     return false;
                 }
             }
-            
+
             return true;
         } catch (\Exception $e) {
-            $this->throwMessage(['error' => $e->getMessage()], '验证过程发生错误: {error}');
+            $this->addError('验证过程发生错误: ' . $e->getMessage());
             return false;
         }
     }

+ 20 - 28
app/Module/Pet/Validators/PetSkillUseValidator.php

@@ -6,20 +6,18 @@ use App\Module\Pet\Enums\PetStatus;
 use App\Module\Pet\Models\PetSkill;
 use App\Module\Pet\Models\PetUser;
 use UCore\Validator;
-use UCore\Validator\ValidationMessage;
 
 /**
  * 宠物技能使用验证器
- * 
+ *
  * 用于验证宠物技能使用条件的合法性
  */
 class PetSkillUseValidator extends Validator
 {
-    use ValidationMessage;
-    
+
     /**
      * 验证方法
-     * 
+     *
      * @param mixed $value 宠物ID
      * @param array $data 所有数据
      * @return bool 验证是否通过
@@ -28,67 +26,61 @@ class PetSkillUseValidator extends Validator
     {
         $petId = $value;
         $skillId = $data['skillId'] ?? 0;
-        
+
         try {
             // 获取宠物信息
             $pet = PetUser::find($petId);
-            
+
             if (!$pet) {
-                $this->throwMessage([], '宠物不存在');
+                $this->addError('宠物不存在');
                 return false;
             }
-            
+
             // 获取技能信息
             $skill = PetSkill::find($skillId);
             if (!$skill) {
-                $this->throwMessage(['skillId' => $skillId], '技能({skillId})不存在');
+                $this->addError('技能(' . $skillId . ')不存在');
                 return false;
             }
-            
+
             // 检查宠物状态
             if ($pet->status !== PetStatus::NORMAL) {
-                $this->throwMessage(['status' => $pet->status->value], '宠物当前状态({status})不允许使用技能');
+                $this->addError('宠物当前状态(' . $pet->status->name . ')不允许使用技能');
                 return false;
             }
-            
+
             // 检查宠物等级是否满足技能要求
             if ($pet->level < $skill->min_level) {
-                $this->throwMessage([
-                    'level' => $pet->level,
-                    'required' => $skill->min_level
-                ], '宠物等级({level})不足,需要等级({required})才能使用该技能');
+                $this->addError('宠物等级(' . $pet->level . ')不足,需要等级(' . $skill->min_level . ')才能使用该技能');
                 return false;
             }
-            
+
             // 检查宠物体力是否足够
             if ($pet->stamina < $skill->stamina_cost) {
-                $this->throwMessage([
-                    'stamina' => $pet->stamina,
-                    'required' => $skill->stamina_cost
-                ], '宠物体力({stamina})不足,需要体力({required})才能使用该技能');
+                $this->addError('宠物体力(' . $pet->stamina . ')不足,需要体力(' . $skill->stamina_cost . ')才能使用该技能');
                 return false;
             }
-            
+
             // 检查技能冷却时间
             $lastUsed = $pet->skillLogs()
                 ->where('skill_id', $skillId)
                 ->orderBy('used_at', 'desc')
                 ->first();
-            
+
             if ($lastUsed) {
                 $cooldownSeconds = $skill->cool_down;
                 $secondsSinceLastUse = now()->diffInSeconds($lastUsed->used_at);
-                
+
                 if ($secondsSinceLastUse < $cooldownSeconds) {
                     $remainingCooldown = $cooldownSeconds - $secondsSinceLastUse;
-                    $this->throwMessage(['remaining' => $remainingCooldown], '技能冷却中,还需等待{remaining}秒');
+                    $this->addError('技能冷却中,还需等待' . $remainingCooldown . '秒');
                     return false;
                 }
             }
-            
+
             return true;
         } catch (\Exception $e) {
-            $this->throwMessage(['error' => $e->getMessage()], '验证过程发生错误: {error}');
+            $this->addError('验证过程发生错误: ' . $e->getMessage());
             return false;
         }
     }

+ 15 - 17
app/Module/Pet/Validators/PetStatusChangeValidator.php

@@ -5,20 +5,18 @@ namespace App\Module\Pet\Validators;
 use App\Module\Pet\Enums\PetStatus;
 use App\Module\Pet\Models\PetUser;
 use UCore\Validator;
-use UCore\Validator\ValidationMessage;
 
 /**
  * 宠物状态变更验证器
- * 
+ *
  * 用于验证宠物状态变更条件的合法性
  */
 class PetStatusChangeValidator extends Validator
 {
-    use ValidationMessage;
-    
+
     /**
      * 验证方法
-     * 
+     *
      * @param mixed $value 宠物ID
      * @param array $data 所有数据
      * @return bool 验证是否通过
@@ -27,39 +25,39 @@ class PetStatusChangeValidator extends Validator
     {
         $petId = $value;
         $newStatus = $data['newStatus'] ?? null;
-        
+
         if (!$newStatus instanceof PetStatus) {
-            $this->throwMessage([], '新状态无效');
+            $this->addError('新状态无效');
             return false;
         }
-        
+
         try {
             // 获取宠物信息
             $pet = PetUser::find($petId);
-            
+
             if (!$pet) {
-                $this->throwMessage([], '宠物不存在');
+                $this->addError('宠物不存在');
                 return false;
             }
-            
+
             // 检查状态变更是否合法
             // 这里可以根据业务需求添加更多的状态变更规则
-            
+
             // 例如:死亡状态的宠物不能变为其他状态
             if ($pet->status === PetStatus::DEAD && $newStatus !== PetStatus::DEAD) {
-                $this->throwMessage([], '死亡状态的宠物不能变更为其他状态');
+                $this->addError('死亡状态的宠物不能变更为其他状态');
                 return false;
             }
-            
+
             // 例如:战斗中的宠物不能直接变为训练中
             if ($pet->status === PetStatus::FIGHTING && $newStatus === PetStatus::TRAINING) {
-                $this->throwMessage([], '战斗中的宠物不能直接变更为训练中状态');
+                $this->addError('战斗中的宠物不能直接变更为训练中状态');
                 return false;
             }
-            
+
             return true;
         } catch (\Exception $e) {
-            $this->throwMessage(['error' => $e->getMessage()], '验证过程发生错误: {error}');
+            $this->addError('验证过程发生错误: ' . $e->getMessage());
             return false;
         }
     }

+ 0 - 2
app/Module/Pet/Validators/PetStatusValidator.php

@@ -5,7 +5,6 @@ namespace App\Module\Pet\Validators;
 use App\Module\Pet\Enums\PetStatus;
 use App\Module\Pet\Models\PetUser;
 use UCore\Validator;
-use UCore\Validator\ValidationMessage;
 
 /**
  * 宠物状态验证器
@@ -14,7 +13,6 @@ use UCore\Validator\ValidationMessage;
  */
 class PetStatusValidator extends Validator
 {
-    use ValidationMessage;
 
     /**
      * 验证方法