From a62039e6bb3c7b6d29ffa50dd52fcb7aa20ff5e6 Mon Sep 17 00:00:00 2001 From: Victor_Jay Date: Sun, 31 May 2026 01:23:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E7=99=BB=E5=BD=95/=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=20redirect=20=E5=8F=82=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E6=9C=AA=E7=99=BB=E5=BD=95=E8=B7=B3=E8=BD=AC=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=90=BA=E5=B8=A6=E6=9D=A5=E6=BA=90=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - login.js/register.js 登录成功后优先跳转 redirect 参数指定页面 - 新增 common.RedirectToLogin 辅助函数,自动携带当前路径 - 替换所有 9 处硬编码 /auth/login 重定向 --- internal/common/helper.go | 13 +++++++++++++ internal/controller/message_page_controller.go | 3 +-- internal/controller/settings_controller.go | 3 +-- internal/controller/space_controller.go | 2 +- internal/controller/studio_controller.go | 10 +++++----- internal/middleware/maintenance.go | 3 +-- templates/MetaLab-2026/static/js/login.js | 11 +++++++++-- templates/MetaLab-2026/static/js/register.js | 9 ++++++++- 8 files changed, 39 insertions(+), 15 deletions(-) diff --git a/internal/common/helper.go b/internal/common/helper.go index 797983c..bacf134 100644 --- a/internal/common/helper.go +++ b/internal/common/helper.go @@ -1,6 +1,9 @@ package common import ( + "fmt" + "net/http" + "net/url" "regexp" "metazone.cc/metalab/internal/model" @@ -74,6 +77,16 @@ func BuildAdminPageData(c *gin.Context, extra gin.H) gin.H { return data } +// RedirectToLogin 重定向到登录页,携带当前页面路径作为 redirect 参数 +func RedirectToLogin(c *gin.Context) { + returnURL := url.QueryEscape(c.Request.URL.Path) + if c.Request.URL.RawQuery != "" { + returnURL = url.QueryEscape(c.Request.URL.Path + "?" + c.Request.URL.RawQuery) + } + c.Redirect(http.StatusFound, fmt.Sprintf("/auth/login?redirect=%s", returnURL)) + c.Abort() +} + // injectSiteInfo 从 context 注入站点展示信息 func injectSiteInfo(c *gin.Context, data gin.H) { if framework, exists := c.Get("site_framework"); exists { diff --git a/internal/controller/message_page_controller.go b/internal/controller/message_page_controller.go index b214285..b6f9c8b 100644 --- a/internal/controller/message_page_controller.go +++ b/internal/controller/message_page_controller.go @@ -14,8 +14,7 @@ import ( func (mc *MessageController) MessagesPage(c *gin.Context) { uidVal, exists := c.Get("uid") if !exists { - c.Redirect(http.StatusFound, "/auth/login") - c.Abort() + common.RedirectToLogin(c) return } uid := uidVal.(uint) diff --git a/internal/controller/settings_controller.go b/internal/controller/settings_controller.go index 70d02ec..b7d50ee 100644 --- a/internal/controller/settings_controller.go +++ b/internal/controller/settings_controller.go @@ -58,8 +58,7 @@ func NewSettingsController(authService profileProvider, avatarService avatarProv func (sc *SettingsController) SettingsPage(c *gin.Context) { uidVal, exists := c.Get("uid") if !exists { - c.Redirect(http.StatusFound, "/auth/login") - c.Abort() + common.RedirectToLogin(c) return } uid := uidVal.(uint) diff --git a/internal/controller/space_controller.go b/internal/controller/space_controller.go index 79014a1..03cec3e 100644 --- a/internal/controller/space_controller.go +++ b/internal/controller/space_controller.go @@ -23,7 +23,7 @@ func NewSpaceController(spaceService spaceUseCase) *SpaceController { func (ctrl *SpaceController) MySpace(c *gin.Context) { uid, _, ok := common.GetGinUser(c) if !ok { - c.Redirect(http.StatusFound, "/auth/login") + common.RedirectToLogin(c) return } c.Redirect(http.StatusFound, "/space/"+strconv.FormatUint(uint64(uid), 10)) diff --git a/internal/controller/studio_controller.go b/internal/controller/studio_controller.go index ea61354..4ffe6da 100644 --- a/internal/controller/studio_controller.go +++ b/internal/controller/studio_controller.go @@ -28,7 +28,7 @@ func NewStudioController(ps studioUseCase) *StudioController { func (ctrl *StudioController) OverviewPage(c *gin.Context) { uid, _, ok := common.GetGinUser(c) if !ok { - c.Redirect(http.StatusFound, "/auth/login") + common.RedirectToLogin(c) return } @@ -56,7 +56,7 @@ func (ctrl *StudioController) OverviewPage(c *gin.Context) { func (ctrl *StudioController) PostsPage(c *gin.Context) { uid, _, ok := common.GetGinUser(c) if !ok { - c.Redirect(http.StatusFound, "/auth/login") + common.RedirectToLogin(c) return } @@ -96,7 +96,7 @@ func (ctrl *StudioController) PostsPage(c *gin.Context) { func (ctrl *StudioController) DraftsPage(c *gin.Context) { uid, _, ok := common.GetGinUser(c) if !ok { - c.Redirect(http.StatusFound, "/auth/login") + common.RedirectToLogin(c) return } @@ -131,7 +131,7 @@ func (ctrl *StudioController) DraftsPage(c *gin.Context) { func (ctrl *StudioController) WritePage(c *gin.Context) { uid, _, ok := common.GetGinUser(c) if !ok { - c.Redirect(http.StatusFound, "/auth/login") + common.RedirectToLogin(c) return } @@ -171,7 +171,7 @@ func (ctrl *StudioController) WritePage(c *gin.Context) { func (ctrl *StudioController) AnalyticsPage(c *gin.Context) { uid, _, ok := common.GetGinUser(c) if !ok { - c.Redirect(http.StatusFound, "/auth/login") + common.RedirectToLogin(c) return } diff --git a/internal/middleware/maintenance.go b/internal/middleware/maintenance.go index 77cd68b..07a45da 100644 --- a/internal/middleware/maintenance.go +++ b/internal/middleware/maintenance.go @@ -86,6 +86,5 @@ func blockAccess(c *gin.Context, path string) { }) return } - c.Redirect(http.StatusFound, "/auth/login") - c.Abort() + common.RedirectToLogin(c) } diff --git a/templates/MetaLab-2026/static/js/login.js b/templates/MetaLab-2026/static/js/login.js index 3976053..127dcd5 100644 --- a/templates/MetaLab-2026/static/js/login.js +++ b/templates/MetaLab-2026/static/js/login.js @@ -15,6 +15,13 @@ if (!loginForm) return; + // 读取 redirect 参数(登录成功后跳转目标) + function getRedirect() { + var m = window.location.search.match(/[?&]redirect=([^&]+)/); + if (m) return decodeURIComponent(m[1]); + return '/'; + } + function checkFormValidity() { var email = emailInput.value.trim(); var password = passwordInput.value; @@ -81,7 +88,7 @@ } else if (xhr.status === 200 && resp && resp.success) { window.MetaLab.toast(toast, '登录成功', false); setTimeout(function () { - window.location.href = '/'; + window.location.href = getRedirect(); }, 800); } else { loginInProgress = false; @@ -112,7 +119,7 @@ if (xhr.status === 200 && resp && resp.success) { window.MetaLab.toast(toast, resp.message || '账号已恢复,欢迎回来', false); setTimeout(function () { - window.location.href = '/'; + window.location.href = getRedirect(); }, 800); } else { loginInProgress = false; diff --git a/templates/MetaLab-2026/static/js/register.js b/templates/MetaLab-2026/static/js/register.js index 91da7a5..a4e27ac 100644 --- a/templates/MetaLab-2026/static/js/register.js +++ b/templates/MetaLab-2026/static/js/register.js @@ -32,6 +32,13 @@ // Guard: exit if not on auth page if (!registerForm) return; + // 读取 redirect 参数(注册成功后跳转目标) + function getRedirect() { + var m = window.location.search.match(/[?&]redirect=([^&]+)/); + if (m) return decodeURIComponent(m[1]); + return '/'; + } + var countdown = 60; var timerInterval = null; var reachedBottom = false; @@ -287,7 +294,7 @@ // Token 已通过 HttpOnly Cookie 自动设置,JS 无需处理 // 注册即登录,跳转首页 setTimeout(function () { - window.location.href = '/'; + window.location.href = getRedirect(); }, 1500); } else { var msg = (resp && resp.message) ? resp.message : '注册失败,请稍后重试';