diff --git a/app/Http/Controllers/Api/NotificationsController.php b/app/Http/Controllers/Api/NotificationsController.php new file mode 100644 index 0000000..ffde699 --- /dev/null +++ b/app/Http/Controllers/Api/NotificationsController.php @@ -0,0 +1,32 @@ +user(); + $notifications = $user->notifications()->paginate(20); + return $this->response->paginator($notifications, new NotificationTransformer()); + } + + /** + * @return mixed + * @throws \ErrorException + */ + public function stats() + { + return $this->response->array([ + 'unread_count' => $this->user()->notification_count, + ]); + } + + public function read() + { + $this->user()->markAsRead(); + return $this->response->noContent(); + } +} diff --git a/app/Transformers/NotificationTransformer.php b/app/Transformers/NotificationTransformer.php new file mode 100644 index 0000000..55415e3 --- /dev/null +++ b/app/Transformers/NotificationTransformer.php @@ -0,0 +1,29 @@ + $notification->id, + 'type' => $notification->type, + 'data' => $notification->data, + 'read_at' => $notification->read_at instanceof Carbon ? $notification->read_at->toDateTimeString() : null, + 'created_at' => $notification->created_at instanceof Carbon ? $notification->created_at->toDateTimeString() : $notification->created_at, + 'updated_at' => $notification->updated_at instanceof Carbon ? $notification->updated_at->toDateTimeString() : $notification->updated_at, + ]; + } +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 860b1bd..3413f66 100644 --- a/routes/api.php +++ b/routes/api.php @@ -87,6 +87,15 @@ $api->version('v1', [ //某个用户的回复列表 $api->get('users/{user}/replies', 'RepliesController@userIndex') ->name('api.users.replies.index'); + // 通知列表 + $api->get('user/notifications', 'NotificationsController@index') + ->name('api.user.notifications.index'); + // 通知统计 + $api->get('user/notifications/stats', 'NotificationsController@stats') + ->name('api.user.notifications.stats'); + // 标记消息通知为已读 + $api->patch('user/read/notifications', 'NotificationsController@read') + ->name('api.user.notifications.read'); }); });