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