调整消息推送
This commit is contained in:
@@ -6,6 +6,7 @@ use app\common\logic\MessageNotificationsLogic;
|
||||
use app\common\validate\MessageNotificationsValidate;
|
||||
use app\common\traits\crud\CrudServiceTrait;
|
||||
use app\common\model\MessageNotifications;
|
||||
use app\common\model\MessageUserStatus;
|
||||
use app\common\model\User;
|
||||
use support\Db;
|
||||
use support\Request;
|
||||
@@ -106,6 +107,111 @@ class MessageNotificationsService
|
||||
return $data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ====== ====== ====== 通知操作的处理 ====== ====== ======
|
||||
/**
|
||||
* 推送消息
|
||||
*/
|
||||
public function pushNotice(array $data = [], string $scene = "Default"): int
|
||||
{
|
||||
// 拼装数据(可能需要处理 消息标题、内容等等数据,可自行处理)
|
||||
$pushData = $this->assemblyProcess($scene, $data);
|
||||
// 写入消息
|
||||
$msgId = $this->insertMessage($pushData);
|
||||
// 推送
|
||||
$this->pushWs($msgId, $pushData);
|
||||
return $msgId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行推送
|
||||
*/
|
||||
public function pushWs(int $msgId, array $pushData)
|
||||
{
|
||||
foreach ($pushData['recipient'] as $userId) {
|
||||
// 查询消息数据
|
||||
$config['model'] = MessageUserStatus::class;
|
||||
$config['order']['id'] = 'desc';
|
||||
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
||||
$config['where']['uid'] = $userId;
|
||||
$config['where']['msg_id'] = $msgId;
|
||||
$config['where']['status'] = 0;
|
||||
$wsData = (new MessageUserStatusService())->logicShow($config)->toArray();
|
||||
|
||||
// 组装数据
|
||||
if (isset($wsData['message']) && $wsData['message']) {
|
||||
$wsData['message']['sender_name'] = User::where('id', $wsData['message']['sender_id'])->value('name') ?: '系统';
|
||||
$wsData['message']['scene_name'] = MessageNotifications::SCENE[$wsData['message']['scene']];
|
||||
$wsData['message']['created_at'] = date('Y-m-d H:i:s', $wsData['message']['created_at']);
|
||||
}
|
||||
|
||||
// 获取未读消息数
|
||||
$counts = MessageUserStatus::where('uid', $userId)->where('status', 0)->count('id');
|
||||
$data = [
|
||||
'notice_data' => $wsData,
|
||||
'unread_counts' => $counts
|
||||
];
|
||||
|
||||
// 执行推送
|
||||
(new \app\common\utils\WsEvents())->pushWsNotice($data, $userId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入消息
|
||||
* @param array $data
|
||||
* @return int
|
||||
*/
|
||||
public function insertMessage(array $data): int
|
||||
{
|
||||
// 写消息
|
||||
$msgId = $this->createMessage([
|
||||
'sender_id' => $data['sender_id'],
|
||||
'sender_type' => $data['sender_type'],
|
||||
'title' => $data['title'],
|
||||
'content' => $data['content'],
|
||||
'scene' => $data['scene'],
|
||||
'recipient' => implode(',', $data['recipient']),
|
||||
'push_method' => $data['push_method'],
|
||||
]);
|
||||
// 写用户状态
|
||||
$this->addUserStatus($msgId, $data['recipient']);
|
||||
return $msgId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据拼装方法调用
|
||||
*/
|
||||
protected function assemblyProcess(string $op, mixed $data): mixed
|
||||
{
|
||||
$hook = "assembly{$op}";
|
||||
return method_exists($this, $hook)
|
||||
? $this->{$hook}($data)
|
||||
: $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 默认数据拼装
|
||||
*/
|
||||
public function assemblyDefault(array $data)
|
||||
{
|
||||
return [
|
||||
'sender_id' => isset($data['sender_id']) && $data['sender_id'] ? $data['sender_id'] : 0,
|
||||
'sender_type' => isset($data['sender_type']) && $data['sender_type'] ? $data['sender_type'] : 'system',
|
||||
'title' => isset($data['title']) && $data['title'] ? $data['title'] : '系统通知',
|
||||
'content' => isset($data['content']) && $data['content'] ? $data['content'] : '系统通知-默认',
|
||||
'scene' => isset($data['scene']) && $data['scene'] ? $data['scene'] : 1,
|
||||
'recipient' => isset($data['recipient']) && count($data['recipient']) > 0 ? $data['recipient'] : $this->getAllUserIds(),
|
||||
'push_method' => isset($data['push_method']) && $data['push_method'] ? $data['push_method'] : 0,
|
||||
];
|
||||
}
|
||||
|
||||
// *** *** 可以扩展数据组拼装方法 *** ***
|
||||
|
||||
// ====== ====== ====== 通知操作的基础方法 ====== ====== ======
|
||||
|
||||
/**
|
||||
* 创建一条消息
|
||||
*/
|
||||
@@ -167,40 +273,4 @@ 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']) : '',
|
||||
'push_method' => $data['push_method'],
|
||||
]);
|
||||
|
||||
// 写用户状态
|
||||
$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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user