fix(auth): 修复用户注册和登录后缓存清理问题
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m43s

在用户注册、登录、修改订阅和删除订阅等操作后,添加清理服务器缓存的逻辑
同时修复电话重置密码验证码解析问题
This commit is contained in:
shanshanzhong 2025-11-01 09:31:38 -07:00
parent 49d3fc1c74
commit 01eab942fd
8 changed files with 82 additions and 5 deletions

View File

@ -48,5 +48,9 @@ func (l *DeleteUserSubscribeLogic) DeleteUserSubscribe(req *types.DeleteUserSubs
l.Errorw("failed to clear subscribe cache", logger.Field("error", err.Error()), logger.Field("subscribeId", userSubscribe.SubscribeId))
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "failed to clear subscribe cache: %v", err.Error())
}
if err = l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); err != nil {
l.Errorf("ClearServerAllCache error: %v", err.Error())
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "failed to clear server cache: %v", err.Error())
}
return nil
}

View File

@ -69,5 +69,10 @@ func (l *UpdateUserSubscribeLogic) UpdateUserSubscribe(req *types.UpdateUserSubs
l.Errorw("failed to clear subscribe cache", logger.Field("error", err.Error()), logger.Field("subscribeId", userSub.SubscribeId))
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "failed to clear subscribe cache: %v", err.Error())
}
if err = l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); err != nil {
l.Errorf("ClearServerAllCache error: %v", err.Error())
return errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "failed to clear server cache: %v", err.Error())
}
return nil
}

View File

@ -303,6 +303,9 @@ func (l *DeviceLoginLogic) activeTrial(userId int64, db *gorm.DB) error {
logger.Field("expire_time", expireTime),
logger.Field("traffic", sub.Traffic),
)
if clearErr := l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); clearErr != nil {
l.Errorf("ClearServerAllCache error: %v", clearErr.Error())
}
return nil
}

View File

@ -2,6 +2,7 @@ package auth
import (
"context"
"encoding/json"
"fmt"
"time"
@ -43,19 +44,32 @@ func (l *TelephoneResetPasswordLogic) TelephoneResetPassword(req *types.Telephon
return nil, errors.Wrapf(xerr.NewErrCode(xerr.TelephoneError), "Invalid phone number")
}
if l.svcCtx.Config.Mobile.Enable {
if !l.svcCtx.Config.Mobile.Enable {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SmsNotEnabled), "sms login is not enabled")
}
// if the email verification is enabled, the verification code is required
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeTelephoneCacheKey, constant.Security, phoneNumber)
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeTelephoneCacheKey, constant.ParseVerifyType(uint8(constant.Security)), phoneNumber)
l.Logger.Infof("TelephoneResetPassword cacheKey: %s, code: %s", cacheKey, code)
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
if err != nil {
l.Errorw("Redis Error", logger.Field("error", err.Error()), logger.Field("cacheKey", cacheKey))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
}
l.Logger.Infof("TelephoneResetPassword cacheKey: %s, code: %s,value : %s", cacheKey, code, value)
if value == "" {
l.Errorf("TelephoneResetPassword value empty: %s", value)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
}
if value != code {
var payload CacheKeyPayload
if err := json.Unmarshal([]byte(value), &payload); err != nil {
l.Errorf("TelephoneResetPassword Unmarshal Error: %s", err.Error())
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
}
if payload.Code != code {
l.Errorf("TelephoneResetPassword code: %s, code: %s", code, payload.Code)
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
}

View File

@ -245,5 +245,12 @@ func (l *TelephoneUserRegisterLogic) activeTrial(uid int64) error {
UUID: uuidx.NewUUID().String(),
Status: 1,
}
return l.svcCtx.UserModel.InsertSubscribe(l.ctx, userSub)
err = l.svcCtx.UserModel.InsertSubscribe(l.ctx, userSub)
if err != nil {
return err
}
if clearErr := l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); clearErr != nil {
l.Errorf("ClearServerAllCache error: %v", clearErr.Error())
}
return err
}

View File

@ -231,5 +231,12 @@ func (l *UserRegisterLogic) activeTrial(uid int64) error {
UUID: uuidx.NewUUID().String(),
Status: 1,
}
return l.svcCtx.UserModel.InsertSubscribe(l.ctx, userSub)
err = l.svcCtx.UserModel.InsertSubscribe(l.ctx, userSub)
if err != nil {
return err
}
if clearErr := l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); clearErr != nil {
l.Errorf("ClearServerAllCache error: %v", clearErr.Error())
}
return err
}

View File

@ -14,6 +14,7 @@ type customServerLogicModel interface {
FilterNodeList(ctx context.Context, params *FilterNodeParams) (int64, []*Node, error)
ClearNodeCache(ctx context.Context, params *FilterNodeParams) error
CountNodesByIdsAndTags(ctx context.Context, nodeIds []int64, tags []string) (int64, error)
ClearServerAllCache(ctx context.Context) error
}
const (
@ -172,6 +173,30 @@ func (m *customServerModel) ClearServerCache(ctx context.Context, serverId int64
return nil
}
func (m *customServerModel) ClearServerAllCache(ctx context.Context) error {
var cursor uint64
var keys []string
prefix := ServerConfigCacheKey + "*"
for {
scanKeys, newCursor, err := m.Cache.Scan(ctx, cursor, prefix, 999).Result()
if err != nil {
m.Logger.Error(ctx, fmt.Sprintf("ClearServerAllCache err:%v", err))
break
}
m.Logger.Info(ctx, fmt.Sprintf("ClearServerAllCache query keys:%v", scanKeys))
keys = append(keys, scanKeys...)
cursor = newCursor
if cursor == 0 {
break
}
}
if len(keys) > 0 {
m.Logger.Info(ctx, fmt.Sprintf("ClearServerAllCache keys:%v", keys))
return m.Cache.Del(ctx, keys...).Err()
}
return nil
}
// InSet 支持多值 OR 查询
func InSet(field string, values []string) func(db *gorm.DB) *gorm.DB {
return func(db *gorm.DB) *gorm.DB {

View File

@ -55,6 +55,15 @@ var (
// GetRegionByIp queries the geolocation of an IP address using supported services.
func GetRegionByIp(ip string) (*GeoLocationResponse, error) {
// 如果是域名,先解析成 IP
if net.ParseIP(ip) == nil {
ips, err := GetIP(ip)
if err != nil || len(ips) == 0 {
return nil, errors.Wrap(err, "无法解析域名为IP")
}
ip = ips[0] // 取第一个解析到的IP
}
for service, enabled := range queryUrls {
if enabled {
response, err := fetchGeolocation(service, ip)
@ -62,6 +71,9 @@ func GetRegionByIp(ip string) (*GeoLocationResponse, error) {
zap.S().Errorf("Failed to fetch geolocation from %s: %v", service, err)
continue
}
if response.Country == "" {
continue
}
return response, nil
}
}