客户端消息通知
This commit is contained in:
@@ -4,6 +4,7 @@ namespace app\common\controller;
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\common\service\MessageUserStatusService;
|
||||
use support\Request;
|
||||
|
||||
class MessageUserStatusController extends BaseController
|
||||
{
|
||||
@@ -22,12 +23,80 @@ class MessageUserStatusController extends BaseController
|
||||
* @param array $data
|
||||
* @return array
|
||||
*/
|
||||
// protected function indexFormat($data)
|
||||
// {
|
||||
// if (isset($data['created_at']))
|
||||
// $data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
|
||||
// if (isset($data['updated_at']))
|
||||
// $data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
|
||||
// return $data;
|
||||
// }
|
||||
protected function indexFormat($data)
|
||||
{
|
||||
if (isset($data['read_at']) && $data['read_at'] !== null) {
|
||||
$data['read_at'] = date('Y-m-d H:i:s', $data['read_at']);
|
||||
} else {
|
||||
$data['read_at'] = ' - ';
|
||||
}
|
||||
|
||||
if (isset($data['message']))
|
||||
$data['message']['created_at'] = date('Y-m-d H:i:s', $data['message']['created_at']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function showFormat($data)
|
||||
{
|
||||
if (isset($data['read_at']) && $data['read_at'] !== null) {
|
||||
$data['read_at'] = date('Y-m-d H:i:s', $data['read_at']);
|
||||
} else {
|
||||
$data['read_at'] = ' - ';
|
||||
}
|
||||
|
||||
if (isset($data['message']))
|
||||
$data['message']['created_at'] = date('Y-m-d H:i:s', $data['message']['created_at']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
protected function unReadFormat($data)
|
||||
{
|
||||
// if (isset($data['read_at']) && $data['read_at'] !== null) {
|
||||
// $data['read_at'] = date('Y-m-d H:i:s', $data['read_at']);
|
||||
// } else {
|
||||
// $data['read_at'] = ' - ';
|
||||
// }
|
||||
|
||||
// if (isset($data['message'])) {
|
||||
// $data['message']['created_at'] = date('Y-m-d H:i:s', $data['message']['created_at']);
|
||||
// }
|
||||
|
||||
foreach ($data as &$item) {
|
||||
if (isset($item['read_at']) && $item['read_at'] !== null) {
|
||||
$item['read_at'] = date('Y-m-d H:i:s', $item['read_at']);
|
||||
} else {
|
||||
$item['read_at'] = ' - ';
|
||||
}
|
||||
|
||||
if (isset($item['message'])) {
|
||||
$item['message']['created_at'] = date('Y-m-d H:i:s', $item['message']['created_at']);
|
||||
}
|
||||
}
|
||||
var_dump($data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function read(Request $request)
|
||||
{
|
||||
try {
|
||||
$data = $this->validate($this->service->getValidator(), $request->post(), 'read');
|
||||
$this->service->serviceRead($request, $data);
|
||||
return $this->success([], '操作成功');
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function listUnRead(Request $request)
|
||||
{
|
||||
try {
|
||||
$raw = $this->service->serviceListUnRead($request);
|
||||
$data = $this->transformData($raw, method_exists($this, 'unReadFormat')
|
||||
? [$this, 'unReadFormat']
|
||||
: null);
|
||||
return $this->success($data);
|
||||
} catch (\Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -90,4 +90,9 @@ class MessageNotifications extends Model
|
||||
return $this->hasMany(MessageUserStatus::class, 'msg_id', 'id')
|
||||
->where('status', 0);
|
||||
}
|
||||
|
||||
public function sender()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'sender_id', 'id');
|
||||
}
|
||||
}
|
||||
@@ -32,10 +32,10 @@ class MessageUserStatus extends Model
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
public $timestamps = true;
|
||||
public $timestamps = false;
|
||||
|
||||
|
||||
protected $dateFormat = 'U';
|
||||
// protected $dateFormat = 'U';
|
||||
|
||||
/**
|
||||
* 允许自动填充模型字段
|
||||
|
||||
@@ -5,6 +5,8 @@ namespace app\common\service;
|
||||
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
|
||||
{
|
||||
@@ -28,4 +30,91 @@ class MessageUserStatusService
|
||||
{
|
||||
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']['user'] = ['sn', 'name'];
|
||||
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at'];
|
||||
$config['where']['uid'] = $request->user->uid;
|
||||
$config['where']['status'] = 0;
|
||||
break;
|
||||
case 'index':
|
||||
$config['order']['id'] = 'desc';
|
||||
// $config['with']['user'] = ['sn', 'name'];
|
||||
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', 'created_at'];
|
||||
$config['where']['uid'] = $request->user->uid;
|
||||
break;
|
||||
case 'show':
|
||||
$config['where']['id'] = $p['id'];
|
||||
// $config['with']['user'] = ['sn', 'name'];
|
||||
$config['with']['message'] = ['sender_id', 'sender_type', 'msg_no', 'title', 'content', '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 afterServiceList($res)
|
||||
{
|
||||
$res->each(function (&$item) {
|
||||
if (isset($item->message)) {
|
||||
$item->message->sender_name = User::where('id', $item->message->sender_id)->value('name') ?: '系统';
|
||||
}
|
||||
});
|
||||
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') ?: '系统';
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
}
|
||||
+11
-1
@@ -64,6 +64,17 @@ Route::group('/common', function () {
|
||||
Route::any('/cache/clear', [app\common\controller\PresetController::class, 'cacheClear']);
|
||||
Route::any('/menus', [app\common\controller\PresetController::class, 'menus']);
|
||||
});
|
||||
|
||||
|
||||
// 消息通知
|
||||
Route::group('/notice', function () {
|
||||
Route::any('/list', [app\common\controller\MessageUserStatusController::class, 'index']);
|
||||
Route::any('/show', [app\common\controller\MessageUserStatusController::class, 'show']);
|
||||
Route::any('/read', [app\common\controller\MessageUserStatusController::class, 'read']);
|
||||
Route::any('/destroy', [app\common\controller\MessageUserStatusController::class, 'destroy']);
|
||||
|
||||
Route::any('/unread', [app\common\controller\MessageUserStatusController::class, 'listUnRead']);
|
||||
});
|
||||
});
|
||||
|
||||
// 后台管理
|
||||
@@ -73,7 +84,6 @@ Route::group('/admin', function () {
|
||||
Route::any('/list', [app\common\controller\MessageNotificationsController::class, 'index']);
|
||||
Route::any('/show', [app\common\controller\MessageNotificationsController::class, 'show']);
|
||||
Route::any('/push', [app\common\controller\MessageNotificationsController::class, 'push']);
|
||||
// Route::any('/update', [app\common\controller\MessageNotificationsController::class, 'update']);
|
||||
Route::any('/destroy', [app\common\controller\MessageNotificationsController::class, 'destroy']);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user