Files
fdesk/app/common/service/MessageNotificationsService.php
T
2025-08-21 15:47:05 +08:00

206 lines
5.8 KiB
PHP

<?php
namespace app\common\service;
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\User;
use support\Db;
use support\Request;
class MessageNotificationsService
{
use CrudServiceTrait;
/**
* 指定当前要操作的模型
*/
protected string $modelClass = MessageNotifications::class;
/**
* 验证器
*/
protected $validator = MessageNotificationsValidate::class;
/**
* 获取验证器
*/
public function getValidator()
{
return new $this->validator;
}
protected function buildQueryConfig(Request $request, string $scene = 'default'): array
{
$p = $request->all();
$config = [];
$config['model'] = $this->modelClass;
switch ($scene) {
case 'index':
$config['order']['id'] = 'desc';
break;
case 'show':
$config['where']['id'] = $p['id'];
break;
case 'store':
break;
case 'update':
$config['where']['id'] = $p['id'];
break;
case 'destroy':
$config['where']['id'] = $p['id'];
break;
}
return $config;
}
public function serviceDestroy(Request $request)
{
$config = $this->buildQueryConfig($request, 'destroy');
$deleted = (new MessageNotificationsLogic())->logicDeleteBy($config);
return $this->postProcess('Destroy', $deleted);
}
/**
* 添加前置处理
*/
public function servicePush(Request $request, $data)
{
$pushData = [
'sender_id' => $request->user->uid,
'sender_type' => 'admin',
'title' => $data['title'],
'content' => $data['content'],
'scene' => 1,
'recipient' => $data['recipient'],
'push_method' => 1
];
$msgId = (new MessageNotificationsService())->pushNotice($pushData);
return ['msg_id' => $msgId];
}
public function afterServiceList($data)
{
$data->each(function (&$item) {
$temp = explode(',', $item->recipient);
$item->recipient_list = User::whereIn('id', $temp)->pluck('name')->toArray();
$item->scene_name = MessageNotifications::SCENE[$item->scene];
$item->push_method_name = MessageNotifications::PUSH_METHOD[$item->push_method];
});
unset($item);
return $data;
}
public function afterServiceShow($data)
{
$temp = explode(',', $data->recipient);
$data->recipient_list = User::whereIn('id', $temp)->pluck('name')->toArray();
$data->scene_name = MessageNotifications::SCENE[$data->scene];
$data->push_method_name = MessageNotifications::PUSH_METHOD[$data->push_method];
return $data;
}
/**
* 创建一条消息
*/
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'],
'push_method' => $data['push_method'],
'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)->where('status', 0);
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();
}
/**
* 批量推送消息
*/
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;
}
}