| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace App\Module\GameItems\Validators;
- use App\Module\GameItems\Logics\ChestOpenCostLogic;
- use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
- use App\Module\GameItems\Logics\Item as ItemLogic;
- use UCore\Validator;
- /**
- * 宝箱开启消耗验证器
- *
- * 验证用户是否有足够的资源开启宝箱
- */
- class ChestOpenCostValidator extends Validator
- {
- /**
- * 验证宝箱开启消耗
- *
- * @param mixed $value 物品ID
- * @param array $data 包含用户ID和数量的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- $itemId = (int)$value;
-
- // 从 args 获取参数键名
- $userIdKey = $this->args[0] ?? 'user_id';
- $quantityKey = $this->args[1] ?? 'quantity';
- $userId = $data[$userIdKey] ?? null;
- $quantity = $data[$quantityKey] ?? 1;
- if (!$userId) {
- $this->addError('用户ID不能为空');
- return false;
- }
- try {
- // 创建消耗逻辑实例
- $chestOpenCostLogic = new ChestOpenCostLogic(
- app(ItemChestOpenCostRepository::class),
- new ItemLogic()
- );
- // 验证宝箱开启消耗
- list($isValid, $message, $costDetails) = $chestOpenCostLogic->validateChestOpenCosts($userId, $itemId, $quantity);
-
- if (!$isValid) {
- $this->addError($message);
- return false;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证宝箱开启消耗时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|