行为映射
go
type SendOTPInput struct {
Identifier string
Scene otpdomian.OTPScene
Channel otpdomian.OTPChannel
CaptchaKey string
Captcha string
}
func (s *service) Send(ctx context.Context, in SendOTPInput) error {
ok, _ := s.captcha.Verify(in.CaptchaKey, in.Captcha)
if !ok {
return resp.CAPTCHA_INVALID
}
// 行为映射
handlers := map[otpdomian.OTPScene]map[otpdomian.OTPChannel]handlerFunc{
otpdomian.OTPRegister: {
otpdomian.OTPEmail: s.handleOTPRegisterByEmail,
otpdomian.OTPPhone: s.handleOTPRegisterByPhone,
},
otpdomian.OTPLogin: {
otpdomian.OTPEmail: s.handleOTPLoginByEmail,
otpdomian.OTPPhone: s.handleOTPLoginByPhone,
},
otpdomian.OTPReset: {
otpdomian.OTPEmail: s.handleOTPResetByEmail,
otpdomian.OTPPhone: s.handleOTPResetByPhone,
},
}
sceneHandlers, ok := handlers[in.Scene]
if !ok {
return resp.OTP_SCENE_NOT_SUPPORTED
}
handler, ok := sceneHandlers[in.Channel]
if !ok {
return resp.OTP_CHANNEL_NOT_SUPPORTED
}
return handler(ctx, in.Identifier)
}
// ================================ handleXXX = implementation detail(实现细节) ================================
type handlerFunc func(ctx context.Context, identifier string) error
func (s *service) handleOTPRegisterByEmail(ctx context.Context, identifier string) error {
return nil
}
func (s *service) handleOTPRegisterByPhone(ctx context.Context, identifier string) error {
return nil
}
func (s *service) handleOTPLoginByEmail(ctx context.Context, identifier string) error {
return nil
}
func (s *service) handleOTPLoginByPhone(ctx context.Context, identifier string) error {
return nil
}
func (s *service) handleOTPResetByEmail(ctx context.Context, identifier string) error {
return nil
}
func (s *service) handleOTPResetByPhone(ctx context.Context, identifier string) error {
return nil
}