ctms/ctms-api/controllers/client/v1/SmsController.php
fm453 314745edf8 优化ctms-api语法、修复已知BUG;
主要修复ctms-api、dacms对PHP新版本的支持问题
2025-04-10 23:19:15 +08:00

363 lines
12 KiB
PHP
Executable File
Raw Permalink 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
# @Author: 嗨噜客(三亚) <fm453>
# @Date: 2022-05-22T07:38:52+08:00
# @Email: fm453@lukegzs.com
# @Last modified by: fm453
# @Last modified time: 2024-08-09T08:53:58+08:00
# @Copyright: www.hiluker.cn
namespace api\controllers\client\v1;
use Yii;
use vendor\aliyun\dysms\Sms;
use common\models\CSms;
use common\models\CVcode;
use addons\models\AcPlat;
class SmsController extends Common
{
public function beforeAction($action)
{
if (!$this->pid) {
$this->result('您正使用本系统内部接口,禁止非法链接使用!');
}
return parent::beforeAction($action);
}
public function actionIndex()
{
$apis = [
'vcode'=>'获取短信验证码'
];
$this->result('您正使用CMTS-CLIENT系统短信管理接口', $apis, 200);
}
public function actionSend()
{
global $START_TIME;
$session = Yii::$app->session;
//数据取出方法
$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'] : $params['aliDySms']['accessKeyId'];
$config['accessKeySecret'] = isset($dataPost['accessKeySecret']) ? $dataPost['accessKeySecret'] : $params['aliDySms']['accessKeySecret'];
$config['sign'] = isset($dataPost['sign']) ? $dataPost['sign'] : $params['aliDySms']['sign'];
if ($config) {
$session->set('sms::config', $config);
} else {
$config = $session->get('sms::config');
}
$option = array();
$sms = new Sms($option, $config);
$address = isset($dataPost['address']) ? $dataPost['address'] : '';
if ($address) {
$session->set('sms::address', $address);
} else {
$address = $session->get('sms::address');
}
$tmpl = isset($dataPost['tmpl']) ? $dataPost['tmpl'] : '';
if ($tmpl) {
$session->set('sms::tmpl', $tmpl);
} else {
$tmpl = $session->get('sms::tmpl');
}
$_guests = isset($dataPost['guests']) ? $dataPost['guests'] : '';
if (!$_guests) {
$guests = $session->get('sms::guests');
} else {
$_guests = explode("\r\n", $_guests);
$guests = array();
foreach ($_guests as $c) {
$_c = explode(',', $c);
if (count($_c)>1) {
$k = trim($_c[1]);
$v = trim($_c[0]);
$guests[$k] = $v;
}
}
unset($_c);
unset($k);
unset($v);
$guests = array_unique($guests);
}
if (!$guests) {
$i = (int)$session->get('sms::success');
$session->set('sms::success', 0);
$msg = $session->get('sms::msg');
$session->set('sms::msg', '');
$this->result('群发完成,共成功发送'.$i.'条;详情:'.$msg);
}
$results = [];
$msg = "";
$content = [];
$content['address'] = $address;
$i = 0;
$j = 50;
foreach ($guests as $p=>$username) {
$j--;
$content['name'] = !empty($username) ? $username.'安诚托车' : '安诚捷顺运车';
$result = $sms->send($p, $tmpl, $content);
$results[$p] = $result;
if (!$result) {
$msg .= "".$p."】发送失败,接口提示:".$sms->errors."\r\n";
} elseif ($result->Code !='OK') {
$msg .= "".$p."】发送失败,接口提示:".$result->Message."\r\n";
} else {
$i++;
$msg .= "".$p."】发送成功";
}
unset($guests[$p]);
if ($j==0) {
break;
}
}
$session->set('sms::guests', $guests);
$i += (int)$session->get('sms::success');
$session->set('sms::success', $i);
$msg .= $session->get('sms::msg');
$session->set('sms::msg', $msg);
$this->result('群发完成,共成功发送'.$i.'条;详情:'.$msg);
}
//发送验证码
public function actionVcode()
{
$pid = $this->pid;
$plat = AcPlat::findOne($pid);
$platExt = json_decode($plat->remark, true);
//数据取出方法
$dataPost = $this->postdata; //YII应用内的真实或模拟POST数据
$phone = $dataPost['phone'] ?? 0;
//验证手机号
$isPhone = FALSE;
if ($phone) {
$isPhone = preg_match('/^1\d{10}$/', $phone) ? TRUE : FALSE;
}
if (!$isPhone) {
$this->result('手机号不正确,请确认');
}
// 手机号掩码
$phoneMask = substr($phone, 0, 3) . str_repeat('*', strlen($phone) - 7) . substr($phone, -4);
$params = Yii::$app->params;
$codetype = $dataPost['scence'] ?? 'vcode'; //验证码使用场景
$config = array();
$config['accessKeyId'] = isset($dataPost['accessKeyId']) ? $dataPost['accessKeyId'] : $params['aliDySms']['accessKeyId'];
$config['accessKeySecret'] = isset($dataPost['accessKeySecret']) ? $dataPost['accessKeySecret'] : $params['aliDySms']['accessKeySecret'];
$config['sign'] = isset($dataPost['sign']) ? $dataPost['sign'] : $params['aliDySms']['sign'];
$option = array();
$sms = new Sms($option, $config);
$_time = time();
$vcodeModel = new CVcode();
//查询上次发送记录
$where=[];
$where['mobile'] = $phone;
$where['deleted'] = 0;
$where['type'] = $codetype;
$res = $vcodeModel->find()->where($where)->one();
if ($res) {
if (($_time-$res->create_at)<300) {
//设置5分钟内验证码仍然可用
$code = $res->code;
$msg = '上次发送的验证码仍然可用;请注意查看手机 ' . $phoneMask;
$data = ['errorcode' => 2];
if (YII_DEBUG) {
$data['code'] = $code;
}
$this->result($msg, $data, 200);
} else {
$res->deleted = 1;
$res->save();
}
}
$content=[];
$code = mt_rand(1000, 9999);
$content['code'] = $code;
//验证码准备入库
$vcode = [];
$vcode['code'] = $code;
$vcode['type'] = $codetype;
$vcode['mobile'] = $phone;
$vcode['create_at'] = $_time;
foreach ($vcode as $key=>$val) {
$vcodeModel->$key = $val;
}
$tmpls = $params['aliDySms']['tmpl'];
switch ($codetype) {
case 'reg':
$tmpl = $tmpls['regVerify'];
break;
case 'login':
$tmpl = $tmpls['loginVcode'];
break;
case 'info':
$tmpl = $tmpls['infoChange'];
break;
case 'passwd':
$tmpl = $tmpls['passwdChange'];
break;
case 'vcode':
default:
$tmpl = $tmpls['vcode'];
break;
}
// 发送前控制
$host = yii::$app->request->hostName;
$debug = $params['aliDySms']['debug'] ?? FALSE;
$domains = $params['aliDySms']['domains'] ?? [];
$sendable = FALSE;
if (in_array($host, $domains) || $debug) {
$sendable = TRUE;
}
$result = TRUE;
if ($sendable) {
$result = $sms->send($phone, $tmpl, $content);
}
//保存发送记录
$smsModel = new CSms();
$smsdata = [];
$sid = $params['siteId'];
$smsdata['sid'] = (int)$sid;
$smsdata['pid'] = $pid;
$smsdata['uid'] = 0;
$smsdata['mid'] = $this->user_id;
$smsdata['mobile'] = $phone;
$smsdata['content'] = json_encode($content, JSON_UNESCAPED_UNICODE);
$smsdata['create_at'] = $_time;
foreach ($smsdata as $key=>$val) {
$smsModel->$key = $val;
}
$smsModel->save();
$errorcode = 0;
$msg = '';
if (!$result) {
$msg .= "发送失败,接口提示:".$sms->errors;
$errorcode = 3;
} elseif (is_array($result) && $result['Code'] !='OK') {
$msg .= "发送失败,接口提示:".$result['Message'];
$errorcode = 4;
} else {
$vcodeModel->save();
$msg .= "发送成功";
}
$data = ['errorcode' => $errorcode];
if (!$debug || YII_DEBUG) {
$data['data'] = $content;
}
$this->result($msg, $data, 200);
}
//发送给财务
public function actionCaiwu()
{
$pid = $this->pid;
$plat = AcPlat::findOne($pid);
$platExt = json_decode($plat->remark, true);
$phone = $platExt['cw_phone'];
if (!$phone) {
$this->result('还没有配置财务手机号,请联系站长管理员进行设置!', ['errorcode'=>1], 200);
}
$vcodeModel = new CVcode();
$codetype = 'caiwu';
//查询上次发送记录
$_time = time();
$where=[];
$where['mobile'] = $phone;
$where['deleted'] = 0;
$where['type'] = $codetype;
$res = $vcodeModel->find()->where($where)->one();
if ($res) {
if (($_time-$res->create_at)<7200) {
//设置2小时内验证码仍然可用
$code = $res->code;
$this->result('上次发送的验证码仍然可用;请注意查看财务手机!', ['errorcode'=>2], 200);
} else {
$res->deleted = 1;
$res->save();
}
}
//数据取出方法
$dataPost = $this->postdata; //YII应用内的真实或模拟POST数据
$params = Yii::$app->params;
$content = [];
$code = mt_rand(1000, 9999);
$content['code'] = $code;
$tmpl = $params['aliDySms']['tmpl']['cwVcode'];
//验证码准备入库
$vcode = [];
$vcode['code'] = $code;
$vcode['type'] = $codetype;
$vcode['mobile'] = $phone;
$vcode['create_at'] = $_time;
foreach ($vcode as $key=>$val) {
$vcodeModel->$key = $val;
}
$config = array();
$config['accessKeyId'] = isset($dataPost['accessKeyId']) ? $dataPost['accessKeyId'] : $params['aliDySms']['accessKeyId'];
$config['accessKeySecret'] = isset($dataPost['accessKeySecret']) ? $dataPost['accessKeySecret'] : $params['aliDySms']['accessKeySecret'];
$config['sign'] = isset($dataPost['sign']) ? $dataPost['sign'] : $params['aliDySms']['sign'];
$option = array();
$sms = new Sms($option, $config);
$result = $sms->send($phone, $tmpl, $content); //编排发送短信
//保存发送记录
$smsModel = new CSms();
$smsdata = [];
$sid = $params['siteId'];
$smsdata['sid'] = (int)$sid;
$smsdata['pid'] = $pid;
$smsdata['uid'] = 0;
$smsdata['mid'] = $this->user_id;
$smsdata['mobile'] = $phone;
$smsdata['content'] = json_encode($content, JSON_UNESCAPED_UNICODE);
$smsdata['create_at'] = $_time;
foreach ($smsdata as $key=>$val) {
$smsModel->$key = $val;
}
$smsModel->save();
$errorcode = 0;
$msg = '';
if (!$result) {
$msg .= "发送失败,接口提示:".$sms->errors;
$errorcode = 3;
} elseif (is_array($result) && $result['Code'] !='OK') {
$msg .= "发送失败,接口提示:".$result['Message'];
$errorcode = 4;
} else {
$vcodeModel->save();
$msg .= "发送成功";
}
$this->result($msg, ['errorcode'=>$errorcode], 200);
}
}