修复登录及验证码功能

This commit is contained in:
2026-09-03 10:12:30 +08:00
parent 57d9866dab
commit 7c38e84e30
13 changed files with 309 additions and 118 deletions
+15 -7
View File
@@ -24,26 +24,34 @@ type Config struct {
DB int
}
// Init 初始化 Redis 连接,失败则降级为内存模式
// Init 初始化 Redis 连接,未开启或连接失败则降级为内存模式
func Init(cfg Config) {
if cfg.Host == "" {
log.Printf("[redis] Redis 未配置,已降级为内存模式运行")
Enabled = false
Client = nil
return
}
Client = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
Password: cfg.Password,
DB: cfg.DB,
Addr: fmt.Sprintf("%s:%s", cfg.Host, cfg.Port),
Password: cfg.Password,
DB: cfg.DB,
DialTimeout: 2 * time.Second,
})
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
if err := Client.Ping(ctx).Err(); err != nil {
log.Printf("[redis] connection failed (%v), fallback to memory mode", err)
log.Printf("[redis] Redis 没开或连接失败 (%v),已降级为内存模式运行", err)
Enabled = false
Client = nil
return
}
Enabled = true
log.Printf("[redis] connected to %s:%s (db=%d)", cfg.Host, cfg.Port, cfg.DB)
log.Printf("[redis] Redis 连接成功: %s:%s (db=%d)", cfg.Host, cfg.Port, cfg.DB)
}
// Set 设置键值(带过期时间,0=不过期)