消息相关

This commit is contained in:
fthvgb1 2018-06-11 22:27:53 +08:00
parent b27ddbbe7d
commit 12024e65d8
3 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace App\Http\Controllers\Api;
use App\Transformers\NotificationTransformer;
class NotificationsController extends Controller
{
public function index()
{
$user = $this->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();
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/11
* Time: 22:00
*/
namespace App\Transformers;
use Carbon\Carbon;
use Illuminate\Notifications\DatabaseNotification;
use League\Fractal\TransformerAbstract;
class NotificationTransformer extends TransformerAbstract
{
public function transform(DatabaseNotification $notification)
{
return [
'id' => $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,
];
}
}

View File

@ -87,6 +87,15 @@ $api->version('v1', [
//某个用户的回复列表 //某个用户的回复列表
$api->get('users/{user}/replies', 'RepliesController@userIndex') $api->get('users/{user}/replies', 'RepliesController@userIndex')
->name('api.users.replies.index'); ->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');
}); });
}); });