diff --git a/gateway/handlers/static.go b/gateway/handlers/static.go index 77e0f66..c84e74c 100644 --- a/gateway/handlers/static.go +++ b/gateway/handlers/static.go @@ -4,24 +4,53 @@ import ( "net/http" "os" + "gateway/models" "gateway/utils" + "github.com/gin-contrib/sessions" "github.com/gin-gonic/gin" + "github.com/jinzhu/gorm" ) +// 获取用户信息的辅助函数 +func getUserInfo(c *gin.Context, db *gorm.DB) map[string]interface{} { + session := sessions.Default(c) + userID := session.Get("user") + if userID == nil { + return nil + } + + var user models.User + if err := db.Preload("Region").First(&user, userID).Error; err != nil { + return nil + } + + return map[string]interface{}{ + "user_id": user.ID, + "full_name": user.FullName, + "region": user.Region.Name, + } +} + // 处理首页请求 -func ServeIndex(c *gin.Context) { - utils.LogAccess(c.ClientIP(), c.Request.URL.Path, c.Request.Method) - http.ServeFile(c.Writer, c.Request, "./static/index.html") +func ServeIndex(db *gorm.DB) gin.HandlerFunc { + return func(c *gin.Context) { + userInfo := getUserInfo(c, db) + utils.LogAccess(c.ClientIP(), c.Request.URL.Path, c.Request.Method, userInfo) + http.ServeFile(c.Writer, c.Request, "./static/index.html") + } } // 处理静态文件请求 -func ServeStatic(c *gin.Context) { - utils.LogAccess(c.ClientIP(), c.Request.URL.Path, c.Request.Method) - filePath := "./static" + c.Request.URL.Path - if _, err := os.Stat(filePath); err == nil { - http.ServeFile(c.Writer, c.Request, filePath) - } else { - c.AbortWithStatus(http.StatusNotFound) +func ServeStatic(db *gorm.DB) gin.HandlerFunc { + return func(c *gin.Context) { + userInfo := getUserInfo(c, db) + utils.LogAccess(c.ClientIP(), c.Request.URL.Path, c.Request.Method, userInfo) + filePath := "./static" + c.Request.URL.Path + if _, err := os.Stat(filePath); err == nil { + http.ServeFile(c.Writer, c.Request, filePath) + } else { + c.AbortWithStatus(http.StatusNotFound) + } } } diff --git a/gateway/main.go b/gateway/main.go index d1eb269..3988af2 100644 --- a/gateway/main.go +++ b/gateway/main.go @@ -55,13 +55,13 @@ func main() { r.Use(middleware.AuthRequired()) // 文档页面路由 - r.GET("/", handlers.ServeIndex) + r.GET("/", handlers.ServeIndex(db)) // 在权限校验中间件后添加退出路由 r.GET("/logout", handlers.Logout) // 新增通用静态文件路由(放在其他路由之后) - r.NoRoute(handlers.ServeStatic) + r.NoRoute(handlers.ServeStatic(db)) // 启动服务 r.Run(":7070") diff --git a/gateway/middleware/auth.go b/gateway/middleware/auth.go index 5d58d73..d6bb8ce 100644 --- a/gateway/middleware/auth.go +++ b/gateway/middleware/auth.go @@ -13,9 +13,15 @@ func AuthRequired() gin.HandlerFunc { session := sessions.Default(c) user := session.Get("user") if user == nil { - // 获取当前请求的完整URL + // 如果是登出路径,直接重定向到登录页面 + if c.Request.URL.Path == "/logout" { + c.Redirect(http.StatusFound, "/login") + c.Abort() + return + } + + // 对于其他路径,保留原有的return_url逻辑 returnURL := c.Request.URL.String() - // URL编码处理,避免特殊字符造成问题 encodedURL := url.QueryEscape(returnURL) c.Redirect(http.StatusFound, "/login?return_url="+encodedURL) c.Abort() diff --git a/gateway/utils/logger.go b/gateway/utils/logger.go index 5a31008..7d0b1a9 100644 --- a/gateway/utils/logger.go +++ b/gateway/utils/logger.go @@ -27,11 +27,20 @@ func InitLogger() { Logger.SetOutput(io.MultiWriter(os.Stdout, logFile)) } -func LogAccess(ip, path, method string) { - Logger.WithFields(logrus.Fields{ +func LogAccess(ip, path, method string, userInfo map[string]interface{}) { + fields := logrus.Fields{ "ip": ip, "path": path, "method": method, "timestamp": time.Now().Format(time.RFC3339), - }).Info("Page accessed") + } + + // 合并用户信息到日志字段中 + if userInfo != nil { + for k, v := range userInfo { + fields[k] = v + } + } + + Logger.WithFields(fields).Info("Page accessed") }