api/app/controllers/Oauth2Controller.php

186 lines
5.9 KiB
PHP
Raw Normal View History

2023-04-07 19:05:18 +08:00
<?php
/**
* Tiffany SMS API重构-接口类
* @author QZQ
*/
header("Content-Type:text/html; Charset=utf-8");
class Oauth2Controller extends ControllerBase{
function initialize(){
parent::initialize();
}
2023-04-10 12:49:07 +08:00
/**
* 测试方法
* http://127.0.0.1/tiffany/api/oauth2/test?qaz=wsx
*/
function testAction(){
$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/oauth2/code";
2023-04-10 16:09:25 +08:00
$url = "http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx";
2023-04-10 12:49:07 +08:00
$post_arr = array();
2023-04-10 14:50:55 +08:00
//$post_arr['appid'] = 111;
2023-04-10 12:49:07 +08:00
$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;
}
2023-04-07 19:05:18 +08:00
/**
* 获取codeGet code
* http://127.0.0.1/tiffany/api/oauth2/code?qaz=wsx
*/
function codeAction(){
2023-04-10 14:50:55 +08:00
//如果未获取到请求数据
2023-04-10 16:09:25 +08:00
if(empty(file_get_contents("php://input"))){
2023-04-10 14:50:55 +08:00
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
2023-04-10 16:09:25 +08:00
$rs['errcode'] = -1;
2023-04-10 14:50:55 +08:00
$rs['data'] = null;
}
//如果有获取到请求数据
else{
//准备获取appid
2023-04-10 16:09:25 +08:00
$request_arr = json_decode(file_get_contents("php://input"), true);
2023-04-10 14:50:55 +08:00
//如果未获取到appid
if(empty($request_arr['appid'])){
//告知用户参数错误 error detail:appid is empty;
$rs['errmsg'] = "参数错误 error detail:appid is empty;";
2023-04-10 16:09:25 +08:00
$rs['errcode'] = 61451;
2023-04-10 14:50:55 +08:00
$rs['data'] = null;
}
//如果有获取到appid
else{
//准备判断appid是否有效
$appid = $request_arr['appid'];
//查询接口信息
2023-04-10 16:12:59 +08:00
$app_info = AppInfo::findFirst("AppId = '$appid' AND Enable = 0");
2023-04-10 14:50:55 +08:00
//如果未查询到接口信息
2023-04-10 16:12:59 +08:00
if(empty($app_info)){
2023-04-10 14:50:55 +08:00
//告知用户找不到app配置信息,appid无效,err detail:找不到app配置信息
$rs['errmsg'] = "找不到app配置信息,appid无效,err detail:找不到app配置信息";
2023-04-10 16:09:25 +08:00
$rs['errcode'] = 40036;
2023-04-10 14:50:55 +08:00
$rs['data'] = null;
}
//如果有查询到接口信息
else{
//获取上次请求日期
2023-04-10 16:12:59 +08:00
$last_req_time = $app_info->LastReqTime;
2023-04-10 14:50:55 +08:00
$last_req_date = date("Y-m-d", strtotime($last_req_time));
2023-04-10 15:24:40 +08:00
//获取当前时间及当天日期
$current_time = time();
$current_datetime = date("Y-m-d H:i:s", $current_time);
$current_date = date("Y-m-d", $current_time);
//获取上次请求日期所在日的请求次数
2023-04-10 16:12:59 +08:00
$today_req_times = $app_info->TodayReqTimes;
2023-04-10 15:24:40 +08:00
//获取接口请求次数限制
2023-04-10 16:12:59 +08:00
$day_max_req_times = $app_info->DayMaxReqTimes;
2023-04-10 15:24:40 +08:00
//如果上次请求日期是当天日期
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:超过请求上限限制";
2023-04-10 16:09:25 +08:00
$rs['errcode'] = 80102;
2023-04-10 15:24:40 +08:00
$rs['data'] = null;
}
//如果当天请求次数尚未达到接口请求次数限制
else{
2023-04-10 16:09:25 +08:00
//当天请求次数加一
$today_req_times = $today_req_times+1;
2023-04-10 15:24:40 +08:00
}
}
//如果上次请求日期不是当天日期
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:超过请求上限限制";
2023-04-10 16:09:25 +08:00
$rs['errcode'] = 80102;
2023-04-10 15:24:40 +08:00
$rs['data'] = null;
}
2023-04-10 16:09:25 +08:00
//如果接口请求次数限制大于0
else{
//初始当天请求次数
$today_req_times = 1;
}
2023-04-10 15:24:40 +08:00
}
//如果以上判断都通过
if(empty($rs['errcode'])){
2023-04-10 16:09:25 +08:00
//准备更新当天请求次数
2023-04-10 16:12:59 +08:00
$app_info->LastModifiedTime = $current_datetime;
2023-04-10 16:09:25 +08:00
//更新字段
2023-04-10 16:12:59 +08:00
$app_info->TodayReqTimes = $today_req_times;
$app_info->LastReqTime = $current_date;
2023-04-10 16:09:25 +08:00
//如果更新失败
2023-04-10 16:12:59 +08:00
if(!($app_info->save())){
2023-04-10 16:09:25 +08:00
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果更新成功
else{
//查询当前接口今天是否有生成过code
2023-04-10 16:12:59 +08:00
$temp_code = TempCode::findFirst("AppId = '$appid'");
2023-04-10 16:09:25 +08:00
//如果当前接口过去未生成过code
2023-04-10 16:12:59 +08:00
if(empty($temp_code)){
2023-04-10 16:09:25 +08:00
//准备新增临时code
2023-04-10 16:12:59 +08:00
$temp_code = new TempCode();
$temp_code->AppId = $appid;
$temp_code->CreateTime = $current_datetime;
$temp_code->LastModifiedTime = $current_datetime;
2023-04-10 16:09:25 +08:00
}
//如果当前接口过去有生成过code
else{
//准备更新临时code
2023-04-10 16:12:59 +08:00
$temp_code->LastModifiedTime = $current_datetime;
2023-04-10 16:09:25 +08:00
}
//更新字段
2023-04-10 16:12:59 +08:00
$temp_code->Code = $code = $this->_get_act_code(32);
$temp_code->IsUsed = 0;
$temp_code->ExpireTime = date("Y-m-d H:i:s", $current_time+300);
2023-04-10 16:09:25 +08:00
//如果保存失败
2023-04-10 16:12:59 +08:00
if(!($temp_code->save())){
2023-04-10 16:09:25 +08:00
//告知用户系统繁忙,请稍候再试
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['errcode'] = -1;
$rs['data'] = null;
}
//如果保存成功
else{
//告知用户ok
$rs['errmsg'] = "ok";
$rs['errcode'] = 0;
$rs['data']['code'] = $code;
}
}
2023-04-10 15:24:40 +08:00
}
2023-04-10 14:50:55 +08:00
}
}
}
//返回结果
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
2023-04-07 19:05:18 +08:00
}
/**
* 根据code获取access_tokenGet access token by code
* http://127.0.0.1/tiffany/api/oauth2/accesstoken?qaz=wsx
*/
function accesstokenAction(){
2023-04-10 16:09:25 +08:00
//如果未获取到请求数据
if(empty($this->request->getPost())){
//告知用户系统繁忙,请稍候再试
$rs['errcode'] = -1;
$rs['errmsg'] = "系统繁忙,请稍候再试";
$rs['data'] = null;
}
//如果有获取到请求数据
else{
}
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
2023-04-07 19:05:18 +08:00
}
}
?>