From 59846dea8d25379eef94cd35eb2b213e0f620faa Mon Sep 17 00:00:00 2001 From: JACKYMYPERSON Date: Sun, 28 Sep 2025 19:18:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=94=A8=E6=88=B7=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=B6=88=E6=81=AF=E9=80=9A=E7=9F=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../getnotifications/getnotifications.go | 76 +++++++++++++++++++ router/setupRouter.go | 5 ++ 2 files changed, 81 insertions(+) create mode 100644 controllers/notifications/getnotifications/getnotifications.go diff --git a/controllers/notifications/getnotifications/getnotifications.go b/controllers/notifications/getnotifications/getnotifications.go new file mode 100644 index 0000000..6237b1c --- /dev/null +++ b/controllers/notifications/getnotifications/getnotifications.go @@ -0,0 +1,76 @@ +package getnotifications + +import ( + "fmt" + "github.com/gin-gonic/gin" + "net/http" + "strconv" + "time" + "toutoukan/init/databaseInit" +) + +type Notification struct { + // ⭐️ 新增: 主键 id + ID uint `gorm:"primaryKey" json:"id"` // GORM 会自动识别为自增主键 + SenderID string `json:"sender_id"` + ReceiverID string `json:"receiver_id"` + Status int8 `json:"status"` + Sequence string `json:"sequence"` + CreatedAt time.Time `json:"created_at"` + Content string `json:"content"` + IsRead int8 `json:"is_read"` + MsgType string `json:"msg_type"` + Target string `json:"target"` +} + +// TableName 指定 GORM 使用的表名 +func (Notification) TableName() string { + return "user_msg" +} + +type UidRequest struct { + Uid string `json:"uid" binding:"required"` // binding:"required" 确保 uid 字段必须存在 +} + +// --- Gin Handler --- + +// GetNotifications 使用 GORM 进行数据库查询 +func GetNotifications(c *gin.Context) { + // 1. 从请求 Body 中获取 uid + var requestBody UidRequest + if err := c.ShouldBindJSON(&requestBody); err != nil { + // 如果 JSON 格式不正确或缺少 uid 字段,ShouldBindJSON 会返回错误 + c.JSON(http.StatusBadRequest, gin.H{"message": "请求参数错误: " + err.Error()}) + return + } + userID := requestBody.Uid + + // 2. 获取分页参数 (这部分逻辑不变) + page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) + limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20")) + offset := (page - 1) * limit + + // 3. 准备用于接收结果的切片 + var notifications []Notification + + // 4. 使用 GORM 进行链式查询 (这部分逻辑不变) + result := databaseInit.UserDB. + Where("receiver_id = ?", userID). + Order("created_at DESC"). + Limit(limit). + Offset(offset). + Find(¬ifications) + + // 5. GORM 的错误处理 (这部分逻辑不变) + if result.Error != nil { + c.JSON(http.StatusInternalServerError, gin.H{"message": fmt.Sprintf("查询数据库失败: %v", result.Error)}) + return + } + + // 6. 返回 JSON 响应 (这部分逻辑不变) + c.JSON(http.StatusOK, gin.H{ + "data": notifications, + "message": "获取消息列表成功", + "success": true, + }) +} diff --git a/router/setupRouter.go b/router/setupRouter.go index 0384560..ebafbe3 100644 --- a/router/setupRouter.go +++ b/router/setupRouter.go @@ -8,6 +8,7 @@ import ( "toutoukan/controllers/comments/publishComments" "toutoukan/controllers/goods" "toutoukan/controllers/kills" + "toutoukan/controllers/notifications/getnotifications" "toutoukan/controllers/search" "toutoukan/controllers/system" "toutoukan/controllers/user" @@ -55,6 +56,10 @@ func SetupRouter() *gin.Engine { commentGroup.POST("/get", getcomments.GetComments) commentGroup.POST("/publish", publishComments.PublishComment) } + notificationGroup := r.Group("/notification") + { + notificationGroup.POST("/get", getnotifications.GetNotifications) + } return r }