This commit is contained in:
2026-01-12 13:20:17 +08:00
parent ead5f17b7c
commit 68a9e32131
5 changed files with 86 additions and 64 deletions

View File

@@ -226,10 +226,30 @@ func (s *ApiApplicationServiceImpl) validateApiCall(ctx context.Context, cmd *co
// 4. 验证IP白名单非开发环境
if !s.config.App.IsDevelopment() && !cmd.Options.IsDebug {
// 添加调试日志
s.logger.Info("开始验证白名单",
zap.String("userId", apiUser.UserId),
zap.String("clientIP", cmd.ClientIP),
zap.Bool("isDevelopment", s.config.App.IsDevelopment()),
zap.Bool("isDebug", cmd.Options.IsDebug),
zap.Int("whiteListCount", len(apiUser.WhiteList)))
// 输出白名单详细信息(用于调试)
for idx, item := range apiUser.WhiteList {
s.logger.Info("白名单项",
zap.Int("index", idx),
zap.String("ipAddress", item.IPAddress),
zap.String("remark", item.Remark))
}
if !apiUser.IsWhiteListed(cmd.ClientIP) {
s.logger.Error("IP不在白名单内", zap.String("userId", apiUser.UserId), zap.String("ip", cmd.ClientIP))
s.logger.Error("IP不在白名单内",
zap.String("userId", apiUser.UserId),
zap.String("ip", cmd.ClientIP),
zap.Int("whiteListSize", len(apiUser.WhiteList)))
return nil, ErrInvalidIP
}
s.logger.Info("白名单验证通过", zap.String("ip", cmd.ClientIP))
}
// 5. 验证钱包状态

View File

@@ -20,60 +20,60 @@ func SetupGormCache(db *gorm.DB, cacheService interfaces.CacheService, cfg *conf
// 以下是原有的缓存配置代码,已注释掉
/*
// 创建缓存配置
cacheConfig := cache.CacheConfig{
DefaultTTL: 30 * time.Minute,
TablePrefix: "gorm_cache",
MaxCacheSize: 1000,
CacheComplexSQL: false,
EnableStats: true,
EnableWarmup: true,
PenetrationGuard: true,
BloomFilter: false,
AutoInvalidate: true,
InvalidateDelay: 100 * time.Millisecond,
// 创建缓存配置
cacheConfig := cache.CacheConfig{
DefaultTTL: 30 * time.Minute,
TablePrefix: "gorm_cache",
MaxCacheSize: 1000,
CacheComplexSQL: false,
EnableStats: true,
EnableWarmup: true,
PenetrationGuard: true,
BloomFilter: false,
AutoInvalidate: true,
InvalidateDelay: 100 * time.Millisecond,
// 配置启用缓存的表
EnabledTables: []string{
// "product",
// "product_category",
// "enterprise_info_submit_records",
// "sms_codes",
// "wallets",
// "subscription",
// "product_category",
// "product_documentation",
// "enterprise_infos",
// "api_users",
// 添加更多需要缓存的表
},
// 配置启用缓存的表
EnabledTables: []string{
// "product",
// "product_category",
// "enterprise_info_submit_records",
// "sms_codes",
// "wallets",
// "subscription",
// "product_category",
// "product_documentation",
// "enterprise_infos",
// "api_users",
// 添加更多需要缓存的表
},
// 配置禁用缓存的表(日志表等)
DisabledTables: []string{
"audit_logs", // 审计日志
"system_logs", // 系统日志
"operation_logs", // 操作日志
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 配置禁用缓存的表(日志表等)
DisabledTables: []string{
"audit_logs", // 审计日志
"system_logs", // 系统日志
"operation_logs", // 操作日志
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
// 创建缓存插件
cachePlugin := cache.NewGormCachePlugin(cacheService, logger, cacheConfig)
// 创建缓存插件
cachePlugin := cache.NewGormCachePlugin(cacheService, logger, cacheConfig)
// 注册插件到GORM
if err := db.Use(cachePlugin); err != nil {
logger.Error("注册GORM缓存插件失败", zap.Error(err))
return err
}
// 注册插件到GORM
if err := db.Use(cachePlugin); err != nil {
logger.Error("注册GORM缓存插件失败", zap.Error(err))
return err
}
logger.Info("GORM缓存插件已成功注册",
zap.Duration("default_ttl", cacheConfig.DefaultTTL),
zap.Strings("enabled_tables", cacheConfig.EnabledTables),
zap.Strings("disabled_tables", cacheConfig.DisabledTables),
)
logger.Info("GORM缓存插件已成功注册",
zap.Duration("default_ttl", cacheConfig.DefaultTTL),
zap.Strings("enabled_tables", cacheConfig.EnabledTables),
zap.Strings("disabled_tables", cacheConfig.DisabledTables),
)
*/
// return nil
@@ -103,13 +103,13 @@ func GetCacheConfig(cfg *config.Config) cache.CacheConfig {
},
DisabledTables: []string{
"api_calls", // API调用日志表变化频繁不适合缓存
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
return cacheConfig
}
@@ -134,13 +134,13 @@ func GetCacheConfig(cfg *config.Config) cache.CacheConfig {
},
DisabledTables: []string{
"api_calls", // API调用日志表变化频繁不适合缓存
"api_calls", // API调用日志表变化频繁不适合缓存
},
}
// 初始化全局缓存配置管理器
cache.InitCacheConfigManager(cacheConfig)
return cacheConfig
}

View File

@@ -15,7 +15,7 @@ func convertTianYanChaError(err error) error {
// 检查天眼查服务的错误类型,转换为处理器层的标准错误
if errors.Is(err, tianyancha.ErrNotFound) {
return errors.Join(processors.ErrNotFound, err)
return nil
}
if errors.Is(err, tianyancha.ErrDatasource) {
return errors.Join(processors.ErrDatasource, err)

View File

@@ -166,6 +166,7 @@ func (t *TianYanChaService) CallAPI(ctx context.Context, apiCode string, params
Message: tianYanChaResp.Reason,
Data: tianYanChaResp.Result,
}, nil
}
// 成功情况

View File

@@ -10,10 +10,10 @@ import (
// UserRoutes 用户路由注册器
type UserRoutes struct {
handler *handlers.UserHandler
authMiddleware *middleware.JWTAuthMiddleware
handler *handlers.UserHandler
authMiddleware *middleware.JWTAuthMiddleware
adminAuthMiddleware *middleware.AdminAuthMiddleware
logger *zap.Logger
logger *zap.Logger
}
// NewUserRoutes 创建用户路由注册器
@@ -24,10 +24,10 @@ func NewUserRoutes(
logger *zap.Logger,
) *UserRoutes {
return &UserRoutes{
handler: handler,
authMiddleware: authMiddleware,
handler: handler,
authMiddleware: authMiddleware,
adminAuthMiddleware: adminAuthMiddleware,
logger: logger,
logger: logger,
}
}
@@ -56,11 +56,12 @@ func (r *UserRoutes) Register(router *sharedhttp.GinRouter) {
adminGroup := usersGroup.Group("/admin")
adminGroup.Use(r.adminAuthMiddleware.Handle())
{
adminGroup.GET("/list", r.handler.ListUsers) // 管理员查看用户列表
adminGroup.GET("/list", r.handler.ListUsers) // 管理员查看用户列表
adminGroup.GET("/:user_id", r.handler.GetUserDetail) // 管理员获取用户详情
adminGroup.GET("/stats", r.handler.GetUserStats) // 管理员获取用户统计信息
adminGroup.GET("/stats", r.handler.GetUserStats) // 管理员获取用户统计信息
}
}
r.logger.Info("用户路由注册完成")
}