消息推送
This commit is contained in:
@@ -46,4 +46,12 @@ class MessageNotifications extends Model
|
||||
* 不允许自动填充模型字段
|
||||
*/
|
||||
protected $guarded = [];
|
||||
|
||||
// 消息创建
|
||||
const SCENE = [
|
||||
self::SCENE0 => '系统消息',
|
||||
self::SCENE1 => '系统通知'
|
||||
];
|
||||
const SCENE0 = 0;
|
||||
const SCENE1 = 1;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -222,6 +222,29 @@ class WsEvents
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* 推送消息通知
|
||||
*/
|
||||
public function pushNotice(int $msgId, string $title, string $content, array $recipient)
|
||||
{
|
||||
$payload = WsResponse::success('notify.push', [
|
||||
'msg_id' => $msgId,
|
||||
'title' => $title,
|
||||
'content' => $content,
|
||||
'created_at' => time()
|
||||
]);
|
||||
|
||||
if (count($recipient) > 0) {
|
||||
// 推送给指定用户
|
||||
foreach ($recipient as $uid) {
|
||||
Gateway::sendToUid($uid, $payload);
|
||||
}
|
||||
} else {
|
||||
// 推送给全体在线用户
|
||||
Gateway::sendToAll($payload);
|
||||
}
|
||||
}
|
||||
|
||||
// ================= 辅助方法 =================
|
||||
protected function eventToMethod(string $event): string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user