| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace App\Module\Ulogic\Validator;
- use App\Module\Ulogic\Times;
- use UCore\Exception\LogicException;
- use UCore\Validator;
- use UCore\Validator\ConsumeValidator;
- /**
- * 用户计数限制(数据库,永久计数)
- *
- */
- class ULimitTimeValidator extends Validator implements ConsumeValidator
- {
- protected $type;
- protected $sid;
- protected $limit;
- protected $user_id;
- public function validate(mixed $value, array $data): bool
- {
- $type = get_class($this->validation);
- $this->user_id = $value;
- $this->type = $type;
- $sid = $this->args['sid'] ?? 0;
- $this->sid = $sid;
- $limit = $this->args['limit'] ?? 0;
- if ($limit <= 0) {
- throw new LogicException('Limit is null');
- }
- $res = self::check($value, $this->type, $sid, $this->limit);
- if (!$res) {
- $this->validation->addError('', "限制 {$this->limit} 次");
- }
- return $res;
- }
- /**
- * 限流检查
- * @param $user_id
- * @param $type
- * @param $limit
- * @return bool
- */
- public static function check($user_id, $type, $limit, $sid = 0)
- {
- if (Times::check($user_id, $type, $sid, $limit)) {
- return true;
- }
- return false;
- }
- public function consume($times = 1)
- {
- Times::add($this->user_id, $this->type, $this->sid, $times);
- }
- }
|