优化代码

main
gzydong 2021-07-10 10:55:25 +08:00
parent 6e99a88a85
commit 51f2c36f16
8 changed files with 118 additions and 34 deletions

View File

@ -10,6 +10,7 @@
namespace App\Controller\Api\V1; namespace App\Controller\Api\V1;
use App\Event\UserLogin;
use Hyperf\Di\Annotation\Inject; use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller; use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping; use Hyperf\HttpServer\Annotation\RequestMapping;
@ -48,17 +49,19 @@ class AuthController extends CController
'platform' => 'required|in:h5,ios,windows,mac,web', 'platform' => 'required|in:h5,ios,windows,mac,web',
]); ]);
$userInfo = $this->userService->login($params['mobile'], $params['password']); $user = $this->userService->login($params['mobile'], $params['password']);
if (!$userInfo) { if (!$user) {
return $this->response->fail('账号不存在或密码填写错误!'); return $this->response->fail('账号不存在或密码填写错误!');
} }
try { try {
$token = $this->guard()->login($userInfo); $token = $this->guard()->login($user);
} catch (\Exception $exception) { } catch (\Exception $exception) {
return $this->response->error('登录异常,请稍后再试!'); return $this->response->error('登录异常,请稍后再试!');
} }
event()->dispatch(new UserLogin($this->request, $user));
return $this->response->success([ return $this->response->success([
'authorize' => [ 'authorize' => [
'type' => 'Bearer', 'type' => 'Bearer',
@ -66,11 +69,11 @@ class AuthController extends CController
'expires_in' => $this->guard()->getJwtManager()->getTtl(), 'expires_in' => $this->guard()->getJwtManager()->getTtl(),
], ],
'user_info' => [ 'user_info' => [
'nickname' => $userInfo->nickname, 'nickname' => $user->nickname,
'avatar' => $userInfo->avatar, 'avatar' => $user->avatar,
'gender' => $userInfo->gender, 'gender' => $user->gender,
'motto' => $userInfo->motto, 'motto' => $user->motto,
'email' => $userInfo->email, 'email' => $user->email,
] ]
], '账号登录成功...'); ], '账号登录成功...');
} }

View File

@ -76,12 +76,10 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
} }
if (!$isOnline) { if (!$isOnline) {
MessageProducer::publish( MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [
MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ 'user_id' => $user_id,
'user_id' => $user_id, 'status' => 1,
'status' => 1, ]));
])
);
} }
} }
@ -124,12 +122,10 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
// 判断是否存在异地登录 // 判断是否存在异地登录
$isOnline = $this->client->isOnlineAll($user_id); $isOnline = $this->client->isOnlineAll($user_id);
if (!$isOnline) { if (!$isOnline) {
MessageProducer::publish( MessageProducer::publish(MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [
MessageProducer::create(TalkMessageEvent::EVENT_ONLINE_STATUS, [ 'user_id' => $user_id,
'user_id' => $user_id, 'status' => 0,
'status' => 0, ]));
])
);
} }
} }
} }

43
app/Event/UserLogin.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace App\Event;
use App\Model\User;
use Hyperf\HttpServer\Contract\RequestInterface;
class UserLogin
{
/**
* @var User
*/
public $user;
/**
* 登录平台
*
* @var string
*/
public $platform;
/**
* IP地址
*
* @var string
*/
public $ip;
/**
* UserLogin constructor.
*
* @param RequestInterface $request
* @param User $user
*/
public function __construct(RequestInterface $request, User $user)
{
$this->user = $user;
$this->platform = $request->input('platform', '');
$this->ip = $request->getServerParams()['remote_addr'];
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace App\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use App\Event\UserLogin;
use Hyperf\Event\Annotation\Listener;
/**
* @Listener
*/
class UserLoginListener implements ListenerInterface
{
public function listen(): array
{
// 返回一个该监听器要监听的事件数组,可以同时监听多个事件
return [
UserLogin::class,
];
}
/**
* @param object|UserLogin $event
*/
public function process(object $event)
{
// echo $event->user->id . ':' . $event->platform . ':' . $event->ip;
// 推送登录提示信息
}
}

View File

@ -64,6 +64,10 @@ class SubscribeHandleService
*/ */
public function handle(array $data) public function handle(array $data)
{ {
if (!isset($data['uuid'], $data['event'], $data['data'], $data['options'])) {
return false;
}
if (isset(self::EVENTS[$data['event']])) { if (isset(self::EVENTS[$data['event']])) {
call_user_func([$this, self::EVENTS[$data['event']]], $data); call_user_func([$this, self::EVENTS[$data['event']]], $data);
} }

View File

@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
namespace App\Service; namespace App\Service;

View File

@ -5,7 +5,6 @@
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
use Hyperf\Amqp\Message\ProducerMessage;
use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface; use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Logger\LoggerFactory; use Hyperf\Logger\LoggerFactory;
@ -16,7 +15,6 @@ use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer; use Swoole\WebSocket\Server as WebSocketServer;
use Hyperf\Utils\Str; use Hyperf\Utils\Str;
use Hyperf\Redis\Redis; use Hyperf\Redis\Redis;
use Hyperf\Amqp\Producer;
/** /**
* 容器实例 * 容器实例
@ -78,6 +76,14 @@ function cache()
return container()->get(Psr\SimpleCache\CacheInterface::class); return container()->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 队列 * 推送消息至 RabbitMQ 队列
* *
* @param ProducerMessage $message * @param \Hyperf\Amqp\Message\ProducerMessage $message
* @param bool $confirm * @param bool $confirm
* @param int $timeout * @param int $timeout
* @return bool * @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);
} }
/** /**

View File

@ -28,15 +28,16 @@
"hyperf/database": "~2.0.0", "hyperf/database": "~2.0.0",
"hyperf/async-queue": "~2.0.0", "hyperf/async-queue": "~2.0.0",
"hyperf/websocket-server": "^2.0", "hyperf/websocket-server": "^2.0",
"hyperf/constants": "^2.0", "hyperf/constants": "~2.0.0",
"hyperf/validation": "^2.0", "hyperf/validation": "~2.0.0",
"hyperf/filesystem": "^2.0", "hyperf/filesystem": "~2.0.0",
"hashids/hashids": "^4.0", "hashids/hashids": "^4.0",
"ext-json": "*", "ext-json": "*",
"hyperf/view": "^2.0", "hyperf/view": "~2.0.0",
"hyperf/view-engine": "^2.0", "hyperf/view-engine": "~2.0.0",
"phpmailer/phpmailer": "^6.2", "phpmailer/phpmailer": "^6.2",
"96qbhy/hyperf-auth": "^2.3" "96qbhy/hyperf-auth": "^2.3",
"hyperf/event": "~2.0.0"
}, },
"require-dev": { "require-dev": {
"swoole/ide-helper": "^4.5", "swoole/ide-helper": "^4.5",