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 : '注册失败,请稍后重试';