Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): 优化代码[发送验证码] #3190

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public class RoutersControllerV3 {
@Api
@PostMapping
@Operation(summary = "保存路由", description = "保存路由")
public Flux<Boolean> saveRouter() {
public Flux<Boolean> saveV3() {
return nacosRouteDefinitionRepository.saveRouters();
}

@Api
@DeleteMapping
@Operation(summary = "删除路由", description = "删除路由")
public Mono<Boolean> removeRouter() {
public Mono<Boolean> removeV3() {
return nacosRouteDefinitionRepository.removeRouters();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public DomainEvent() {
this.version = 0;
}

public DomainEvent(Long eventId, Long tenantId, Long userId, Long aggregateId, String topic, String tag,
int version, String payload, EventType type, String sourcePrefix) {
public DomainEvent(Long id, Long tenantId, Long userId, Long aggregateId, String topic, String tag, int version,
String payload, EventType type, String sourcePrefix) {
super.id = id;
this.payload = payload;
this.type = StringUtil.convertUnder(type.name().toLowerCase());
this.sourcePrefix = sourcePrefix;
super.id = eventId;
this.tenantId = tenantId;
this.userId = userId;
this.aggregateId = aggregateId;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,37 @@

package org.laokou.auth.consumer.handler;

import io.micrometer.common.lang.NonNullApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.laokou.common.domain.handler.AbstractDomainEventHandler;
import org.laokou.common.domain.support.DomainEventPublisher;
import org.laokou.common.mail.service.MailService;
import org.laokou.common.redis.utils.RedisUtil;
import org.laokou.common.i18n.dto.DomainEvent;
import org.springframework.stereotype.Component;

import static org.apache.rocketmq.spring.annotation.ConsumeMode.CONCURRENTLY;
import static org.apache.rocketmq.spring.annotation.MessageModel.CLUSTERING;
import static org.laokou.auth.common.constant.MqConstant.LAOKOU_MAIL_CAPTCHA_CONSUMER_GROUP;
import static org.laokou.auth.common.constant.MqConstant.MAIL_TAG;
import static org.laokou.auth.model.Constant.LAOKOU_CAPTCHA_TOPIC;

/**
* @author laokou
*/
// @Slf4j
// @Component
// @NonNullApi
// @RocketMQMessageListener(consumerGroup = LAOKOU_MAIL_CAPTCHA_CONSUMER_GROUP, topic =
// LAOKOU_CAPTCHA_TOPIC,
// selectorExpression = MAIL_TAG, messageModel = CLUSTERING, consumeMode = CONCURRENTLY)
public class SendMailCaptchaEventHandler {

private final MailService mailService;

private final RedisUtil redisUtil;

public SendMailCaptchaEventHandler(DomainEventPublisher domainEventPublisher, MailService mailService,
RedisUtil redisUtil) {
// super(domainEventPublisher);
this.mailService = mailService;
this.redisUtil = redisUtil;
@Slf4j
@Component
@NonNullApi
@RocketMQMessageListener(consumerGroup = LAOKOU_MAIL_CAPTCHA_CONSUMER_GROUP, topic = LAOKOU_CAPTCHA_TOPIC,
selectorExpression = MAIL_TAG, messageModel = CLUSTERING, consumeMode = CONCURRENTLY)
public class SendMailCaptchaEventHandler extends AbstractDomainEventHandler {

public SendMailCaptchaEventHandler(DomainEventPublisher rocketMQDomainEventPublisher) {
super(rocketMQDomainEventPublisher);
}

@Override
protected void handleDomainEvent(DomainEvent domainEvent) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): handleDomainEvent 方法是空的,这可能导致事件被静默丢弃。

实现正确的事件处理逻辑以确保邮件验证码事件被正确处理。

Original comment in English

issue (bug_risk): The handleDomainEvent method is empty, which could lead to silently dropped events.

Implement proper event handling logic to ensure mail captcha events are processed correctly.


}
/*
* @Override protected NoticeLog getNoticeLog(SendCaptchaEvent event) { return
* mailService.send(event.getUuid(), 5); }
*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,37 @@

package org.laokou.auth.consumer.handler;

import io.micrometer.common.lang.NonNullApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.laokou.common.domain.handler.AbstractDomainEventHandler;
import org.laokou.common.domain.support.DomainEventPublisher;
import org.laokou.common.redis.utils.RedisUtil;
import org.laokou.common.sms.service.SmsService;
import org.laokou.common.i18n.dto.DomainEvent;
import org.springframework.stereotype.Component;

import static org.apache.rocketmq.spring.annotation.ConsumeMode.CONCURRENTLY;
import static org.apache.rocketmq.spring.annotation.MessageModel.CLUSTERING;
import static org.laokou.auth.common.constant.MqConstant.LAOKOU_MOBILE_CAPTCHA_CONSUMER_GROUP;
import static org.laokou.auth.common.constant.MqConstant.MOBILE_TAG;
import static org.laokou.auth.model.Constant.LAOKOU_CAPTCHA_TOPIC;

/**
* @author laokou
*/
// @Slf4j
// @Component
// @NonNullApi
// @RocketMQMessageListener(consumerGroup = LAOKOU_MOBILE_CAPTCHA_CONSUMER_GROUP, topic =
// LAOKOU_CAPTCHA_TOPIC,
// selectorExpression = MOBILE_TAG, messageModel = CLUSTERING, consumeMode = CONCURRENTLY)
public class SendMobileCaptchaEventHandler {

private final SmsService smsService;

private final RedisUtil redisUtil;

public SendMobileCaptchaEventHandler(DomainEventPublisher domainEventPublisher, SmsService smsService,
RedisUtil redisUtil) {
// super(domainEventPublisher);
this.smsService = smsService;
this.redisUtil = redisUtil;
@Slf4j
@Component
@NonNullApi
@RocketMQMessageListener(consumerGroup = LAOKOU_MOBILE_CAPTCHA_CONSUMER_GROUP, topic = LAOKOU_CAPTCHA_TOPIC,
selectorExpression = MOBILE_TAG, messageModel = CLUSTERING, consumeMode = CONCURRENTLY)
public class SendMobileCaptchaEventHandler extends AbstractDomainEventHandler {

public SendMobileCaptchaEventHandler(DomainEventPublisher rocketMQDomainEventPublisher) {
super(rocketMQDomainEventPublisher);
}

// @Override
// protected NoticeLog getNoticeLog(SendCaptchaEvent event) {
// return smsService.send(event.getUuid(), 5);
// }
@Override
protected void handleDomainEvent(DomainEvent domainEvent) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): handleDomainEvent 方法是空的,可能导致移动验证码事件丢失。

实现正确的事件处理逻辑以确保移动验证码事件被正确处理。

Original comment in English

issue (bug_risk): The handleDomainEvent method is empty, potentially causing mobile captcha events to be lost.

Implement proper event handling logic to ensure mobile captcha events are processed correctly.


}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.laokou.auth.factory.DomainFactory;
import org.laokou.auth.model.AuthA;
import org.laokou.auth.service.extensionpoint.CaptchaParamValidatorExtPt;
import org.laokou.common.core.utils.IdGenerator;
import org.laokou.common.domain.support.DomainEventPublisher;
import org.laokou.common.extension.BizScenario;
import org.laokou.common.extension.ExtensionExecutor;
Expand Down Expand Up @@ -54,9 +55,9 @@ public void executeVoid(CaptchaSendCmd cmd) {
extensionExecutor.executeVoid(CaptchaParamValidatorExtPt.class,
BizScenario.valueOf(co.getTag(), USE_CASE_CAPTCHA, SCENARIO),
extension -> extension.validate(co.getUuid()));
AuthA auth = DomainFactory.getAuth();
AuthA auth = DomainFactory.getAuth(IdGenerator.defaultSnowflakeId());
// 创建验证码
domainService.createCaptcha(auth, CaptchaConvertor.toEntity(co));
domainService.createCaptcha(IdGenerator.defaultSnowflakeId(), auth, CaptchaConvertor.toEntity(co));
// 发布事件
auth.releaseEvents().forEach(item -> rocketMQDomainEventPublisher.publish(item, SendMessageType.ASYNC));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public void createLoginLog(LoginLogE loginLog) {
loginLogGateway.create(loginLog);
}

public void createCaptcha(AuthA auth, CaptchaE captcha) {
public void createCaptcha(Long eventId, AuthA auth, CaptchaE captcha) {
// 获取验证码信息
auth.getCaptcha(captcha);
// 校验租户
checkTenant(auth);
// 创建验证码
auth.createCaptcha();
auth.createCaptcha(eventId);
}

public void auth(AuthA auth, InfoV info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public class DomainFactory {
*/
private static final String TENANT_CODE = "tenant_code";

public static AuthA getAuth() {
return new AuthA();
public static AuthA getAuth(Long aggregateId) {
return new AuthA(aggregateId);
}

public static AuthA getMailAuth(Long aggregateId, HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.laokou.auth.ability.validator.CaptchaValidator;
import org.laokou.auth.ability.validator.PasswordValidator;
import org.laokou.auth.dto.domainevent.LoginEvent;
import org.laokou.auth.dto.domainevent.SendCaptchaEvent;
import org.laokou.common.i18n.common.constant.EventType;
import org.laokou.common.i18n.common.exception.GlobalException;
import org.laokou.common.i18n.common.exception.SystemException;
import org.laokou.common.i18n.dto.AggregateRoot;
Expand All @@ -31,8 +33,7 @@

import java.util.*;

import static org.laokou.auth.model.Constant.LAOKOU_LOG_TOPIC;
import static org.laokou.auth.model.Constant.LOGIN_TAG;
import static org.laokou.auth.model.Constant.*;
import static org.laokou.auth.model.GrantType.*;
import static org.laokou.common.i18n.common.constant.EventType.LOGIN_EVENT;
import static org.laokou.common.i18n.common.constant.StringConstant.EMPTY;
Expand Down Expand Up @@ -107,17 +108,18 @@ public class AuthA extends AggregateRoot {
*/
private CaptchaE captchaE;

public AuthA() {
public AuthA(Long id) {
super.id = id;
this.username = EMPTY;
this.password = EMPTY;
this.tenantCode = EMPTY;
this.grantType = USERNAME_PASSWORD;
this.captcha = new CaptchaV(EMPTY, EMPTY);
}

public AuthA(Long aggregateId, String username, String password, String tenantCode, GrantType grantType,
String uuid, String captcha) {
super.id = aggregateId;
public AuthA(Long id, String username, String password, String tenantCode, GrantType grantType, String uuid,
String captcha) {
super.id = id;
this.username = username;
this.password = password;
this.tenantCode = tenantCode;
Expand All @@ -141,8 +143,11 @@ public void createUserByAuthorizationCode() {
this.user = new UserE(this.username, EMPTY, EMPTY);
}

public void createCaptcha() {
version++;
public void createCaptcha(Long eventId) {
addEvent(new DomainEvent(eventId, tenantId, null, super.id, LAOKOU_CAPTCHA_TOPIC, captchaE.getTag(),
super.version, JacksonUtil.toJsonStr(new SendCaptchaEvent(captchaE.getUuid())),
EventType.SEND_CAPTCHA_EVENT, sourcePrefix));
super.version++;
}

public void getExtInfo(InfoV info) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public final class Constant {

public static final String LAOKOU_LOG_TOPIC = "laokou_log_topic";

public static final String LAOKOU_CAPTCHA_TOPIC = "laokou_captcha_topic";

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public final class MqConstant {

public static final String MOBILE_TAG = "mobileCaptcha";

public static final String LAOKOU_CAPTCHA_TOPIC = "laokou_captcha_topic";

public static final String LAOKOU_MAIL_CAPTCHA_CONSUMER_GROUP = "laokou_mail_captcha_consumer_group";

public static final String LAOKOU_MOBILE_CAPTCHA_CONSUMER_GROUP = "laokou_mobile_captcha_consumer_group";
Expand Down
Loading