feat: add /admin/templates/preview endpoint; add preview button to template editor

This commit is contained in:
auto-bot
2025-12-24 10:58:43 +08:00
parent 74b8b8e8ed
commit 0def77dc30
2 changed files with 64 additions and 1 deletions
+37
View File
@@ -67,9 +67,14 @@
<label class="form-label">模板内容</label>
<textarea class="form-control" name="template_content" id="templateContent" rows="3" required placeholder="收到{trans_amt}元"></textarea>
</div>
<div class="mb-3">
<label class="form-label">示例 Payload (JSON, 可选,用于预览)</label>
<textarea class="form-control font-monospace" id="templateSample" rows="4" placeholder='{"trans_amt": 100, "trans_order_info": {"remark": "abc"}}'></textarea>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
<button type="button" class="btn btn-outline-secondary" onclick="previewTemplate()">预览</button>
<button type="submit" class="btn btn-primary">保存</button>
</div>
</div>
@@ -88,6 +93,7 @@
document.getElementById('templateId').value = "";
document.getElementById('templateName').value = "";
document.getElementById('templateContent').value = "";
document.getElementById('templateSample').value = '';
modal.show();
}
@@ -97,7 +103,38 @@
document.getElementById('templateId').value = id;
document.getElementById('templateName').value = name;
document.getElementById('templateContent').value = content;
document.getElementById('templateSample').value = '';
modal.show();
}
async function previewTemplate() {
const content = document.getElementById('templateContent').value;
let sample = {};
try {
const sampleText = document.getElementById('templateSample').value;
if (sampleText && sampleText.trim()) {
sample = JSON.parse(sampleText);
}
} catch (e) {
alert('示例 payload JSON 格式错误: ' + e);
return;
}
try {
const res = await fetch('/admin/templates/preview', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ template_content: content, sample_payload: sample })
});
const data = await res.json();
if (res.ok) {
alert('渲染结果:\\n' + data.rendered);
} else {
alert('预览失败: ' + (data.error || '未知错误'));
}
} catch (e) {
alert('请求错误: ' + e);
}
}
</script>
{% endblock %}