Files
jiang13-bbs/backend/handler/friendlink.go
freefire 4f3f3a265c feat: 友链/书库/移动端导航/排行榜等多模块功能
新增模块:
- 友链(friendlink)后端处理 + 前端管理页与友链板
- 书库(library):导入、章节、封面、阅读页、横竖版自适应 AdaptiveCoverSlot
- 顶栏导航(header_nav)配置与 MobileTabBar/MobileRailDrawers 移动端抽屉
- 排行榜 service 测试、站点页面测试、时间线发布(timeline_release)

其它改动:
- 后端 handlers/services 全量小幅调整
- 前端组件、库函数、URL/品牌/站点 URL 工具更新
- Lightbox 图片、MdEntries 条目卡、coverColor 派生色相等前端能力
2026-09-30 16:30:07 +08:00

209 lines
6.4 KiB
Go
Raw Permalink 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 handler
import (
"errors"
"net/http"
"strconv"
"github.com/freefire/jiang13-bbs/service"
"github.com/gin-gonic/gin"
)
// currentUserID 从 gin 上下文取登录用户 ID;RequireAuth 已保证存在
func currentUserID(c *gin.Context) (uint, bool) {
v, ok := c.Get("user")
if !ok {
return 0, false
}
claims, ok := v.(*service.UserClaims)
if !ok || claims.ID == 0 {
return 0, false
}
return claims.ID, true
}
// GET /api/friend-links 公开列表(仅 approved)
func (h *Handlers) GetFriendLinks(c *gin.Context) {
links, err := h.FriendLink.ListApproved()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取友情链接失败"})
return
}
c.JSON(http.StatusOK, gin.H{"links": links})
}
// POST /api/friend-links/apply 登录用户提交申请
func (h *Handlers) ApplyFriendLink(c *gin.Context) {
uid, ok := currentUserID(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "请先登录"})
return
}
var in service.FriendLinkInput
if err := c.ShouldBindJSON(&in); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
fl, err := h.FriendLink.Apply(in, uid)
if err != nil {
if errors.Is(err, service.ErrFriendLinkInvalid) {
c.JSON(http.StatusBadRequest, gin.H{"error": "友情链接参数无效"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "提交申请失败"})
return
}
c.JSON(http.StatusOK, gin.H{"link": fl})
}
// GET /api/friend-links/mine 当前登录用户自己的申请记录(含待审/拒绝与拒绝理由)
func (h *Handlers) MyFriendLinks(c *gin.Context) {
uid, ok := currentUserID(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "请先登录"})
return
}
links, err := h.FriendLink.ListMine(uid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取申请记录失败"})
return
}
c.JSON(http.StatusOK, gin.H{"links": links})
}
// PUT /api/friend-links/:id 申请人修改自己的申请;已通过/已驳回的修改后回退待审
func (h *Handlers) OwnerUpdateFriendLink(c *gin.Context) {
uid, ok := currentUserID(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "请先登录"})
return
}
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
var in service.FriendLinkInput
if err := c.ShouldBindJSON(&in); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
if err := h.FriendLink.OwnerUpdate(uint(id), uid, in); err != nil {
if errors.Is(err, service.ErrFriendLinkNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "友情链接不存在"})
return
}
if errors.Is(err, service.ErrFriendLinkInvalid) {
c.JSON(http.StatusBadRequest, gin.H{"error": "友情链接参数无效"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "保存修改失败"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}
// GET /api/admin/friend-links 后台全量列表
func (h *Handlers) AdminListFriendLinks(c *gin.Context) {
links, err := h.FriendLink.ListAll()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "获取友情链接失败"})
return
}
c.JSON(http.StatusOK, gin.H{"links": links})
}
// POST /api/admin/friend-links 站长直接新建(status=approved)
func (h *Handlers) AdminCreateFriendLink(c *gin.Context) {
var in service.FriendLinkInput
if err := c.ShouldBindJSON(&in); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
fl, err := h.FriendLink.AdminCreate(in)
if err != nil {
if errors.Is(err, service.ErrFriendLinkInvalid) {
c.JSON(http.StatusBadRequest, gin.H{"error": "友情链接参数无效"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "创建友情链接失败"})
return
}
c.JSON(http.StatusOK, gin.H{"link": fl})
}
// PUT /api/admin/friend-links/:id 全量更新内容(不改 status)
func (h *Handlers) AdminUpdateFriendLink(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
var in service.FriendLinkInput
if err := c.ShouldBindJSON(&in); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
if err := h.FriendLink.AdminUpdate(uint(id), in); err != nil {
if errors.Is(err, service.ErrFriendLinkNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "友情链接不存在"})
return
}
if errors.Is(err, service.ErrFriendLinkInvalid) {
c.JSON(http.StatusBadRequest, gin.H{"error": "友情链接参数无效"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新友情链接失败"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}
// POST /api/admin/friend-links/:id/status 审批:body {status, reject_reason?}
func (h *Handlers) AdminSetFriendLinkStatus(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
var body struct {
Status string `json:"status"`
RejectReason string `json:"reject_reason"`
}
if err := c.ShouldBindJSON(&body); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
if err := h.FriendLink.AdminSetStatus(uint(id), body.Status, body.RejectReason); err != nil {
if errors.Is(err, service.ErrFriendLinkNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "友情链接不存在"})
return
}
if errors.Is(err, service.ErrFriendLinkInvalid) {
c.JSON(http.StatusBadRequest, gin.H{"error": "审批参数无效"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "更新状态失败"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}
// DELETE /api/admin/friend-links/:id 软删除
func (h *Handlers) AdminDeleteFriendLink(c *gin.Context) {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil || id == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "请求参数无效"})
return
}
if err := h.FriendLink.AdminDelete(uint(id)); err != nil {
if errors.Is(err, service.ErrFriendLinkNotFound) {
c.JSON(http.StatusNotFound, gin.H{"error": "友情链接不存在"})
return
}
c.JSON(http.StatusInternalServerError, gin.H{"error": "删除友情链接失败"})
return
}
c.JSON(http.StatusOK, gin.H{"ok": true})
}