- 新增 internal/sitecode 包:FromRequest 头优先解析、FromQueryThenHeader 探测接口专用、ExtractHost/IsLocalHost 提取 Host 域名 - 替换 auth/order/listing/chat/notification/withdrawal/wallet/distribution 八模块 17 处各自内联的解析逻辑,统一优先级与 TrimSpace 行为 - 修复 notification 漏 TrimSpace、探测接口会话头压过显式 query、 GetPublicSiteInfo 把 localhost Host 当站点域名查库等不一致 - 顺手清理 auth 中无引用的 extractHost 死代码 - gofmt 修复 withdrawal/presenter、chat/file/payment 测试四个历史遗留格式问题
126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package notification
|
|
|
|
import (
|
|
"errors"
|
|
"strconv"
|
|
|
|
"hfb_sys/backend/internal/middleware"
|
|
"hfb_sys/backend/internal/sitecode"
|
|
"hfb_sys/backend/pkg/response"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Handler struct {
|
|
service *Service
|
|
}
|
|
|
|
func NewHandler(service *Service) *Handler {
|
|
return &Handler{service: service}
|
|
}
|
|
|
|
func parsePagination(c *gin.Context) (int, int) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
if pageSize > 100 {
|
|
pageSize = 100
|
|
}
|
|
return page, pageSize
|
|
}
|
|
|
|
func (h *Handler) List(c *gin.Context) {
|
|
userID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
page, pageSize := parsePagination(c)
|
|
siteCode := parseSiteCode(c)
|
|
result, err := h.service.List(c.Request.Context(), userID, page, pageSize, siteCode)
|
|
if err != nil {
|
|
writeNotificationError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|
|
|
|
func (h *Handler) UnreadCount(c *gin.Context) {
|
|
userID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
siteCode := parseSiteCode(c)
|
|
result, err := h.service.UnreadCount(c.Request.Context(), userID, siteCode)
|
|
if err != nil {
|
|
writeNotificationError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, result)
|
|
}
|
|
|
|
func (h *Handler) MarkAllRead(c *gin.Context) {
|
|
userID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
siteCode := parseSiteCode(c)
|
|
count, err := h.service.MarkAllRead(c.Request.Context(), userID, siteCode)
|
|
if err != nil {
|
|
writeNotificationError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"read_count": count})
|
|
}
|
|
|
|
func (h *Handler) MarkRead(c *gin.Context) {
|
|
userID, ok := currentUserID(c)
|
|
if !ok {
|
|
response.Unauthorized(c, "缺少用户上下文")
|
|
return
|
|
}
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
response.BadRequest(c, "ID 不正确")
|
|
return
|
|
}
|
|
siteCode := parseSiteCode(c)
|
|
if err := h.service.MarkRead(c.Request.Context(), userID, id, siteCode); err != nil {
|
|
writeNotificationError(c, err)
|
|
return
|
|
}
|
|
response.OK(c, gin.H{"read": true})
|
|
}
|
|
|
|
func parseSiteCode(c *gin.Context) string {
|
|
return sitecode.FromRequest(c)
|
|
}
|
|
|
|
func currentUserID(c *gin.Context) (uint64, bool) {
|
|
value, ok := c.Get(middleware.ContextUserID)
|
|
if !ok {
|
|
return 0, false
|
|
}
|
|
userID, ok := value.(uint64)
|
|
return userID, ok
|
|
}
|
|
|
|
func writeNotificationError(c *gin.Context, err error) {
|
|
response.RecordError(c, err)
|
|
switch {
|
|
case errors.Is(err, ErrDependencyUnavailable):
|
|
response.ServiceUnavailable(c, "数据库未连接")
|
|
case errors.Is(err, ErrNotificationNotFound):
|
|
response.NotFound(c, "通知不存在")
|
|
default:
|
|
response.ServiceUnavailable(c, "通知服务暂时不可用")
|
|
}
|
|
}
|