消息推送

This commit is contained in:
mzl
2025-08-20 18:12:07 +08:00
parent 78e28a99dc
commit 9f96b8d625
13 changed files with 451 additions and 41 deletions
+35 -21
View File
@@ -4,6 +4,7 @@ namespace app\common\utils;
use GatewayWorker\Lib\Gateway;
use app\common\utils\WsResponse;
use app\common\service\MessageNotificationsService;
class WsEvents
{
@@ -72,17 +73,17 @@ class WsEvents
return;
}
// // 连接是否满足条件
// 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 ($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 (!isset($payload['get']['token'])) {
@@ -136,11 +137,6 @@ class WsEvents
// ================= 消息通知事件 =================
/**
* 管理端主动推送通知
* data = [
* 'to_uids' => [uid1, uid2], // 可选,为空表示全员
* 'title' => '标题',
* 'content' => '内容',
* ]
*/
public function notifyPush(?string $traceId)
{
@@ -158,7 +154,20 @@ class WsEvents
return;
}
$msgId = rand(1000, 9999); // 生成消息ID,可用数据库存储
// 写消息
$msgService = new MessageNotificationsService();
$msgId = $msgService->createMessage([
'sender_id' => $this->eventData['sender_id'] ?? 0,
'sender_type' => $this->eventData['sender_type'] ?? 'system',
'title' => $title,
'content' => $content,
'scene' => $this->eventData['scene'] ?? 0,
'recipient' => $this->eventData['to_uids'] ? implode(',', $this->eventData['to_uids']) : '',
]);
// 写用户状态
$uids = !empty($toUids) ? $toUids : $msgService->getAllUserIds();
$msgService->addUserStatus($msgId, $uids);
$payload = WsResponse::success('notify.push', [
'msg_id' => $msgId,
@@ -181,18 +190,18 @@ class WsEvents
Gateway::sendToClient($this->clientId, WsResponse::success(
'notify.push.result',
['msg_id' => $msgId],
$traceId
$traceId,
'消息发送成功'
));
}
/**
* 客户端标记通知已读
* data = ['msg_ids' => [1,2,3]]
*/
public function notifyRead(?string $traceId)
{
$msgIds = $this->eventData['msg_ids'] ?? [];
if (empty($msgIds)) {
$msgIds = $this->eventData['msg_ids'];
if (!is_array($msgIds) && $msgIds !== 'all') {
Gateway::sendToClient($this->clientId, WsResponse::error(
'notify.read',
400,
@@ -201,6 +210,11 @@ class WsEvents
));
return;
}
// 更新消息为已读
$msgService = new MessageNotificationsService();
$msgService->markAsRead($msgIds, Gateway::getUidByClientId($this->clientId));
Gateway::sendToClient($this->clientId, WsResponse::success(
'notify.read.result',
['msg_ids' => $msgIds],