短信模版查询。修改。申请

This commit is contained in:
ASUS 2025-10-15 11:24:55 +08:00
parent 1dbd96cb03
commit dec3ee24ce
3 changed files with 120 additions and 35 deletions

View File

@ -2,6 +2,8 @@
namespace app\command;
use app\model\AlibabaSms;
use support\Log;
use support\think\Db;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
@ -26,13 +28,44 @@ class QuerySmsStatus extends Command
}
/**
* 查询模版审核状态 1小时执行一次
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$list = Db::table('sms_user_push_event')->where(['push_type' => 2])->select()->toArray();
$list = Db::table('sms_shop_template')->where(['status' => 1])->select()->toArray();
Log::info('短信模版审核查询开始->' . json_encode($list));
if($list) {
foreach ($list as $k => $value) {
$data = [
'TemplateCode' => $value['template_code']
];
$res = AlibabaSms::GetSmsTemplate($data);
if($res['Code'] == 'OK') {
$msg = '';
if($res['TemplateStatus'] == 0) {
$status = 1;
}elseif ($res['TemplateStatus'] == 1) {
$status = 2;
}elseif ($res['TemplateStatus'] == 2) {
$status = -1;
$msg = $res['AuditInfo']['RejectInfo'];
}elseif ($res['TemplateStatus'] == 10) {
$status = -1;
$msg = '取消审核';
}
$update_data = [
'status' => $status,
'fail_msg' => $msg?:$msg,
];
$update_status = Db::table('sms_shop_template')->where(['id' => $value['id']])->update($update_data);
Log::info('模版查询完毕数据库更新结果---》' . $update_status . '-->更新数据 . ' . json_encode($update_data));
}
sleep(1);
}
}
return self::SUCCESS;
}

View File

@ -69,19 +69,15 @@ class AlibabaSms extends Model
// 申请新短信模版
public function CreateSmsTemplate($data)
public static function CreateSmsTemplate($data)
{
$client = self::createClient();
$createSmsTemplateRequest = new CreateSmsTemplateRequest([]);
$createSmsTemplateRequest = new CreateSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
$res = $client->createSmsTemplateWithOptions($createSmsTemplateRequest, $runtime);
Log::info('申请新短信模版接口返回---》' . json_encode($res->body->toArray()));
if($res->body->code == 'OK') {
return true;
}else {
return false;
}
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
@ -89,24 +85,21 @@ class AlibabaSms extends Model
}
Utils::assertAsString($error->message);
Log::info('申请新短信模版错误--' . $error->message);
return false;
}
}
// 查询模版审核详情
public function GetSmsTemplate($data)
public static function GetSmsTemplate($data)
{
$client = self::createClient();
$getSmsTemplateRequest = new GetSmsTemplateRequest([]);
$getSmsTemplateRequest = new GetSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
$res = $client->getSmsTemplateWithOptions($getSmsTemplateRequest, $runtime);
Log::info('查询模版审核详情接口返回---》' . json_encode($res->body->toArray()));
if($res->body->code == 'OK') {
return $res->body->toArray();
}else {
return false;
}
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
@ -118,19 +111,15 @@ class AlibabaSms extends Model
}
// 修改模版
public function UpdateSmsTemplate($data)
public static function UpdateSmsTemplate($data)
{
$client = self::createClient();
$updateSmsTemplateRequest = new UpdateSmsTemplateRequest([]);
$updateSmsTemplateRequest = new UpdateSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
$res = $client->updateSmsTemplateWithOptions($updateSmsTemplateRequest, $runtime);
Log::info('修改模版接口返回---》' . json_encode($res->body->toArray()));
if($res->body->code == 'OK') {
return true;
}else {
return false;
}
return $res->body->toArray();
}
catch (Exception $error) {
if (!($error instanceof TeaError)) {
@ -145,10 +134,10 @@ class AlibabaSms extends Model
// 删除短信模版
public function DeleteSmsTemplate($data)
public static function DeleteSmsTemplate($data)
{
$client = self::createClient();
$deleteSmsTemplateRequest = new DeleteSmsTemplateRequest([]);
$deleteSmsTemplateRequest = new DeleteSmsTemplateRequest($data);
$runtime = new RuntimeOptions([]);
try {
// 复制代码运行请自行打印 API 的返回值

View File

@ -11,21 +11,84 @@ use Webman\RedisQueue\Consumer;
// 申请新短信模版
class ApplySmsTemp implements Consumer
{
public $queue = 'apply.sms.temp';
public function consume($data)
{
Log::info('消息队列【申请新短信模版】接收到消息' . $data);
// 查询没有提交审核的模版
$temp_list = Db::table('sms_shop_template')->where(['shop_id' => 1, 'status' => 0])->select()->toArray();
if ($temp_list) {
// 区分数据
$arr = explode(',', $data);
if(is_array($arr)) {
$shop_id = $arr[0];
$shop_template_id = $arr[1];
}else {
Log::info('apply.sms.temp数据格式错误');
return false;
}
try {
// 查询没有提交审核的模版
$temp = Db::table('sms_shop_template')->where(['id' => $shop_template_id])->where('status', 'in', [0, -2])->find();
if($temp) {
// 待申请
if($temp['status'] == 0) {
$data = [
'TemplateName' => $temp['title'],
'TemplateContent' => $temp['content'],
'Remark' => !empty($temp['remark']) ? $temp['remark'] : '',
'TemplateType' => 2,
'RelatedSignName' => config('cons.sms_sign'),
];
// 调用接口
$res = AlibabaSms::CreateSmsTemplate($data);
}elseif ($temp['status'] = -2) {
// 修改模版申请
$data = [
'TemplateCode' => $temp['template_code'],
'TemplateName' => $temp['title'],
'TemplateContent' => $temp['content'],
'Remark' => !empty($temp['remark']) ? $temp['remark'] : '',
'TemplateType' => 2,
'RelatedSignName' => config('cons.sms_sign'),
];
// 调用接口
$res = AlibabaSms::UpdateSmsTemplate($data);
}
if($res['Code'] == 'OK') {
$status = 1;
}else {
$status = -1;
}
$fail_msg = $res['Message'];
$template_code = '';
$order_id = '';
if(!empty($res['TemplateCode'])) {
$template_code = $res['TemplateCode'];
}
if(!empty($res['OrderId'])) {
$order_id = $res['OrderId'];
}
$update_data = [
'status' => $status,
'template_code' => $template_code,
'fail_msg' => $fail_msg,
'order_id' => $order_id,
'update_time' => date('Y-m-d H:i:s'),
];
// 更改状态
$update_status = Db::table('sms_shop_template')->where(['id' => $temp['id']])->update($update_data);
Log::info('模版操作完毕数据库更新结果---》' . $update_status . '-->更新数据 . ' . json_encode($update_data));
}else {
Log::info('【' . $shop_template_id . '】未查询到需要审核的模版');
}
}catch (\Exception $e) {
Log::info('申请/修改短信模版出错' . $e->getMessage());
}
}
public function onConsumeFailure(\Throwable $e, $package)
{
Log::error("消息队列执行异常:" . $e->getMessage());
Log::info($e->getTraceAsString());
}
}