优化代码

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;
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,
]
], '账号登录成功...');
}

View File

@ -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,
]));
}
}
}

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)
{
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);
}

View File

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

View File

@ -5,7 +5,6 @@
|--------------------------------------------------------------------------
*/
use Hyperf\Amqp\Message\ProducerMessage;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Logger\LoggerFactory;
@ -16,7 +15,6 @@ use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;
use Hyperf\Utils\Str;
use Hyperf\Redis\Redis;
use Hyperf\Amqp\Producer;
/**
* 容器实例
@ -78,6 +76,14 @@ function cache()
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 队列
*
* @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);
}
/**

View File

@ -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",