63 lines
2.4 KiB
JavaScript
63 lines
2.4 KiB
JavaScript
// 简易同步器:与 IIS API 交互
|
|
const SyncAPI = (function () {
|
|
const base = AppConfig.API_BASE_URL; // 例如 /Api/Events.ashx
|
|
|
|
async function listByMonth(year, month) {
|
|
await (window.Auth && Auth.ensure ? Auth.ensure() : Promise.resolve());
|
|
try {
|
|
if (window.UI && UI.appendGlobalModalLog) UI.appendGlobalModalLog('开始调用 listByMonth');
|
|
} catch (_) {}
|
|
const url = `${base}?year=${year}&month=${month}&_ts=${Date.now()}`;
|
|
const resp = await fetch(url, { cache: 'no-store', credentials: 'include' });
|
|
const j = await resp.json();
|
|
if (!j.ok) throw new Error(j.error || 'listByMonth failed');
|
|
try {
|
|
const count = Array.isArray(j.data) ? j.data.length : 0;
|
|
if (window.UI && UI.appendGlobalModalLog) UI.appendGlobalModalLog('listByMonth 返回成功,数量=' + count);
|
|
} catch (_) {}
|
|
return j.data || [];
|
|
}
|
|
|
|
async function upsertEvent(ev) {
|
|
await (window.Auth && Auth.ensure ? Auth.ensure() : Promise.resolve());
|
|
try {
|
|
if (window.UI && UI.appendGlobalModalLog) UI.appendGlobalModalLog('开始调用 upsertEvent');
|
|
} catch (_) {}
|
|
const resp = await fetch(base, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'text/plain' },
|
|
credentials: 'include',
|
|
body: JSON.stringify(ev),
|
|
});
|
|
const j = await resp.json();
|
|
if (!j.ok) throw new Error(j.error || 'upsert failed');
|
|
try {
|
|
if (window.UI && UI.appendGlobalModalLog) UI.appendGlobalModalLog('upsertEvent 返回成功');
|
|
} catch (_) {}
|
|
}
|
|
|
|
async function deleteEvent(id) {
|
|
await (window.Auth && Auth.ensure ? Auth.ensure() : Promise.resolve());
|
|
const url = `${base}?id=${encodeURIComponent(id)}`;
|
|
try {
|
|
const resp = await fetch(url, { method: 'DELETE', credentials: 'include' });
|
|
if (resp.ok) {
|
|
const j = await resp.json();
|
|
if (!j.ok) throw new Error(j.error || 'delete failed');
|
|
return;
|
|
}
|
|
if (resp.status !== 405) {
|
|
const t = await resp.text();
|
|
throw new Error(t || 'delete failed');
|
|
}
|
|
} catch (e) {}
|
|
const resp2 = await fetch(`${url}&_method=DELETE`, { method: 'POST', headers: { 'Content-Type': 'text/plain' }, credentials: 'include', body: '' });
|
|
const j2 = await resp2.json();
|
|
if (!j2.ok) throw new Error(j2.error || 'delete failed');
|
|
}
|
|
|
|
return { listByMonth, upsertEvent, deleteEvent };
|
|
})();
|
|
|
|
window.SyncAPI = SyncAPI;
|