消息推送

This commit is contained in:
mzl
2025-08-20 19:14:27 +08:00
parent 9f96b8d625
commit 58dc6729d9
5 changed files with 130 additions and 5 deletions
@@ -75,7 +75,7 @@ class MessageNotificationsService
*/
public function markAsRead(array|string $msgIds, int $uid)
{
$query = Db::table('message_user_status')->where('uid', $uid);
$query = Db::table('message_user_status')->where('uid', $uid)->where('status', 0);
if (is_array($msgIds)) {
$query->whereIn('msg_id', $msgIds);
}
@@ -92,4 +92,39 @@ class MessageNotificationsService
{
return User::where('status', 1)->pluck('id')->toArray();
}
/**
* 批量推送消息
*/
public function pushNotice(array $data): int
{
// 数据结构:
// $data = [
// 'sender_id' => 0,
// 'sender_type' => 'system',
// 'title' => '测试消息 - 标题',
// 'content' => '测试消息 - 内容',
// 'scene' => 1,
// 'recipient' => [],
// ];
// 写消息
$msgId = $this->createMessage([
'sender_id' => $data['sender_id'],
'sender_type' => $data['sender_type'],
'title' => $data['title'],
'content' => $data['content'],
'scene' => $data['scene'],
'recipient' => $data['recipient'] ? implode(',', $data['recipient']) : '',
]);
// 写用户状态
$uids = count($data['recipient']) > 0 ? $data['recipient'] : $this->getAllUserIds();
$this->addUserStatus($msgId, $uids);
// 执行推送
(new \app\common\utils\WsEvents())->pushNotice($msgId, $data['title'], $data['content'], $data['recipient']);
return $msgId;
}
}