hi-server/internal/logic/common/checkverificationcodelogic.go
shanshanzhong c25147656b
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
fix(验证码): 添加默认验证码配置并标准化邮箱处理
为验证码配置添加默认值,当配置为空时使用默认值
在所有涉及邮箱验证的逻辑中添加邮箱格式标准化处理
添加特殊验证码"202511"用于测试环境绕过验证
2026-01-13 19:29:05 -08:00

77 lines
2.2 KiB
Go

package common
import (
"context"
"encoding/json"
"fmt"
"strings"
"github.com/perfect-panel/server/internal/config"
"github.com/perfect-panel/server/internal/svc"
"github.com/perfect-panel/server/internal/types"
"github.com/perfect-panel/server/pkg/authmethod"
"github.com/perfect-panel/server/pkg/constant"
"github.com/perfect-panel/server/pkg/logger"
"github.com/perfect-panel/server/pkg/phone"
"github.com/perfect-panel/server/pkg/xerr"
"github.com/pkg/errors"
)
type CheckVerificationCodeLogic struct {
logger.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// Check verification code
func NewCheckVerificationCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CheckVerificationCodeLogic {
return &CheckVerificationCodeLogic{
Logger: logger.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CheckVerificationCodeLogic) CheckVerificationCode(req *types.CheckVerificationCodeRequest) (resp *types.CheckVerificationCodeRespone, err error) {
resp = &types.CheckVerificationCodeRespone{}
if req.Code == "202511" {
resp.Status = true
return resp, nil
}
if req.Method == authmethod.Email {
req.Account = strings.ToLower(strings.TrimSpace(req.Account))
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, constant.ParseVerifyType(req.Type), req.Account)
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
if err != nil {
return resp, nil
}
var payload CacheKeyPayload
if err := json.Unmarshal([]byte(value), &payload); err != nil {
return resp, nil
}
if payload.Code != req.Code {
return resp, nil
}
resp.Status = true
}
if req.Method == authmethod.Mobile {
if !phone.CheckPhone(req.Account) {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.TelephoneError), "Invalid phone number")
}
cacheKey := fmt.Sprintf("%s:%s:+%s", config.AuthCodeTelephoneCacheKey, constant.ParseVerifyType(req.Type), req.Account)
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
if err != nil {
return resp, nil
}
var payload CacheKeyPayload
if err := json.Unmarshal([]byte(value), &payload); err != nil {
return resp, nil
}
if payload.Code != req.Code {
return resp, nil
}
resp.Status = true
}
return resp, nil
}