This repository has been archived on 2026-06-21. You can view files and clone it, but cannot push or open issues or pull requests.
Files
MetaLab/internal/controller/message_controller.go
Victor_Jay f26da8f06e refactor: 清理死代码 + 修复ISP接口注释
- 移除未使用的DTO类型(ChangePasswordRequest/DeleteAccountRequest)
- 移除service层未引用的错误重导出(auth_service/audit_service)
- 删除未使用的SiteSettingRepo(数据访问由config.SiteSettings直接处理)
- 删除未使用的FindByStatus方法(user_repo)
- 删除未使用的AutoMigrate方法(audit_repo/notification_repo)
- 删除未使用的PaginatedResult/NewPaginatedResult(pagination.go)
- 修复接口注释:notifProvider(5→4)、auditUseCase(4→3)、siteSettingUseCase(3→4)
- 移除auditStatusProvider未使用的FindByUsername方法
- 统一使用common.ErrXxx直接引用,消除跨service错误重导出耦合
2026-05-27 13:52:42 +08:00

130 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controller
import (
"net/http"
"strconv"
"metazone.cc/metalab/internal/common"
"metazone.cc/metalab/internal/model"
"github.com/gin-gonic/gin"
)
// notifProvider MessageController 所需的通知服务接口ISP4 个方法)
type notifProvider interface {
List(userID uint, page, pageSize int) (*model.NotificationListResult, error)
CountUnread(userID uint) (int64, error)
MarkRead(id, userID uint) error
MarkAllRead(userID uint) error
}
// MessageController 消息中心控制器
type MessageController struct {
notifService notifProvider
}
// NewMessageController 构造函数
func NewMessageController(notifService notifProvider) *MessageController {
return &MessageController{notifService: notifService}
}
// MessagesPage 消息中心页面需登录noindex
func (mc *MessageController) MessagesPage(c *gin.Context) {
uidVal, exists := c.Get("uid")
if !exists {
c.Redirect(http.StatusFound, "/auth/login")
c.Abort()
return
}
uid := uidVal.(uint)
// 从 query 取分页参数
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
if page < 1 {
page = 1
}
pageSize := 20
result, err := mc.notifService.List(uid, page, pageSize)
if err != nil {
result = &model.NotificationListResult{Items: []model.Notification{}, Total: 0, Page: 1}
}
totalPages := result.TotalPages
if totalPages == 0 && result.Total > 0 {
totalPages = int((result.Total + int64(pageSize) - 1) / int64(pageSize))
}
c.HTML(http.StatusOK, "messages/index.html", common.BuildPageData(c, gin.H{
"Title": "消息中心",
"ExtraCSS": "/static/css/messages.css",
"Messages": result.Items,
"Total": result.Total,
"Page": result.Page,
"TotalPages": totalPages,
"PrevPage": result.Page - 1,
"NextPage": result.Page + 1,
"HasPrev": result.Page > 1,
"HasNext": result.Page < totalPages,
"UnreadCount": result.Unread,
"NotifyTypeNames": model.NotifyTypeNames,
"AuditTypeNames": model.AuditTypeNames,
}))
}
// UnreadCount 获取未读消息数API供导航栏轮询
func (mc *MessageController) UnreadCount(c *gin.Context) {
uidVal, exists := c.Get("uid")
if !exists {
common.Ok(c, gin.H{"unread": 0})
return
}
count, err := mc.notifService.CountUnread(uidVal.(uint))
if err != nil {
common.Ok(c, gin.H{"unread": 0})
return
}
common.Ok(c, gin.H{"unread": count})
}
// MarkRead 标记单条消息已读API
func (mc *MessageController) MarkRead(c *gin.Context) {
uidVal, exists := c.Get("uid")
if !exists {
common.Error(c, http.StatusUnauthorized, "请先登录")
return
}
idStr := c.Param("id")
id, err := strconv.ParseUint(idStr, 10, 64)
if err != nil {
common.Error(c, http.StatusBadRequest, "消息 ID 无效")
return
}
if err := mc.notifService.MarkRead(uint(id), uidVal.(uint)); err != nil {
common.Error(c, http.StatusInternalServerError, "操作失败")
return
}
common.OkMessage(c, "已标记为已读")
}
// MarkAllRead 一键全部已读API
func (mc *MessageController) MarkAllRead(c *gin.Context) {
uidVal, exists := c.Get("uid")
if !exists {
common.Error(c, http.StatusUnauthorized, "请先登录")
return
}
if err := mc.notifService.MarkAllRead(uidVal.(uint)); err != nil {
common.Error(c, http.StatusInternalServerError, "操作失败")
return
}
common.OkMessage(c, "已全部标为已读")
}