fix: 更新当前界面,支持多公帐号切换
This commit is contained in:
153
app/static/settings.js
Normal file
153
app/static/settings.js
Normal file
@@ -0,0 +1,153 @@
|
||||
const $ = (id) => document.getElementById(id);
|
||||
|
||||
function setStatus(msg, danger = false) {
|
||||
const el = $("status");
|
||||
if (!el) return;
|
||||
el.style.color = danger ? "#b42318" : "#0f5f3d";
|
||||
el.textContent = msg;
|
||||
}
|
||||
|
||||
function setLoading(button, loading, idleText, loadingText) {
|
||||
if (!button) return;
|
||||
button.disabled = loading;
|
||||
button.textContent = loading ? loadingText : idleText;
|
||||
}
|
||||
|
||||
async function postJSON(url, body) {
|
||||
const res = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.detail || "请求失败");
|
||||
return data;
|
||||
}
|
||||
|
||||
async function authMe() {
|
||||
const res = await fetch("/api/auth/me");
|
||||
const data = await res.json();
|
||||
if (!data.logged_in) {
|
||||
window.location.href = "/auth?next=/settings";
|
||||
return null;
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
function renderAccounts(me) {
|
||||
const sel = $("accountSelect");
|
||||
if (!sel) return;
|
||||
const list = Array.isArray(me.wechat_accounts) ? me.wechat_accounts : [];
|
||||
const active = me.active_wechat_account && me.active_wechat_account.id ? Number(me.active_wechat_account.id) : 0;
|
||||
sel.innerHTML = "";
|
||||
if (!list.length) {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = "";
|
||||
opt.textContent = "暂无公众号,请先绑定";
|
||||
sel.appendChild(opt);
|
||||
return;
|
||||
}
|
||||
list.forEach((a) => {
|
||||
const opt = document.createElement("option");
|
||||
opt.value = String(a.id);
|
||||
opt.textContent = `${a.account_name} (${a.appid})`;
|
||||
if ((active && a.id === active) || a.active) opt.selected = true;
|
||||
sel.appendChild(opt);
|
||||
});
|
||||
}
|
||||
|
||||
async function refresh() {
|
||||
const me = await authMe();
|
||||
if (!me) return;
|
||||
renderAccounts(me);
|
||||
}
|
||||
|
||||
const accountSelect = $("accountSelect");
|
||||
const bindBtn = $("bindBtn");
|
||||
const logoutBtn = $("logoutBtn");
|
||||
const changePwdBtn = $("changePwdBtn");
|
||||
|
||||
if (accountSelect) {
|
||||
accountSelect.addEventListener("change", async () => {
|
||||
const id = Number(accountSelect.value || 0);
|
||||
if (!id) return;
|
||||
try {
|
||||
const out = await postJSON("/api/auth/wechat/switch", { account_id: id });
|
||||
if (!out.ok) {
|
||||
setStatus(out.detail || "切换失败", true);
|
||||
return;
|
||||
}
|
||||
setStatus("已切换当前公众号。");
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
setStatus(e.message || "切换失败", true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (bindBtn) {
|
||||
bindBtn.addEventListener("click", async () => {
|
||||
setLoading(bindBtn, true, "绑定并设为当前账号", "绑定中...");
|
||||
try {
|
||||
const out = await postJSON("/api/auth/wechat/bind", {
|
||||
account_name: ($("accountName") && $("accountName").value.trim()) || "",
|
||||
appid: ($("appid") && $("appid").value.trim()) || "",
|
||||
secret: ($("secret") && $("secret").value.trim()) || "",
|
||||
author: "",
|
||||
thumb_media_id: "",
|
||||
thumb_image_path: "",
|
||||
});
|
||||
if (!out.ok) {
|
||||
setStatus(out.detail || "绑定失败", true);
|
||||
return;
|
||||
}
|
||||
setStatus("公众号绑定成功,已切换为当前账号。");
|
||||
if ($("appid")) $("appid").value = "";
|
||||
if ($("secret")) $("secret").value = "";
|
||||
await refresh();
|
||||
} catch (e) {
|
||||
setStatus(e.message || "绑定失败", true);
|
||||
} finally {
|
||||
setLoading(bindBtn, false, "绑定并设为当前账号", "绑定中...");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (logoutBtn) {
|
||||
logoutBtn.addEventListener("click", async () => {
|
||||
setLoading(logoutBtn, true, "退出登录", "退出中...");
|
||||
try {
|
||||
await postJSON("/api/auth/logout", {});
|
||||
window.location.href = "/auth?next=/";
|
||||
} catch (e) {
|
||||
setStatus(e.message || "退出失败", true);
|
||||
} finally {
|
||||
setLoading(logoutBtn, false, "退出登录", "退出中...");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (changePwdBtn) {
|
||||
changePwdBtn.addEventListener("click", async () => {
|
||||
setLoading(changePwdBtn, true, "修改密码", "提交中...");
|
||||
try {
|
||||
const out = await postJSON("/api/auth/password/change", {
|
||||
old_password: ($("oldPassword") && $("oldPassword").value) || "",
|
||||
new_password: ($("newPassword") && $("newPassword").value) || "",
|
||||
});
|
||||
if (!out.ok) {
|
||||
setStatus(out.detail || "修改密码失败", true);
|
||||
return;
|
||||
}
|
||||
setStatus("密码修改成功。");
|
||||
if ($("oldPassword")) $("oldPassword").value = "";
|
||||
if ($("newPassword")) $("newPassword").value = "";
|
||||
} catch (e) {
|
||||
setStatus(e.message || "修改密码失败", true);
|
||||
} finally {
|
||||
setLoading(changePwdBtn, false, "修改密码", "提交中...");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
refresh();
|
||||
Reference in New Issue
Block a user