From fff771eddce3963da2439e0eeeaf870b9ea84c5d Mon Sep 17 00:00:00 2001 From: liaofei <136327134@qq.com> Date: Wed, 24 Aug 2022 12:19:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=87=8F=E6=B6=88=E6=81=AF=E9=98=9F?= =?UTF-8?q?=E5=88=97=E5=91=BD=E4=BB=A4=E8=A1=8C=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crmeb/app/adminapi/controller/Login.php | 6 +-- crmeb/app/event.php | 3 ++ crmeb/app/jobs/CheckQueueJob.php | 35 ++++++++++++++++++ crmeb/app/listener/admin/AdminInfo.php | 32 ++++++++++++++++ crmeb/app/listener/admin/AdminLogin.php | 37 +++++++++++++++++++ crmeb/app/listener/queue/QueueStart.php | 26 +++++++++++++ .../system/admin/SystemAdminServices.php | 12 ++++-- crmeb/app/subscribes/TaskSubscribe.php | 1 + .../think-queue/src/queue/command/Listen.php | 2 + 9 files changed, 148 insertions(+), 6 deletions(-) create mode 100644 crmeb/app/jobs/CheckQueueJob.php create mode 100644 crmeb/app/listener/admin/AdminInfo.php create mode 100644 crmeb/app/listener/admin/AdminLogin.php create mode 100644 crmeb/app/listener/queue/QueueStart.php diff --git a/crmeb/app/adminapi/controller/Login.php b/crmeb/app/adminapi/controller/Login.php index c4053f4ab..d8fb0b5ec 100644 --- a/crmeb/app/adminapi/controller/Login.php +++ b/crmeb/app/adminapi/controller/Login.php @@ -56,8 +56,8 @@ class Login extends AuthController */ public function login() { - [$account, $password, $imgcode] = $this->request->postMore([ - 'account', 'pwd', ['imgcode', ''] + [$account, $password, $imgcode, $key] = $this->request->postMore([ + 'account', 'pwd', ['imgcode', ''], ['key', ''] ], true); if (!app()->make(Captcha::class)->check($imgcode)) { @@ -66,7 +66,7 @@ class Login extends AuthController $this->validate(['account' => $account, 'pwd' => $password], \app\adminapi\validate\setting\SystemAdminValidata::class, 'get'); - return app('json')->success($this->services->login($account, $password, 'admin')); + return app('json')->success($this->services->login($account, $password, 'admin', $key)); } /** diff --git a/crmeb/app/event.php b/crmeb/app/event.php index b6d5b2a9a..89d9badfc 100644 --- a/crmeb/app/event.php +++ b/crmeb/app/event.php @@ -22,7 +22,10 @@ return [ 'HttpEnd' => [\app\listener\http\HttpEnd::class], //HTTP请求结束回调事件 'LogLevel' => [], 'LogWrite' => [], + 'queue.start' => [\app\listener\queue\QueueStart::class], 'user.login' => [\app\listener\user\Login::class], // + 'admin.info' => [\app\listener\admin\AdminInfo::class],//管理员登录前获取登录信息事件 + 'admin.login' => [\app\listener\admin\AdminLogin::class],//管理员登录 'user.register' => [\app\listener\user\Register::class], //用户注册后置事件 'wechat.auth' => [\app\listener\wechat\Auth::class], //用户授权后置事件 'order.orderCreateAfter' => [\app\listener\order\OrderCreateAfter::class], //订单创建后置事件 diff --git a/crmeb/app/jobs/CheckQueueJob.php b/crmeb/app/jobs/CheckQueueJob.php new file mode 100644 index 000000000..bed02b36d --- /dev/null +++ b/crmeb/app/jobs/CheckQueueJob.php @@ -0,0 +1,35 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\jobs; + + +use crmeb\basic\BaseJobs; +use crmeb\traits\QueueTrait; + +/** + * 检测消息队列是否执行 + * Class CheckQueueJob + * @package app\jobs + */ +class CheckQueueJob extends BaseJobs +{ + use QueueTrait; + + + public function handle($key) + { + $path = runtime_path() . '.queue'; + file_put_contents($path, $key); + } +} diff --git a/crmeb/app/listener/admin/AdminInfo.php b/crmeb/app/listener/admin/AdminInfo.php new file mode 100644 index 000000000..f96925fe5 --- /dev/null +++ b/crmeb/app/listener/admin/AdminInfo.php @@ -0,0 +1,32 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\listener\admin; + + +use app\jobs\CheckQueueJob; +use crmeb\interfaces\ListenerInterface; + +/** + * 登录前获取信息 + * Class AdminInfo + * @package app\listener\admin + */ +class AdminInfo implements ListenerInterface +{ + + public function handle($event): void + { + CheckQueueJob::dispatch($event); + } +} diff --git a/crmeb/app/listener/admin/AdminLogin.php b/crmeb/app/listener/admin/AdminLogin.php new file mode 100644 index 000000000..7a4f9533e --- /dev/null +++ b/crmeb/app/listener/admin/AdminLogin.php @@ -0,0 +1,37 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\listener\admin; + +/** + * Class AdminLogin + * @package app\listener\admin + */ +class AdminLogin +{ + + public function handle($event) + { + try { + [$key] = $event; + //检测消息队列是否执行 + $path = root_path('runtime') . '.queue'; + $content = file_get_contents($path); + $res = $key === $content; + unlink($path); + return $res; + } catch (\Throwable $e) { + return false; + } + } +} diff --git a/crmeb/app/listener/queue/QueueStart.php b/crmeb/app/listener/queue/QueueStart.php new file mode 100644 index 000000000..9bc206c9d --- /dev/null +++ b/crmeb/app/listener/queue/QueueStart.php @@ -0,0 +1,26 @@ + + * +---------------------------------------------------------------------- + */ + +namespace app\listener\queue; + + +use think\console\Output; + +class QueueStart +{ + + public function handle(Output $output) + { + $output->writeln('消息队列已启动,正在运行中,windows请不要关闭命令行。linux请使用Supervisor进行进程守护'); + } +} diff --git a/crmeb/app/services/system/admin/SystemAdminServices.php b/crmeb/app/services/system/admin/SystemAdminServices.php index ae85aab71..75777ecbf 100644 --- a/crmeb/app/services/system/admin/SystemAdminServices.php +++ b/crmeb/app/services/system/admin/SystemAdminServices.php @@ -21,6 +21,7 @@ use app\dao\system\admin\SystemAdminDao; use app\services\system\SystemMenusServices; use crmeb\services\FormBuilder; use crmeb\services\workerman\ChannelService; +use think\facade\Event; /** * 管理员service @@ -87,13 +88,14 @@ class SystemAdminServices extends BaseServices * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function login(string $account, string $password, string $type) + public function login(string $account, string $password, string $type, string $key = '') { $adminInfo = $this->verifyLogin($account, $password); $tokenInfo = $this->createToken($adminInfo->id, $type); /** @var SystemMenusServices $services */ $services = app()->make(SystemMenusServices::class); [$menus, $uniqueAuth] = $services->getMenusList($adminInfo->roles, (int)$adminInfo['level']); + $queue = Event::until('admin.login', [$key]); return [ 'token' => $tokenInfo['token'], 'expires_time' => $tokenInfo['params']['exp'], @@ -107,7 +109,8 @@ class SystemAdminServices extends BaseServices 'logo' => sys_config('site_logo'), 'logo_square' => sys_config('site_logo_square'), 'version' => get_crmeb_version(), - 'newOrderAudioLink' => get_file_link(sys_config('new_order_audio_link', '')) + 'newOrderAudioLink' => get_file_link(sys_config('new_order_audio_link', '')), + 'queue' => $queue ]; } @@ -117,12 +120,15 @@ class SystemAdminServices extends BaseServices */ public function getLoginInfo() { + $key = uniqid(); + event('admin.info', [$key]); return [ 'slide' => sys_data('admin_login_slide') ?? [], 'logo_square' => sys_config('site_logo_square'),//透明 'logo_rectangle' => sys_config('site_logo'),//方形 'login_logo' => sys_config('login_logo'),//登陆 - 'site_name' => sys_config('site_name') + 'site_name' => sys_config('site_name'), + 'key' => $key ]; } diff --git a/crmeb/app/subscribes/TaskSubscribe.php b/crmeb/app/subscribes/TaskSubscribe.php index 9cb1347a1..032127d41 100644 --- a/crmeb/app/subscribes/TaskSubscribe.php +++ b/crmeb/app/subscribes/TaskSubscribe.php @@ -44,6 +44,7 @@ class TaskSubscribe */ public function onTask_6() { + file_put_contents(runtime_path() . '.timer', time()); } /** diff --git a/crmeb/vendor/topthink/think-queue/src/queue/command/Listen.php b/crmeb/vendor/topthink/think-queue/src/queue/command/Listen.php index 61e47c1be..4769413b4 100644 --- a/crmeb/vendor/topthink/think-queue/src/queue/command/Listen.php +++ b/crmeb/vendor/topthink/think-queue/src/queue/command/Listen.php @@ -55,6 +55,8 @@ class Listen extends Command $timeout = $input->getOption('timeout'); $sleep = $input->getOption('sleep'); $tries = $input->getOption('tries'); + + $this->app->event->trigger('queue.start', [$output]); $this->listener->listen($connection, $queue, $delay, $sleep, $tries, $memory, $timeout); }