短信接口增加ip频次限制

webUi^2
zhaocheng 5 years ago
parent d5dc26a26e
commit a4ce25e97f
  1. 16
      frontend/controllers/ApiController.php

@ -13,6 +13,8 @@ use common\models\SendLogT;
use common\models\UserLogT; use common\models\UserLogT;
use common\models\UserT; use common\models\UserT;
use Yii; use Yii;
use yii\captcha\Captcha;
use yii\captcha\CaptchaAction;
use yii\db\Exception; use yii\db\Exception;
use yii\web\Response; use yii\web\Response;
use yii\web\UploadedFile; use yii\web\UploadedFile;
@ -845,6 +847,18 @@ class ApiController extends \yii\web\Controller
header("Access-Control-Allow-Headers: Content-Type, X-Requested-With, Cache-Control,Authorization,Beeba-Sign,Beeba-Timestamp"); header("Access-Control-Allow-Headers: Content-Type, X-Requested-With, Cache-Control,Authorization,Beeba-Sign,Beeba-Timestamp");
Yii::$app->response->format = Response::FORMAT_JSON; Yii::$app->response->format = Response::FORMAT_JSON;
$params = Yii::$app->request->post(); $params = Yii::$app->request->post();
$cache = Yii::$app->cache;
$now = time();
$last_visited_name = 'hy_last_visit_ip_' . Yii::$app->request->getUserIP();
$last_visited_time = $cache->get($last_visited_name);
if ($last_visited_time && $now - $last_visited_time < 90) {
$result['success'] = false;
$result['msg'] = '接口调用过于频繁';
$result['data'] = $now - $last_visited_time;
return $result;
} else {
$cache->set($last_visited_name, $now);
}
if (!isset($params['mobile']) || $params['mobile'] == '') { if (!isset($params['mobile']) || $params['mobile'] == '') {
$result['success'] = false; $result['success'] = false;
$result['msg'] = '手机号无效'; $result['msg'] = '手机号无效';
@ -888,7 +902,7 @@ class ApiController extends \yii\web\Controller
$user = UserT::findOne(['name' => $name, 'phone' => $mobile]); $user = UserT::findOne(['name' => $name, 'phone' => $mobile]);
if($name == '' || !preg_match('/^1[345789]\d{9}$/', $mobile)){ if($name == '' || !preg_match('/^1[345789]\d{9}$/', $mobile)){
$result['code'] = -1; $result['code'] = -1;
$result['msg'] = '参数错误'.'手机'.$mobile.'-'.(!preg_match('/^1[345789]\d{9}$/', $mobile)).'name'.$name.'--'.($name == ''); $result['msg'] = '参数错误';
return $result; return $result;
} }
if($code == '' || Yii::$app->session->get('hy_vcode_'.$mobile) != $code){ if($code == '' || Yii::$app->session->get('hy_vcode_'.$mobile) != $code){

Loading…
Cancel
Save