You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
aquaculture/app/Http/Controllers/User/NotificationController.php

185 lines
5.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Http\Controllers\User; // 定义命名空间
use App\Http\Controllers\Controller; // 引入基础控制器类
use App\Models\User; // 引入用户模型
use App\Notifications\CouponCodeNotification; // 引入优惠券通知类(未使用)
use App\Services\NotificationServe; // 引入通知服务类
use Carbon\Carbon; // 引入 Carbon 日期处理库
use Illuminate\Http\Request; // 引入请求类
use Illuminate\Notifications\DatabaseNotification; // 引入数据库通知模型
/**
* 用户通知控制器
*
* 该控制器处理用户通知的展示、阅读和管理功能。
*/
class NotificationController extends Controller
{
/**
* 显示用户的通知列表
*
* @param Request $request
* @return \Illuminate\View\View
*/
public function index(Request $request)
{
/**
* @var $user User
*/
$user = auth()->user(); // 获取当前用户
// 根据请求的 tab 参数确定查询的通知类型
switch ($request->input('tab', 1)) {
case 2:
$query = $user->notifications(); // 所有通知
break;
case 3:
$query = $user->readNotifications(); // 已读通知
break;
case 1:
default:
$query = $user->unreadNotifications(); // 未读通知
break;
}
// 分页获取通知
$notifications = $query->paginate();
// 为每个通知设置标题
foreach ($notifications as $notification) {
$notification->title = NotificationServe::getTitle($notification);
}
// 获取未读和已读通知的数量
$unreadCount = $user->unreadNotifications()->count();
$readCount = $user->readNotifications()->count();
// 返回通知列表视图
return view('user.notifications.index', compact('notifications', 'unreadCount', 'readCount'));
}
/**
* 标记指定通知为已读
*
* @param int $id 通知的 ID
* @return \Illuminate\Http\JsonResponse
*/
public function read($id)
{
/**
* @var $user User
*/
$user = auth()->user(); // 获取当前用户
/**
* @var $notification DatabaseNotification
*/
$notification = $user->notifications()->find($id); // 查找指定通知
if (is_null($notification)) {
return responseJsonAsBadRequest('无效的通知'); // 返回无效通知的错误
}
$notification->markAsRead(); // 标记通知为已读
return responseJson(); // 返回成功的 JSON 响应
}
/**
* 标记所有未读通知为已读
*
* @return \Illuminate\Http\JsonResponse
*/
public function readAll()
{
/**
* @var $user User
*/
$user = auth()->user(); // 获取当前用户
// 更新所有未读通知为已读
$count = $user->unreadNotifications()->update(['read_at' => Carbon::now()]);
// 返回已读通知数量的 JSON 响应
return responseJson(200, "本次已读{$count}条消息");
}
/**
* 显示指定通知的详细信息
*
* @param int $id 通知的 ID
* @return \Illuminate\View\View|\Illuminate\Http\Response
*/
public function show($id)
{
/**
* @var $user User
*/
$user = auth()->user(); // 获取当前用户
/**
* @var $notification DatabaseNotification
*/
$notification = $user->notifications()->find($id); // 查找指定通知
if (is_null($notification)) {
return abort(403, '无效的通知'); // 返回403错误
}
// 查找上一条和下一条通知
$last = $user->notifications()->where('created_at', '<', $notification->created_at)->first();
$next = $user->notifications()->where('created_at', '>', $notification->created_at)->first();
// 标记通知为已读
$notification->markAsRead();
// 获取通知的视图
$view = NotificationServe::getView($notification);
if (! view()->exists($view)) {
abort(404, '未知的的消息'); // 如果视图不存在返回404错误
}
// 设置通知的标题
$notification->title = NotificationServe::getTitle($notification);
$data = $notification->data; // 获取通知的数据
// 返回通知详细信息的视图
return view('user.notifications.show', compact('last', 'next', 'notification', 'view', 'data'));
}
/**
* 获取未读通知的数量
*
* @return \Illuminate\Http\JsonResponse
*/
public function getUnreadCount()
{
/**
* @var $user User
*/
$user = auth()->user(); // 获取当前用户
/**
* @var $notification DatabaseNotification
*/
$count = $user->unreadNotifications()->count(); // 获取未读通知的数量
// 初始化通知的标题、内容和 ID
$title = '';
$content = '';
$id = null;
if ($count > 0) {
$notification = $user->unreadNotifications()->first(); // 获取第一条未读通知
// 前端弹窗内容和标题相反显示,所以变量名会有点怪
$id = $notification->id; // 获取通知 ID
$title = NotificationServe::getContent($notification); // 获取通知内容
$content = NotificationServe::getTitle($notification); // 获取通知标题
}
// 返回未读通知数量的 JSON 响应
return responseJson(200, 'success', compact('count', 'title', 'content', 'id'));
}
}