api/app/controllers/Oauth2Controller.php
2023-04-10 15:24:40 +08:00

124 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* Tiffany SMS API重构-接口类
* @author QZQ
*/
header("Content-Type:text/html; Charset=utf-8");
class Oauth2Controller extends ControllerBase{
function initialize(){
parent::initialize();
}
/**
* 测试方法
* http://127.0.0.1/tiffany/api/oauth2/test?qaz=wsx
*/
function testAction(){
$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/code";
$post_arr = array();
//$post_arr['appid'] = 111;
$post_arr['appid'] = "0e355010-67b9-4aa6-a53f-c92c972094a7";
$post_json = json_encode($post_arr);
$result = $this->__http_post_request($url, $post_json, true);
print_r($result); die;
}
/**
* 获取codeGet code
* http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx
*/
function codeAction(){
//如果未获取到请求数据
if(empty($this->request->getPost())){
//告知用户系统繁忙,请稍候再试
$rs['errcode'] = -1;
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['data'] = null;
}
//如果有获取到请求数据
else{
//准备获取appid
$request_arr = json_decode($this->request->getPost(), true);
//如果未获取到appid
if(empty($request_arr['appid'])){
//告知用户参数错误 error detail:appid is empty;
$rs['errcode'] = 61451;
$rs['errmsg'] = "参数错误 error detail:appid is empty;";
$rs['data'] = null;
}
//如果有获取到appid
else{
//准备判断appid是否有效
$appid = $request_arr['appid'];
//查询接口信息
$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['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['errcode'] = 80102;
$rs['errmsg'] = "request today reached the request limit,err detail:超过请求上限限制";
$rs['data'] = null;
}
//如果当天请求次数尚未达到接口请求次数限制
else{
}
}
//如果上次请求日期不是当天日期
else{
//如果接口请求次数限制不大于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['data'] = null;
}
}
//如果以上判断都通过
if(empty($rs['errcode'])){
//开启事务
$this->db->begin();
//准备更新请求次数
}
}
}
}
//返回结果
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
}
/**
* 根据code获取access_tokenGet access token by code
* http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx
*/
function accesstokenAction(){
}
}
?>