// 主入口:串联 UI、认证、地图与 AI (function () { const $ = sel => document.querySelector(sel); // ================== 用户界面 ================== function renderUserArea() { const user = window.Auth.currentUser(); const area = $('#userArea'); if (user) { area.innerHTML = `👤 ${user}` + ``; $('#logoutBtn').addEventListener('click', () => { window.Auth.logout(); renderUserArea(); loadSpots(); }); } else { area.innerHTML = ``; $('#loginBtn').addEventListener('click', openModal); } } // ================== 弹窗 ================== function openModal() { $('#loginModal').classList.remove('hidden'); } function closeModal() { $('#loginModal').classList.add('hidden'); $('#usernameInput').value = ''; $('#passwordInput').value = ''; } function bindModal() { $('#closeModal').addEventListener('click', closeModal); $('#doLogin').addEventListener('click', () => { const u = $('#usernameInput').value.trim(); const p = $('#passwordInput').value; const res = window.Auth.login(u, p); if (!res.ok) return alert(res.msg); closeModal(); renderUserArea(); loadSpots(); }); $('#doRegister').addEventListener('click', () => { const u = $('#usernameInput').value.trim(); const p = $('#passwordInput').value; const res = window.Auth.register(u, p); if (!res.ok) return alert(res.msg); closeModal(); renderUserArea(); loadSpots(); }); } // ================== 景点数据存储 ================== function loadSpots() { const key = window.Auth.dataKey('spots'); if (!key) { // 未登录:清空地图 window.MapAPI.setSpots([]); renderSpotList([]); return; } let spots = []; try { spots = JSON.parse(localStorage.getItem(key)) || []; } catch (e) { spots = []; } window.MapAPI.setSpots(spots); renderSpotList(spots); } function saveSpots(spots) { const key = window.Auth.dataKey('spots'); if (!key) return; localStorage.setItem(key, JSON.stringify(spots)); } // 当 MapAPI 内部数据变化时回调 window.MapAPI.onSpotsChange = function (spots) { saveSpots(spots); renderSpotList(spots); }; // ================== 景点列表渲染 ================== let currentFilter = 'all'; function renderSpotList(spots) { const list = $('#spotList'); list.innerHTML = ''; const filtered = spots.filter(s => { if (currentFilter === 'all') return true; return s.status === currentFilter; }); if (filtered.length === 0) { list.innerHTML = '