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); } }