新增订阅消息发送模版

This commit is contained in:
2023-03-24 14:37:31 +08:00
parent 3ca98021a0
commit cfba03797e
7 changed files with 435 additions and 168 deletions
+36 -31
View File
@@ -23,43 +23,47 @@ use Psr\Container\NotFoundExceptionInterface;
#[Consumer(exchange: 'amqp.direct', routingKey: 'SendSmsMessage', queue: 'send.sms.message.queue', nums: 1)]
class SendSmsMessageConsumer extends ConsumerMessage
{
/**
* @param $data
* [
* "template_code" => "SMS_123",
* "template_param" => [
* // 参数不确定,主要看短信模版的不同
* ],
* "scene_desc" => "场景描述",
* "phone" => "手机号",
* "user_id" => "用户id(被推送者)"
* ]
* @param AMQPMessage $message
* @return string
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function consumeMessage($data, AMQPMessage $message): string
{
/**
* data = [
* "template_code" => "SMS_123",
* "template_param" => [
* // 参数不确定,主要看短信模版的不同
* ],
* "scene_desc" => "场景描述",
* "phone" => "手机号",
* "user_id" => "用户id(被推送者)"
* ]
*/
Log::getInstance()->error("开始执行 发送短信 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
if (!isset($data['template_code']) || !isset($data['template_param'])){
Log::getInstance()->error("发送短信失败:发送参数错误");
Log::getInstance()->error("队列发送短信失败:发送参数错误");
return Result::DROP;// 销毁
}
// 验证发送参数
$res = $this->checkTemplateParam($data['template_code'],$data['template_param']);
if (!$res){
Log::getInstance()->error("发送短信失败:发送参数错误");
Log::getInstance()->error("队列发送短信失败:发送参数错误");
return Result::DROP;// 销毁
}
try {
$redis = $this->container->get(Redis::class);
$redis = $this->container->get(Redis::class);
$redis_key = "sms_" . $data['phone'] . $data['template_code'];
$redis_key = "sms_" . $data['phone'] . $data['template_code'];
try {
$result = $redis->get($redis_key);
if (!empty($result)){
if ( $result > 3){
if ( $result > 2){
// 超出规定时间内最大获取次数
Log::getInstance()->error("发送短信失败:超出最大发送重试次数");
Log::getInstance()->error("队列发送短信失败:超出最大发送重试次数");
return Result::DROP;// 销毁
}
}
@@ -67,15 +71,19 @@ class SendSmsMessageConsumer extends ConsumerMessage
// 发送短信
Dysms::sendSms($data['phone'],$data['template_param'],$data['template_code'],$data['scene_desc']);
// 删除缓存
if (!empty($result)){
$redis->del($redis_key);
}
} catch (\Exception $e) {
Log::getInstance()->error("发送短信失败:" . $e->getMessage());
Log::getInstance()->error("队列发送短信失败:" . $e->getMessage());
// 增加发送次数
$redis->incr($redis_key);
return Result::REQUEUE; // 重回队列
} catch (NotFoundExceptionInterface|ContainerExceptionInterface $e) {
Log::getInstance()->error("发送短信失败:" . $e->getMessage());
return Result::DROP; // 销毁
}
Log::getInstance()->info("发送短信成功");
Log::getInstance()->info("队列发送短信成功");
return Result::ACK;
}
@@ -184,16 +192,13 @@ class SendSmsMessageConsumer extends ConsumerMessage
// 医生修改简介审核拒绝
break;
default:
// code...
// 非法模版
return false;
break;
}
return true;
}
// 记录发送失败log
private function addSendFailLog(array $data){
$data = array();
// $data['to_user_id']
}
}
}