修正bug,调整逻辑,原代码接口请求次数限制仅作用于获取code

This commit is contained in:
qinzongqing 2023-04-17 17:37:40 +08:00
parent 5e032d19fd
commit 34dbd8cf5e
4 changed files with 120 additions and 118 deletions

View File

@ -212,9 +212,9 @@ class ControllerBase extends Phalcon\Mvc\Controller{
} }
/** /**
* 更新接口请求次数 * 校验所传appid是否有效
*/ */
function __update_app_info($appid=""){ function __check_appid($appid=""){
//查询接口信息 //查询接口信息
$AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0"); $AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0");
//如果未查询到接口信息 //如果未查询到接口信息
@ -226,65 +226,10 @@ class ControllerBase extends Phalcon\Mvc\Controller{
} }
//如果有查询到接口信息 //如果有查询到接口信息
else{ else{
//获取上次请求日期 //告知用户"ok"
$last_req_time = $AppInfo->LastReqTime; $rs['errmsg'] = "ok";
$last_req_date = date("Y-m-d", strtotime($last_req_time)); $rs['errcode'] = 0;
//获取上次请求日期所在日的请求次数 $rs['data']['AppInfo'] = $AppInfo;
$today_req_times = $AppInfo->TodayReqTimes;
//获取接口请求次数限制
$day_max_req_times = $AppInfo->DayMaxReqTimes;
//如果上次请求日期是当天日期
if($last_req_date==$this->current_date){
//如果当天请求次数已达到接口请求次数限制
if($today_req_times>=$day_max_req_times){
//告知用户"request today reached the request limit,err detail:超过请求上限限制"
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
$rs['errcode'] = 80102;
$rs['data'] = null;
}
//如果当天请求次数尚未达到接口请求次数限制
else{
//当天请求次数加一
$today_req_times = $today_req_times+1;
}
}
//如果上次请求日期不是当天日期
else{
//如果接口请求次数限制不大于0
if($day_max_req_times<=0){
//告知用户"request today reached the request limit,err detail:超过请求上限限制"
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
$rs['errcode'] = 80102;
$rs['data'] = null;
}
//如果接口请求次数限制大于0
else{
//初始当天请求次数
$today_req_times = 1;
}
}
//如果以上判断都通过
if(empty($rs['errcode'])){
//准备更新当天请求次数
$AppInfo->LastModifiedTime = $this->current_datetime;
//其他字段
$AppInfo->TodayReqTimes = $today_req_times;
$AppInfo->LastReqTime = $this->current_date;
//如果更新失败
if(!($AppInfo->save())){
//告知用户"系统繁忙,请稍候再试"
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果更新成功
else{
//告知用户"ok"
$rs['errmsg'] = "ok";
$rs['errcode'] = 0;
$rs['data']['AppInfo'] = $AppInfo;
}
}
} }
//返回结果 //返回结果
return $rs; return $rs;

View File

@ -19,12 +19,12 @@ class Oauth2Controller extends ControllerBase{
function testAction(){ function testAction(){
//$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/code"; //$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/code";
//$url = "http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx"; //$url = "http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx";
//$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/accesstoken"; $url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/accesstoken";
$url = "http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx"; //$url = "http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx";
$post_arr = array(); $post_arr = array();
$post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7"; $post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7";
$post_arr['appsecrect'] = "b82015bd-8d4c-4df8-87a8-c25477a8976f"; $post_arr['appsecrect'] = "b82015bd-8d4c-4df8-87a8-c25477a8976f";
$post_arr['code'] = "b877e2d843ef2c21fc28e7126423d217"; $post_arr['code'] = "6a1df95b00d0414d8cc545425afac40c";
$post_json = json_encode($post_arr); $post_json = json_encode($post_arr);
$result = $this->__http_post_request($url, $post_json, true); $result = $this->__http_post_request($url, $post_json, true);
print_r($result); die; print_r($result); die;
@ -48,45 +48,102 @@ class Oauth2Controller extends ControllerBase{
$rs = $this->__check_params_arr($request_arr, $params_arr); $rs = $this->__check_params_arr($request_arr, $params_arr);
//如果基本参数都不为空 //如果基本参数都不为空
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//准备更新接口请求次数 //准备校验所传appid是否有效
$appid = $request_arr['appid']; $appid = $request_arr['appid'];
//更新接口请求次数 //校验所传appid是否有效
$rs = $this->__update_app_info($appid); $rs = $this->__check_appid($appid);
//如果接口请求次数更新成功 //如果所传appid有效
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//查询当前接口过去是否有生成过code //获取接口信息
$TempCode = TempCode::findFirst("AppId = '$appid'"); $AppInfo = $rs['data']['AppInfo'];
//如果当前接口过去未生成过code //获取上次请求code日期
if(empty($TempCode)){ $last_req_time = $AppInfo->LastReqTime;
//准备新增临时code $last_req_date = date("Y-m-d", strtotime($last_req_time));
$TempCode = new TempCode(); //获取上次请求code日期所在日的请求code次数
$TempCode->AppId = $appid; $today_req_times = $AppInfo->TodayReqTimes;
$TempCode->CreateTime = $this->current_datetime; //获取接口请求code次数限制
$TempCode->LastModifiedTime = $this->current_datetime; $day_max_req_times = $AppInfo->DayMaxReqTimes;
//如果上次请求code日期是当天日期
if($last_req_date==$this->current_date){
//如果当天请求code次数已达到接口请求code次数限制
if($today_req_times>=$day_max_req_times){
//告知用户"request today reached the request limit,err detail:超过请求上限限制"
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
$rs['errcode'] = 80102;
$rs['data'] = null;
}
//如果当天请求code次数尚未达到接口请求code次数限制
else{
//当天请求code次数加一
$today_req_times = $today_req_times+1;
}
} }
//如果当前接口过去有生成过code //如果上次请求code日期不是当天日期
else{ else{
//准备更新临时code //如果接口请求code次数限制不大于0
$TempCode->LastModifiedTime = $this->current_datetime; if($day_max_req_times<=0){
//告知用户"request today reached the request limit,err detail:超过请求上限限制"
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
$rs['errcode'] = 80102;
$rs['data'] = null;
}
//如果接口请求code次数限制大于0
else{
//初始当天请求code次数
$today_req_times = 1;
}
} }
//其他字段 //如果以上判断都通过
$TempCode->Code = $code = md5(uniqid(mt_rand(), true)); if(empty($rs['errcode'])){
$TempCode->IsUsed = 0; //准备更新当天请求code次数
$TempCode->ExpireTime = date("Y-m-d H:i:s", $this->current_time+300); $AppInfo->LastModifiedTime = $this->current_datetime;
//如果保存失败 //其他字段
if(!($TempCode->save())){ $AppInfo->TodayReqTimes = $today_req_times;
//告知用户"系统繁忙,请稍候再试" $AppInfo->LastReqTime = $this->current_date;
$rs['errmsg'] = "系统繁忙,请稍候再试"; //如果更新失败
$rs['errcode'] = -1; if(!($AppInfo->save())){
$rs['data'] = null; //告知用户"系统繁忙,请稍候再试"
} $rs['errmsg'] = "系统繁忙,请稍候再试";
//如果保存成功 $rs['errcode'] = -1;
else{ $rs['data'] = null;
//告知用户"ok" }
$rs['errmsg'] = "ok"; //如果更新成功
$rs['errcode'] = 0; else{
$rs['data'] = array(); //查询当前接口过去是否有生成过code
$rs['data']['code'] = $code; $TempCode = TempCode::findFirst("AppId = '$appid'");
//如果当前接口过去未生成过code
if(empty($TempCode)){
//准备新增临时code
$TempCode = new TempCode();
$TempCode->AppId = $appid;
$TempCode->CreateTime = $this->current_datetime;
$TempCode->LastModifiedTime = $this->current_datetime;
}
//如果当前接口过去有生成过code
else{
//准备更新临时code
$TempCode->LastModifiedTime = $this->current_datetime;
}
//其他字段
$TempCode->Code = $code = md5(uniqid(mt_rand(), true));
$TempCode->IsUsed = 0;
$TempCode->ExpireTime = date("Y-m-d H:i:s", $this->current_time+300);
//如果保存失败
if(!($TempCode->save())){
//告知用户"系统繁忙,请稍候再试"
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果保存成功
else{
//告知用户"ok"
$rs['errmsg'] = "ok";
$rs['errcode'] = 0;
$rs['data'] = array();
$rs['data']['code'] = $code;
}
}
} }
} }
} }
@ -119,11 +176,11 @@ class Oauth2Controller extends ControllerBase{
$rs = $this->__check_params_arr($request_arr, $params_arr); $rs = $this->__check_params_arr($request_arr, $params_arr);
//如果基本参数都不为空 //如果基本参数都不为空
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//准备更新接口请求次数 //准备校验所传appid是否有效
$appid = $request_arr['appid']; $appid = $request_arr['appid'];
//更新接口请求次数 //校验所传appid是否有效
$rs = $this->__update_app_info($appid); $rs = $this->__check_appid($appid);
//如果接口请求次数更新成功 //如果所传appid有效
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//获取接口信息 //获取接口信息
$AppInfo = $rs['data']['AppInfo']; $AppInfo = $rs['data']['AppInfo'];

View File

@ -95,11 +95,11 @@ class SmsController extends ControllerBase{
$rs = $this->__check_params_arr($request_arr, $params_arr); $rs = $this->__check_params_arr($request_arr, $params_arr);
//如果基本参数都不为空 //如果基本参数都不为空
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//准备更新接口请求次数 //准备校验所传appid是否有效
$appid = $request_arr['appid']; $appid = $request_arr['appid'];
//更新接口请求次数 //校验所传appid是否有效
$rs = $this->__update_app_info($appid); $rs = $this->__check_appid($appid);
//如果接口请求次数更新成功 //如果所传appid有效
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//获取接口信息 //获取接口信息
$AppInfo = $rs['data']['AppInfo']; $AppInfo = $rs['data']['AppInfo'];
@ -220,11 +220,11 @@ class SmsController extends ControllerBase{
} }
//如果所传验证码类型是1或2 //如果所传验证码类型是1或2
else{ else{
//准备更新接口请求次数 //准备校验所传appid是否有效
$appid = $request_arr['appid']; $appid = $request_arr['appid'];
//更新接口请求次数 //校验所传appid是否有效
$rs = $this->__update_app_info($appid); $rs = $this->__check_appid($appid);
//如果接口请求次数更新成功 //如果所传appid有效
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//获取接口信息 //获取接口信息
$AppInfo = $rs['data']['AppInfo']; $AppInfo = $rs['data']['AppInfo'];
@ -400,11 +400,11 @@ class SmsController extends ControllerBase{
$rs = $this->__check_params_arr($request_arr, $params_arr); $rs = $this->__check_params_arr($request_arr, $params_arr);
//如果基本参数都不为空 //如果基本参数都不为空
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//准备更新接口请求次数 //准备校验所传appid是否有效
$appid = $request_arr['appid']; $appid = $request_arr['appid'];
//更新接口请求次数 //校验所传appid是否有效
$rs = $this->__update_app_info($appid); $rs = $this->__check_appid($appid);
//如果接口请求次数更新成功 //如果所传appid有效
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//获取接口信息 //获取接口信息
$AppInfo = $rs['data']['AppInfo']; $AppInfo = $rs['data']['AppInfo'];

View File

@ -56,11 +56,11 @@ class UrlsController extends ControllerBase{
$rs = $this->__check_params_arr($request_arr, $params_arr); $rs = $this->__check_params_arr($request_arr, $params_arr);
//如果基本参数都不为空 //如果基本参数都不为空
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//准备更新接口请求次数 //准备校验所传appid是否有效
$appid = $request_arr['appid']; $appid = $request_arr['appid'];
//更新接口请求次数 //校验所传appid是否有效
$rs = $this->__update_app_info($appid); $rs = $this->__check_appid($appid);
//如果接口请求次数更新成功 //如果所传appid有效
if(empty($rs['errcode'])){ if(empty($rs['errcode'])){
//获取接口信息 //获取接口信息
$AppInfo = $rs['data']['AppInfo']; $AppInfo = $rs['data']['AppInfo'];