From d2a46c47389c399ca019ff3e06ac92541d2fe8c7 Mon Sep 17 00:00:00 2001 From: ASUS <515617283@qq.com> Date: Tue, 14 Oct 2025 15:10:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9D=E6=AD=A5MQ?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/command/ApplySmsTemp.php | 82 +++++++++++++++++++ app/command/BirthdayGiftSms.php | 82 +++++++++++++++++++ app/command/QuerySmsStatus.php | 39 +++++++++ app/command/SendMarkSms.php | 82 +++++++++++++++++++ app/command/SendWechatTemp.php | 82 +++++++++++++++++++ app/common/controller/ApiController.php | 21 +++++ ...{IndexController.php => YskController.php} | 16 ++-- app/exception/MyBusinessException.php | 16 ++++ app/model/AlibabaSms.php | 12 +++ app/queue/redis/ApplySmsTemp.php | 22 +++++ app/queue/redis/BirthdayGiftSms.php | 21 +++++ app/queue/redis/SendMarkSms.php | 21 +++++ app/queue/redis/SendWechatTemp.php | 21 +++++ config/plugin/webman/redis-queue/redis.php | 4 +- config/redis.php | 6 +- 15 files changed, 512 insertions(+), 15 deletions(-) create mode 100644 app/command/ApplySmsTemp.php create mode 100644 app/command/BirthdayGiftSms.php create mode 100644 app/command/QuerySmsStatus.php create mode 100644 app/command/SendMarkSms.php create mode 100644 app/command/SendWechatTemp.php create mode 100644 app/common/controller/ApiController.php rename app/controller/{IndexController.php => YskController.php} (67%) create mode 100644 app/exception/MyBusinessException.php create mode 100644 app/queue/redis/ApplySmsTemp.php create mode 100644 app/queue/redis/BirthdayGiftSms.php create mode 100644 app/queue/redis/SendMarkSms.php create mode 100644 app/queue/redis/SendWechatTemp.php diff --git a/app/command/ApplySmsTemp.php b/app/command/ApplySmsTemp.php new file mode 100644 index 0000000..9fc4380 --- /dev/null +++ b/app/command/ApplySmsTemp.php @@ -0,0 +1,82 @@ +addArgument('name', InputArgument::OPTIONAL, 'Name description'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + + $host = RabbitMqConfig::$host; + $port = RabbitMqConfig::$port; + $user = RabbitMqConfig::$user; + $password = RabbitMqConfig::$password; + $queue = RabbitMqConfig::$queue_t . '-apply.sms.temp'; + + // 防止空闲时间断线必须设置心跳 + $connection = new AMQPStreamConnection($host, $port, $user, $password, + '/', + false, + 'AMQPLAIN', + null, + 'en_US', + 60, + 60, + null, + false, + 30 + ); + $rabbit_channel = $connection->channel(); + $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ + 'x-message-ttl' => ['I', 180000] + ]); + + $callback = function ($msg) use ($queue){ + $date_time = date('Y-m-d H:i:s'); + $data = $msg->body; + Log::info('收到消息--->' . $data . '--->' . $date_time . "\n"); + // 发给队列 + Redis::send('apply.sms.temp', $data); + $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); + }; + $rabbit_channel->basic_consume($queue, '', false, false, false, false, $callback); + while ($rabbit_channel->is_consuming()) { + $rabbit_channel->wait(); + } + $rabbit_channel->close(); + $connection->close(); + return self::SUCCESS; + } + +} diff --git a/app/command/BirthdayGiftSms.php b/app/command/BirthdayGiftSms.php new file mode 100644 index 0000000..1c3479f --- /dev/null +++ b/app/command/BirthdayGiftSms.php @@ -0,0 +1,82 @@ +addArgument('name', InputArgument::OPTIONAL, 'Name description'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + + $host = RabbitMqConfig::$host; + $port = RabbitMqConfig::$port; + $user = RabbitMqConfig::$user; + $password = RabbitMqConfig::$password; + $queue = RabbitMqConfig::$queue_t . '-birthday.gift.sms'; + + // 防止空闲时间断线必须设置心跳 + $connection = new AMQPStreamConnection($host, $port, $user, $password, + '/', + false, + 'AMQPLAIN', + null, + 'en_US', + 60, + 60, + null, + false, + 30 + ); + $rabbit_channel = $connection->channel(); + $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ + 'x-message-ttl' => ['I', 180000] + ]); + + $callback = function ($msg) use ($queue){ + $date_time = date('Y-m-d H:i:s'); + $data = $msg->body; + Log::info('收到消息--->' . $data . '--->' . $date_time . "\n"); + // 发给队列 + Redis::send('birthday.gift.sms', $data); + $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); + }; + $rabbit_channel->basic_consume($queue, '', false, false, false, false, $callback); + while ($rabbit_channel->is_consuming()) { + $rabbit_channel->wait(); + } + $rabbit_channel->close(); + $connection->close(); + return self::SUCCESS; + } + +} diff --git a/app/command/QuerySmsStatus.php b/app/command/QuerySmsStatus.php new file mode 100644 index 0000000..1fe5c03 --- /dev/null +++ b/app/command/QuerySmsStatus.php @@ -0,0 +1,39 @@ +addArgument('name', InputArgument::OPTIONAL, 'Name description'); + } + + /** + * @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(); + return self::SUCCESS; + } + +} diff --git a/app/command/SendMarkSms.php b/app/command/SendMarkSms.php new file mode 100644 index 0000000..69b573b --- /dev/null +++ b/app/command/SendMarkSms.php @@ -0,0 +1,82 @@ +addArgument('name', InputArgument::OPTIONAL, 'Name description'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + + $host = RabbitMqConfig::$host; + $port = RabbitMqConfig::$port; + $user = RabbitMqConfig::$user; + $password = RabbitMqConfig::$password; + $queue = RabbitMqConfig::$queue_t . '-send.mark.sms'; + + // 防止空闲时间断线必须设置心跳 + $connection = new AMQPStreamConnection($host, $port, $user, $password, + '/', + false, + 'AMQPLAIN', + null, + 'en_US', + 60, + 60, + null, + false, + 30 + ); + $rabbit_channel = $connection->channel(); + $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ + 'x-message-ttl' => ['I', 180000] + ]); + + $callback = function ($msg) use ($queue){ + $date_time = date('Y-m-d H:i:s'); + $data = $msg->body; + Log::info('收到消息--->' . $data . '--->' . $date_time . "\n"); + // 发给队列 + Redis::send('send.mark.sms', $data); + $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); + }; + $rabbit_channel->basic_consume($queue, '', false, false, false, false, $callback); + while ($rabbit_channel->is_consuming()) { + $rabbit_channel->wait(); + } + $rabbit_channel->close(); + $connection->close(); + return self::SUCCESS; + } + +} diff --git a/app/command/SendWechatTemp.php b/app/command/SendWechatTemp.php new file mode 100644 index 0000000..6d549e8 --- /dev/null +++ b/app/command/SendWechatTemp.php @@ -0,0 +1,82 @@ +addArgument('name', InputArgument::OPTIONAL, 'Name description'); + } + + /** + * @param InputInterface $input + * @param OutputInterface $output + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output): int + { + + $host = RabbitMqConfig::$host; + $port = RabbitMqConfig::$port; + $user = RabbitMqConfig::$user; + $password = RabbitMqConfig::$password; + $queue = RabbitMqConfig::$queue_t . '-send.wechat.temp'; + + // 防止空闲时间断线必须设置心跳 + $connection = new AMQPStreamConnection($host, $port, $user, $password, + '/', + false, + 'AMQPLAIN', + null, + 'en_US', + 60, + 60, + null, + false, + 30 + ); + $rabbit_channel = $connection->channel(); + $rabbit_channel->queue_declare($queue, false, true, false, false, false, [ + 'x-message-ttl' => ['I', 180000] + ]); + + $callback = function ($msg) use ($queue){ + $date_time = date('Y-m-d H:i:s'); + $data = $msg->body; + Log::info('收到消息--->' . $data . '--->' . $date_time . "\n"); + // 发给队列 + Redis::send('send.wechat.temp', $data); + $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']); + }; + $rabbit_channel->basic_consume($queue, '', false, false, false, false, $callback); + while ($rabbit_channel->is_consuming()) { + $rabbit_channel->wait(); + } + $rabbit_channel->close(); + $connection->close(); + return self::SUCCESS; + } + +} diff --git a/app/common/controller/ApiController.php b/app/common/controller/ApiController.php new file mode 100644 index 0000000..dfb1cf1 --- /dev/null +++ b/app/common/controller/ApiController.php @@ -0,0 +1,21 @@ +header('token'); + // 基类 + if(empty($get_token)) { + throw new MyBusinessException('参数错误', 500); + } + if($get_token != self::$token) { + throw new MyBusinessException('参数错误', 500); + } + } +} \ No newline at end of file diff --git a/app/controller/IndexController.php b/app/controller/YskController.php similarity index 67% rename from app/controller/IndexController.php rename to app/controller/YskController.php index 6a755b1..96e2103 100644 --- a/app/controller/IndexController.php +++ b/app/controller/YskController.php @@ -2,10 +2,15 @@ namespace app\controller; +use app\common\controller\ApiController; +use app\exception\MyBusinessException; +use app\model\AlibabaSms; use support\Request; use support\think\Db; use support\think\Model; -class IndexController +use Webman\RedisQueue\Client; + +class YskController extends ApiController { public function index(Request $request) { @@ -22,17 +27,8 @@ class IndexController // $data = \app\model\MessagePushTask::send_push_msg(); // $data = \app\model\MessagePushTask::send_sms_msg(); - return json(123); } - public function view(Request $request) - { - return view('index/view', ['name' => 'webman']); - } - public function json(Request $request) - { - return json(['code' => 0, 'msg' => 'ok']); - } } diff --git a/app/exception/MyBusinessException.php b/app/exception/MyBusinessException.php new file mode 100644 index 0000000..16e457f --- /dev/null +++ b/app/exception/MyBusinessException.php @@ -0,0 +1,16 @@ + $this->getCode() ?: 500, 'message' => $this->getMessage()]); + } +} \ No newline at end of file diff --git a/app/model/AlibabaSms.php b/app/model/AlibabaSms.php index 62473cb..e044104 100644 --- a/app/model/AlibabaSms.php +++ b/app/model/AlibabaSms.php @@ -2,6 +2,7 @@ namespace app\model; +use support\think\Db; use think\Exception; use support\Log; use think\Model; @@ -32,6 +33,7 @@ class AlibabaSms extends Model } /** + * 发送短信 * @param array $args * @return void */ @@ -43,6 +45,7 @@ class AlibabaSms extends Model try { // 复制代码运行请自行打印 API 的返回值 $res = $client->sendSmsWithOptions($sendSmsRequest, $runtime); + Log::info('短信接口返回---》' . json_encode($res->body->toArray())); if($res->body->code == 'OK') { return true; }else { @@ -60,6 +63,15 @@ class AlibabaSms extends Model } } + + /** + * 查询短信发送结果 + */ + public function QuerySendDetails($data) + { + + } + } diff --git a/app/queue/redis/ApplySmsTemp.php b/app/queue/redis/ApplySmsTemp.php new file mode 100644 index 0000000..445e0ef --- /dev/null +++ b/app/queue/redis/ApplySmsTemp.php @@ -0,0 +1,22 @@ + [ - 'host' => 'redis://127.0.0.1:6379', + 'host' => 'redis://192.168.1.42:6379', 'options' => [ - 'auth' => null, // 密码,字符串类型,可选参数 + 'auth' => 'Chaozg123.', // 密码,字符串类型,可选参数 'db' => 0, // 数据库 'prefix' => '', // key 前缀 'max_attempts' => 5, // 消费失败后,重试次数 diff --git a/config/redis.php b/config/redis.php index 510b81c..c7f4dae 100644 --- a/config/redis.php +++ b/config/redis.php @@ -14,14 +14,14 @@ return [ 'default' => [ - 'password' => '222222', + 'password' => 'Chaozg123.', 'host' => '192.168.1.42', 'port' => 6379, 'database' => 0, // Connection pool, supports only Swoole or Swow drivers. 'pool' => [ - 'max_connections' => 5, - 'min_connections' => 1, + 'max_connections' => 100, + 'min_connections' => 10, 'wait_timeout' => 3, 'idle_timeout' => 60, 'heartbeat_interval' => 50,