| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- <?php
- namespace App\Module\GameItems\Validators;
- use UCore\Validator;
- /**
- * 物品是否过期
- *
- */
- class IsExpiredValidator extends Validator
- {
- /**
- * @param mixed $value item_user 表的 id
- * @param array $data
- * @return bool
- */
- public function validate(mixed $value, array $data): bool
- {
- if (empty($value)) {
- $this->setError('物品ID不能为空');
- return false;
- }
- $itemUser = ItemUser::find($value);
- if (!$itemUser) {
- $this->setError('物品不存在');
- return false;
- }
- // 1. 检查item_user表的过期时间
- if (!is_null($itemUser->expire_at)) {
- if (now()->gt($itemUser->expire_at)) {
- $this->setError('物品已过期');
- return false;
- }
- return true;
- }
- // 2. 检查item表的全局过期时间
- $item = $itemUser->item;
- if ($item->isExpired()) {
- $this->setError('物品已全局过期');
- return false;
- }
-
-
- return true;
- }
- }
|