refactor: align framework cleanup with new bootstrap flow

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
2025-11-19 12:47:25 +08:00
parent 39d14ec093
commit d66323487b
67 changed files with 3020 additions and 3992 deletions

View File

@@ -35,7 +35,7 @@ func GetUserTypeFromContext(ctx context.Context) int {
if ctx == nil {
return 0
}
if userType, ok := ctx.Value(constants.ContextKeyUserID).(int); ok {
if userType, ok := ctx.Value(constants.ContextKeyUserType).(int); ok {
return userType
}
return 0
@@ -84,17 +84,18 @@ type AuthConfig struct {
// 验证失败返回 error
TokenValidator func(token string) (userID uint, userType int, shopID uint, err error)
// Skip 跳过认证的路径
Skip []string
// SkipPaths 跳过认证的路径列表
SkipPaths []string
}
// Auth 认证中间件
// 从请求中提取 token验证后将用户信息设置到 context
// 所有错误统一返回 AppError由全局 ErrorHandler 处理
func Auth(config AuthConfig) fiber.Handler {
return func(c *fiber.Ctx) error {
// 检查是否跳过认证
path := c.Path()
for _, skipPath := range config.Skip {
for _, skipPath := range config.SkipPaths {
if path == skipPath {
return c.Next()
}
@@ -110,26 +111,22 @@ func Auth(config AuthConfig) fiber.Handler {
}
if token == "" {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"code": errors.CodeUnauthorized,
"message": "未提供认证令牌",
})
return errors.New(errors.CodeMissingToken, "未提供认证令牌")
}
// 验证 token
if config.TokenValidator == nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"code": errors.CodeInternalError,
"message": "认证验证器未配置",
})
return errors.New(errors.CodeInternalError, "认证验证器未配置")
}
userID, userType, shopID, err := config.TokenValidator(token)
if err != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"code": errors.CodeUnauthorized,
"message": "认证令牌无效",
})
// 如果验证器返回的是 AppError直接返回
if appErr, ok := err.(*errors.AppError); ok {
return appErr
}
// 否则包装为 AppError
return errors.Wrap(errors.CodeInvalidToken, "认证令牌无效", err)
}
// 将用户信息设置到 context