Merge branch 'develop'

This commit is contained in:
mzl
2025-10-12 17:10:14 +08:00
parent 3a7449e2e9
commit 89d633389c
43 changed files with 4173 additions and 364 deletions
+88 -21
View File
@@ -53,6 +53,16 @@ class WsEvents
}
}
protected function eventToMethod(string $event): string
{
$parts = explode('.', $event);
$method = array_shift($parts);
foreach ($parts as $part) {
$method .= ucfirst($part);
}
return $method;
}
// ================= 系统事件 =================
/**
@@ -74,16 +84,16 @@ class WsEvents
}
// 连接是否满足条件
// if ($payload['server']['HTTP_ORIGIN'] != 'https://wstool.js.org') {
// flg('[x] [TOKEN认证] HTTP_ORIGIN不满足条件', false);
// Gateway::sendToClient($client_id, WsResponse::error(
// 'system.auth',
// 1001,
// '[x] [TOKEN认证] HTTP_ORIGIN不满足条件'
// ));
// Gateway::closeClient($client_id);
// return;
// }
if (getenv('ENVIRONMENT') === 'master' && $payload['server']['HTTP_ORIGIN'] === getenv('MAIN_DOMAIN')) {
flg('[x] [TOKEN认证] HTTP_ORIGIN不满足条件', false);
Gateway::sendToClient($client_id, WsResponse::error(
'system.auth',
1001,
'[x] [TOKEN认证] HTTP_ORIGIN不满足条件'
));
Gateway::closeClient($client_id);
return;
}
// 签名检查
if (!isset($payload['get']['token'])) {
@@ -99,9 +109,14 @@ class WsEvents
// 解析TOKEN
$user = (new Jwt())->verifyToken($payload['get']['token']);
// 绑定用户ID
Gateway::bindUid($client_id, $user->uid);
// 绑定SESSION
$_SESSION['uid'] = $user->uid;
$_SESSION['name'] = $user->uname;
// 响应
Gateway::sendToClient($client_id, WsResponse::success(
'system.auth',
@@ -119,6 +134,13 @@ class WsEvents
}
}
public function logout(string $client_id)
{
if ($_SESSION !== null) {
var_dump($_SESSION);
}
}
/**
* 系统心跳
*
@@ -134,6 +156,18 @@ class WsEvents
}
public function systemConnect(?string $traceId)
{
$num = (new \app\common\utils\approval\service\ApprovalFlowService())->getInitTodoCount($_SESSION['uid']);
$this->todoCounts($_SESSION['uid'], $num);
Gateway::sendToClient($this->clientId, WsResponse::success(
'system.connect.receive',
['time' => time()],
$traceId
));
}
// ================= 消息通知事件 =================
/**
* 管理端主动推送通知
@@ -223,22 +257,55 @@ class WsEvents
));
}
/**
* 推送消息通知
/*
init.pull
approval.todo.counts 审批待办数量
*/
public function pushWsNotice(array $wsData, int $userId)
// ================= 主动推送 =================
public function pushWebsocket(string $event, int|string $uid, array $data)
{
Gateway::sendToUid($userId, WsResponse::success('notify.receive', $wsData));
return match (gettype($uid)) {
'integer' => Gateway::sendToUid($uid, WsResponse::success($event, $data)),
'string' => Gateway::sendToClient($uid, WsResponse::success($event, $data)),
default => throw new \InvalidArgumentException('UID must be integer or string'),
};
}
// ================= 辅助方法 =================
protected function eventToMethod(string $event): string
// ================= 临时业务处理 =================
public function todoCounts(int $userId, int $num = 0)
{
$parts = explode('.', $event);
$method = array_shift($parts);
foreach ($parts as $part) {
$method .= ucfirst($part);
$ly = [
1 => 186,
3 => 226,
];
if ($num === 0) {
$num = (new \app\common\utils\approval\service\ApprovalFlowService())->getInitTodoCount($userId);
}
return $method;
$systemIds = \app\common\model\UserSystems::where('user_id', $userId)->pluck('system_id')->toArray();
if (count($systemIds) > 0) {
foreach ($systemIds as $systemId) {
if (isset($ly[$systemId])) {
$chain = \app\common\service\MenuBadgeService::buildBadgeChain(menuId: $ly[$systemId], leafCount: $num);
$this->pushWebsocket('approval.todo.counts', $userId, $chain);
}
}
}
}
}