up. 优化代码内容
This commit is contained in:
parent
5a1fd83430
commit
b0020f6623
18
app/entity/gateway/script/BaseScript.php
Normal file
18
app/entity/gateway/script/BaseScript.php
Normal file
@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace app\entity\gateway\script;
|
||||
|
||||
abstract class BaseScript
|
||||
{
|
||||
public function __construct(array $_bind = [])
|
||||
{
|
||||
foreach ($_bind as $key => $value) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return get_object_vars($this);
|
||||
}
|
||||
}
|
||||
@ -1,19 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace app\entity\gateway\el;
|
||||
namespace app\entity\gateway\script;
|
||||
|
||||
class ElNotification
|
||||
use AllowDynamicProperties;
|
||||
|
||||
/**
|
||||
* Element Plus notification 组件
|
||||
* @doc https://element-plus.org/zh-CN/component/notification.html
|
||||
*/
|
||||
#[AllowDynamicProperties] class ElNotification extends BaseScript
|
||||
{
|
||||
public string $title = '';
|
||||
public string $message = '';
|
||||
public bool $dangerouslyUseHTMLString = false;
|
||||
public string $type;
|
||||
public ?string $icon;
|
||||
public int $duration;
|
||||
public string $position;
|
||||
|
||||
public function __construct(
|
||||
array $_attr = [],
|
||||
array $_bind = [],
|
||||
string $title = '',
|
||||
string $message = '',
|
||||
bool $dangerouslyUseHTMLString = false,
|
||||
@ -21,7 +19,6 @@ class ElNotification
|
||||
string $icon = null,
|
||||
int $duration = 4500,
|
||||
string $position = 'top-right',
|
||||
|
||||
)
|
||||
{
|
||||
$this->title = $title;
|
||||
@ -31,15 +28,6 @@ class ElNotification
|
||||
$this->icon = $icon;
|
||||
$this->duration = $duration;
|
||||
$this->position = $position;
|
||||
if (!empty($_attr)) {
|
||||
foreach ($_attr as $key => $value) {
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
return get_object_vars($this);
|
||||
parent::__construct($_bind);
|
||||
}
|
||||
}
|
||||
27
app/entity/gateway/script/Swal.php
Normal file
27
app/entity/gateway/script/Swal.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace app\entity\gateway\script;
|
||||
|
||||
|
||||
use AllowDynamicProperties;
|
||||
|
||||
/**
|
||||
* sweetalert2 弹窗提示框组件
|
||||
* @doc https://sweetalert2.github.io/#examples
|
||||
*/
|
||||
#[AllowDynamicProperties] class Swal extends BaseScript
|
||||
{
|
||||
|
||||
public function __construct(
|
||||
array $_bind = [],
|
||||
string $title = '',
|
||||
string $text = '',
|
||||
string $icon = null,
|
||||
)
|
||||
{
|
||||
$this->title = $title;
|
||||
$this->text = $text;
|
||||
$this->icon = $icon;
|
||||
parent::__construct($_bind);
|
||||
}
|
||||
}
|
||||
@ -2,9 +2,8 @@
|
||||
|
||||
namespace app\entity\gateway\ws;
|
||||
|
||||
use app\entity\gateway\el\ElNotification;
|
||||
use app\enum\gateway\WsEventEnum;
|
||||
use app\enum\gateway\WsTypeEnum;
|
||||
use app\entity\gateway\script\BaseScript;
|
||||
use app\enum\gateway\message\{ScriptEnum, SystemEnum};
|
||||
|
||||
class OutMessage
|
||||
{
|
||||
@ -12,12 +11,26 @@ class OutMessage
|
||||
private string $event;
|
||||
private array $data;
|
||||
|
||||
public function __construct(WsTypeEnum $type, WsEventEnum $event, mixed $data = [])
|
||||
public function __construct(
|
||||
BaseScript|ScriptEnum|SystemEnum|array $event,
|
||||
mixed $data = []
|
||||
)
|
||||
{
|
||||
$this->type = $type->value;
|
||||
// 通过Base::create();创建的
|
||||
if (is_array($event) && count($event) == 3) {
|
||||
$this->type = $event[0];
|
||||
$this->event = $event[1];
|
||||
$this->data = $data;
|
||||
return;
|
||||
} else if ($event instanceof ScriptEnum) {
|
||||
$this->type = 'script';
|
||||
$this->event = $event->value;
|
||||
|
||||
if($data instanceof ElNotification) {
|
||||
} else if ($event instanceof SystemEnum) {
|
||||
$this->type = 'system';
|
||||
$this->event = $event->value;
|
||||
}
|
||||
// data处理
|
||||
if ($data instanceof BaseScript) {
|
||||
$this->data = $data->toArray();
|
||||
} else {
|
||||
$this->data = (array)$data;
|
||||
|
||||
@ -2,16 +2,17 @@
|
||||
|
||||
namespace app\entity\gateway\ws;
|
||||
|
||||
use app\enum\gateway\WsEventEnum;
|
||||
use app\enum\gateway\WsTypeEnum;
|
||||
use app\enum\gateway\message\MessageType;
|
||||
use app\enum\gateway\message\ScriptEnum;
|
||||
use app\enum\gateway\message\SystemEnum;
|
||||
|
||||
/**
|
||||
* Ws消息入参
|
||||
*/
|
||||
class PutMessage
|
||||
{
|
||||
readonly public ?WsTypeEnum $type;
|
||||
readonly public ?WsEventEnum $event;
|
||||
readonly public ?MessageType $type;
|
||||
readonly public SystemEnum|ScriptEnum|null $event;
|
||||
readonly public array $data;
|
||||
public string $wsClientId;
|
||||
|
||||
@ -22,8 +23,10 @@ class PutMessage
|
||||
public function __construct(string $ws_client_id, string $type, string $event, array $data = [])
|
||||
{
|
||||
$this->wsClientId = $ws_client_id;
|
||||
$this->type = WsTypeEnum::tryFrom($type);
|
||||
$this->event = WsEventEnum::tryFrom($event);
|
||||
if ($type == MessageType::System) {
|
||||
$this->type = MessageType::System;
|
||||
$this->event = SystemEnum::tryFrom($event);
|
||||
}
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
@ -43,10 +46,12 @@ class PutMessage
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getClientId(): string
|
||||
{
|
||||
return $this->userClient['id'] ?? '';
|
||||
}
|
||||
|
||||
public function getClientName()
|
||||
{
|
||||
return $this->userClient['name'] ?? '';
|
||||
|
||||
@ -1,13 +0,0 @@
|
||||
<?php
|
||||
namespace app\enum\gateway;
|
||||
|
||||
|
||||
/**
|
||||
* Ws消息组类型
|
||||
*/
|
||||
enum WsTypeEnum: string
|
||||
{
|
||||
case System = 'system';
|
||||
case Ping = 'ping';
|
||||
case Pong = 'pong';
|
||||
}
|
||||
15
app/enum/gateway/message/MessageType.php
Normal file
15
app/enum/gateway/message/MessageType.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace app\enum\gateway\message;
|
||||
|
||||
|
||||
/**
|
||||
* 网关/消息类型
|
||||
*/
|
||||
enum MessageType: string
|
||||
{
|
||||
case System = 'system';
|
||||
case Script = 'script';
|
||||
case Pong = 'pong';
|
||||
case Ping = 'ping';
|
||||
}
|
||||
21
app/enum/gateway/message/ScriptEnum.php
Normal file
21
app/enum/gateway/message/ScriptEnum.php
Normal file
@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace app\enum\gateway\message;
|
||||
|
||||
use app\entity\gateway\script\BaseScript;
|
||||
|
||||
/**
|
||||
* 网关/消息/Script类型枚举
|
||||
*/
|
||||
enum ScriptEnum: string
|
||||
{
|
||||
case ElNotification = 'ElNotification';
|
||||
case Swal = 'Swal';
|
||||
|
||||
public static function create(BaseScript $class): array
|
||||
{
|
||||
$event = basename(str_replace('\\', '/', get_class($class)));
|
||||
$data = $class->toArray();
|
||||
return ['script', $event, $data];
|
||||
}
|
||||
}
|
||||
@ -1,17 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace app\enum\gateway;
|
||||
namespace app\enum\gateway\message;
|
||||
|
||||
|
||||
/**
|
||||
* Ws事件组类型
|
||||
* 网关/消息/System类型枚举
|
||||
*/
|
||||
enum WsEventEnum: string
|
||||
enum SystemEnum: string
|
||||
{
|
||||
case Login = 'login';
|
||||
case Login_SUCCESS = 'login_success';
|
||||
case Lock_Client = 'lock_client';
|
||||
case Client_quit_room = 'client_quit_room';
|
||||
case Notification = 'notification';
|
||||
|
||||
}
|
||||
@ -12,8 +12,8 @@ use app\entity\gateway\ws\OutMessage;
|
||||
use app\entity\gateway\ws\PutMessage;
|
||||
use app\entity\SysRole;
|
||||
use app\entity\SysUser;
|
||||
use app\enum\gateway\WsEventEnum;
|
||||
use app\enum\gateway\WsTypeEnum;
|
||||
use app\enum\gateway\message\MessageType;
|
||||
use app\enum\gateway\message\SystemEnum;
|
||||
use app\service\admin\LoginService;
|
||||
use app\service\GatewayClientService;
|
||||
use think\exception\ValidateException;
|
||||
@ -38,7 +38,7 @@ class SysGatewayEvent
|
||||
/*
|
||||
* 客户端登录
|
||||
*/
|
||||
if ($put->type == WsTypeEnum::System && $put->event == WsEventEnum::Login) {
|
||||
if ($put->type == MessageType::System && $put->event == SystemEnum::Login) {
|
||||
self::authEvent($put);
|
||||
return;
|
||||
}
|
||||
@ -56,12 +56,12 @@ class SysGatewayEvent
|
||||
// 3. 调度: 根据类型执行不同的业务
|
||||
switch ($put->type) {
|
||||
// 客户端回应服务端的心跳
|
||||
case WsTypeEnum::Pong:
|
||||
case MessageType::Pong:
|
||||
break;
|
||||
// 系统类型组操作
|
||||
case WsTypeEnum::System:
|
||||
case MessageType::System:
|
||||
switch ($put->event) {
|
||||
case WsEventEnum::Lock_Client:
|
||||
case SystemEnum::Lock_Client:
|
||||
/**
|
||||
* 触发后续其他事件
|
||||
*/
|
||||
@ -82,8 +82,7 @@ class SysGatewayEvent
|
||||
* 客户端退出了某个房间
|
||||
*/
|
||||
GatewayClientService::sendToGroup($group, new OutMessage(
|
||||
WsTypeEnum::System,
|
||||
WsEventEnum::Client_quit_room,
|
||||
SystemEnum::Client_quit_room
|
||||
));
|
||||
}
|
||||
}
|
||||
@ -137,8 +136,7 @@ class SysGatewayEvent
|
||||
* 推送登录成功的消息到客户端
|
||||
*/
|
||||
GatewayClientService::sendToClient($put->wsClientId, new OutMessage(
|
||||
WsTypeEnum::System,
|
||||
WsEventEnum::Login_SUCCESS,
|
||||
SystemEnum::Login_SUCCESS,
|
||||
['userId' => $auth->userId]
|
||||
));
|
||||
/**
|
||||
|
||||
@ -2,14 +2,15 @@
|
||||
|
||||
namespace app\subscribe;
|
||||
|
||||
use app\entity\gateway\el\ElNotification;
|
||||
use app\entity\gateway\script\ElNotification;
|
||||
use app\entity\gateway\script\Swal;
|
||||
use app\entity\gateway\ws\OutMessage;
|
||||
use app\entity\gateway\ws\PutMessage;
|
||||
use app\entity\SysUser;
|
||||
use app\entity\SysUserClient;
|
||||
use app\entity\SysUserClientLog;
|
||||
use app\enum\gateway\WsEventEnum;
|
||||
use app\enum\gateway\WsTypeEnum;
|
||||
use app\enum\gateway\message\ScriptEnum;
|
||||
use app\enum\gateway\message\SystemEnum;
|
||||
use app\service\GatewayClientService;
|
||||
use think\db\exception\DataNotFoundException;
|
||||
use think\db\exception\DbException;
|
||||
@ -61,19 +62,26 @@ class GatewaySubscribe
|
||||
if($client && $client['is_lock']) {
|
||||
// 通知客户端锁定屏幕
|
||||
GatewayClientService::sendToClient($put->wsClientId,new OutMessage(
|
||||
WsTypeEnum::System,
|
||||
WsEventEnum::Lock_Client,
|
||||
SystemEnum::Lock_Client
|
||||
));
|
||||
}
|
||||
/*
|
||||
* 客户端登录通知
|
||||
*/
|
||||
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
|
||||
|
||||
// 方式0. 通过enum枚举类型指定
|
||||
GatewayClientService::sendToClient($put->wsClientId, new OutMessage(
|
||||
WsTypeEnum::System,
|
||||
WsEventEnum::Notification,
|
||||
ScriptEnum::ElNotification,
|
||||
new ElNotification(title: '登录成功,欢迎您', message: "当前登录Ip: $ip", position: 'bottom-right'),
|
||||
));
|
||||
|
||||
// 方式1. 通过注入Array
|
||||
GatewayClientService::sendToClient($put->wsClientId, new OutMessage(
|
||||
ScriptEnum::create(
|
||||
new Swal(title: '登录成功,欢迎您', text: "当前登录Ip: $ip")
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,8 +118,7 @@ class GatewaySubscribe
|
||||
]);
|
||||
// 通知客户端锁定屏幕
|
||||
GatewayClientService::sendToClient($put->wsClientId,new OutMessage(
|
||||
WsTypeEnum::System,
|
||||
WsEventEnum::Lock_Client,
|
||||
SystemEnum::Lock_Client
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user