98 lines
3.0 KiB
JavaScript
98 lines
3.0 KiB
JavaScript
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=/profile";
|
|
return null;
|
|
}
|
|
return data;
|
|
}
|
|
|
|
async function loadProfile() {
|
|
const res = await fetch("/api/profile");
|
|
const data = await res.json();
|
|
if (!res.ok || !data.ok) throw new Error(data.detail || "个人信息加载失败");
|
|
const profile = data.profile || {};
|
|
if ($("profileSubscriberName")) $("profileSubscriberName").value = profile.subscriber_name || "";
|
|
if ($("profileSubscriberPhone")) $("profileSubscriberPhone").value = profile.subscriber_phone || "";
|
|
if ($("profileShippingAddress")) $("profileShippingAddress").value = profile.shipping_address || "";
|
|
}
|
|
|
|
const saveProfileBtn = $("saveProfileBtn");
|
|
const logoutBtn = $("logoutBtn");
|
|
|
|
if (saveProfileBtn) {
|
|
saveProfileBtn.addEventListener("click", async () => {
|
|
setLoading(saveProfileBtn, true, "保存个人信息", "保存中...");
|
|
try {
|
|
const subscriberName = (($("profileSubscriberName") && $("profileSubscriberName").value) || "").trim();
|
|
const subscriberPhone = (($("profileSubscriberPhone") && $("profileSubscriberPhone").value) || "").trim();
|
|
const shippingAddress = (($("profileShippingAddress") && $("profileShippingAddress").value) || "").trim();
|
|
if (!subscriberName) {
|
|
setStatus("请填写订阅人姓名", true);
|
|
return;
|
|
}
|
|
if (!shippingAddress) {
|
|
setStatus("请填写收货地址", true);
|
|
return;
|
|
}
|
|
const out = await postJSON("/api/profile", {
|
|
subscriber_name: subscriberName,
|
|
subscriber_phone: subscriberPhone,
|
|
shipping_address: shippingAddress,
|
|
});
|
|
if (!out.ok) {
|
|
setStatus(out.detail || "保存失败", true);
|
|
return;
|
|
}
|
|
setStatus("个人信息已保存。");
|
|
} catch (e) {
|
|
setStatus(e.message || "保存失败", true);
|
|
} finally {
|
|
setLoading(saveProfileBtn, false, "保存个人信息", "保存中...");
|
|
}
|
|
});
|
|
}
|
|
|
|
if (logoutBtn) {
|
|
logoutBtn.addEventListener("click", async () => {
|
|
try {
|
|
await postJSON("/api/auth/logout", {});
|
|
} finally {
|
|
window.location.href = "/auth?next=/";
|
|
}
|
|
});
|
|
}
|
|
|
|
(async () => {
|
|
const me = await authMe();
|
|
if (!me) return;
|
|
await loadProfile();
|
|
})();
|