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; } }