From 2ea9788ec71eab58d8b01b563591176dd9ea592b Mon Sep 17 00:00:00 2001 From: Victor_Jay Date: Wed, 3 Jun 2026 13:11:56 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=85=B3=E6=B3=A8=E7=8A=B6=E6=80=81=20A?= =?UTF-8?q?PI=20=E6=B7=BB=E5=8A=A0=20Optional=20=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E4=B8=AD=E9=97=B4=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - /api/users/:uid/follow-status 等公开 GET 接口缺少 Optional 认证 - 导致 GetGinUser 无法获取当前用户 uid,始终返回 i_follow=false - 关注按钮加载时无法显示正确初始状态(始终显示"关注") - 三条 GET 接口统一加入 authMdw.Optional() 组 - 不影响未登录用户(未登录依然返回 false) --- internal/router/api.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/router/api.go b/internal/router/api.go index c07a590..ba30860 100644 --- a/internal/router/api.go +++ b/internal/router/api.go @@ -136,10 +136,14 @@ func setupAPIRoutes(r *gin.Engine, cfg *config.Config, d *dependencies) { } // --- 关注 API --- - // 公开(未登录返回 false) - r.GET("/api/users/:uid/follow-status", d.followCtrl.GetStatus) - r.GET("/api/users/:uid/followers", d.followCtrl.Followers) - r.GET("/api/users/:uid/following", d.followCtrl.Following) + // 公开(Optional 认证以获取当前用户关注状态,未登录返回 false) + followPublic := r.Group("/api/users/:uid") + followPublic.Use(d.authMdw.Optional()) + { + followPublic.GET("/follow-status", d.followCtrl.GetStatus) + followPublic.GET("/followers", d.followCtrl.Followers) + followPublic.GET("/following", d.followCtrl.Following) + } // 需登录:切换关注 followAPI := r.Group("/api/users/:uid") followAPI.Use(d.authMdw.Required())