修复:修正 消息中心 spug 测试发送
This commit is contained in:
+86
-13
@@ -5,6 +5,7 @@ import com.xiaowang.messagehub.enums.ChannelProviderCodeEnum;
|
||||
import com.xiaowang.messagehub.model.ChannelHealthCheckResult;
|
||||
import com.xiaowang.messagehub.model.ManagedChannelConfig;
|
||||
import com.xiaowang.messagehub.model.SmsDispatchRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
@@ -17,6 +18,7 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class SpugSmsChannelProvider implements SmsChannelProvider {
|
||||
|
||||
@@ -45,15 +47,18 @@ public class SpugSmsChannelProvider implements SmsChannelProvider {
|
||||
|
||||
HttpURLConnection connection = null;
|
||||
try {
|
||||
String baseUrl = channelConfig.getStringConfig("baseUrl");
|
||||
String url = trimTrailingSlash(baseUrl) + "/sms/" + request.getTemplateExternalId();
|
||||
String url = buildSendUrl(channelConfig.getStringConfig("baseUrl"), request.getTemplateExternalId());
|
||||
Map<String, Object> payload = new LinkedHashMap<>(request.getTemplateParams());
|
||||
payload.put("to", request.getReceiver());
|
||||
payload.putIfAbsent("targets", request.getReceiver());
|
||||
payload.putIfAbsent("to", request.getReceiver());
|
||||
|
||||
connection = (HttpURLConnection) new URL(url).openConnection();
|
||||
connection.setRequestMethod("POST");
|
||||
connection.setRequestProperty("Content-Type", "application/json");
|
||||
connection.setRequestProperty("X-API-Key", channelConfig.getStringSecret("apiKey"));
|
||||
connection.setRequestProperty("Accept", "application/json");
|
||||
if (StringUtils.hasText(channelConfig.getStringSecret("apiKey"))) {
|
||||
connection.setRequestProperty("X-API-Key", channelConfig.getStringSecret("apiKey"));
|
||||
}
|
||||
connection.setDoOutput(true);
|
||||
connection.setConnectTimeout(5000);
|
||||
connection.setReadTimeout(10000);
|
||||
@@ -63,16 +68,15 @@ public class SpugSmsChannelProvider implements SmsChannelProvider {
|
||||
}
|
||||
|
||||
int responseCode = connection.getResponseCode();
|
||||
if (responseCode != HttpURLConnection.HTTP_OK) {
|
||||
throw new IllegalStateException("Spug 请求失败,HTTP 状态码: " + responseCode);
|
||||
String responseBody = readResponseBody(connection, responseCode);
|
||||
log.info("Spug 短信发送响应,url: {}, receiver: {}, httpCode: {}, body: {}",
|
||||
url, request.getReceiver(), responseCode, responseBody);
|
||||
|
||||
if (responseCode < 200 || responseCode >= 300) {
|
||||
throw new IllegalStateException("Spug 请求失败,HTTP 状态码: " + responseCode + ",响应: " + responseBody);
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
while (reader.readLine() != null) {
|
||||
// Drain response stream to complete request lifecycle.
|
||||
}
|
||||
}
|
||||
validateBusinessResponse(responseBody);
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("Spug 短信发送失败: " + exception.getMessage(), exception);
|
||||
} finally {
|
||||
@@ -89,7 +93,9 @@ public class SpugSmsChannelProvider implements SmsChannelProvider {
|
||||
HttpURLConnection connection = (HttpURLConnection) new URL(channelConfig.getStringConfig("baseUrl"))
|
||||
.openConnection();
|
||||
connection.setRequestMethod("GET");
|
||||
connection.setRequestProperty("X-API-Key", channelConfig.getStringSecret("apiKey"));
|
||||
if (StringUtils.hasText(channelConfig.getStringSecret("apiKey"))) {
|
||||
connection.setRequestProperty("X-API-Key", channelConfig.getStringSecret("apiKey"));
|
||||
}
|
||||
connection.setConnectTimeout(3000);
|
||||
connection.setReadTimeout(3000);
|
||||
int responseCode = connection.getResponseCode();
|
||||
@@ -107,6 +113,73 @@ public class SpugSmsChannelProvider implements SmsChannelProvider {
|
||||
return value.endsWith("/") ? value.substring(0, value.length() - 1) : value;
|
||||
}
|
||||
|
||||
private String buildSendUrl(String baseUrl, String templateExternalId) {
|
||||
String normalizedBaseUrl = trimTrailingSlash(baseUrl);
|
||||
if (normalizedBaseUrl.endsWith("/send")) {
|
||||
return normalizedBaseUrl + "/" + templateExternalId;
|
||||
}
|
||||
if (normalizedBaseUrl.contains("/send/" + templateExternalId)) {
|
||||
return normalizedBaseUrl;
|
||||
}
|
||||
return normalizedBaseUrl + "/send/" + templateExternalId;
|
||||
}
|
||||
|
||||
private String readResponseBody(HttpURLConnection connection, int responseCode) throws Exception {
|
||||
if (responseCode >= 200 && responseCode < 400) {
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
|
||||
return readAll(reader);
|
||||
}
|
||||
}
|
||||
|
||||
if (connection.getErrorStream() == null) {
|
||||
return "";
|
||||
}
|
||||
|
||||
try (BufferedReader reader = new BufferedReader(
|
||||
new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))) {
|
||||
return readAll(reader);
|
||||
}
|
||||
}
|
||||
|
||||
private String readAll(BufferedReader reader) throws Exception {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
if (builder.length() > 0) {
|
||||
builder.append('\n');
|
||||
}
|
||||
builder.append(line);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private void validateBusinessResponse(String responseBody) {
|
||||
if (!StringUtils.hasText(responseBody)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
Map<String, Object> response = objectMapper.readValue(responseBody, Map.class);
|
||||
Object code = response.get("code");
|
||||
Object message = response.get("message");
|
||||
if (code instanceof Number && ((Number) code).intValue() != 0) {
|
||||
throw new IllegalStateException("Spug 返回失败,code=" + code + ",message=" + message);
|
||||
}
|
||||
if (code instanceof String && !"0".equals(code) && !"success".equalsIgnoreCase((String) code)) {
|
||||
throw new IllegalStateException("Spug 返回失败,code=" + code + ",message=" + message);
|
||||
}
|
||||
Object success = response.get("success");
|
||||
if (success instanceof Boolean && !((Boolean) success)) {
|
||||
throw new IllegalStateException("Spug 返回失败,message=" + message);
|
||||
}
|
||||
} catch (IllegalStateException exception) {
|
||||
throw exception;
|
||||
} catch (Exception exception) {
|
||||
log.warn("Spug 响应不是标准 JSON,保留原始返回体: {}", responseBody);
|
||||
}
|
||||
}
|
||||
|
||||
private void requireText(String value, String message) {
|
||||
if (!StringUtils.hasText(value)) {
|
||||
throw new IllegalArgumentException(message);
|
||||
|
||||
@@ -78,6 +78,13 @@
|
||||
- 实时生成短信预览
|
||||
- 服务端会校验模板是否启用、占位符是否填写完整
|
||||
|
||||
### 9. 测试发送链路与 Spug 兼容修正
|
||||
|
||||
- 后台测试发送文案明确为“直连通道,不经过 MQ 队列监听器”
|
||||
- 修正 Spug 短信发送路径,兼容 `send/{templateId}` 口径
|
||||
- 增强 Spug 响应体校验,不再只按 HTTP 200 判定成功
|
||||
- 新增 Spug 发送响应日志,便于排查第三方平台返回结果
|
||||
|
||||
## 数据库 SQL
|
||||
|
||||
本轮新增 SQL 文件:
|
||||
|
||||
@@ -175,7 +175,7 @@
|
||||
<div class="panel-header">
|
||||
<div>
|
||||
<h2>测试发送</h2>
|
||||
<p>这里会直接按所选通道发送测试消息,用于验证账号、密钥和通道连通性。</p>
|
||||
<p>这里会直接按所选通道发送测试消息,用于验证账号、密钥和通道连通性,不经过 MQ 队列监听器。</p>
|
||||
</div>
|
||||
</div>
|
||||
<form id="testForm" class="form-grid">
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
# MessageHub 测试发送链路与 Spug 兼容修复
|
||||
|
||||
## 日期
|
||||
|
||||
2026-03-23
|
||||
|
||||
## 问题现象
|
||||
|
||||
- 后台点击“测试发送”后,没有进入 `SmsQueueListener`
|
||||
- 监听器中“收到短信消息”的日志没有打印
|
||||
- 后台提示成功时,实际短信仍可能未收到
|
||||
|
||||
## 结论
|
||||
|
||||
### 1. 测试发送本来就不经过 MQ 监听器
|
||||
|
||||
- 后台测试发送接口调用的是:
|
||||
- `POST /admin/api/channels/test-send`
|
||||
- 当前实现会直接按所选通道调用短信/邮件提供商
|
||||
- 它不会往 `smsQueue` 投递消息
|
||||
- 所以 `SmsQueueListener` 中的“收到短信消息”日志不打印是当前设计结果,不是监听器失效
|
||||
|
||||
### 2. Spug Provider 原实现存在对接口径不匹配风险
|
||||
|
||||
- 原实现使用的是:
|
||||
- `POST {baseUrl}/sms/{templateExternalId}`
|
||||
- 请求头:`X-API-Key`
|
||||
- 请求体接收目标字段:`to`
|
||||
- 结合现有资料与 `push.spug.cc` 常见模板短信用法,这套路径更可能应为:
|
||||
- `POST {baseUrl}/send/{templateExternalId}`
|
||||
- 因此原逻辑可能出现:
|
||||
- 后台接口返回成功
|
||||
- 实际平台未真正接收或未真正发送
|
||||
|
||||
## 本轮修复
|
||||
|
||||
### 1. Spug 发送路径兼容修正
|
||||
|
||||
- 将发送路径改为兼容:
|
||||
- `{baseUrl}/send/{templateExternalId}`
|
||||
- 若 `baseUrl` 已带 `/send`,则避免重复拼接
|
||||
|
||||
### 2. 请求参数兼容增强
|
||||
|
||||
- 请求体同时兼容写入:
|
||||
- `targets`
|
||||
- `to`
|
||||
- 保留模板参数原样发送
|
||||
|
||||
### 3. 成功判定增强
|
||||
|
||||
- 不再只看 HTTP 200
|
||||
- 新增响应体读取与日志输出
|
||||
- 若响应体为 JSON,会进一步校验:
|
||||
- `code`
|
||||
- `success`
|
||||
- `message`
|
||||
- 避免“HTTP 成功但业务失败”被误判成发送成功
|
||||
|
||||
### 4. 后台说明补充
|
||||
|
||||
- 测试发送区文案已明确:
|
||||
- 测试发送是直连通道
|
||||
- 不经过 MQ 队列监听器
|
||||
|
||||
## 验证
|
||||
|
||||
- 已执行:`mvn -q -DskipTests compile`
|
||||
- 编译通过
|
||||
|
||||
## 涉及文件
|
||||
|
||||
- [SpugSmsChannelProvider.java](/E:/code/xiaowang/MessageHub/src/main/java/com/xiaowang/messagehub/service/channel/sms/SpugSmsChannelProvider.java)
|
||||
- [index.html](/E:/code/xiaowang/MessageHub/src/main/resources/static/messagehub-admin/index.html)
|
||||
Reference in New Issue
Block a user