功能:自动生成消息中心模板 ID
This commit is contained in:
-1
@@ -8,7 +8,6 @@ import javax.validation.constraints.NotNull;
|
||||
@Data
|
||||
public class ChannelSmsTemplateSaveRequest {
|
||||
|
||||
@NotBlank(message = "模板编码不能为空")
|
||||
private String templateCode;
|
||||
|
||||
@NotBlank(message = "模板名称不能为空")
|
||||
|
||||
+50
-1
@@ -30,6 +30,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Collections;
|
||||
@@ -47,6 +48,9 @@ import java.util.regex.Pattern;
|
||||
public class MessageChannelService {
|
||||
|
||||
private static final Pattern TEMPLATE_PLACEHOLDER_PATTERN = Pattern.compile("\\$\\{([^{}]+)}");
|
||||
private static final char[] TEMPLATE_CODE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
|
||||
private static final int TEMPLATE_CODE_LENGTH = 16;
|
||||
private static final SecureRandom TEMPLATE_CODE_RANDOM = new SecureRandom();
|
||||
|
||||
private final MessageChannelConfigMapper channelConfigMapper;
|
||||
private final PushTemplateMapper pushTemplateMapper;
|
||||
@@ -368,7 +372,7 @@ public class MessageChannelService {
|
||||
private void applySmsTemplateRequest(PushTemplateDO target,
|
||||
ChannelSmsTemplateSaveRequest request,
|
||||
Long currentId) {
|
||||
String templateCode = requireTrimmedText(request.getTemplateCode(), "模板编码不能为空");
|
||||
String templateCode = resolveSmsTemplateCode(request.getTemplateCode(), target.getTemplateCode());
|
||||
PushTemplateDO existing = pushTemplateMapper.selectByTemplateCode(templateCode);
|
||||
if (existing != null && !Objects.equals(existing.getId(), currentId)) {
|
||||
throw new IllegalArgumentException("模板编码已存在: " + templateCode);
|
||||
@@ -386,6 +390,51 @@ public class MessageChannelService {
|
||||
target.setDescription(trimToNull(request.getDescription()));
|
||||
}
|
||||
|
||||
private String resolveSmsTemplateCode(String requestedTemplateCode, String existingTemplateCode) {
|
||||
if (StringUtils.hasText(existingTemplateCode)) {
|
||||
return existingTemplateCode;
|
||||
}
|
||||
return generateUniqueSmsTemplateCode();
|
||||
}
|
||||
|
||||
private String generateUniqueSmsTemplateCode() {
|
||||
for (int attempt = 0; attempt < 20; attempt++) {
|
||||
String candidate = buildRandomSmsTemplateCode();
|
||||
if (pushTemplateMapper.selectByTemplateCode(candidate) == null) {
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
throw new IllegalStateException("短信模板 ID 生成失败,请重试");
|
||||
}
|
||||
|
||||
private String buildRandomSmsTemplateCode() {
|
||||
char[] chars = new char[TEMPLATE_CODE_LENGTH];
|
||||
chars[0] = randomUppercaseLetter();
|
||||
chars[1] = randomLowercaseLetter();
|
||||
for (int index = 2; index < chars.length; index++) {
|
||||
chars[index] = TEMPLATE_CODE_ALPHABET[TEMPLATE_CODE_RANDOM.nextInt(TEMPLATE_CODE_ALPHABET.length)];
|
||||
}
|
||||
shuffle(chars);
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
private void shuffle(char[] chars) {
|
||||
for (int index = chars.length - 1; index > 0; index--) {
|
||||
int swapIndex = TEMPLATE_CODE_RANDOM.nextInt(index + 1);
|
||||
char current = chars[index];
|
||||
chars[index] = chars[swapIndex];
|
||||
chars[swapIndex] = current;
|
||||
}
|
||||
}
|
||||
|
||||
private char randomUppercaseLetter() {
|
||||
return (char) ('A' + TEMPLATE_CODE_RANDOM.nextInt(26));
|
||||
}
|
||||
|
||||
private char randomLowercaseLetter() {
|
||||
return (char) ('a' + TEMPLATE_CODE_RANDOM.nextInt(26));
|
||||
}
|
||||
|
||||
private ManagedChannelConfig buildManagedChannel(MessageChannelConfigDO entity) {
|
||||
return new ManagedChannelConfig(entity, readJsonMap(entity.getConfigJson()), readSecretJson(entity.getSecretJson()));
|
||||
}
|
||||
|
||||
@@ -108,6 +108,17 @@
|
||||
- 后台页面补充 Gmail / QQ 邮箱配置说明
|
||||
- 健康检查失败时会优先返回中文可操作提示,降低排障成本
|
||||
|
||||
### 12. 模板 ID 自动生成优化
|
||||
|
||||
- 新增短信模板时,模板 ID 改为系统自动生成
|
||||
- 生成规则为:
|
||||
- 16 位
|
||||
- 仅包含英文字母
|
||||
- 同时包含大写和小写
|
||||
- 编辑已有模板时保持原模板 ID 不变
|
||||
- 外部模板 ID 继续保留为可选字段
|
||||
- 后台页面提示调整为“第三方平台模板标识”,不再局限于 Spug
|
||||
|
||||
## 数据库 SQL
|
||||
|
||||
本轮新增 SQL 文件:
|
||||
|
||||
@@ -236,6 +236,29 @@ function extractPlaceholdersFromContent(contentPattern) {
|
||||
return placeholders;
|
||||
}
|
||||
|
||||
function generateSmsTemplateCode() {
|
||||
const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
const lowercase = "abcdefghijklmnopqrstuvwxyz";
|
||||
const alphabet = uppercase + lowercase;
|
||||
const chars = [
|
||||
uppercase[Math.floor(Math.random() * uppercase.length)],
|
||||
lowercase[Math.floor(Math.random() * lowercase.length)]
|
||||
];
|
||||
|
||||
while (chars.length < 16) {
|
||||
chars.push(alphabet[Math.floor(Math.random() * alphabet.length)]);
|
||||
}
|
||||
|
||||
for (let index = chars.length - 1; index > 0; index -= 1) {
|
||||
const swapIndex = Math.floor(Math.random() * (index + 1));
|
||||
const current = chars[index];
|
||||
chars[index] = chars[swapIndex];
|
||||
chars[swapIndex] = current;
|
||||
}
|
||||
|
||||
return chars.join("");
|
||||
}
|
||||
|
||||
function renderPlaceholderChips(placeholders) {
|
||||
if (!placeholders || placeholders.length === 0) {
|
||||
return '<span class="table-note">未识别到占位符</span>';
|
||||
@@ -523,7 +546,7 @@ function updateSmsTemplatePlaceholderPreview() {
|
||||
function resetSmsTemplateForm(template = null) {
|
||||
state.editingSmsTemplate = template;
|
||||
elements.smsTemplateId.value = template?.id || "";
|
||||
elements.smsTemplateCode.value = template?.templateCode || "";
|
||||
elements.smsTemplateCode.value = template?.templateCode || generateSmsTemplateCode();
|
||||
elements.smsTemplateName.value = template?.templateName || "";
|
||||
elements.smsTemplateExternalId.value = template?.externalId || "";
|
||||
fillSmsTemplateChannelOptions(template?.channelId ?? "");
|
||||
@@ -534,6 +557,10 @@ function resetSmsTemplateForm(template = null) {
|
||||
elements.templateFormSubtitle.textContent = template
|
||||
? `当前模板 ID:${template.id}。已绑定通道的模板只能在对应短信通道中测试和发送。`
|
||||
: "保存后会立即加入测试发送模板列表;不绑定通道时,该模板默认对所有短信通道通用。";
|
||||
elements.templateFormTitle.textContent = template ? "编辑短信模板" : "新增短信模板";
|
||||
elements.templateFormSubtitle.textContent = template
|
||||
? `当前模板 ID:${template.id}。已绑定通道的模板只能在对应短信通道中测试和发送。`
|
||||
: "新增时会自动生成 16 位大小写字母模板 ID;外部模板 ID 可按第三方平台需要选填。";
|
||||
updateSmsTemplatePlaceholderPreview();
|
||||
}
|
||||
|
||||
@@ -552,7 +579,7 @@ function renderSmsTemplateTable() {
|
||||
row.innerHTML = `
|
||||
<td>
|
||||
<strong>${escapeHtml(template.templateName)}</strong>
|
||||
<div class="table-note">模板编码:<code>${escapeHtml(template.templateCode)}</code></div>
|
||||
<div class="table-note">模板ID:<code>${escapeHtml(template.templateCode)}</code></div>
|
||||
${template.description ? `<div class="table-note">${escapeHtml(template.description)}</div>` : ""}
|
||||
</td>
|
||||
<td>
|
||||
@@ -870,7 +897,7 @@ async function onSubmitSmsTemplateForm(event) {
|
||||
event.preventDefault();
|
||||
|
||||
const payload = {
|
||||
templateCode: elements.smsTemplateCode.value.trim(),
|
||||
templateCode: elements.smsTemplateCode.value.trim() || generateSmsTemplateCode(),
|
||||
templateName: elements.smsTemplateName.value.trim(),
|
||||
externalId: elements.smsTemplateExternalId.value.trim(),
|
||||
channelId: elements.smsTemplateChannelId.value ? Number(elements.smsTemplateChannelId.value) : null,
|
||||
|
||||
@@ -192,7 +192,7 @@
|
||||
<input id="testSubject" type="text" placeholder="留空则使用默认主题">
|
||||
</label>
|
||||
<label class="test-only-sms">
|
||||
<span>模板编码</span>
|
||||
<span>模板ID</span>
|
||||
<select id="testTemplateCode">
|
||||
<option value="">请选择短信模板</option>
|
||||
</select>
|
||||
@@ -228,7 +228,7 @@
|
||||
<button id="createTemplateButton" class="ghost-button small">新增模板</button>
|
||||
</div>
|
||||
<div class="hint-box">
|
||||
<code>模板编码</code> 用于业务系统和测试发送选择模板,<code>外部模板ID</code> 可填写 Spug 模板编号。
|
||||
<code>模板ID</code> 会在新增时自动生成 16 位大小写字母编码,<code>外部模板ID</code> 可填写任意第三方平台模板标识,也可以留空。
|
||||
模板可绑定到某个短信通道,绑定后只能在该通道上测试和发送;不绑定则视为全局通用模板。
|
||||
</div>
|
||||
<div id="templateEmptyState" class="empty-state" hidden>暂无短信模板,请先新增。</div>
|
||||
@@ -261,16 +261,16 @@
|
||||
<form id="smsTemplateForm" class="form-grid">
|
||||
<input type="hidden" id="smsTemplateId">
|
||||
<label>
|
||||
<span>模板编码</span>
|
||||
<input id="smsTemplateCode" type="text" maxlength="64" required placeholder="例如:SPUG0001">
|
||||
<span>模板ID(自动生成)</span>
|
||||
<input id="smsTemplateCode" type="text" maxlength="16" required readonly placeholder="保存前自动生成">
|
||||
</label>
|
||||
<label>
|
||||
<span>模板名称</span>
|
||||
<input id="smsTemplateName" type="text" maxlength="100" required placeholder="例如:验证码短信模板">
|
||||
</label>
|
||||
<label>
|
||||
<span>外部模板ID</span>
|
||||
<input id="smsTemplateExternalId" type="text" maxlength="128" placeholder="填写 Spug 模板编号,可留空后续补充">
|
||||
<span>外部模板ID(可选)</span>
|
||||
<input id="smsTemplateExternalId" type="text" maxlength="128" placeholder="填写第三方平台模板标识,可留空">
|
||||
</label>
|
||||
<label>
|
||||
<span>绑定短信通道</span>
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
# 2026-03-23 MessageHub 模板 ID 自动生成
|
||||
|
||||
## 本轮目标
|
||||
|
||||
- 新增短信模板时,不再手工录入模板 ID
|
||||
- 改为系统自动生成 16 位大小写字母模板 ID
|
||||
- 外部模板 ID 保持可选,并把页面文案改成通用第三方模板标识
|
||||
|
||||
## 主要变更
|
||||
|
||||
### 1. 后端生成模板 ID
|
||||
|
||||
- [MessageChannelService.java](/E:/code/xiaowang/MessageHub/src/main/java/com/xiaowang/messagehub/service/channel/MessageChannelService.java)
|
||||
- 新增模板时由服务端自动生成模板 ID
|
||||
- 生成规则:
|
||||
- 长度固定 16 位
|
||||
- 仅使用 `A-Z`、`a-z`
|
||||
- 至少包含一个大写字母和一个小写字母
|
||||
- 会在入库前检查唯一性,避免和现有模板 ID 冲突
|
||||
- 编辑已有模板时保持原模板 ID 不变,不允许被改写
|
||||
|
||||
### 2. 请求约束调整
|
||||
|
||||
- [ChannelSmsTemplateSaveRequest.java](/E:/code/xiaowang/MessageHub/src/main/java/com/xiaowang/messagehub/dto/admin/ChannelSmsTemplateSaveRequest.java)
|
||||
- 取消对 `templateCode` 的必填校验
|
||||
- 模板 ID 改由后端统一生成和维护
|
||||
|
||||
### 3. 后台页面调整
|
||||
|
||||
- [index.html](/E:/code/xiaowang/MessageHub/src/main/resources/static/messagehub-admin/index.html)
|
||||
- 表单字段改成 `模板ID(自动生成)`
|
||||
- 新增表单中模板 ID 输入框改为只读
|
||||
- `外部模板ID` 文案改为:
|
||||
- 可选
|
||||
- 可填写任意第三方平台模板标识
|
||||
- 不再只提示 Spug
|
||||
- [app.js](/E:/code/xiaowang/MessageHub/src/main/resources/static/messagehub-admin/app.js)
|
||||
- 新增模板表单重置时,前端会先预生成一个 16 位大小写字母模板 ID 供展示
|
||||
- 提交时如果页面值为空,也会再次兜底生成
|
||||
- 模板列表和测试发送文案统一改成“模板 ID”
|
||||
|
||||
## 数据库
|
||||
|
||||
- 本轮没有数据库结构变动
|
||||
- 不需要执行新的 SQL
|
||||
|
||||
## 验证
|
||||
|
||||
- `MessageHub` 已通过:
|
||||
- `mvn -q -DskipTests compile`
|
||||
- 后台脚本已通过:
|
||||
- `node --check MessageHub/src/main/resources/static/messagehub-admin/app.js`
|
||||
Reference in New Issue
Block a user