消息推送
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\service;
|
||||
|
||||
use app\common\validate\MessageNotificationsValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\MessageNotifications;
|
||||
use app\common\model\User;
|
||||
use support\Db;
|
||||
|
||||
class MessageNotificationsService
|
||||
{
|
||||
|
||||
use CrudServiceTrait;
|
||||
|
||||
/**
|
||||
* 指定当前要操作的模型
|
||||
*/
|
||||
protected string $modelClass = MessageNotifications::class;
|
||||
|
||||
/**
|
||||
* 验证器
|
||||
*/
|
||||
protected $validator = MessageNotificationsValidate::class;
|
||||
|
||||
/**
|
||||
* 获取验证器
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return new $this->validator;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建一条消息
|
||||
*/
|
||||
public function createMessage(array $data): int
|
||||
{
|
||||
$msgId = Db::table('message_notifications')->insertGetId([
|
||||
'msg_no' => generateMsgId(),
|
||||
'sender_id' => $data['sender_id'],
|
||||
'sender_type' => $data['sender_type'],
|
||||
'title' => $data['title'],
|
||||
'content' => $data['content'],
|
||||
'scene' => $data['scene'],
|
||||
'recipient' => $data['recipient'],
|
||||
'created_at' => time(),
|
||||
]);
|
||||
|
||||
return $msgId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量插入用户消息状态
|
||||
*/
|
||||
public function addUserStatus(int $msgId, array $uids)
|
||||
{
|
||||
$rows = [];
|
||||
foreach ($uids as $uid) {
|
||||
$rows[] = [
|
||||
'msg_id' => $msgId,
|
||||
'uid' => $uid,
|
||||
'status' => 0,
|
||||
'read_at' => null
|
||||
];
|
||||
}
|
||||
if ($rows) {
|
||||
Db::table('message_user_status')->insert($rows);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 标记消息已读
|
||||
*/
|
||||
public function markAsRead(array|string $msgIds, int $uid)
|
||||
{
|
||||
$query = Db::table('message_user_status')->where('uid', $uid);
|
||||
if (is_array($msgIds)) {
|
||||
$query->whereIn('msg_id', $msgIds);
|
||||
}
|
||||
return $query->update([
|
||||
'status' => 1,
|
||||
'read_at' => time()
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有用户ID
|
||||
*/
|
||||
public function getAllUserIds(): array
|
||||
{
|
||||
return User::where('status', 1)->pluck('id')->toArray();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user