| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace App\Module\System\Services;
- use App\Module\Ulogic\Model\UserDaytime;
- /**
- * 连续次数处理
- *
- */
- class ContinuousTimes
- {
- /**
- * 增加计数
- *
- * @param $user_id
- * @param $stype
- * @param $sid
- * @param $times
- * @param $diff
- * @return int|mixed
- */
- static public function add($user_id, $stype, $sid, $times = 1, $diff = 120)
- {
- $old = \App\Module\System\Models\ContinuousTimes::class::query()->where([
- 'user_id' => $user_id,
- 'stype' => $stype,
- 'sid' => $sid,
- ])->first();
- if (!$old) {
- $old = new \App\Module\System\Models\ContinuousTimes();
- $old->user_id = $user_id;
- $old->stype = $stype;
- $old->sid = $sid;
- $old->last_time = 0;
- $old->number = 0;
- }
- $old->diff = $diff;
- if (($old->last_time + $diff) < time()) {
- // 不连续了
- $old->number = 0;
- }
- $old->last_time = time();
- $old->number += $times;
- $old->save();
- // dump($old->stype,$stype);
- return $old->number;
- }
- /**
- * 检查是否超过限制
- * @param $user_id
- * @param $stype
- * @param $sid
- * @param $times
- * @return bool
- */
- static public function check($user_id, $stype, $sid, $times = 1)
- {
- /**
- * @var \App\Module\System\Models\ContinuousTimes $old
- */
- $old = \App\Module\System\Models\ContinuousTimes::query()->where([
- 'user_id' => $user_id,
- 'stype' => $stype,
- 'sid' => $sid,
- ])->first();
- if (!$old) {
- return true;
- }
- $diff = $old->diff;
- if (($old->last_time + $diff) < time()) {
- // 不连续了
- $old->number = 0;
- }
- if ($old->number >= $times) {
- return false;
- }
- return true;
- }
- }
|