// 用户认证模块 const API_BASE = '/api'; function getToken() { return localStorage.getItem('bazi_token'); } function setToken(token) { localStorage.setItem('bazi_token', token); } function clearToken() { localStorage.removeItem('bazi_token'); localStorage.removeItem('bazi_user'); } function getCurrentUser() { const user = localStorage.getItem('bazi_user'); return user ? JSON.parse(user) : null; } function setCurrentUser(user) { localStorage.setItem('bazi_user', JSON.stringify(user)); } async function apiRequest(url, options = {}) { const token = getToken(); const headers = { 'Content-Type': 'application/json', ...options.headers }; if (token) headers['Authorization'] = 'Bearer ' + token; try { const response = await fetch(API_BASE + url, { ...options, headers }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return await response.json(); } catch (error) { console.error('API请求失败:', url, error); return null; // 返回null,让调用者处理 } } async function register(phone, password) { const data = await apiRequest('/register', { method: 'POST', body: JSON.stringify({ username: phone, password }) }); if (data.code === 200) { // 注册成功,后端返回 {code, message, token, username} setToken(data.token); setCurrentUser({ username: data.username, phone: data.username, vip_status: data.vip_status || 'none', is_vip: data.is_vip || 0 }); return { success: true }; } return { success: false, message: data.message }; } async function login(phone, password) { const data = await apiRequest('/login', { method: 'POST', body: JSON.stringify({ username: phone, password }) }); console.log('登录API返回:', data); // 调试日志 if (data && data.code === 200) { // 后端返回 {code, message, token, username, vip_status, is_vip} setToken(data.token); setCurrentUser({ username: data.username, phone: data.username, vip_status: data.vip_status || 'none', is_vip: data.is_vip || 0 }); return { success: true }; } return { success: false, message: data?.message || '登录失败,请检查网络连接' }; } function logout() { clearToken(); window.location.reload(); } // 更新UI function updateAuthUI() { const user = getCurrentUser(); const menu = document.getElementById('userMenu'); if (user && menu) { const vipBadge = user.is_vip ? '♛ VIP' : ''; menu.innerHTML = `${user.phone}${vipBadge}`; } } // 弹窗控制 let isLoginMode = true; function showAuthModal() { document.getElementById('authModal').classList.add('show'); isLoginMode = true; updateAuthModal(); } function closeAuthModal() { document.getElementById('authModal').classList.remove('show'); } function toggleAuthMode() { isLoginMode = !isLoginMode; updateAuthModal(); } function updateAuthModal() { document.getElementById('authTitle').textContent = isLoginMode ? '用户登录' : '用户注册'; document.getElementById('authBtn').textContent = isLoginMode ? '登录' : '注册'; const switchText = document.getElementById('authSwitchText'); const switchLink = document.getElementById('authSwitchLink'); if (switchText) switchText.textContent = isLoginMode ? '还没有账号?' : '已有账号?'; if (switchLink) switchLink.textContent = isLoginMode ? '立即注册' : '立即登录'; // 注册模式显示确认密码框 const confirmPwd = document.getElementById('authConfirmPassword'); if (confirmPwd) confirmPwd.style.display = isLoginMode ? 'none' : 'block'; // 清空输入框 document.getElementById('authPhone').value = ''; document.getElementById('authPassword').value = ''; if (confirmPwd) confirmPwd.value = ''; } // 表单提交 async function handleAuthSubmit(e) { e.preventDefault(); const phone = document.getElementById('authPhone').value.trim(); const password = document.getElementById('authPassword').value; const confirmPassword = document.getElementById('authConfirmPassword').value; const btn = document.getElementById('authBtn'); btn.textContent = '处理中...'; btn.disabled = true; if (!isLoginMode && password !== confirmPassword) { alert('两次密码输入不一致'); btn.textContent = '注册'; btn.disabled = false; return; } const result = isLoginMode ? await login(phone, password) : await register(phone, password); if (result.success) { alert(isLoginMode ? '登录成功!' : '注册成功!'); closeAuthModal(); updateAuthUI(); } else { alert(result.message || '操作失败'); } btn.textContent = isLoginMode ? '登录' : '注册'; btn.disabled = false; } // 支付 function showPaymentModal() { document.getElementById('paymentModal').classList.add('show'); } function closePaymentModal() { document.getElementById('paymentModal').classList.remove('show'); } async function confirmPay() { if (!getToken()) { alert('请先登录'); closePaymentModal(); showAuthModal(); return; } const order = await apiRequest('/order/create', { method: 'POST', body: JSON.stringify({ product_type: 'ADVANCED', amount: 29.9 }) }); if (order.code === 200) { const pay = await apiRequest('/order/pay', { method: 'POST', body: JSON.stringify({ order_no: order.data.order_no, pay_method: 'wechat' }) }); if (pay.code === 200) { alert('支付成功!'); closePaymentModal(); updateAuthUI(); } else alert('支付失败:' + pay.message); } } // 初始化 document.addEventListener('DOMContentLoaded', () => { updateAuthUI(); const authForm = document.getElementById('authForm'); if (authForm) { authForm.addEventListener('submit', handleAuthSubmit); } });