|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
namespace App\Module\Pet\Validators;
|
|
|
|
|
|
-use App\Module\GameItems\Models\Item;
|
|
|
+use App\Module\GameItems\Services\ItemService;
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use UCore\Validator;
|
|
|
@@ -48,19 +48,22 @@ class PetFoodValidator extends Validator
|
|
|
|
|
|
if ($cachedResult !== null) {
|
|
|
if (!$cachedResult) {
|
|
|
- $this->throwMessage(['itemId' => $itemId], '物品({itemId})不是宠物口粮');
|
|
|
+ $this->addError("物品({$itemId})不是宠物口粮");
|
|
|
}
|
|
|
return $cachedResult;
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
+ // 创建物品服务实例
|
|
|
+ $itemService = new ItemService();
|
|
|
+
|
|
|
// 获取物品信息
|
|
|
- $item = Item::find($itemId);
|
|
|
+ $item = $itemService->getItemInfo($itemId);
|
|
|
|
|
|
if (!$item) {
|
|
|
// 物品不存在
|
|
|
Cache::put($cacheKey, false, $this->cacheExpiration);
|
|
|
- $this->throwMessage(['itemId' => $itemId], '物品({itemId})不存在');
|
|
|
+ $this->addError("物品({$itemId})不存在");
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
@@ -71,7 +74,7 @@ class PetFoodValidator extends Validator
|
|
|
Cache::put($cacheKey, $isPetFood, $this->cacheExpiration);
|
|
|
|
|
|
if (!$isPetFood) {
|
|
|
- $this->throwMessage(['itemId' => $itemId], '物品({itemId})不是宠物口粮');
|
|
|
+ $this->addError("物品({$itemId})不是宠物口粮");
|
|
|
}
|
|
|
|
|
|
return $isPetFood;
|
|
|
@@ -81,7 +84,7 @@ class PetFoodValidator extends Validator
|
|
|
'error' => $e->getMessage()
|
|
|
]);
|
|
|
|
|
|
- $this->throwMessage(['error' => $e->getMessage()], '验证过程发生错误: {error}');
|
|
|
+ $this->addError('验证过程发生错误: ' . $e->getMessage());
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
@@ -89,16 +92,18 @@ class PetFoodValidator extends Validator
|
|
|
/**
|
|
|
* 检查物品是否具有宠物口粮属性
|
|
|
*
|
|
|
- * @param Item $item 物品对象
|
|
|
+ * @param object|array $item 物品信息
|
|
|
* @return bool 是否具有宠物口粮属性
|
|
|
*/
|
|
|
- protected function checkPetFoodAttributes(Item $item): bool
|
|
|
+ protected function checkPetFoodAttributes($item): bool
|
|
|
{
|
|
|
// 检查物品是否有pet_power或pet_exp属性
|
|
|
$numericAttributes = $item->numeric_attributes ?? [];
|
|
|
|
|
|
if (is_string($numericAttributes)) {
|
|
|
$numericAttributes = json_decode($numericAttributes, true) ?? [];
|
|
|
+ } elseif (is_object($numericAttributes)) {
|
|
|
+ $numericAttributes = (array)$numericAttributes;
|
|
|
}
|
|
|
|
|
|
// 检查是否有宠物相关属性
|
|
|
@@ -118,8 +123,11 @@ class PetFoodValidator extends Validator
|
|
|
public function getPetFoodAttributes(int $itemId): array
|
|
|
{
|
|
|
try {
|
|
|
+ // 创建物品服务实例
|
|
|
+ $itemService = new ItemService();
|
|
|
+
|
|
|
// 获取物品信息
|
|
|
- $item = Item::find($itemId);
|
|
|
+ $item = $itemService->getItemInfo($itemId);
|
|
|
|
|
|
if (!$item) {
|
|
|
return [
|
|
|
@@ -133,6 +141,8 @@ class PetFoodValidator extends Validator
|
|
|
|
|
|
if (is_string($numericAttributes)) {
|
|
|
$numericAttributes = json_decode($numericAttributes, true) ?? [];
|
|
|
+ } elseif (is_object($numericAttributes)) {
|
|
|
+ $numericAttributes = (array)$numericAttributes;
|
|
|
}
|
|
|
|
|
|
return [
|