备份提交

This commit is contained in:
qinzongqing 2023-04-10 16:09:25 +08:00
parent 17b38485fd
commit 1e41961950
6 changed files with 84 additions and 22 deletions

View File

@ -136,11 +136,11 @@ class ControllerBase extends Phalcon\Mvc\Controller{
} }
/** /**
* 随机生成8位字符 * 随机生成字符
*/ */
function _get_act_code(){ function _get_act_code($digit=32){
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; $str = 'abcdefghijklmnopqrstuvwxyz0123456789';
$code = substr(str_shuffle($str), 0, 8); $code = substr(str_shuffle($str), 0, $digit);
return $code; return $code;
} }

View File

@ -17,6 +17,7 @@ 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";
$post_arr = array(); $post_arr = array();
//$post_arr['appid'] = 111; //$post_arr['appid'] = 111;
$post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7"; $post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7";
@ -31,21 +32,21 @@ class Oauth2Controller extends ControllerBase{
*/ */
function codeAction(){ function codeAction(){
//如果未获取到请求数据 //如果未获取到请求数据
if(empty($this->request->getPost())){ if(empty(file_get_contents("php://input"))){
//告知用户系统繁忙,请稍候再试 //告知用户系统繁忙,请稍候再试
$rs['errcode'] = -1;
$rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null; $rs['data'] = null;
} }
//如果有获取到请求数据 //如果有获取到请求数据
else{ else{
//准备获取appid //准备获取appid
$request_arr = json_decode($this->request->getPost(), true); $request_arr = json_decode(file_get_contents("php://input"), true);
//如果未获取到appid //如果未获取到appid
if(empty($request_arr['appid'])){ if(empty($request_arr['appid'])){
//告知用户参数错误 error detail:appid is empty; //告知用户参数错误 error detail:appid is empty;
$rs['errcode'] = 61451;
$rs['errmsg'] = "参数错误 error detail:appid is empty;"; $rs['errmsg'] = "参数错误 error detail:appid is empty;";
$rs['errcode'] = 61451;
$rs['data'] = null; $rs['data'] = null;
} }
//如果有获取到appid //如果有获取到appid
@ -53,12 +54,12 @@ class Oauth2Controller extends ControllerBase{
//准备判断appid是否有效 //准备判断appid是否有效
$appid = $request_arr['appid']; $appid = $request_arr['appid'];
//查询接口信息 //查询接口信息
$appinfo = Appinfo::findFirst("Appid = '$appid' AND Enable = 0"); $appinfo = Appinfo::findFirst("AppId = '$appid' AND Enable = 0");
//如果未查询到接口信息 //如果未查询到接口信息
if(empty($appinfo)){ if(empty($appinfo)){
//告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息 //告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息
$rs['errcode'] = 40036;
$rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息"; $rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息";
$rs['errcode'] = 40036;
$rs['data'] = null; $rs['data'] = null;
} }
//如果有查询到接口信息 //如果有查询到接口信息
@ -79,13 +80,14 @@ class Oauth2Controller extends ControllerBase{
//如果当天请求次数已达到接口请求次数限制 //如果当天请求次数已达到接口请求次数限制
if($today_req_times>=$day_max_req_times){ if($today_req_times>=$day_max_req_times){
//告知用户request today reached the request limit,err detail:超过请求上限限制 //告知用户request today reached the request limit,err detail:超过请求上限限制
$rs['errcode'] = 80102;
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
$rs['errcode'] = 80102;
$rs['data'] = null; $rs['data'] = null;
} }
//如果当天请求次数尚未达到接口请求次数限制 //如果当天请求次数尚未达到接口请求次数限制
else{ else{
//当天请求次数加一
$today_req_times = $today_req_times+1;
} }
} }
//如果上次请求日期不是当天日期 //如果上次请求日期不是当天日期
@ -93,17 +95,66 @@ class Oauth2Controller extends ControllerBase{
//如果接口请求次数限制不大于0 //如果接口请求次数限制不大于0
if($day_max_req_times<=0){ if($day_max_req_times<=0){
//告知用户request today reached the request limit,err detail:超过请求上限限制 //告知用户request today reached the request limit,err detail:超过请求上限限制
$rs['errcode'] = 80102;
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
$rs['errcode'] = 80102;
$rs['data'] = null; $rs['data'] = null;
} }
//如果接口请求次数限制大于0
else{
//初始当天请求次数
$today_req_times = 1;
}
} }
//如果以上判断都通过 //如果以上判断都通过
if(empty($rs['errcode'])){ 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,8 +168,19 @@ class Oauth2Controller extends ControllerBase{
* http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx * http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx
*/ */
function accesstokenAction(){ function accesstokenAction(){
//如果未获取到请求数据
if(empty($this->request->getPost())){
//告知用户系统繁忙,请稍候再试
$rs['errcode'] = -1;
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['data'] = null;
}
//如果有获取到请求数据
else{
} }
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
}
} }
?> ?>

View File

@ -3,7 +3,7 @@
* AccessToken表 * AccessToken表
* @author QZQ * @author QZQ
*/ */
class Accesstoken extends AppModel{ class Accesstoken extends ModelBase{
public function initialize(){ public function initialize(){
$this->setSource("smsapioauth2_accesstoken"); $this->setSource("smsapioauth2_accesstoken");

View File

@ -3,7 +3,7 @@
* Appinfo表 * Appinfo表
* @author QZQ * @author QZQ
*/ */
class Appinfo extends AppModel{ class Appinfo extends ModelBase{
public function initialize(){ public function initialize(){
$this->setSource("smsapioauth2_appinfo"); $this->setSource("smsapioauth2_appinfo");

View File

@ -3,7 +3,7 @@
* Refreshtoken表 * Refreshtoken表
* @author QZQ * @author QZQ
*/ */
class Refreshtoken extends AppModel{ class Refreshtoken extends ModelBase{
public function initialize(){ public function initialize(){
$this->setSource("smsapioauth2_refreshtoken"); $this->setSource("smsapioauth2_refreshtoken");

View File

@ -3,7 +3,7 @@
* Tempcode表 * Tempcode表
* @author QZQ * @author QZQ
*/ */
class Tempcode extends AppModel{ class Tempcode extends ModelBase{
public function initialize(){ public function initialize(){
$this->setSource("smsapioauth2_tempcode"); $this->setSource("smsapioauth2_tempcode");