ctms/dacms-api/controllers/AlimsgController.php
2025-04-10 23:19:13 +08:00

230 lines
8.3 KiB
PHP
Executable File

<?php
/**
* @Author: fm453
* @Date: 2018-06-04 11:06:07
* @Last Modified by: fm453
* @Last Modified time: 2019-11-11 13:11:55
* @Email: fm453@lukegzs.com
*/
namespace frontend\controllers;
use Yii;
use yii\helpers\Url;
use vendor\aliyun\dysms\Sms;
require dirname(__FILE__).'/_public/public.php';
class AlimsgController extends \yii\web\Controller
{
public $enableCsrfValidation = false;//取消对POST数据的csrf令牌验证
public function actionIndex()
{
global $_HI;
global $_FM;
$_HI['action'] = 'index';
/*自定义常量*/
/*默认配置*/
$_HI['accessKeyId'] = 'LTAIQrJFtHfqsyOt'; // AccessKeyId
$_HI['accessKeySecret'] = 'tvGieI3vNI7Si3imauCB4rJ3OX4AR8'; // AccessKeySecret
$_HI['sign'] = '嗨路客';
$_HI['tmpl'] = 'SMS_116695055';
$_HI['reportQueueName'] = 'Alicom-Queue-31737903-SmsReport'; //状态报告接收
$_HI['UpQueueName'] = 'Alicom-Queue-31737903-SmsUp'; //上行消息接收
return $this->render('index');
}
public function actionSend()
{
global $_HI;
global $_FM;
global $START_TIME;
$_HI['action'] = 'post';
//数据取出方法
$dataPost = Yii::$app->request->post(); //YII应用内的真实或模拟POST数据
$dataGet = Yii::$app->request->get(); //YII应用内的真实或模拟GET数据
$params = Yii::$app->params;
$config = array();
$config['accessKeyId'] = isset($dataPost['accessKeyId']) ? $dataPost['accessKeyId'] : '';
$config['accessKeySecret'] = isset($dataPost['accessKeySecret']) ? $dataPost['accessKeySecret'] : '';
$config['sign'] = isset($dataPost['sign']) ? $dataPost['sign'] : '';
$option = array();
$_content = isset($dataPost['content']) ? $dataPost['content'] : '';
$_content = explode("\r\n",$_content);
$content = array();
foreach($_content as $c){
$_c = explode(',', $c);
$k = trim($_c[0]);
$v = trim($_c[1]);
$content[$k] = $v;
}
unset($_c);unset($k);unset($v);
$phone = isset($dataPost['phone']) ? $dataPost['phone'] : '';
if($phone){
$phone = explode(',', $phone);
$phone = array_unique($phone); //去重
$phone = array_filter($phone); //去空
}
$tmpl = isset($dataPost['tmpl']) ? $dataPost['tmpl'] : '';
if(isset($_GET['post']) && $_GET['post']==1){
$_POST = array('config'=>$config,'option'=>$option,'phone'=>$phone,'content'=>$content,'tmpl'=>$tmpl);
Yii::$app->request->setBodyParams($_POST); //重写模拟POST数据
// Yii::$app->request->setQueryParams($_POST); //重写模拟GET数据
return $this->actionPost($_POST);
}
$sms = new Sms($option,$config);
$results = array();
$msg = "";
foreach($phone as $p){
$result = $sms->send($p,$tmpl,$content);
$results[$p] = $result;
if(!$result){
$msg .= "".$p."】发送失败,接口提示:".$sms->errors."\r\n";
}elseif(is_array($result) && $result['Code'] !='OK'){
$msg .= "".$p."】发送失败,接口提示:".$result['Message']."\r\n";
}else{
$msg .= "".$p."】发送成功"."\r\n";
}
}
$message = array();
$message['title'] = "阿里云短信发送测试";
$message['class'] = "success";
$message['button'] = array();
$message['content'] = "短信发送完毕;接口返回:".$msg;
$message['button'][] = array("class"=>"btn btn-success","title"=>"首页","link"=>Url::toRoute('site/index'));
$message['button'][] = array("class"=>"btn btn-info","title"=>"接口","link"=>Url::toRoute(['api/index', 'appid' => Yii::$app->user->identity->username]));
return $this->render('//layouts/message.php',[
'message'=>$message
]);
}
public function actionPost()
{
global $_HI;
global $_FM;
global $START_TIME;
$_HI['action'] = 'post';
//数据取出方法
$dataPost = Yii::$app->request->post(); //YII应用内的真实或模拟POST数据
$dataGet = Yii::$app->request->get(); //YII应用内的真实或模拟GET数据
$params = Yii::$app->params;
fmFunc_stat_times_in($username=$_FM['user']['name'],1); //增加一次接口入站次数累计
fmFunc_stat_bytes_in($username=$_FM['user']['name'],strlen(json_encode($dataPost))); //增加一次接口入站数据量累计
$return = array();
$results = array();
$msg = "";
$errorCode = 0;
if(!isset($dataPost['config']) || !is_array($dataPost['config'])){
$errorCode = '45300403';
$return['errorcode'] = $errorCode;
$return['msg'] = Yii::t('errorcode',$errorCode);
return json_encode($return);
}
$config = $dataPost['config'];
if(isset($dataPost['option'])){
if(!is_array($dataPost['option'])){
$errorCode = '45300404';
$return['errorcode'] = $errorCode;
$return['msg'] = Yii::t('errorcode',$errorCode);
return json_encode($return);
}
$option = $dataPost['option'];
}else{
$option = array();
}
if(!isset($dataPost['tmpl']) || !is_string($dataPost['tmpl'])){
$errorCode = '45300405';
$return['errorcode'] = $errorCode;
$return['msg'] = Yii::t('errorcode',$errorCode);
return json_encode($return);
}
$tmpl = $dataPost['tmpl'];
if(!isset($dataPost['phone']) || !is_array($dataPost['phone'])){
$errorCode = '45300406';
$return['errorcode'] = $errorCode;
$return['msg'] = Yii::t('errorcode',$errorCode);
return json_encode($return);
}
$phone = $dataPost['phone'];
if(isset($dataPost['content'])){
if(!is_array($dataPost['content'])){
$errorCode = '45300407';
$return['errorcode'] = $errorCode;
$return['msg'] = Yii::t('errorcode',$errorCode);
return json_encode($return);
}
$content = $dataPost['content'];
}else{
$content = array();
}
$sms = new Sms($option,$config);
$error = $sms->errors;
if($error){
$errorCode = '45300402';
$return['errorcode'] = $errorCode;
$return['msg'] = Yii::t('errorcode',$errorCode).";接口提示:".$sms->errors.";";;
return json_encode($return);
}
$results = array();
$msg = "";
$errors = 0;
$pn = count($phone);
foreach($phone as $p){
$result = $sms->send($p,$tmpl,$content);
var_dump($result);die();
$results[$p] = $result;
if(!$result){
$msg .= "".$p."】发送失败,接口提示:".$sms->errors.";";
$errors += 1;
}elseif(is_array($result) && $result['Code'] !='OK'){
$msg .= "".$p."】发送失败,接口提示:".$result['Message'].";";
$errors += 1;
}else{
$msg .= "".$p."】发送成功".";";
}
}
if($errors>0){
if($errors == $pn){
$errorcode = 45300402;
}else{
$errorcode = 45300401;
}
}
$return['errorcode'] = $errorCode;
$return['msg'] = Yii::t('errorcode',$errorCode).";".$msg;
$return['data'] = $dataPost;
$return['timeused'] = getMillisecond() - $START_TIME;
$return['total'] = isset($count) ? $count : 1;//总记录数
$return['bytes'] = strlen(json_encode($return['data']));//长度字节统计
fmFunc_stat_times_out($username=$_FM['user']['name'],1); //增加一次接口出站次数累计
fmFunc_stat_bytes_out($username=$_FM['user']['name'],$return['bytes']); //增加一次接口出站数据量累计
$return['sms_left'] = fmFunc_stat_sms_send($username=$_FM['user']['name'],$times=$pn-$errors); //统计短信发送并返回剩余条数
return json_encode($return);
}
}