功能:补齐 消息中心 手工初始化 SQL

This commit is contained in:
xiaowang
2026-03-23 12:46:24 +08:00
parent 0aa6d5b4c2
commit c80fa557d2
6 changed files with 429 additions and 42 deletions
@@ -9,27 +9,27 @@ import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
/**
* MessageHub客户端服务实现
* 通过RabbitMQ发送消息到MessageHub服务
*
* @author xiaowang
* @date 2026-03-03
* MessageHub 客户端服务实现。
* 通过 RabbitMQ 发送消息到 MessageHub 服务。
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class MessageHubClientServiceImpl implements MessageHubClientService {
private static final String SMS_QUEUE = "smsQueue";
private static final String SOURCE_SYSTEM = "yuminail";
private static final String VERIFY_TEMPLATE_CODE = "SPUG0001";
private final RabbitTemplate rabbitTemplate;
private final ObjectMapper objectMapper;
private static final String SMS_QUEUE = "smsQueue";
@Override
public void sendSmsVerificationCode(String phone, String code) {
try {
@@ -37,30 +37,31 @@ public class MessageHubClientServiceImpl implements MessageHubClientService {
messageBody.put("receiver", phone);
messageBody.put("channelType", "SMS");
messageBody.put("msgType", "VERIFICATION");
messageBody.put("templateCode", "SPUG0001");
messageBody.put("sourceSystem", "yuminail");
messageBody.put("templateCode", VERIFY_TEMPLATE_CODE);
messageBody.put("sourceSystem", SOURCE_SYSTEM);
messageBody.put("uniqueId", UUID.randomUUID().toString());
messageBody.put("sendMethod", "SPUG");
Map<String, Object> templateParams = new HashMap<>();
templateParams.put("name", "屿Nail");
templateParams.put("name", "YumiNail");
templateParams.put("code", code);
templateParams.put("number", "5");
messageBody.put("templateParams", templateParams);
String jsonBody = objectMapper.writeValueAsString(messageBody);
Message message = MessageBuilder
.withBody(jsonBody.getBytes())
.withBody(jsonBody.getBytes(StandardCharsets.UTF_8))
.setContentType("application/json")
.setContentEncoding(StandardCharsets.UTF_8.name())
.setMessageId(UUID.randomUUID().toString())
.build();
rabbitTemplate.convertAndSend(SMS_QUEUE, message);
log.info("短信验证码消息已发送到队列,手机号: {}, 验证码: {}", phone, code);
} catch (Exception e) {
log.error("发送短信验证码失败", e);
throw new RuntimeException("发送短信验证码失败: " + e.getMessage(), e);
log.info("短信验证码消息已发送到队列,手机号: {}, sourceSystem: {}, templateCode: {}",
phone, SOURCE_SYSTEM, VERIFY_TEMPLATE_CODE);
} catch (Exception exception) {
log.error("发送短信验证码失败", exception);
throw new RuntimeException("发送短信验证码失败: " + exception.getMessage(), exception);
}
}
}
}
@@ -9,28 +9,24 @@ import org.springframework.stereotype.Service;
import java.security.SecureRandom;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
/**
* 验证码服务实现
*
* @author xiaowang
* @date 2026-03-03
* 验证码服务实现。
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class VerificationCodeServiceImpl implements VerificationCodeService {
private final StringRedisTemplate redisTemplate;
private final MessageHubClientService messageHubClientService;
private static final String CODE_PREFIX = "verification:code:";
private static final String SEND_TIME_PREFIX = "verification:send_time:";
private static final int CODE_LENGTH = 6;
private static final int CODE_EXPIRE_MINUTES = 5;
private static final int RESEND_INTERVAL_SECONDS = 60;
private final StringRedisTemplate redisTemplate;
private final MessageHubClientService messageHubClientService;
@Override
public String generateVerificationCode() {
SecureRandom random = new SecureRandom();
@@ -44,34 +40,33 @@ public class VerificationCodeServiceImpl implements VerificationCodeService {
@Override
public void sendVerificationCode(String phone) {
String code = generateVerificationCode();
String codeKey = CODE_PREFIX + phone;
String sendTimeKey = SEND_TIME_PREFIX + phone;
redisTemplate.opsForValue().set(codeKey, code, Duration.ofMinutes(CODE_EXPIRE_MINUTES));
redisTemplate.opsForValue().set(sendTimeKey, String.valueOf(System.currentTimeMillis()),
redisTemplate.opsForValue().set(sendTimeKey, String.valueOf(System.currentTimeMillis()),
Duration.ofSeconds(RESEND_INTERVAL_SECONDS));
messageHubClientService.sendSmsVerificationCode(phone, code);
log.info("验证码已发送,手机号: {}, 验证码: {}", phone, code);
log.info("验证码已发送,手机号: {}", phone);
}
@Override
public boolean verifyCode(String phone, String code) {
String codeKey = CODE_PREFIX + phone;
String storedCode = redisTemplate.opsForValue().get(codeKey);
if (storedCode == null) {
log.warn("验证码不存在或已过期,手机号: {}", phone);
return false;
}
if (!storedCode.equals(code)) {
log.warn("验证码错误,手机号: {}, 输入: {}, 正确: {}", phone, code, storedCode);
log.warn("验证码错误,手机号: {}", phone);
return false;
}
redisTemplate.delete(codeKey);
log.info("验证码验证成功,手机号: {}", phone);
return true;
@@ -83,4 +78,4 @@ public class VerificationCodeServiceImpl implements VerificationCodeService {
String sendTime = redisTemplate.opsForValue().get(sendTimeKey);
return sendTime == null;
}
}
}