鉴权只认 cookie、SEO 只走 Next、sort 与推荐对齐;删除 Bearer、Go sitemap、post_type 回填等冗余路径。 Co-authored-by: Cursor <cursoragent@cursor.com>
62 lines
1.6 KiB
Go
62 lines
1.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/freefire/jiang13-bbs/middleware"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Boards 获取板块列表
|
|
func (h *Handlers) Boards(c *gin.Context) {
|
|
boards, err := h.Board.List()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"boards": nonNilSlice(boards)})
|
|
}
|
|
|
|
// BoardSidebar 首页选中板块后的右栏聚合数据
|
|
func (h *Handlers) BoardSidebar(c *gin.Context) {
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil || id == 0 {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的板块 ID"})
|
|
return
|
|
}
|
|
data, err := h.Board.Sidebar(uint(id))
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "板块不存在"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
// 热门榜点赞态 + 登录用户签到
|
|
if claims := middleware.CurrentUser(c); claims != nil {
|
|
ids := make([]uint, 0, len(data.Hot))
|
|
for _, p := range data.Hot {
|
|
ids = append(ids, p.ID)
|
|
}
|
|
likedMap := h.Like.BatchHasLiked(ids, claims.ID)
|
|
for i := range data.Hot {
|
|
data.Hot[i].Liked = likedMap[data.Hot[i].ID]
|
|
}
|
|
if status, err := h.Checkin.Status(claims.ID); err == nil {
|
|
data.Checkin = status
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"board": data.Board,
|
|
"moderators": data.Moderators,
|
|
"stats": data.Stats,
|
|
"hot": data.Hot,
|
|
"active_users": data.ActiveUsers,
|
|
"checkin": data.Checkin,
|
|
})
|
|
}
|