api/app/controllers/SmsController.php

445 lines
16 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 SmsController extends ControllerBase{
function initialize(){
parent::initialize();
}
/**
2023-04-11 12:21:22 +08:00
* 测试方法
* http://127.0.0.1/tiffany/api/sms/test?qaz=wsx
* http://weapp.wemediacn.net/d/tiffany/api/sms/test?qaz=wsx
2023-04-07 19:05:18 +08:00
*/
2023-04-11 12:21:22 +08:00
function testAction(){
2023-04-12 17:23:08 +08:00
//$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/sms/send";
2023-04-12 17:52:07 +08:00
//$url = "http://127.0.0.1/tiffany/api/sms/send?qaz=wsx";
//$url = "http://wxapp.wemediacn.com/smsoauth2_qa/api/sms/authcode";
$url = "http://127.0.0.1/tiffany/api/sms/authcode?qaz=wsx";
2023-04-11 12:21:22 +08:00
$post_arr = array();
2023-04-12 17:52:07 +08:00
$post_arr['appid'] = "";
$post_arr['mobile'] = "";
$post_arr['access_token'] = "";
$post_arr['content'] = "";
$post_arr['format'] = "";
$post_arr['len'] = "";
$post_arr['type'] = "";
$post_arr['is_resend'] = "";
2023-04-11 12:21:22 +08:00
$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
}
/**
* 发送短信Send SMS
* http://127.0.0.1/tiffany/api/sms/send?qaz=wsx
2023-04-11 12:21:22 +08:00
* http://weapp.wemediacn.net/d/tiffany/api/sms/send?qaz=wsx
2023-04-07 19:05:18 +08:00
*/
function sendAction(){
2023-04-11 12:21:22 +08:00
//如果是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{
2023-04-12 17:52:07 +08:00
//准备获取appid、手机号、access_token和内容
2023-04-11 12:21:22 +08:00
$rs['errmsg'] = "";
//如果未获取到appid
if(empty($request_arr['appid'])){
//告知用户appid is empty;
$rs['errmsg'].= "appid is empty;";
}
//如果未获取到手机号
if(empty($request_arr['mobile'])){
//告知用户mobile is empty;
$rs['errmsg'].= "mobile is empty;";
}
2023-04-12 17:52:07 +08:00
//如果未获取到access_token
if(empty($request_arr['access_token'])){
//告知用户access_token is empty;
$rs['errmsg'].= "access_token is empty;";
}
2023-04-11 12:21:22 +08:00
//如果未获取到内容
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{
2023-04-11 15:10:31 +08:00
//准备判断access_token是否有效
$access_token = $request_arr['access_token'];
//查询接口信息
$AccessToken = AccessToken::findFirst("AppId = '$appid' AND (AccessToken = '$access_token' OR PrevAccessToken = '$access_token')");
//如果未查询到接口信息
if(empty($AccessToken)){
//告知用户accesstoken 不正确,err detail:找不到accesstoken信息
$rs['errmsg'] = "accesstoken 不正确,err detail:找不到accesstoken信息";
$rs['errcode'] = 40039;
$rs['data'] = null;
}
//如果有查询到接口信息
else{
//获取现access_token到期时间
$ExpireTime = $AccessToken->ExpireTime;
//获取前access_token到期时间
$PrevExpireTime = date("Y-m-d H:i:s", strtotime($AccessToken->ExpireTime)-14400+600);
//如果access_token匹配前access_token
if($access_token==$AccessToken->PrevAccessToken){
//如果前access_token已过期
if($current_datetime>$PrevExpireTime){
//告知用户access_token is expired,err detail:accesstoken 已过期
$rs['errmsg'] = "access_token is expired,err detail:accesstoken 已过期";
$rs['errcode'] = 80105;
$rs['data'] = null;
}
}
//如果access_token匹配现access_token
else{
//如果现access_token已过期
if($current_datetime>$ExpireTime){
//告知用户access_token is expired,err detail:accesstoken 已过期
$rs['errmsg'] = "access_token is expired,err detail:accesstoken 已过期";
$rs['errcode'] = 80105;
$rs['data'] = null;
}
}
//如果以上判断都通过
if(empty($rs['errcode'])){
2023-04-12 17:23:08 +08:00
//设置手机号
$mobile = $request_arr['mobile'];
//设置FormatID
$FormatID = !empty($request_arr['format'])?$request_arr['format']:8;
//设置内容
$Content = urlencode($request_arr['content']);
//设置TokenID
//$TokenID = $AppInfo->SmsToken;
$TokenID = "7100477830459267";
//准备发送短信
$url = "http://www.wemediacn.net/webservice/smsservice.asmx/SendSMS?mobile=$mobile&FormatID=$FormatID&Content=$Content&ScheduleDate=2010-1-1&TokenID=$TokenID";
//获取发送结果
$xml = $this->__https_request($url);
//转换发送结果格式
$array = $this->__change_xml_to_array($xml);
//获取发送结果提示
$string = !empty($array[0])?$array[0]:"";
//如果发送失败
if(!(strpos(strtolower($xml), "ok")!==false)){
//告知用户发送失败,err detail:短信发送失败:
//ERROR:200:[非手机号码]
//ERROR:108:[FormatID 非法数值]
$rs['errmsg'] = "发送失败,err detail:短信发送失败:".$string;
$rs['errcode'] = 70000;
$rs['data'] = null;
}
//如果发送成功
else{
//获取messageid
//OK:[202304121675878871004778]
$messageid = str_replace("]", "", str_replace("OK:[", "", $string));
//告知用户ok
$rs['errmsg'] = "ok";
$rs['errcode'] = 0;
$rs['data']['messageid'] = $messageid;
}
2023-04-11 15:10:31 +08:00
}
}
2023-04-11 12:21:22 +08:00
}
}
}
}
}
}
2023-04-12 17:53:58 +08:00
//输出结果
2023-04-11 12:21:22 +08:00
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
2023-04-07 19:05:18 +08:00
}
/**
* 获取手机验证码Get mobile verification
* http://127.0.0.1/tiffany/api/sms/authcode?qaz=wsx
2023-04-11 12:21:22 +08:00
* http://weapp.wemediacn.net/d/tiffany/api/sms/authcode?qaz=wsx
2023-04-07 19:05:18 +08:00
*/
function authcodeAction(){
2023-04-12 17:52:07 +08:00
//如果是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
$rs['errmsg'] = "";
//如果未获取到appid
if(empty($request_arr['appid'])){
//告知用户appid is empty;
$rs['errmsg'].= "appid is empty;";
}
//如果未获取到手机号
if(empty($request_arr['mobile'])){
//告知用户mobile is empty;
$rs['errmsg'].= "mobile is empty;";
}
//如果未获取到access_token
if(empty($request_arr['access_token'])){
//告知用户access_token is empty;
$rs['errmsg'].= "access_token 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{
//准备判断access_token是否有效
$access_token = $request_arr['access_token'];
//查询接口信息
$AccessToken = AccessToken::findFirst("AppId = '$appid' AND (AccessToken = '$access_token' OR PrevAccessToken = '$access_token')");
//如果未查询到接口信息
if(empty($AccessToken)){
//告知用户accesstoken 不正确,err detail:找不到accesstoken信息
$rs['errmsg'] = "accesstoken 不正确,err detail:找不到accesstoken信息";
$rs['errcode'] = 40039;
$rs['data'] = null;
}
//如果有查询到接口信息
else{
//获取现access_token到期时间
$ExpireTime = $AccessToken->ExpireTime;
//获取前access_token到期时间
$PrevExpireTime = date("Y-m-d H:i:s", strtotime($AccessToken->ExpireTime)-14400+600);
//如果access_token匹配前access_token
if($access_token==$AccessToken->PrevAccessToken){
//如果前access_token已过期
if($current_datetime>$PrevExpireTime){
//告知用户access_token is expired,err detail:accesstoken 已过期
$rs['errmsg'] = "access_token is expired,err detail:accesstoken 已过期";
$rs['errcode'] = 80105;
$rs['data'] = null;
}
}
//如果access_token匹配现access_token
else{
//如果现access_token已过期
if($current_datetime>$ExpireTime){
//告知用户access_token is expired,err detail:accesstoken 已过期
$rs['errmsg'] = "access_token is expired,err detail:accesstoken 已过期";
$rs['errcode'] = 80105;
$rs['data'] = null;
}
}
//如果以上判断都通过
if(empty($rs['errcode'])){
}
}
}
}
}
}
}
}
2023-04-12 17:53:58 +08:00
//输出结果
echo json_encode($rs, JSON_UNESCAPED_UNICODE);
2023-04-07 19:05:18 +08:00
}
/**
* 批量查询最新短信发送结果Batch query for the latest SMS sending result
* http://127.0.0.1/tiffany/api/sms/query_reports?qaz=wsx
*/
function queryReportsAction(){
}
}
?>