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}) }