This commit is contained in:
oneao committed 2026-04-20 17:20:47 +08:00
1 parent 89aaef1d6b
commit 6d54a9e407
192 files changed
+971 -51708

No files matched your search

@@ -1,47 +0,0 @@
package middleware
import (
"allapp-go/internal/httpx"
"allapp-go/pkg/jwtx"
"strings"
"github.com/gofiber/fiber/v3"
)
const CtxUserIDKey = "user_id"
// Auth 登录校验中间件
func Auth() fiber.Handler {
return func(c fiber.Ctx) error {
token := strings.TrimSpace(c.Get("Token"))
if token == "" {
return httpx.Unauthorized(c, "未登录")
}
verifyToken, ok := jwtx.VerifyToken(c.Context(), token)
if !ok {
return httpx.Unauthorized(c, "登录已过期")
}
c.Locals(CtxUserIDKey, verifyToken.Data["user_id"])
return c.Next()
}
}
func GetUserID(c fiber.Ctx) (int64, error) {
v := c.Locals(CtxUserIDKey)
if v == nil {
return 0, httpx.Unauthorized(c, "登录已过期")
}
userID, ok := v.(int64)
if !ok {
return 0, httpx.Unauthorized(c, "登录已过期")
}
return userID, nil
}
@@ -1,35 +0,0 @@
package middleware
import (
"allapp-go/pkg/logger"
"time"
"github.com/gofiber/fiber/v3"
"go.uber.org/zap"
)
func Logger() fiber.Handler {
return func(c fiber.Ctx) error {
traceID := c.RequestID()
logger.SetTraceID(c, traceID)
start := time.Now()
err := c.Next()
// ✅ 计算耗时
cost := time.Since(start)
log := logger.FromCtx(c.Context())
log.Info("HTTP 请求",
zap.String("method", c.Method()),
zap.String("path", c.Path()),
zap.Int("status", c.Response().StatusCode()),
zap.String("ip", c.IP()),
zap.String("duration", cost.String()),
)
return err
}
}