136 lines
4.3 KiB
PHP
136 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\service;
|
|
|
|
use app\common\model\MessageNotifications;
|
|
use app\common\validate\MessageUserStatusValidate;
|
|
use app\common\traits\crud\CrudServiceTrait;
|
|
use app\common\model\MessageUserStatus;
|
|
use support\Request;
|
|
use app\common\model\User;
|
|
|
|
class MessageUserStatusService
|
|
{
|
|
|
|
use CrudServiceTrait;
|
|
|
|
/**
|
|
* 指定当前要操作的模型
|
|
*/
|
|
protected string $modelClass = MessageUserStatus::class;
|
|
|
|
/**
|
|
* 验证器
|
|
*/
|
|
protected $validator = MessageUserStatusValidate::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 'unread':
|
|
$config['order']['id'] = 'desc';
|
|
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
|
$config['where']['uid'] = $request->user->uid;
|
|
$config['where']['status'] = 0;
|
|
break;
|
|
case 'index':
|
|
$config['order']['id'] = 'desc';
|
|
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
|
$config['where']['uid'] = $request->user->uid;
|
|
break;
|
|
case 'show':
|
|
$config['where']['id'] = $p['id'];
|
|
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'scene', 'created_at'];
|
|
$config['where']['uid'] = $request->user->uid;
|
|
break;
|
|
case 'store':
|
|
break;
|
|
case 'update':
|
|
case 'read':
|
|
$config['where']['id'] = $p['id'];
|
|
$config['where']['msg_id'] = $p['msg_id'];
|
|
$config['where']['uid'] = $request->user->uid;
|
|
break;
|
|
case 'destroy':
|
|
$config['where']['id'] = $p['id'];
|
|
$config['where']['uid'] = $request->user->uid;
|
|
break;
|
|
}
|
|
|
|
return $config;
|
|
}
|
|
|
|
public function serviceListUnRead(Request $request)
|
|
{
|
|
$config = $this->buildQueryConfig($request, 'unread');
|
|
$result = $this->logicListAll($config);
|
|
return $this->postProcess('ListUnRead', $result);
|
|
}
|
|
|
|
public function serviceRead(Request $request, array $data = [])
|
|
{
|
|
return $this->noticeRead($request->post('msg_ids'), $request->user->uid);
|
|
}
|
|
|
|
public function noticeRead(array|string $msgIds, int $uid): bool
|
|
{
|
|
$model = MessageUserStatus::where('uid', $uid)->where('status', 0);
|
|
if (is_array($msgIds)) {
|
|
$model->whereIn('msg_id', $msgIds);
|
|
}
|
|
$udata['read_at'] = time();
|
|
$udata['status'] = 1;
|
|
return $model->update($udata);
|
|
}
|
|
|
|
|
|
protected function afterServiceIndex($res)
|
|
{
|
|
$res->each(function (&$item) {
|
|
if (isset($item->message)) {
|
|
$item->message->sender_name = User::where('id', $item->message->sender_id)->value('name') ?: '系统';
|
|
$item->message->scene_name = MessageNotifications::SCENE[$item->message->scene];
|
|
}
|
|
});
|
|
unset($item);
|
|
return $res;
|
|
}
|
|
|
|
protected function afterServiceShow($res)
|
|
{
|
|
if (isset($res->message)) {
|
|
$res->message->sender_name = User::where('id', $res->message->sender_id)->value('name') ?: '系统';
|
|
$res->message->scene_name = MessageNotifications::SCENE[$res->message->scene];
|
|
}
|
|
// 标记已读
|
|
if ($res->status == 0) {
|
|
$this->noticeRead([$res->msg_id], $res->uid);
|
|
}
|
|
return $res;
|
|
}
|
|
|
|
protected function afterServiceListUnRead($res)
|
|
{
|
|
$res->each(function (&$item) {
|
|
if (isset($item->message)) {
|
|
$item->message->sender_name = User::where('id', $item->message->sender_id)->value('name') ?: '系统';
|
|
$item->message->scene_name = MessageNotifications::SCENE[$item->message->scene];
|
|
}
|
|
});
|
|
unset($item);
|
|
return $res;
|
|
}
|
|
} |