后端 Go+Gin:认证/CSRF/限流、板块、帖子、评论、点赞、通知、用户资料、置顶推荐;前端 Next.js 16:发帖/编辑/删除、搜索、分页、点赞、通知中心、设置;基础设施 docker-compose 与配置模板;添加 .gitignore 与专有许可证(保留所有权利)
65 lines
1.8 KiB
Go
65 lines
1.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/freefire/jiang13-bbs/middleware"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Notifications 获取当前用户的通知列表(分页)
|
|
func (h *Handlers) Notifications(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
size, _ := strconv.Atoi(c.DefaultQuery("size", "20"))
|
|
|
|
list, total, err := h.Notification.List(claims.ID, page, size)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"notifications": list,
|
|
"total": total,
|
|
"page": page,
|
|
"size": size,
|
|
})
|
|
}
|
|
|
|
// UnreadCount 获取未读通知数
|
|
func (h *Handlers) UnreadCount(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
count, err := h.Notification.UnreadCount(claims.ID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"count": count})
|
|
}
|
|
|
|
// MarkRead 标记单条通知为已读
|
|
func (h *Handlers) MarkRead(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的通知 ID"})
|
|
return
|
|
}
|
|
if err := h.Notification.MarkRead(uint(id), claims.ID); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "已标记为已读"})
|
|
}
|
|
|
|
// MarkAllRead 标记所有通知为已读
|
|
func (h *Handlers) MarkAllRead(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
if err := h.Notification.MarkAllRead(claims.ID); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"message": "已全部标记为已读"})
|
|
}
|