新增订阅消息发送模版
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Amqp\Consumer;
|
||||
|
||||
use App\Amqp\Producer\SendSmsMessageProducer;
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
use App\Model\LogMessagePush;
|
||||
@@ -14,9 +15,11 @@ use App\Model\UserPatient;
|
||||
use App\Services\UserService;
|
||||
use App\Utils\Log;
|
||||
use Extend\Wechat\Wechat;
|
||||
use Hyperf\Amqp\Producer;
|
||||
use Hyperf\Amqp\Result;
|
||||
use Hyperf\Amqp\Annotation\Consumer;
|
||||
use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
@@ -28,6 +31,7 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
|
||||
|
||||
/**
|
||||
* 发送订阅消息
|
||||
* 每条消息都需携带发送短信参数,用户可能拒绝接受订阅消息
|
||||
*/
|
||||
#[Consumer(exchange: 'amqp.direct', routingKey: 'SendSubMessage', queue: 'send.sub.message.queue', nums: 1)]
|
||||
class SendSubMessageConsumer extends ConsumerMessage
|
||||
@@ -35,18 +39,30 @@ class SendSubMessageConsumer extends ConsumerMessage
|
||||
/**
|
||||
* @param $data
|
||||
* [
|
||||
* "push_user_id" // 用户id(被推送者)
|
||||
* "template_title" // 推送的模版名称
|
||||
* "params" => [ // 推送所需的参数
|
||||
* "page" // 跳转页面
|
||||
* "data" => [
|
||||
* "thing1" => [
|
||||
* "value" => [
|
||||
* "参数1"
|
||||
* ]
|
||||
* "sub_data" => [
|
||||
* "push_user_id" // 用户id(被推送者)
|
||||
* "template_title" // 推送的模版名称
|
||||
* "send_reason" // 发送原因
|
||||
* "params" => [ // 推送所需的参数
|
||||
* "page" // 跳转页面
|
||||
* "data" => [
|
||||
* "key1" => "value1" // 入参数据结构
|
||||
// * "thing1" => [ // 发送时处理的数据结构
|
||||
// * "value" => [
|
||||
// * "参数1"
|
||||
// * ]
|
||||
// * ]
|
||||
* ]
|
||||
* ]
|
||||
*
|
||||
* ],
|
||||
* ]
|
||||
* "sms_data" => [ // 短信所需的参数,如订阅发送失败需发送短信
|
||||
* "template_code" => "SMS_123",
|
||||
* "template_param" => [
|
||||
* // 参数不确定,主要看短信模版的不同
|
||||
* ],
|
||||
* "scene_desc" => "场景描述",
|
||||
* "phone" => "手机号",
|
||||
* "user_id" => "用户id(被推送者)"
|
||||
* ]
|
||||
* ]
|
||||
* @param AMQPMessage $message
|
||||
@@ -61,36 +77,69 @@ class SendSubMessageConsumer extends ConsumerMessage
|
||||
*/
|
||||
public function consumeMessage($data, AMQPMessage $message): string
|
||||
{
|
||||
Log::getInstance()->info("开始执行 发送订阅消息 队列:" . json_encode($data, JSON_UNESCAPED_UNICODE));
|
||||
|
||||
try {
|
||||
// 获取被推送用户信息
|
||||
$params = array();
|
||||
$params['user_id'] = $data['push_user_id'];
|
||||
$user = User::getOne($params);
|
||||
if (empty($user)){
|
||||
$this->addMessagePushLog($data,"未查询到被推送用户信息");
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:未查询到被推送用户信息");
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
// 验证发送参数
|
||||
if (empty($data['sub_data']['params']['data'])){
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:无推送数据");
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],"无推送数据");
|
||||
|
||||
return Result::DROP;
|
||||
}
|
||||
|
||||
// 处理发送参数
|
||||
$send_data = array();
|
||||
foreach ($data['sub_data']['params']['data'] as $key => $item){
|
||||
$send_data[$key] = [
|
||||
"value" => [
|
||||
$item
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
if (empty($send_data)){
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:发送参数处理失败");
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],"发送参数处理失败");
|
||||
|
||||
return Result::DROP;
|
||||
}
|
||||
|
||||
|
||||
// 获取open_id
|
||||
$UserService = new UserService();
|
||||
$open_id = $UserService->getOpenIdWithUserId($user['user_id'],$user['user_type']);
|
||||
if (empty($open_id)){
|
||||
$this->addMessagePushLog($data,"未获取到被推送用户open_id");
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:未获取到被推送用户open_id");
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],"未获取到被推送用户open_id");
|
||||
|
||||
// 执行发送短信步骤
|
||||
$this->addSendSmsMessageQueue($data['sms_data']);
|
||||
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
// 获取订阅消息模版数据
|
||||
$params = array();
|
||||
$params['client_type'] = $user['user_type']; // 客户端类型(1:患者端 2:医师端 3:药师端)
|
||||
$params['template_title'] = $data['template_title'];
|
||||
$sub_template = SubTemplate::getOne($params);
|
||||
if (empty($sub_template)){
|
||||
$this->addMessagePushLog($data,"未查询到推送模版id");
|
||||
// 获取消息模版
|
||||
$sub_template = $this->getSubTemplate($user['user_type'],$data['sub_data']['template_title']);
|
||||
if (empty($template_title)){
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:未获取到需要发送的消息模版数据");
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],"未获取到需要发送的消息模版数据");
|
||||
|
||||
// 执行发送短信步骤
|
||||
$this->addSendSmsMessageQueue($data['sms_data']);
|
||||
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
$sub_template = $sub_template->toArray();
|
||||
|
||||
// 处理发送环境
|
||||
$miniprogram_state = "developer";
|
||||
if (env("APP_ENV") == "prod"){
|
||||
@@ -99,9 +148,9 @@ class SendSubMessageConsumer extends ConsumerMessage
|
||||
|
||||
$options = [
|
||||
"template_id" => $sub_template['wx_template_id'],
|
||||
"page" => $data['params']['page'],
|
||||
"page" => $sub_template['page'] ?: "",
|
||||
"touser" => $open_id,
|
||||
"data" => $data['params']['data'],
|
||||
"data" => $send_data,
|
||||
"miniprogram_state" => $miniprogram_state,
|
||||
"lang" => "zh_CN",
|
||||
];
|
||||
@@ -109,48 +158,217 @@ class SendSubMessageConsumer extends ConsumerMessage
|
||||
// 发起推送
|
||||
$Wechat = new Wechat($user['user_type']);
|
||||
$result = $Wechat->sendSubscribeMessage($options);
|
||||
Log::getInstance()->info("订阅消息推送成功:" . $result);
|
||||
if (empty($result)){
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:推送失败");
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],"推送失败");
|
||||
}
|
||||
|
||||
// 记录推送记录
|
||||
$this->addMessagePushLog($data,"",$sub_template['wx_template_id'],1,json_encode($options,JSON_UNESCAPED_UNICODE));
|
||||
if (!isset($result['errcode'])){
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:推送失败,返回值错误" . json_encode($result,JSON_UNESCAPED_UNICODE));
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],"推送失败,返回值错误");
|
||||
}
|
||||
|
||||
if ($result['errcode'] == 43101){
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:用户拒绝接收消息");
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],"用户拒绝接收消息");
|
||||
|
||||
// 执行发送短信步骤
|
||||
$this->addSendSmsMessageQueue($data['sms_data']);
|
||||
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
Log::getInstance()->info("队列发送订阅消息执行成功");
|
||||
$this->saveSuccessPushLog($user['user_type'],$data['sub_data']);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->addMessagePushLog($data,$e->getMessage());
|
||||
return Result::ACK;
|
||||
Log::getInstance()->error("队列发送订阅消息执行失败:" . $e->getMessage());
|
||||
$this->saveErrorPushLog($user['user_type'],$data['sub_data'],$e->getMessage());
|
||||
}
|
||||
|
||||
return Result::ACK;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加日志
|
||||
* @param array $params 推送参数
|
||||
* @param string $wx_template_id 微信推送模版id
|
||||
* @param int $status 推送状态(1:成功 2:失败)
|
||||
* @param string $fail_reason 推送失败原因
|
||||
* @param string $content 推送内容
|
||||
* 记录成功日志
|
||||
* @param string|int $user_type 用户类型
|
||||
* @param array $sub_data 需发送的订阅消息数据
|
||||
* @return void
|
||||
*/
|
||||
public function addMessagePushLog(array $params,string $fail_reason = '',string $wx_template_id = "",int $status = 2,string $content = '')
|
||||
private function saveSuccessPushLog(string|int $user_type,array $sub_data): void
|
||||
{
|
||||
$data = array();
|
||||
$data['push_user_id'] = $params['push_user_id'];
|
||||
$data['template_title'] = $params['template_title'];
|
||||
if (!empty($wx_template_id)){
|
||||
$data['wx_template_id'] = $wx_template_id;
|
||||
}
|
||||
|
||||
$data['params'] = json_encode($params,JSON_UNESCAPED_UNICODE);
|
||||
$data['status'] = $status;
|
||||
if (!empty($fail_reason)){
|
||||
$data['fail_reason'] = $fail_reason;
|
||||
}
|
||||
if (!empty($content)){
|
||||
$data['content'] = $content;
|
||||
}
|
||||
|
||||
$data['to_user_id'] = $sub_data['push_user_id'];
|
||||
$data['user_type'] = $user_type;
|
||||
$data['type'] = 2;
|
||||
$data['status'] = 1;
|
||||
$data['send_reason'] = $sub_data['send_reason'];
|
||||
$data['content'] = json_encode($sub_data['params'],JSON_UNESCAPED_UNICODE);
|
||||
$log_message_push = LogMessagePush::addLogMessagePush($data);
|
||||
if (empty($log_message_push)){
|
||||
Log::getInstance()->error("增加推送日志失败:" . json_encode($data,JSON_UNESCAPED_UNICODE));
|
||||
Log::getInstance()->error("队列发送订阅消息成功,增加推送日志失败:" . json_encode($data,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录成功日志
|
||||
* @param string|int $user_type 用户类型
|
||||
* @param array $sub_data 需发送的订阅消息数据
|
||||
* @param string $fail_reason 失败原因
|
||||
* @return void
|
||||
*/
|
||||
private function saveErrorPushLog(string|int $user_type,array $sub_data,string $fail_reason): void
|
||||
{
|
||||
$data = array();
|
||||
$data['to_user_id'] = $sub_data['push_user_id'];
|
||||
$data['user_type'] = $user_type;
|
||||
$data['type'] = 2;
|
||||
$data['status'] = 2;
|
||||
$data['fail_reason'] = $fail_reason ?: "";
|
||||
$data['send_reason'] = $sub_data['send_reason'];
|
||||
$data['content'] = json_encode($sub_data['params'],JSON_UNESCAPED_UNICODE);
|
||||
$log_message_push = LogMessagePush::addLogMessagePush($data);
|
||||
if (empty($log_message_push)){
|
||||
Log::getInstance()->error("队列发送订阅消息成功,增加推送日志失败:" . json_encode($data,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加短信队列
|
||||
* 接收所有异常
|
||||
* 只返回true,失败情况不处理
|
||||
* @param array $sms_data 短信发送参数
|
||||
* @return bool
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
private function addSendSmsMessageQueue(array $sms_data): bool
|
||||
{
|
||||
try {
|
||||
if (empty($sms_data)){
|
||||
// 无需发送短信
|
||||
Log::getInstance()->info("无数据需添加短信队列");
|
||||
return true;
|
||||
}
|
||||
|
||||
Log::getInstance()->info("添加短信队列");
|
||||
|
||||
// 增加至发送短信队列
|
||||
$message = new SendSmsMessageProducer($sms_data);
|
||||
$producer = $this->container->get(Producer::class);
|
||||
$res = $producer->produce($message);
|
||||
if (!$res) {
|
||||
Log::getInstance()->error("添加短信队列失败" . json_encode($sms_data,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::getInstance()->error("添加短信队列失败" . $e->getMessage());
|
||||
}
|
||||
|
||||
Log::getInstance()->info("添加短信队列成功");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据库订阅消息模版
|
||||
* 接收所有异常
|
||||
* 异常记录log,不进行处理
|
||||
* @param string|int $user_type 用户类型
|
||||
* @param string $template_title 模版名称
|
||||
* @return array
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
public function getSubTemplate(string|int $user_type, string $template_title): array
|
||||
{
|
||||
try {
|
||||
Log::getInstance()->info("获取数据库消息订阅模版");
|
||||
|
||||
// 获取订阅消息模版数据
|
||||
$params = array();
|
||||
$params['client_type'] = $user_type; // 客户端类型(1:患者端 2:医师端 3:药师端)
|
||||
$params['template_title'] = $template_title;
|
||||
$sub_template = SubTemplate::getOne($params);
|
||||
if (empty($sub_template)){
|
||||
// 下载消息模版数据
|
||||
Log::getInstance()->info("消息模版:" . $template_title . " 在数据库中为空");
|
||||
$this->downSubTemplate($user_type);
|
||||
}
|
||||
|
||||
// 重新获取订阅消息模版
|
||||
$params = array();
|
||||
$params['client_type'] = $user_type; // 客户端类型(1:患者端 2:医师端 3:药师端)
|
||||
$params['template_title'] = $template_title;
|
||||
$sub_template = SubTemplate::getOne($params);
|
||||
} catch (\Exception $e) {
|
||||
Log::getInstance()->error("获取数据库消息订阅模版失败:" . $e->getMessage());
|
||||
}
|
||||
|
||||
if (empty($sub_template)){
|
||||
return [];
|
||||
}else{
|
||||
return $sub_template->toArray();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载消息订阅模版
|
||||
* 接收所有异常
|
||||
* 只返回true,失败情况不处理
|
||||
* @param string|int $user_type
|
||||
* @return bool
|
||||
* @throws ClientExceptionInterface
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws DecodingExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws RedirectionExceptionInterface
|
||||
* @throws ServerExceptionInterface
|
||||
* @throws TransportExceptionInterface
|
||||
*/
|
||||
private function downSubTemplate(string|int $user_type): bool
|
||||
{
|
||||
try {
|
||||
Log::getInstance()->info("下载微信消息订阅模版");
|
||||
|
||||
$weChat = new Wechat($user_type);
|
||||
$result = $weChat->getTemplate();
|
||||
if (empty($result)){
|
||||
return true;
|
||||
}
|
||||
|
||||
$template = json_decode($result, true);
|
||||
|
||||
foreach ($template['data'] as $item) {
|
||||
$params = array();
|
||||
$params['wx_template_id'] = $item['priTmplId'];
|
||||
$sub_template = SubTemplate::getOne($params);
|
||||
if (empty($sub_template)) {
|
||||
// 新增模版
|
||||
$data = array();
|
||||
$data['client_type'] = 1;
|
||||
$data['wx_template_id'] = $item['priTmplId'];
|
||||
$data['template_title'] = $item['title'];
|
||||
$data['template_type'] = $item['type'];
|
||||
$data['template_content'] = $item['content'];
|
||||
$sub_template = SubTemplate::addSubTemplate($data);
|
||||
if (empty($sub_template)) {
|
||||
// 此处添加失败不做处理,记录log,
|
||||
Log::getInstance()->error("下载微信消息订阅模版失败:" . json_encode($data,JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::getInstance()->error("下载微信消息订阅模版失败:" . $e->getMessage());
|
||||
}
|
||||
|
||||
Log::getInstance()->info("下载微信消息订阅模版成功");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user