| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace App\Module\GameItems\Validators;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use App\Module\GameItems\Models\Item;
- use App\Module\GameItems\Models\ItemChestConfig;
- use UCore\Validator;
- /**
- * 宝箱物品验证器
- *
- * 验证物品是否为宝箱类型且配置正确
- */
- class ChestItemValidator extends Validator
- {
- /**
- * 验证宝箱物品
- *
- * @param mixed $value 物品ID
- * @param array $data 包含其他数据的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- $itemId = (int)$value;
- try {
- // 获取物品信息
- $item = Item::find($itemId);
- if (!$item) {
- $this->addError("物品不存在");
- return false;
- }
- // 检查是否为宝箱类型
- if ($item->type !== ITEM_TYPE::CHEST) {
- $this->addError("该物品不是宝箱类型");
- return false;
- }
- // 检查宝箱是否有配置
- $chestConfig = ItemChestConfig::where('item_id', $itemId)
- ->where('is_active', true)
- ->first();
- if (!$chestConfig) {
- $this->addError("宝箱没有配置或配置未激活");
- return false;
- }
- // 检查是否配置了奖励组
- if (!$chestConfig->reward_group_id) {
- $this->addError("宝箱没有配置奖励组");
- return false;
- }
- // 将宝箱信息保存到验证对象中,供后续使用
- $chestItemKey = $this->args[0] ?? null;
- if ($chestItemKey) {
- $this->validation->$chestItemKey = $item;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证宝箱物品时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|