提交进度

This commit is contained in:
qinzongqing 2023-04-11 12:21:22 +08:00
parent f60aa5235b
commit 8ea15ab3ca
2 changed files with 450 additions and 286 deletions

View File

@ -14,16 +14,17 @@ class Oauth2Controller extends ControllerBase{
/** /**
* 测试方法 * 测试方法
* http://127.0.0.1/tiffany/api/oauth2/test?qaz=wsx * http://127.0.0.1/tiffany/api/oauth2/test?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/oauth2/test?qaz=wsx
*/ */
function testAction(){ function testAction(){
//$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/accesstoken";
//$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://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx"; $url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/accesstoken";
//$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'] = "j2407ptyalr6is31fc8ogez9quvxhmwd"; $post_arr['code'] = "e985c2245a7445cb8487d9a4dd4d1c2a";
$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;
@ -32,6 +33,7 @@ class Oauth2Controller extends ControllerBase{
/** /**
* 获取codeGet code * 获取codeGet code
* http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx * http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/oauth2/code?qaz=wsx
*/ */
function codeAction(){ function codeAction(){
//如果未获取到请求数据 //如果未获取到请求数据
@ -43,119 +45,136 @@ class Oauth2Controller extends ControllerBase{
} }
//如果有获取到请求数据 //如果有获取到请求数据
else{ else{
//准备获取appid //准备判断请求数据整体是否为空
$request_arr = json_decode(file_get_contents("php://input"), true); $request_arr = json_decode(file_get_contents("php://input"), true);
//如果未获取到appid //如果请求数据整体为空
if(empty($request_arr['appid'])){ if(empty($request_arr)){
//告知用户参数错误 error detail:appid is empty; //告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "参数错误 error detail:appid is empty;"; $rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = 61451; $rs['errcode'] = -1;
$rs['data'] = null; $rs['data'] = null;
} }
//如果有获取到appid //如果请求数据整体不为空
else{ else{
//准备判断appid是否有效 //准备获取appid
$appid = $request_arr['appid']; $rs['errmsg'] = "";
//查询接口信息 //如果未获取到appid
$AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0"); if(empty($request_arr['appid'])){
//如果未查询到接口信息 //告知用户appid is empty;
if(empty($AppInfo)){ $rs['errmsg'].= "appid is empty;";
//告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息 }
$rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息"; //如果以上判断未通过
$rs['errcode'] = 40036; if(!empty($rs['errmsg'])){
//告知用户参数错误 error detail:
$rs['errmsg'] = "参数错误 error detail:".$rs['errmsg'];
$rs['errcode'] = 61451;
$rs['data'] = null; $rs['data'] = null;
} }
//如果有查询到接口信息 //如果以上判断都通过
else{ else{
//获取上次请求日期 //准备判断appid是否有效
$last_req_time = $AppInfo->LastReqTime; $appid = $request_arr['appid'];
$last_req_date = date("Y-m-d", strtotime($last_req_time)); //查询接口信息
//获取当前时间及当天日期 $AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0");
$current_time = time(); //如果未查询到接口信息
$current_datetime = date("Y-m-d H:i:s", $current_time); if(empty($AppInfo)){
$current_date = date("Y-m-d", $current_time); //告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息
//获取上次请求日期所在日的请求次数 $rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息";
$today_req_times = $AppInfo->TodayReqTimes; $rs['errcode'] = 40036;
//获取接口请求次数限制 $rs['data'] = null;
$day_max_req_times = $AppInfo->DayMaxReqTimes;
//如果上次请求日期是当天日期
if($last_req_date==$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{ else{
//如果接口请求次数限制不大于0 //获取上次请求日期
if($day_max_req_times<=0){ $last_req_time = $AppInfo->LastReqTime;
//告知用户request today reached the request limit,err detail:超过请求上限限制 $last_req_date = date("Y-m-d", strtotime($last_req_time));
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; //获取当前时间及当天日期
$rs['errcode'] = 80102; $current_time = time();
$rs['data'] = null; $current_datetime = date("Y-m-d H:i:s", $current_time);
} $current_date = date("Y-m-d", $current_time);
//如果接口请求次数限制大于0 //获取上次请求日期所在日的请求次数
else{ $today_req_times = $AppInfo->TodayReqTimes;
//初始当天请求次数 //获取接口请求次数限制
$today_req_times = 1; $day_max_req_times = $AppInfo->DayMaxReqTimes;
} //如果上次请求日期是当天日期
} if($last_req_date==$current_date){
//如果以上判断都通过 //如果当天请求次数已达到接口请求次数限制
if(empty($rs['errcode'])){ if($today_req_times>=$day_max_req_times){
//准备更新当天请求次数 //告知用户request today reached the request limit,err detail:超过请求上限限制
$AppInfo->LastModifiedTime = $current_datetime; $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
//其他字段 $rs['errcode'] = 80102;
$AppInfo->TodayReqTimes = $today_req_times; $rs['data'] = null;
$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{ else{
//准备更新临时code //当天请求次数加一
$TempCode->LastModifiedTime = $current_datetime; $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 = $current_datetime;
//其他字段 //其他字段
$TempCode->Code = $code = $this->_get_act_code(32); $AppInfo->TodayReqTimes = $today_req_times;
$TempCode->IsUsed = 0; $AppInfo->LastReqTime = $current_date;
$TempCode->ExpireTime = date("Y-m-d H:i:s", $current_time+300); //如果更新失败
//如果保存失败 if(!($AppInfo->save())){
if(!($TempCode->save())){
//告知用户系统繁忙,请稍候再试 //告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试"; $rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1; $rs['errcode'] = -1;
$rs['data'] = null; $rs['data'] = null;
} }
//如果保存成功 //如果更新成功
else{ else{
//告知用户ok //查询当前接口过去是否有生成过code
$rs['errmsg'] = "ok"; $TempCode = TempCode::findFirst("AppId = '$appid'");
$rs['errcode'] = 0; //如果当前接口过去未生成过code
$rs['data']['code'] = $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;
}
} }
} }
} }
@ -169,202 +188,182 @@ class Oauth2Controller extends ControllerBase{
/** /**
* 根据code获取access_tokenGet access token by code * 根据code获取access_tokenGet access token by code
* http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx * http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/oauth2/accesstoken?qaz=wsx
*/ */
function accesstokenAction(){ function accesstokenAction(){
//如果未获取到请求数据 //如果未获取到请求数据
if(empty(file_get_contents("php://input"))){ 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、appsecrect和code //准备判断请求数据整体是否为空
//注意:
//接口中的appsecrect字段本身就是用了错别词比secret多了一个字母
//但接口信息表AppInfo里的AppSecret字段则没有多出一个字母要当心
$request_arr = json_decode(file_get_contents("php://input"), true); $request_arr = json_decode(file_get_contents("php://input"), true);
//如果未获取到appid //如果请求数据整体为空
if(empty($request_arr['appid'])){ if(empty($request_arr)){
//告知用户appid is empty; //告知用户系统繁忙,请稍候再试
$rs['errmsg'].= "appid is empty;"; $rs['errmsg'] = "系统繁忙,请稍候再试";
} $rs['errcode'] = -1;
//如果未获取到appsecrect
if(empty($request_arr['appsecrect'])){
//告知用户appsecrect is empty;
$rs['errmsg'].= "appsecrect is empty;";
}
//如果未获取到code
if(empty($request_arr['code'])){
//告知用户code is empty;
$rs['errmsg'].= "code is empty;";
}
//如果以上判断未通过
if(!empty($rs['errmsg'])){
//告知用户参数错误 error detail:
$rs['errmsg'] = "参数错误 error detail:".$rs['errmsg'];
$rs['errcode'] = 61451;
$rs['data'] = null; $rs['data'] = null;
} }
//如果以上判断都通过 //如果请求数据整体不为空
else{ else{
//准备判断appid是否有效 //准备获取appid、appsecrect和code
$appid = $request_arr['appid']; //注意:
//查询接口信息 //接口中的appsecrect字段本身就是用了错别词比secret多了一个字母
$AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0"); //但接口信息表AppInfo里的AppSecret字段则没有多出一个字母要当心
//如果未查询到接口信息 $rs['errmsg'] = "";
if(empty($AppInfo)){ //如果未获取到appid
//告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息 if(empty($request_arr['appid'])){
$rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息"; //告知用户appid is empty;
$rs['errcode'] = 40036; $rs['errmsg'].= "appid is empty;";
}
//如果未获取到appsecrect
if(empty($request_arr['appsecrect'])){
//告知用户appsecrect is empty;
$rs['errmsg'].= "appsecrect is empty;";
}
//如果未获取到code
if(empty($request_arr['code'])){
//告知用户code is empty;
$rs['errmsg'].= "code is empty;";
}
//如果以上判断未通过
if(!empty($rs['errmsg'])){
//告知用户参数错误 error detail:
$rs['errmsg'] = "参数错误 error detail:".$rs['errmsg'];
$rs['errcode'] = 61451;
$rs['data'] = null; $rs['data'] = null;
} }
//如果有查询到接口信息 //如果以上判断都通过
else{ else{
//获取上次请求日期 //准备判断appid是否有效
$last_req_time = $AppInfo->LastReqTime; $appid = $request_arr['appid'];
$last_req_date = date("Y-m-d", strtotime($last_req_time)); //查询接口信息
//获取当前时间及当天日期 $AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0");
$current_time = time(); //如果未查询到接口信息
$current_datetime = date("Y-m-d H:i:s", $current_time); if(empty($AppInfo)){
$current_date = date("Y-m-d", $current_time); //告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息
//获取上次请求日期所在日的请求次数 $rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息";
$today_req_times = $AppInfo->TodayReqTimes; $rs['errcode'] = 40036;
//获取接口请求次数限制 $rs['data'] = null;
$day_max_req_times = $AppInfo->DayMaxReqTimes;
//如果上次请求日期是当天日期
if($last_req_date==$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{ else{
//如果接口请求次数限制不大于0 //获取上次请求日期
if($day_max_req_times<=0){ $last_req_time = $AppInfo->LastReqTime;
//告知用户request today reached the request limit,err detail:超过请求上限限制 $last_req_date = date("Y-m-d", strtotime($last_req_time));
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制"; //获取当前时间及当天日期
$rs['errcode'] = 80102; $current_time = time();
$rs['data'] = null; $current_datetime = date("Y-m-d H:i:s", $current_time);
} $current_date = date("Y-m-d", $current_time);
//如果接口请求次数限制大于0 //获取上次请求日期所在日的请求次数
else{ $today_req_times = $AppInfo->TodayReqTimes;
//初始当天请求次数 //获取接口请求次数限制
$today_req_times = 1; $day_max_req_times = $AppInfo->DayMaxReqTimes;
} //如果上次请求日期是当天日期
} if($last_req_date==$current_date){
//如果以上判断都通过 //如果当天请求次数已达到接口请求次数限制
if(empty($rs['errcode'])){ if($today_req_times>=$day_max_req_times){
//准备更新当天请求次数 //告知用户request today reached the request limit,err detail:超过请求上限限制
$AppInfo->LastModifiedTime = $current_datetime; $rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
//其他字段 $rs['errcode'] = 80102;
$AppInfo->TodayReqTimes = $today_req_times;
$AppInfo->LastReqTime = $current_date;
//如果更新失败
if(!($AppInfo->save())){
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果更新成功
else{
//准备判断appsecrect是否匹配
$appsecrect = $request_arr['appsecrect'];
//如果appsecrect不匹配
if($appsecrect!=$AppInfo->AppSecret){
//告知用户密钥不匹配,err detail:密钥不匹配
$rs['errmsg'] = "密钥不匹配,err detail:密钥不匹配";
$rs['errcode'] = 40037;
$rs['data'] = null; $rs['data'] = null;
} }
//如果appsecrect匹配 //如果当天请求次数尚未达到接口请求次数限制
else{ else{
//准备判断code是否匹配 //当天请求次数加一
$code = $request_arr['code']; $today_req_times = $today_req_times+1;
//查询临时code }
$TempCode = TempCode::findFirst("AppId = '$appid' AND Code = '$code'"); }
//如果未查询到临时code //如果上次请求日期不是当天日期
if(empty($TempCode)){ else{
//告知用户code 不正确,err detail:code 不匹配 //如果接口请求次数限制不大于0
$rs['errmsg'] = "code 不正确,err detail:code 不匹配"; if($day_max_req_times<=0){
$rs['errcode'] = 40038; //告知用户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 = $current_datetime;
//其他字段
$AppInfo->TodayReqTimes = $today_req_times;
$AppInfo->LastReqTime = $current_date;
//如果更新失败
if(!($AppInfo->save())){
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果更新成功
else{
//准备判断appsecrect是否匹配
$appsecrect = $request_arr['appsecrect'];
//如果appsecrect不匹配
if($appsecrect!=$AppInfo->AppSecret){
//告知用户密钥不匹配,err detail:密钥不匹配
$rs['errmsg'] = "密钥不匹配,err detail:密钥不匹配";
$rs['errcode'] = 40037;
$rs['data'] = null; $rs['data'] = null;
} }
//如果有查询到临时code //如果appsecrect匹配
else{ else{
//准备判断code是否已失效 //准备判断code是否匹配
$expire_time = $TempCode->ExpireTime; $code = $request_arr['code'];
//如果code已失效 //查询临时code
if($current_datetime>=$expire_time){ $TempCode = TempCode::findFirst("AppId = '$appid' AND Code = '$code'");
//告知用户authorize code is expired,err detail:code 已过期 //如果未查询到临时code
$rs['errmsg'] = "authorize code is expired,err detail:code 已过期"; if(empty($TempCode)){
$rs['errcode'] = 80103; //告知用户code 不正确,err detail:code 不匹配
$rs['errmsg'] = "code 不正确,err detail:code 不匹配";
$rs['errcode'] = 40038;
$rs['data'] = null; $rs['data'] = null;
} }
//如果code未失效 //如果有查询到临时code
else{ else{
//准备判断code是否已使用过 //准备判断code是否已失效
$is_used = $TempCode->IsUsed; $expire_time = $TempCode->ExpireTime;
//如果code已使用过 //如果code已失效
if(!empty($is_used)){ if($current_datetime>=$expire_time){
//告知用户 //告知用户authorize code is expired,err detail:code 已过期
$rs['errmsg'] = "authorize code is used,err detail:code已使用"; $rs['errmsg'] = "authorize code is expired,err detail:code 已过期";
$rs['errcode'] = 80104; $rs['errcode'] = 80103;
$rs['data'] = null; $rs['data'] = null;
} }
//如果code未使用过 //如果code未失效
else{ else{
//开启事务 //准备判断code是否已使用过
$this->db->begin(); $is_used = $TempCode->IsUsed;
//准备更新临时code //如果code已使用过
$TempCode->LastModifiedTime = $current_datetime; if(!empty($is_used)){
//其他字段 //告知用户
$TempCode->IsUsed = 1; $rs['errmsg'] = "authorize code is used,err detail:code已使用";
//如果保存失败 $rs['errcode'] = 80104;
if(!($TempCode->save())){
//回滚事务
$this->db->rollback();
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null; $rs['data'] = null;
} }
//如果保存成功 //如果code未使用过
else{ else{
//查询当前接口过去是否有生成过access_token //开启事务
$AccessToken = AccessToken::findFirst("AppId = '$appid'"); $this->db->begin();
//如果当前接口过去未生成过access_token //准备更新临时code
if(empty($AccessToken)){ $TempCode->LastModifiedTime = $current_datetime;
//准备新增AccessToken
$AccessToken = new AccessToken();
$AccessToken->Id = $this->_get_act_code(32);
$AccessToken->AppId = $appid;
$AccessToken->PrevAccessToken = null;
$AccessToken->CreateTime = $current_datetime;
$AccessToken->LastModifiedTime = $current_datetime;
}
//如果当前接口过去有生成过access_token
else{
//准备更新AccessToken
$AccessToken->PrevAccessToken = $AccessToken->AccessToken;
$AccessToken->LastModifiedTime = $current_datetime;
}
//其他字段 //其他字段
$AccessToken->AccessToken = $access_token = $this->_get_act_code(32); $TempCode->IsUsed = 1;
$AccessToken->ExpireTime = date("Y-m-d H:i:s", $current_time+14400);
//如果保存失败 //如果保存失败
if(!($AccessToken->save())){ if(!($TempCode->save())){
//回滚事务 //回滚事务
$this->db->rollback(); $this->db->rollback();
//告知用户系统繁忙,请稍候再试 //告知用户系统繁忙,请稍候再试
@ -374,14 +373,47 @@ class Oauth2Controller extends ControllerBase{
} }
//如果保存成功 //如果保存成功
else{ else{
//执行事务 //查询当前接口过去是否有生成过access_token
$this->db->commit(); $AccessToken = AccessToken::findFirst("AppId = '$appid'");
//告知用户ok //如果当前接口过去未生成过access_token
$rs['errmsg'] = "ok"; if(empty($AccessToken)){
$rs['errcode'] = 0; //准备新增AccessToken
$rs['data']['access_token'] = $access_token; $AccessToken = new AccessToken();
$rs['data']['expire_in'] = 14400; $AccessToken->Id = $this->_get_act_code(32);
$rs['data']['refresh_token'] = null; $AccessToken->AppId = $appid;
$AccessToken->PrevAccessToken = null;
$AccessToken->CreateTime = $current_datetime;
$AccessToken->LastModifiedTime = $current_datetime;
}
//如果当前接口过去有生成过access_token
else{
//准备更新AccessToken
$AccessToken->PrevAccessToken = $AccessToken->AccessToken;
$AccessToken->LastModifiedTime = $current_datetime;
}
//其他字段
$AccessToken->AccessToken = $access_token = $this->_get_act_code(32);
$AccessToken->ExpireTime = date("Y-m-d H:i:s", $current_time+14400);
//如果保存失败
if(!($AccessToken->save())){
//回滚事务
$this->db->rollback();
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果保存成功
else{
//执行事务
$this->db->commit();
//告知用户ok
$rs['errmsg'] = "ok";
$rs['errcode'] = 0;
$rs['data']['access_token'] = $access_token;
$rs['data']['expire_in'] = 14400;
$rs['data']['refresh_token'] = null;
}
} }
} }
} }

View File

@ -12,32 +12,172 @@ class SmsController extends ControllerBase{
} }
/** /**
* 获取codeGet code * 测试方法
* http://127.0.0.1/tiffany/api/sms/code?qaz=wsx * http://127.0.0.1/tiffany/api/sms/test?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/sms/test?qaz=wsx
*/ */
function codeAction(){ function testAction(){
$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/sms/send";
} //$url = "http://127.0.0.1/tiffany/api/sms/send?qaz=wsx";
$post_arr = array();
/** $post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7";
* 根据code获取access_tokenGet access token by code $post_arr['access_token'] = "4ff40da297e143ed83f45b8499f64300";
* http://127.0.0.1/tiffany/api/sms/accesstoken?qaz=wsx $post_arr['mobile'] = "999";
*/ $post_arr['content'] = "test";
function accesstokenAction(){ $post_arr['format'] = "555";
$post_json = json_encode($post_arr);
$result = $this->__http_post_request($url, $post_json, true);
print_r($result); die;
} }
/** /**
* 发送短信Send SMS * 发送短信Send SMS
* http://127.0.0.1/tiffany/api/sms/send?qaz=wsx * http://127.0.0.1/tiffany/api/sms/send?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/sms/send?qaz=wsx
*/ */
function sendAction(){ function sendAction(){
//如果是GET请求
if($this->request->isGet()){
//告知请求的资源不支持 http 方法“GET”。
$rs['Message'] = "请求的资源不支持 http 方法“GET”。";
}
//如果未获取到请求数据
elseif(empty(file_get_contents("php://input"))){
//告知用户系统繁忙,请稍候再试
$rs['errcode'] = -1;
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['data'] = null;
}
//如果有获取到请求数据
else{
//准备判断请求数据整体是否为空
$request_arr = json_decode(file_get_contents("php://input"), true);
//如果请求数据整体为空
if(empty($request_arr)){
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果请求数据整体不为空
else{
//准备获取appid、access_token、手机号、内容和format
$rs['errmsg'] = "";
//如果未获取到appid
if(empty($request_arr['appid'])){
//告知用户appid is empty;
$rs['errmsg'].= "appid is empty;";
}
//如果未获取到access_token
if(empty($request_arr['access_token'])){
//告知用户access_token is empty;
$rs['errmsg'].= "access_token is empty;";
}
//如果未获取到手机号
if(empty($request_arr['mobile'])){
//告知用户mobile is empty;
$rs['errmsg'].= "mobile is empty;";
}
//如果未获取到内容
if(empty($request_arr['content'])){
//告知用户content is empty;
$rs['errmsg'].= "content is empty;";
}
//如果以上判断未通过
if(!empty($rs['errmsg'])){
//告知用户参数错误 error detail:
$rs['errmsg'] = "参数错误 error detail:".$rs['errmsg'];
$rs['errcode'] = 61451;
$rs['data'] = null;
}
//如果以上判断都通过
else{
//准备判断appid是否有效
$appid = $request_arr['appid'];
//查询接口信息
$AppInfo = AppInfo::findFirst("AppId = '$appid' AND Enable = 0");
//如果未查询到接口信息
if(empty($AppInfo)){
//告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息
$rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息";
$rs['errcode'] = 40036;
$rs['data'] = null;
}
//如果有查询到接口信息
else{
//获取上次请求日期
$last_req_time = $AppInfo->LastReqTime;
$last_req_date = date("Y-m-d", strtotime($last_req_time));
//获取当前时间及当天日期
$current_time = time();
$current_datetime = date("Y-m-d H:i:s", $current_time);
$current_date = date("Y-m-d", $current_time);
//获取上次请求日期所在日的请求次数
$today_req_times = $AppInfo->TodayReqTimes;
//获取接口请求次数限制
$day_max_req_times = $AppInfo->DayMaxReqTimes;
//如果上次请求日期是当天日期
if($last_req_date==$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 = $current_datetime;
//其他字段
$AppInfo->TodayReqTimes = $today_req_times;
$AppInfo->LastReqTime = $current_date;
//如果更新失败
if(!($AppInfo->save())){
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果更新成功
else{
//{"errmsg":"accesstoken 不正确,err detail:找不到accesstoken信息","errcode":40039,"data":null}
//{"errmsg":"发送失败,err detail:短信发送失败:ERROR:200:[非手机号码]","errcode":70000,"data":null}
//{"errmsg":"发送失败,err detail:短信发送失败:ERROR:108:[FormatID 非法数值]","errcode":70000,"data":null}
}
}
}
}
}
}
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
} }
/** /**
* 获取手机验证码Get mobile verification * 获取手机验证码Get mobile verification
* http://127.0.0.1/tiffany/api/sms/authcode?qaz=wsx * http://127.0.0.1/tiffany/api/sms/authcode?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/sms/authcode?qaz=wsx
*/ */
function authcodeAction(){ function authcodeAction(){
@ -51,13 +191,5 @@ class SmsController extends ControllerBase{
} }
/**
* 长链转短链Long URL shortening
* http://127.0.0.1/tiffany/api/sms/query_reports?qaz=wsx
*/
function shorturlAction(){
}
} }
?> ?>