|
|
楼主 |
发表于 2021-4-26 14:16:33
|
显示全部楼层
本帖最后由 win 于 2021-4-26 14:17 编辑
举个例子:当用户点击某个按钮超过7次,则5分钟后再能进行*作
public function sendsms()
{
$phone = Request::param('phone');
//此方法的公共变量
$ckip = request()->ip(); //获取当前IP地址
$nowTime = time(); //当前时间
$chas = UserModel::where('phone', $phone)->find();
$phonecha = $chas['phone'];
Cache::store('redis')->connect('127.0.0.1', 6379);
$tip = Cache::store('redis')->get($ckip);
if ($tip > 7) {
Cache::store('redis')->expire($ckip, 300);
$qutime = Cache::get($ckip);
$shengyuTime = ceil(($qutime - $nowTime)/60); //剩余分钟
$mins = '*作次数过多,请休息5分钟,剩余'.$shengyuTime.'分钟';
return ['status' => 0, 'message' => $mins];
exit();
}
if ($phone != $phonecha) {
Cache::store('redis')->incr($ckip);
return ['status' => 2, 'message' => '手机号未注册'];
}
if($chas){
$caotime = time() + 300; //5分钟后再进行*作
Cache::set($ckip,$caotime,300);
Cache::store('redis')->incr($ckip);
/*自己的代码*/
//写入发送日志
$datas = ['phone' => $phone, 'phpcode' => $code, 'phpip' => $ckip,'yongtu' => '发送验证码', 'sms' =>1];
DhsmsModel::create($datas);
return [$content, 'status' => 1, 'message' => '发送成功'];
}else {
if (Cache::store('redis')->exists($ckip)) {
Cache::store('redis')->incr($ckip);
} else {
//第一次错误将错误信息存入redis中
Cache::store('redis')->set($ckip, 1);
}
}
}
|
|