fix(auth): 修复用户注册和登录后缓存清理问题
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m43s
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 6m43s
在用户注册、登录、修改订阅和删除订阅等操作后,添加清理服务器缓存的逻辑 同时修复电话重置密码验证码解析问题
This commit is contained in:
parent
49d3fc1c74
commit
01eab942fd
@ -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))
|
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())
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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))
|
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())
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -303,6 +303,9 @@ func (l *DeviceLoginLogic) activeTrial(userId int64, db *gorm.DB) error {
|
|||||||
logger.Field("expire_time", expireTime),
|
logger.Field("expire_time", expireTime),
|
||||||
logger.Field("traffic", sub.Traffic),
|
logger.Field("traffic", sub.Traffic),
|
||||||
)
|
)
|
||||||
|
if clearErr := l.svcCtx.NodeModel.ClearServerAllCache(l.ctx); clearErr != nil {
|
||||||
|
l.Errorf("ClearServerAllCache error: %v", clearErr.Error())
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -43,19 +44,32 @@ func (l *TelephoneResetPasswordLogic) TelephoneResetPassword(req *types.Telephon
|
|||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.TelephoneError), "Invalid phone number")
|
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")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SmsNotEnabled), "sms login is not enabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the email verification is enabled, the verification code is required
|
// 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()
|
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Errorw("Redis Error", logger.Field("error", err.Error()), logger.Field("cacheKey", cacheKey))
|
l.Errorw("Redis Error", logger.Field("error", err.Error()), logger.Field("cacheKey", cacheKey))
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
|
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")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -245,5 +245,12 @@ func (l *TelephoneUserRegisterLogic) activeTrial(uid int64) error {
|
|||||||
UUID: uuidx.NewUUID().String(),
|
UUID: uuidx.NewUUID().String(),
|
||||||
Status: 1,
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -231,5 +231,12 @@ func (l *UserRegisterLogic) activeTrial(uid int64) error {
|
|||||||
UUID: uuidx.NewUUID().String(),
|
UUID: uuidx.NewUUID().String(),
|
||||||
Status: 1,
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,6 +14,7 @@ type customServerLogicModel interface {
|
|||||||
FilterNodeList(ctx context.Context, params *FilterNodeParams) (int64, []*Node, error)
|
FilterNodeList(ctx context.Context, params *FilterNodeParams) (int64, []*Node, error)
|
||||||
ClearNodeCache(ctx context.Context, params *FilterNodeParams) error
|
ClearNodeCache(ctx context.Context, params *FilterNodeParams) error
|
||||||
CountNodesByIdsAndTags(ctx context.Context, nodeIds []int64, tags []string) (int64, error)
|
CountNodesByIdsAndTags(ctx context.Context, nodeIds []int64, tags []string) (int64, error)
|
||||||
|
ClearServerAllCache(ctx context.Context) error
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -172,6 +173,30 @@ func (m *customServerModel) ClearServerCache(ctx context.Context, serverId int64
|
|||||||
return nil
|
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 查询
|
// InSet 支持多值 OR 查询
|
||||||
func InSet(field string, values []string) func(db *gorm.DB) *gorm.DB {
|
func InSet(field string, values []string) func(db *gorm.DB) *gorm.DB {
|
||||||
return func(db *gorm.DB) *gorm.DB {
|
return func(db *gorm.DB) *gorm.DB {
|
||||||
|
|||||||
12
pkg/ip/ip.go
12
pkg/ip/ip.go
@ -55,6 +55,15 @@ var (
|
|||||||
|
|
||||||
// GetRegionByIp queries the geolocation of an IP address using supported services.
|
// GetRegionByIp queries the geolocation of an IP address using supported services.
|
||||||
func GetRegionByIp(ip string) (*GeoLocationResponse, error) {
|
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 {
|
for service, enabled := range queryUrls {
|
||||||
if enabled {
|
if enabled {
|
||||||
response, err := fetchGeolocation(service, ip)
|
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)
|
zap.S().Errorf("Failed to fetch geolocation from %s: %v", service, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if response.Country == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
return response, nil
|
return response, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user