This commit is contained in:
Daniel
2026-04-01 18:49:09 +08:00
parent 124a5f0192
commit babf24a0b0
7 changed files with 533 additions and 50 deletions

View File

@@ -1,6 +1,25 @@
const $ = (id) => document.getElementById(id);
const statusEl = $("status");
const rewriteBtn = $("rewriteBtn");
const wechatBtn = $("wechatBtn");
const imBtn = $("imBtn");
function countText(v) {
return (v || "").trim().length;
}
function updateCounters() {
$("sourceCount").textContent = `${countText($("sourceText").value)}`;
$("summaryCount").textContent = `${countText($("summary").value)}`;
$("bodyCount").textContent = `${countText($("body").value)}`;
}
function setLoading(button, loading, idleText, loadingText) {
if (!button) return;
button.disabled = loading;
button.textContent = loading ? loadingText : idleText;
}
function setStatus(msg, danger = false) {
statusEl.style.color = danger ? "#b42318" : "#0f5f3d";
@@ -26,6 +45,7 @@ $("rewriteBtn").addEventListener("click", async () => {
}
setStatus("AI 改写中...");
setLoading(rewriteBtn, true, "AI 改写并排版", "AI 改写中...");
try {
const data = await postJSON("/api/rewrite", {
source_text: sourceText,
@@ -38,14 +58,23 @@ $("rewriteBtn").addEventListener("click", async () => {
$("title").value = data.title || "";
$("summary").value = data.summary || "";
$("body").value = data.body_markdown || "";
setStatus("改写完成,可直接发布。");
updateCounters();
if (data.mode === "fallback") {
const note = (data.quality_notes || [])[0] || "当前为保底改写稿";
setStatus(`改写完成(保底模式):${note}`, true);
} else {
setStatus("改写完成,可直接发布。");
}
} catch (e) {
setStatus(`改写失败: ${e.message}`, true);
} finally {
setLoading(rewriteBtn, false, "AI 改写并排版", "AI 改写中...");
}
});
$("wechatBtn").addEventListener("click", async () => {
setStatus("正在发布到公众号草稿箱...");
setLoading(wechatBtn, true, "发布到公众号草稿箱", "发布中...");
try {
const data = await postJSON("/api/publish/wechat", {
title: $("title").value,
@@ -56,11 +85,14 @@ $("wechatBtn").addEventListener("click", async () => {
setStatus("公众号草稿发布成功");
} catch (e) {
setStatus(`公众号发布失败: ${e.message}`, true);
} finally {
setLoading(wechatBtn, false, "发布到公众号草稿箱", "发布中...");
}
});
$("imBtn").addEventListener("click", async () => {
setStatus("正在发送到 IM...");
setLoading(imBtn, true, "发送到 IM", "发送中...");
try {
const data = await postJSON("/api/publish/im", {
title: $("title").value,
@@ -70,5 +102,13 @@ $("imBtn").addEventListener("click", async () => {
setStatus("IM 发送成功");
} catch (e) {
setStatus(`IM 发送失败: ${e.message}`, true);
} finally {
setLoading(imBtn, false, "发送到 IM", "发送中...");
}
});
["sourceText", "summary", "body"].forEach((id) => {
$(id).addEventListener("input", updateCounters);
});
updateCounters();