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

75 lines
1.9 KiB
Go
Raw Permalink Normal View History

2024-08-22 14:27:13 +00:00
package accountlogic
import (
2024-08-24 06:46:26 +00:00
"app-cloudep-member-server/internal/domain"
2024-08-22 14:27:13 +00:00
"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 {
2024-08-24 06:46:26 +00:00
logx.WithCallerSkip(1).WithFields(
logx.Field("app", domain.AppName),
logx.Field("func", "Redis.Get"),
logx.Field("req", in),
).Error(err.Error())
2024-08-22 14:27:13 +00:00
return nil, ers.DBError("failed to connect to redis", err.Error())
}
if get == "" {
return nil, ers.DBError("failed to get data from redis")
}
2024-08-22 15:13:09 +00:00
if get != fmt.Sprintf("%s", in.VerifyCode) {
2024-08-22 14:27:13 +00:00
return nil, ers.ArkInternal("Illegible Verify Code")
}
_, err = l.svcCtx.Redis.Del(rk)
if err != nil {
2024-08-24 06:46:26 +00:00
logx.WithCallerSkip(1).WithFields(
logx.Field("app", domain.AppName),
logx.Field("func", "Redis.Del"),
logx.Field("key", rk),
).Error(err.Error())
2024-08-22 14:27:13 +00:00
return nil, ers.DBError("failed to del redis key", rk)
}
return &member.OKResp{}, nil
}