diff --git a/app/Controller/Api/V1/AuthController.php b/app/Controller/Api/V1/AuthController.php index 5bc74c2..1cd375e 100644 --- a/app/Controller/Api/V1/AuthController.php +++ b/app/Controller/Api/V1/AuthController.php @@ -10,6 +10,7 @@ namespace App\Controller\Api\V1; +use App\Event\UserLogin; use Hyperf\Di\Annotation\Inject; use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\RequestMapping; @@ -48,17 +49,19 @@ class AuthController extends CController 'platform' => 'required|in:h5,ios,windows,mac,web', ]); - $userInfo = $this->userService->login($params['mobile'], $params['password']); - if (!$userInfo) { + $user = $this->userService->login($params['mobile'], $params['password']); + if (!$user) { return $this->response->fail('账号不存在或密码填写错误!'); } try { - $token = $this->guard()->login($userInfo); + $token = $this->guard()->login($user); } catch (\Exception $exception) { return $this->response->error('登录异常,请稍后再试!'); } + event()->dispatch(new UserLogin($this->request, $user)); + return $this->response->success([ 'authorize' => [ 'type' => 'Bearer', @@ -66,11 +69,11 @@ class AuthController extends CController 'expires_in' => $this->guard()->getJwtManager()->getTtl(), ], 'user_info' => [ - 'nickname' => $userInfo->nickname, - 'avatar' => $userInfo->avatar, - 'gender' => $userInfo->gender, - 'motto' => $userInfo->motto, - 'email' => $userInfo->email, + 'nickname' => $user->nickname, + 'avatar' => $user->avatar, + 'gender' => $user->gender, + 'motto' => $user->motto, + 'email' => $user->email, ] ], '账号登录成功...'); } diff --git a/app/Controller/WebSocketController.php b/app/Controller/WebSocketController.php index 9c6882e..dd4dddc 100644 --- a/app/Controller/WebSocketController.php +++ b/app/Controller/WebSocketController.php @@ -76,12 +76,10 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos } if (!$isOnline) { - MessageProducer::publish( - MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ - 'user_id' => $user_id, - 'status' => 1, - ]) - ); + MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ + 'user_id' => $user_id, + 'status' => 1, + ])); } } @@ -124,12 +122,10 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos // 判断是否存在异地登录 $isOnline = $this->client->isOnlineAll($user_id); if (!$isOnline) { - MessageProducer::publish( - MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ - 'user_id' => $user_id, - 'status' => 0, - ]) - ); + MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ + 'user_id' => $user_id, + 'status' => 0, + ])); } } } diff --git a/app/Event/UserLogin.php b/app/Event/UserLogin.php new file mode 100644 index 0000000..4fc6057 --- /dev/null +++ b/app/Event/UserLogin.php @@ -0,0 +1,43 @@ +user = $user; + + $this->platform = $request->input('platform', ''); + + $this->ip = $request->getServerParams()['remote_addr']; + } +} diff --git a/app/Listener/UserLoginListener.php b/app/Listener/UserLoginListener.php new file mode 100644 index 0000000..28bd1c6 --- /dev/null +++ b/app/Listener/UserLoginListener.php @@ -0,0 +1,30 @@ +user->id . ':' . $event->platform . ':' . $event->ip; + // 推送登录提示信息 + } +} diff --git a/app/Service/Message/SubscribeHandleService.php b/app/Service/Message/SubscribeHandleService.php index 521d927..e33957a 100644 --- a/app/Service/Message/SubscribeHandleService.php +++ b/app/Service/Message/SubscribeHandleService.php @@ -64,6 +64,10 @@ class SubscribeHandleService */ public function handle(array $data) { + if (!isset($data['uuid'], $data['event'], $data['data'], $data['options'])) { + return false; + } + if (isset(self::EVENTS[$data['event']])) { call_user_func([$this, self::EVENTS[$data['event']]], $data); } diff --git a/app/Service/SmsService.php b/app/Service/SmsService.php index e670fb6..01e898c 100644 --- a/app/Service/SmsService.php +++ b/app/Service/SmsService.php @@ -1,4 +1,5 @@ get(Psr\SimpleCache\CacheInterface::class); } +/** + * Dispatch an event and call the listeners. + */ +function event() +{ + return container()->get(\Psr\EventDispatcher\EventDispatcherInterface::class); +} + /** * 控制台日志 * @@ -244,14 +250,14 @@ function parse_ids($ids) /** * 推送消息至 RabbitMQ 队列 * - * @param ProducerMessage $message - * @param bool $confirm - * @param int $timeout - * @return bool + * @param \Hyperf\Amqp\Message\ProducerMessage $message + * @param bool $confirm + * @param int $timeout + * @return mixed */ -function push_amqp(ProducerMessage $message, bool $confirm = false, int $timeout = 5) +function push_amqp(\Hyperf\Amqp\Message\ProducerMessage $message, bool $confirm = false, int $timeout = 5) { - return container()->get(Producer::class)->produce($message, $confirm, $timeout); + return container()->get(\Hyperf\Amqp\Producer::class)->produce($message, $confirm, $timeout); } /** diff --git a/composer.json b/composer.json index 43acbeb..a18a122 100644 --- a/composer.json +++ b/composer.json @@ -28,15 +28,16 @@ "hyperf/database": "~2.0.0", "hyperf/async-queue": "~2.0.0", "hyperf/websocket-server": "^2.0", - "hyperf/constants": "^2.0", - "hyperf/validation": "^2.0", - "hyperf/filesystem": "^2.0", + "hyperf/constants": "~2.0.0", + "hyperf/validation": "~2.0.0", + "hyperf/filesystem": "~2.0.0", "hashids/hashids": "^4.0", "ext-json": "*", - "hyperf/view": "^2.0", - "hyperf/view-engine": "^2.0", + "hyperf/view": "~2.0.0", + "hyperf/view-engine": "~2.0.0", "phpmailer/phpmailer": "^6.2", - "96qbhy/hyperf-auth": "^2.3" + "96qbhy/hyperf-auth": "^2.3", + "hyperf/event": "~2.0.0" }, "require-dev": { "swoole/ide-helper": "^4.5",