From 6679fe8a49a9c7cae349df066c8332486980866c Mon Sep 17 00:00:00 2001 From: mkm <727897186@qq.com> Date: Fri, 10 May 2024 14:21:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=98=9F=E5=88=97=E5=92=8C?= =?UTF-8?q?=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/IndexController.php | 28 +++++------- app/queue/redis/MyMailSend.php | 52 ++++++++++++++++++++++ config/plugin/webman/push/app.php | 6 +-- config/plugin/webman/redis-queue/redis.php | 8 ++-- 4 files changed, 69 insertions(+), 25 deletions(-) create mode 100644 app/queue/redis/MyMailSend.php diff --git a/app/api/controller/IndexController.php b/app/api/controller/IndexController.php index 69ac922..0399e9a 100644 --- a/app/api/controller/IndexController.php +++ b/app/api/controller/IndexController.php @@ -7,7 +7,9 @@ use app\admin\logic\tools\GeneratorLogic; use app\common\service\pay\PayService; use app\common\service\wechat\WeChatOaService; use Webman\Config; +use Webman\RedisQueue\Redis; use Yansongda\Pay\Pay; +use Webman\Push\Api; class IndexController extends BaseApiController { @@ -15,24 +17,14 @@ class IndexController extends BaseApiController public function index() { - Pay::config(Config::get('payment')); - $order = [ - 'description' => '测试 - yansongda - 1', - 'out_trade_no' => time().'', - 'payer' => [ - 'auth_code' => 'xxxxxxxxxxx' - ], - 'amount' => [ - 'total' => 1, - ], - 'scene_info' => [ - 'store_info'=>[ - 'id' => '5678' - ] - ], - ]; - - $result = Pay::wechat()->pos($order); + $queue = 'send-mail'; + // 数据,可以直接传数组,无需序列化 + $data = ['to' => 'tom@gmail.com', 'content' => 'hello']; + // 投递消息 + Redis::send($queue, $data); + // 投递延迟消息,消息会在60秒后处理 + Redis::send($queue, $data, 60); + return json(['msg' =>create_password(123456, '11d3')]); } diff --git a/app/queue/redis/MyMailSend.php b/app/queue/redis/MyMailSend.php new file mode 100644 index 0000000..d46d095 --- /dev/null +++ b/app/queue/redis/MyMailSend.php @@ -0,0 +1,52 @@ +trigger('user-1', 'message', [ + 'from_uid' => 2, + 'content' => '你好,这个是消息内容' + ]); + // 无需反序列化 + var_export($data); // 输出 ['to' => 'tom@gmail.com', 'content' => 'hello'] + } + // 消费失败回调 + /* + $package = [ + 'id' => 1357277951, // 消息ID + 'time' => 1709170510, // 消息时间 + 'delay' => 0, // 延迟时间 + 'attempts' => 2, // 消费次数 + 'queue' => 'send-mail', // 队列名 + 'data' => ['to' => 'tom@gmail.com', 'content' => 'hello'], // 消息内容 + 'max_attempts' => 5, // 最大重试次数 + 'error' => '错误信息' // 错误信息 + ] + */ + public function onConsumeFailure(\Throwable $e, $package) + { + echo "consume failure\n"; + echo $e->getMessage() . "\n"; + // 无需反序列化 + var_export($package); + } +} \ No newline at end of file diff --git a/config/plugin/webman/push/app.php b/config/plugin/webman/push/app.php index 6a0c847..b93c899 100644 --- a/config/plugin/webman/push/app.php +++ b/config/plugin/webman/push/app.php @@ -1,10 +1,10 @@ true, - 'websocket' => 'websocket://0.0.0.0:3131', - 'api' => 'http://0.0.0.0:3232', + 'websocket' => getenv('PUSH_WEBSOCKET_URL'), + 'api' => getenv("PUSH_URL"), 'app_key' => '2ce3ce22329517213caa7dad261f5695', 'app_secret' => 'e77d30638dfc7776e2d41121797bf2b2', - 'channel_hook' => 'http://127.0.0.1:8787/plugin/webman/push/hook', + 'channel_hook' => 'http://127.0.0.1:8546/plugin/webman/push/hook', 'auth' => '/plugin/webman/push/auth' ]; \ No newline at end of file diff --git a/config/plugin/webman/redis-queue/redis.php b/config/plugin/webman/redis-queue/redis.php index 0876302..90f20a8 100644 --- a/config/plugin/webman/redis-queue/redis.php +++ b/config/plugin/webman/redis-queue/redis.php @@ -1,12 +1,12 @@ [ - 'host' => 'redis://127.0.0.1:6379', + 'host' => 'redis://'.getenv('REDIS_HOST').':6379', 'options' => [ - 'auth' => null, // 密码,字符串类型,可选参数 + 'auth' => getenv('REDIS_PASSWORD'), // 密码,字符串类型,可选参数 'db' => 0, // 数据库 - 'prefix' => '', // key 前缀 - 'max_attempts' => 5, // 消费失败后,重试次数 + 'prefix' => 'queue:', // key 前缀 + 'max_attempts' => 3, // 消费失败后,重试次数 'retry_seconds' => 5, // 重试间隔,单位秒 ] ],