diff --git a/app/controllers/ControllerBase.php b/app/controllers/ControllerBase.php index b74ac34..be3e9d7 100644 --- a/app/controllers/ControllerBase.php +++ b/app/controllers/ControllerBase.php @@ -136,11 +136,11 @@ class ControllerBase extends Phalcon\Mvc\Controller{ } /** - * 随机生成8位字符 + * 随机生成字符串 */ - function _get_act_code(){ - $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - $code = substr(str_shuffle($str), 0, 8); + function _get_act_code($digit=32){ + $str = 'abcdefghijklmnopqrstuvwxyz0123456789'; + $code = substr(str_shuffle($str), 0, $digit); return $code; } diff --git a/app/controllers/Oauth2Controller.php b/app/controllers/Oauth2Controller.php index 8039af2..cce547a 100644 --- a/app/controllers/Oauth2Controller.php +++ b/app/controllers/Oauth2Controller.php @@ -17,6 +17,7 @@ class Oauth2Controller extends ControllerBase{ */ function testAction(){ $url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/code"; + $url = "http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx"; $post_arr = array(); //$post_arr['appid'] = 111; $post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7"; @@ -31,21 +32,21 @@ class Oauth2Controller extends ControllerBase{ */ function codeAction(){ //如果未获取到请求数据 - if(empty($this->request->getPost())){ + if(empty(file_get_contents("php://input"))){ //告知用户系统繁忙,请稍候再试 - $rs['errcode'] = -1; $rs['errmsg'] = "系统繁忙,请稍候再试"; + $rs['errcode'] = -1; $rs['data'] = null; } //如果有获取到请求数据 else{ //准备获取appid - $request_arr = json_decode($this->request->getPost(), true); + $request_arr = json_decode(file_get_contents("php://input"), true); //如果未获取到appid if(empty($request_arr['appid'])){ //告知用户参数错误 error detail:appid is empty; - $rs['errcode'] = 61451; $rs['errmsg'] = "参数错误 error detail:appid is empty;"; + $rs['errcode'] = 61451; $rs['data'] = null; } //如果有获取到appid @@ -53,12 +54,12 @@ class Oauth2Controller extends ControllerBase{ //准备判断appid是否有效 $appid = $request_arr['appid']; //查询接口信息 - $appinfo = Appinfo::findFirst("Appid = '$appid' AND Enable = 0"); + $appinfo = Appinfo::findFirst("AppId = '$appid' AND Enable = 0"); //如果未查询到接口信息 if(empty($appinfo)){ //告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息 - $rs['errcode'] = 40036; $rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息"; + $rs['errcode'] = 40036; $rs['data'] = null; } //如果有查询到接口信息 @@ -79,13 +80,14 @@ class Oauth2Controller extends ControllerBase{ //如果当天请求次数已达到接口请求次数限制 if($today_req_times>=$day_max_req_times){ //告知用户request today reached the request limit,err detail:超过请求上限限制 - $rs['errcode'] = 80102; $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; + $rs['errcode'] = 80102; $rs['data'] = null; } //如果当天请求次数尚未达到接口请求次数限制 else{ - + //当天请求次数加一 + $today_req_times = $today_req_times+1; } } //如果上次请求日期不是当天日期 @@ -93,17 +95,66 @@ class Oauth2Controller extends ControllerBase{ //如果接口请求次数限制不大于0 if($day_max_req_times<=0){ //告知用户request today reached the request limit,err detail:超过请求上限限制 - $rs['errcode'] = 80102; $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'])){ - //开启事务 - $this->db->begin(); - //准备更新请求次数 - + //准备更新当天请求次数 + $appinfo->LastModifiedTime = $current_datetime; + //更新字段 + $appinfo->TodayReqTimes = $today_req_times; + $appinfo->LastReqTime = $current_date; + //如果更新失败 + if(!($appinfo->save())){ + //告知用户系统繁忙,请稍候再试 + $rs['errmsg'] = "系统繁忙,请稍候再试"; + $rs['errcode'] = -1; + $rs['data'] = null; + } + //如果更新成功 + else{ + //查询当前接口今天是否有生成过code + $tempcode = Tempcode::findFirst("AppId = '$appid'"); + //如果当前接口过去未生成过code + if(empty($tempcode)){ + //准备新增临时code + $tempcode = new Tempcode(); + $tempcode->AppId = $appid; + $tempcode->CreateTime = $current_datetime; + $tempcode->LastModifiedTime = $current_datetime; + } + //如果当前接口过去有生成过code + else{ + //准备更新临时code + $tempcode->LastModifiedTime = $current_datetime; + } + //更新字段 + $tempcode->Code = $code = $this->_get_act_code(32); + $tempcode->IsUsed = 0; + $tempcode->ExpireTime = date("Y-m-d H:i:s", $current_time+300); + //如果保存失败 + if(!($tempcode->save())){ + //告知用户系统繁忙,请稍候再试 + $rs['errmsg'] = "系统繁忙,请稍候再试"; + $rs['errcode'] = -1; + $rs['data'] = null; + } + //如果保存成功 + else{ + //告知用户ok + $rs['errmsg'] = "ok"; + $rs['errcode'] = 0; + $rs['data']['code'] = $code; + } + } } } } @@ -117,7 +168,18 @@ class Oauth2Controller extends ControllerBase{ * http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx */ function accesstokenAction(){ - + //如果未获取到请求数据 + if(empty($this->request->getPost())){ + //告知用户系统繁忙,请稍候再试 + $rs['errcode'] = -1; + $rs['errmsg'] = "系统繁忙,请稍候再试"; + $rs['data'] = null; + } + //如果有获取到请求数据 + else{ + + } + echo json_encode($rs, JSON_UNESCAPED_UNICODE); } } diff --git a/app/models/Accesstoken.php b/app/models/Accesstoken.php index 1ae826f..c9b31dd 100644 --- a/app/models/Accesstoken.php +++ b/app/models/Accesstoken.php @@ -3,7 +3,7 @@ * AccessToken表 * @author QZQ */ -class Accesstoken extends AppModel{ +class Accesstoken extends ModelBase{ public function initialize(){ $this->setSource("smsapioauth2_accesstoken"); diff --git a/app/models/Appinfo.php b/app/models/Appinfo.php index 609defa..df98588 100644 --- a/app/models/Appinfo.php +++ b/app/models/Appinfo.php @@ -3,7 +3,7 @@ * Appinfo表 * @author QZQ */ -class Appinfo extends AppModel{ +class Appinfo extends ModelBase{ public function initialize(){ $this->setSource("smsapioauth2_appinfo"); diff --git a/app/models/Refreshtoken.php b/app/models/Refreshtoken.php index f120b5c..f3c209a 100644 --- a/app/models/Refreshtoken.php +++ b/app/models/Refreshtoken.php @@ -3,7 +3,7 @@ * Refreshtoken表 * @author QZQ */ -class Refreshtoken extends AppModel{ +class Refreshtoken extends ModelBase{ public function initialize(){ $this->setSource("smsapioauth2_refreshtoken"); diff --git a/app/models/Tempcode.php b/app/models/Tempcode.php index bbb8bb9..332d4a7 100644 --- a/app/models/Tempcode.php +++ b/app/models/Tempcode.php @@ -3,7 +3,7 @@ * Tempcode表 * @author QZQ */ -class Tempcode extends AppModel{ +class Tempcode extends ModelBase{ public function initialize(){ $this->setSource("smsapioauth2_tempcode");