$openid, 'event' => $event])->whereTime('createtime', '-2 minutes')->find(); if ($captcha) { return "验证码发送速度过快,请稍后重试"; } $code = Random::alnum(4); $data = [ 'event' => $event, 'openid' => $openid, 'code' => $code, 'ip' => $ip, ]; self::create($data); return "你的验证码是:{$code},2分钟内输入有效"; } /** * 检测验证码 * @param $code string 验证码 * @param $event string 事件 * @param $ip string IP * @return bool */ public static function check($code, $event, $ip = null) { $ip = is_null($ip) ? request()->ip() : $ip; $captcha = self::where(['ip' => $ip, 'event' => $event])->whereTime('createtime', '-2 minutes')->find(); if ($captcha && $captcha->code == $code && $captcha->times < 10) { $captcha->setInc("times"); return true; } //验证大于10次或超时 if ($captcha && ($captcha->times >= 10 || time() - $captcha->createtime > 120)) { $captcha->delete(); } return false; } }