window.EAR = (() => {
const csrf = document.querySelector('#csrf input[name="__RequestVerificationToken"]')?.value || '';
const escape = value => { const div = document.createElement('div'); div.textContent = String(value ?? ''); return div.innerHTML.replaceAll('"', '"').replaceAll("'", '''); };
const labels = { Dining: '餐饮', Cafe: '咖啡馆', Park: '公园', Bookstore: '书店', Gallery: '展馆', Entertainment: '休闲娱乐' };
const icons = { Dining: '◒', Cafe: '☕', Park: '♧', Bookstore: '▤', Gallery: '◇', Entertainment: '✿' };
let timer;
function toast(text, error = false) {
const node = document.getElementById('toast');
node.textContent = text; node.className = 'show' + (error ? ' error' : '');
clearTimeout(timer); timer = setTimeout(() => { node.className = ''; }, 4200);
}
async function api(url, method = 'GET', data = null, signal = undefined) {
const response = await fetch(url, { method, credentials: 'same-origin', signal,
headers: { 'Accept': 'application/json', ...(method === 'GET' ? {} : { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrf }) },
body: data == null ? undefined : JSON.stringify(data) });
if (response.status === 401) { location.href = '/Account/Login?returnUrl=' + encodeURIComponent(location.pathname); throw new Error('请先登录。'); }
const body = response.status === 204 ? null : await response.json().catch(() => null);
if (!response.ok) {
const error = new Error(body?.errors ? Object.values(body.errors).flat().join(' ') : body?.title || (response.status === 429 ? '操作较频繁,请稍后再试。' : '请求未完成,请稍后重试。'));
error.status = response.status; throw error;
}
return body;
}
function localInput(value = new Date()) { return new Date(new Date(value).getTime() + 8 * 3600000).toISOString().slice(0, 16); }
function timestamp(value) { return new Date(value + ':00+08:00').toISOString(); }
function displayDate(value) { return new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false }).format(new Date(value)); }
function status(form, text, error = false) { const node = form.querySelector('.form-status'); if(node) { node.textContent = text; node.classList.toggle('error', error); } }
function busy(form, value) { form.querySelectorAll('button[type="submit"]').forEach(b => { b.disabled = value; }); }
function ranges(root = document) { root.querySelectorAll('input[type="range"]').forEach(input => { const out = input.parentElement.querySelector('.range-value'); if (out) out.textContent = input.value + ' / 5'; }); }
function price(value) { return value == null ? '人均未知' : value === 0 ? '免费' : '人均 ¥' + Number(value).toFixed(0); }
function distance(value) { return value < 1000 ? Math.round(value) + ' m' : (value / 1000).toFixed(1) + ' km'; }
document.querySelectorAll('.local-now').forEach(x => { x.value = localInput(); });
document.addEventListener('input', e => {
if (e.target.type === 'range') ranges(e.target.parentElement);
if (e.target.tagName === 'TEXTAREA') { const count = e.target.parentElement.querySelector('.char-count'); if(count) count.textContent = e.target.value.length + ' / 5000'; }
});
const today = document.getElementById('today-date');
if(today) today.textContent = new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', month: 'long', day: 'numeric', weekday: 'short' }).format(new Date());
document.addEventListener('click', async e => {
const button = e.target.closest('[data-favorite]');
if(!button) return;
button.disabled = true;
try { await api('/api/favorites', 'POST', { placeId: button.dataset.favorite }); toast('已加入想去清单'); button.textContent = '♥ 已收藏'; }
catch(error) { toast(error.message, true); } finally { button.disabled = false; }
});
ranges();
return { api, toast, escape, labels, icons, localInput, timestamp, displayDate, status, busy, ranges, price, distance };
})();
window.EAR = (() => {
const csrf = document.querySelector('#csrf input[name="__RequestVerificationToken"]')?.value || '';
const escape = value => { const div = document.createElement('div'); div.textContent = String(value ?? ''); return div.innerHTML.replaceAll('"', '"').replaceAll("'", '''); };
const labels = { Dining: '餐饮', Cafe: '咖啡馆', Park: '公园', Bookstore: '书店', Gallery: '展馆', Entertainment: '休闲娱乐' };
const icons = { Dining: '◒', Cafe: '☕', Park: '♧', Bookstore: '▤', Gallery: '◇', Entertainment: '✿' };
let timer;
function toast(text, error = false) {
const node = document.getElementById('toast');
node.textContent = text; node.className = 'show' + (error ? ' error' : '');
clearTimeout(timer); timer = setTimeout(() => { node.className = ''; }, 4200);
}
async function api(url, method = 'GET', data = null, signal = undefined) {
const response = await fetch(url, { method, credentials: 'same-origin', signal,
headers: { 'Accept': 'application/json', ...(method === 'GET' ? {} : { 'Content-Type': 'application/json', 'X-CSRF-TOKEN': csrf }) },
body: data == null ? undefined : JSON.stringify(data) });
if (response.status === 401) { location.href = '/Account/Login?returnUrl=' + encodeURIComponent(location.pathname); throw new Error('请先登录。'); }
const body = response.status === 204 ? null : await response.json().catch(() => null);
if (!response.ok) {
const error = new Error(body?.errors ? Object.values(body.errors).flat().join(' ') : body?.title || (response.status === 429 ? '操作较频繁,请稍后再试。' : '请求未完成,请稍后重试。'));
error.status = response.status; throw error;
}
return body;
}
function localInput(value = new Date()) { return new Date(new Date(value).getTime() + 8 * 3600000).toISOString().slice(0, 16); }
function timestamp(value) { return new Date(value + ':00+08:00').toISOString(); }
function displayDate(value) { return new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', hour12: false }).format(new Date(value)); }
function status(form, text, error = false) { const node = form.querySelector('.form-status'); if(node) { node.textContent = text; node.classList.toggle('error', error); } }
function busy(form, value) { form.querySelectorAll('button[type="submit"]').forEach(b => { b.disabled = value; }); }
function ranges(root = document) { root.querySelectorAll('input[type="range"]').forEach(input => { const out = input.parentElement.querySelector('.range-value'); if (out) out.textContent = input.value + ' / 5'; }); }
function price(value) { return value == null ? '人均未知' : value === 0 ? '免费' : '人均 ¥' + Number(value).toFixed(0); }
function distance(value) { return value < 1000 ? Math.round(value) + ' m' : (value / 1000).toFixed(1) + ' km'; }
document.querySelectorAll('.local-now').forEach(x => { x.value = localInput(); });
document.addEventListener('input', e => {
if (e.target.type === 'range') ranges(e.target.parentElement);
if (e.target.tagName === 'TEXTAREA') { const count = e.target.parentElement.querySelector('.char-count'); if(count) count.textContent = e.target.value.length + ' / 5000'; }
});
const today = document.getElementById('today-date');
if(today) today.textContent = new Intl.DateTimeFormat('zh-CN', { timeZone: 'Asia/Shanghai', month: 'long', day: 'numeric', weekday: 'short' }).format(new Date());
document.addEventListener('click', async e => {
const button = e.target.closest('[data-favorite]');
if(!button) return;
button.disabled = true;
try { await api('/api/favorites', 'POST', { placeId: button.dataset.favorite }); toast('已加入想去清单'); button.textContent = '♥ 已收藏'; }
catch(error) { toast(error.message, true); } finally { button.disabled = false; }
});
ranges();
return { api, toast, escape, labels, icons, localInput, timestamp, displayDate, status, busy, ranges, price, distance };
})();