app-cloudep-member-server/internal/logic/account/verify_refresh_code_logic.go

75 lines
1.9 KiB
Go

package accountlogic
import (
"app-cloudep-member-server/internal/domain"
"context"
"fmt"
ers "code.30cm.net/digimon/library-go/errors"
"app-cloudep-member-server/gen_result/pb/member"
"app-cloudep-member-server/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type VerifyRefreshCodeLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewVerifyRefreshCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VerifyRefreshCodeLogic {
return &VerifyRefreshCodeLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// VerifyRefreshCode 驗證忘記密碼 token
func (l *VerifyRefreshCodeLogic) VerifyRefreshCode(in *member.VerifyRefreshCodeReq) (*member.OKResp, error) {
// 驗證資料
if err := l.svcCtx.Validate.ValidateAll(&generateRefreshCodeReq{
Account: in.GetAccount(),
CodeType: in.GetCodeType(),
}); err != nil {
return nil, ers.InvalidFormat(err.Error())
}
checkType, status := getCodeNameByCode(in.GetCodeType())
if !status {
return nil, ers.InvalidFormat(fmt.Errorf("failed to get correct code type").Error())
}
rk := fmt.Sprintf("verify:%s:%s", checkType, in.GetAccount())
get, err := l.svcCtx.Redis.Get(rk)
if err != nil {
logx.WithCallerSkip(1).WithFields(
logx.Field("app", domain.AppName),
logx.Field("func", "Redis.Get"),
logx.Field("req", in),
).Error(err.Error())
return nil, ers.DBError("failed to connect to redis", err.Error())
}
if get == "" {
return nil, ers.DBError("failed to get data from redis")
}
if get != fmt.Sprintf("%s", in.VerifyCode) {
return nil, ers.ArkInternal("Illegible Verify Code")
}
_, err = l.svcCtx.Redis.Del(rk)
if err != nil {
logx.WithCallerSkip(1).WithFields(
logx.Field("app", domain.AppName),
logx.Field("func", "Redis.Del"),
logx.Field("key", rk),
).Error(err.Error())
return nil, ers.DBError("failed to del redis key", rk)
}
return &member.OKResp{}, nil
}