up. 优化代码内容

This commit is contained in:
扶桑花间 2025-08-29 23:32:20 +08:00
parent 5a1fd83430
commit b0020f6623
11 changed files with 153 additions and 76 deletions

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

View File

@ -1,19 +1,17 @@
<?php <?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( public function __construct(
array $_attr = [], array $_bind = [],
string $title = '', string $title = '',
string $message = '', string $message = '',
bool $dangerouslyUseHTMLString = false, bool $dangerouslyUseHTMLString = false,
@ -21,7 +19,6 @@ class ElNotification
string $icon = null, string $icon = null,
int $duration = 4500, int $duration = 4500,
string $position = 'top-right', string $position = 'top-right',
) )
{ {
$this->title = $title; $this->title = $title;
@ -31,15 +28,6 @@ class ElNotification
$this->icon = $icon; $this->icon = $icon;
$this->duration = $duration; $this->duration = $duration;
$this->position = $position; $this->position = $position;
if (!empty($_attr)) { parent::__construct($_bind);
foreach ($_attr as $key => $value) {
$this->$key = $value;
}
}
}
public function toArray(): array
{
return get_object_vars($this);
} }
} }

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

View File

@ -2,9 +2,8 @@
namespace app\entity\gateway\ws; namespace app\entity\gateway\ws;
use app\entity\gateway\el\ElNotification; use app\entity\gateway\script\BaseScript;
use app\enum\gateway\WsEventEnum; use app\enum\gateway\message\{ScriptEnum, SystemEnum};
use app\enum\gateway\WsTypeEnum;
class OutMessage class OutMessage
{ {
@ -12,12 +11,26 @@ class OutMessage
private string $event; private string $event;
private array $data; 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; $this->event = $event->value;
} else if ($event instanceof SystemEnum) {
if($data instanceof ElNotification) { $this->type = 'system';
$this->event = $event->value;
}
// data处理
if ($data instanceof BaseScript) {
$this->data = $data->toArray(); $this->data = $data->toArray();
} else { } else {
$this->data = (array)$data; $this->data = (array)$data;

View File

@ -2,16 +2,17 @@
namespace app\entity\gateway\ws; namespace app\entity\gateway\ws;
use app\enum\gateway\WsEventEnum; use app\enum\gateway\message\MessageType;
use app\enum\gateway\WsTypeEnum; use app\enum\gateway\message\ScriptEnum;
use app\enum\gateway\message\SystemEnum;
/** /**
* Ws消息入参 * Ws消息入参
*/ */
class PutMessage class PutMessage
{ {
readonly public ?WsTypeEnum $type; readonly public ?MessageType $type;
readonly public ?WsEventEnum $event; readonly public SystemEnum|ScriptEnum|null $event;
readonly public array $data; readonly public array $data;
public string $wsClientId; public string $wsClientId;
@ -22,8 +23,10 @@ class PutMessage
public function __construct(string $ws_client_id, string $type, string $event, array $data = []) public function __construct(string $ws_client_id, string $type, string $event, array $data = [])
{ {
$this->wsClientId = $ws_client_id; $this->wsClientId = $ws_client_id;
$this->type = WsTypeEnum::tryFrom($type); if ($type == MessageType::System) {
$this->event = WsEventEnum::tryFrom($event); $this->type = MessageType::System;
$this->event = SystemEnum::tryFrom($event);
}
$this->data = $data; $this->data = $data;
} }
@ -43,10 +46,12 @@ class PutMessage
return true; return true;
} }
public function getClientId(): string public function getClientId(): string
{ {
return $this->userClient['id'] ?? ''; return $this->userClient['id'] ?? '';
} }
public function getClientName() public function getClientName()
{ {
return $this->userClient['name'] ?? ''; return $this->userClient['name'] ?? '';

View File

@ -1,13 +0,0 @@
<?php
namespace app\enum\gateway;
/**
* Ws消息组类型
*/
enum WsTypeEnum: string
{
case System = 'system';
case Ping = 'ping';
case Pong = 'pong';
}

View 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';
}

View 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];
}
}

View File

@ -1,17 +1,15 @@
<?php <?php
namespace app\enum\gateway; namespace app\enum\gateway\message;
/** /**
* Ws事件组类型 * 网关/消息/System类型枚举
*/ */
enum WsEventEnum: string enum SystemEnum: string
{ {
case Login = 'login'; case Login = 'login';
case Login_SUCCESS = 'login_success'; case Login_SUCCESS = 'login_success';
case Lock_Client = 'lock_client'; case Lock_Client = 'lock_client';
case Client_quit_room = 'client_quit_room'; case Client_quit_room = 'client_quit_room';
case Notification = 'notification';
} }

View File

@ -12,8 +12,8 @@ use app\entity\gateway\ws\OutMessage;
use app\entity\gateway\ws\PutMessage; use app\entity\gateway\ws\PutMessage;
use app\entity\SysRole; use app\entity\SysRole;
use app\entity\SysUser; use app\entity\SysUser;
use app\enum\gateway\WsEventEnum; use app\enum\gateway\message\MessageType;
use app\enum\gateway\WsTypeEnum; use app\enum\gateway\message\SystemEnum;
use app\service\admin\LoginService; use app\service\admin\LoginService;
use app\service\GatewayClientService; use app\service\GatewayClientService;
use think\exception\ValidateException; 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); self::authEvent($put);
return; return;
} }
@ -56,12 +56,12 @@ class SysGatewayEvent
// 3. 调度: 根据类型执行不同的业务 // 3. 调度: 根据类型执行不同的业务
switch ($put->type) { switch ($put->type) {
// 客户端回应服务端的心跳 // 客户端回应服务端的心跳
case WsTypeEnum::Pong: case MessageType::Pong:
break; break;
// 系统类型组操作 // 系统类型组操作
case WsTypeEnum::System: case MessageType::System:
switch ($put->event) { switch ($put->event) {
case WsEventEnum::Lock_Client: case SystemEnum::Lock_Client:
/** /**
* 触发后续其他事件 * 触发后续其他事件
*/ */
@ -82,8 +82,7 @@ class SysGatewayEvent
* 客户端退出了某个房间 * 客户端退出了某个房间
*/ */
GatewayClientService::sendToGroup($group, new OutMessage( GatewayClientService::sendToGroup($group, new OutMessage(
WsTypeEnum::System, SystemEnum::Client_quit_room
WsEventEnum::Client_quit_room,
)); ));
} }
} }
@ -137,8 +136,7 @@ class SysGatewayEvent
* 推送登录成功的消息到客户端 * 推送登录成功的消息到客户端
*/ */
GatewayClientService::sendToClient($put->wsClientId, new OutMessage( GatewayClientService::sendToClient($put->wsClientId, new OutMessage(
WsTypeEnum::System, SystemEnum::Login_SUCCESS,
WsEventEnum::Login_SUCCESS,
['userId' => $auth->userId] ['userId' => $auth->userId]
)); ));
/** /**

View File

@ -2,14 +2,15 @@
namespace app\subscribe; 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\OutMessage;
use app\entity\gateway\ws\PutMessage; use app\entity\gateway\ws\PutMessage;
use app\entity\SysUser; use app\entity\SysUser;
use app\entity\SysUserClient; use app\entity\SysUserClient;
use app\entity\SysUserClientLog; use app\entity\SysUserClientLog;
use app\enum\gateway\WsEventEnum; use app\enum\gateway\message\ScriptEnum;
use app\enum\gateway\WsTypeEnum; use app\enum\gateway\message\SystemEnum;
use app\service\GatewayClientService; use app\service\GatewayClientService;
use think\db\exception\DataNotFoundException; use think\db\exception\DataNotFoundException;
use think\db\exception\DbException; use think\db\exception\DbException;
@ -61,19 +62,26 @@ class GatewaySubscribe
if($client && $client['is_lock']) { if($client && $client['is_lock']) {
// 通知客户端锁定屏幕 // 通知客户端锁定屏幕
GatewayClientService::sendToClient($put->wsClientId,new OutMessage( GatewayClientService::sendToClient($put->wsClientId,new OutMessage(
WsTypeEnum::System, SystemEnum::Lock_Client
WsEventEnum::Lock_Client,
)); ));
} }
/* /*
* 客户端登录通知 * 客户端登录通知
*/ */
$ip = $_SERVER['REMOTE_ADDR'] ?? ''; $ip = $_SERVER['REMOTE_ADDR'] ?? '';
// 方式0. 通过enum枚举类型指定
GatewayClientService::sendToClient($put->wsClientId, new OutMessage( GatewayClientService::sendToClient($put->wsClientId, new OutMessage(
WsTypeEnum::System, ScriptEnum::ElNotification,
WsEventEnum::Notification,
new ElNotification(title: '登录成功,欢迎您', message: "当前登录Ip: $ip", position: 'bottom-right'), 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( GatewayClientService::sendToClient($put->wsClientId,new OutMessage(
WsTypeEnum::System, SystemEnum::Lock_Client
WsEventEnum::Lock_Client,
)); ));
} }