(function() { // 获取当前页面路径 var currentPath = window.location.pathname; // 使用CDN路径 var imgPath = 'https://pps.dingdanxt.cn/xinzhiyu/Aiimg/img/a2.png'; // 添加现代化样式 var styles = document.createElement('style'); styles.textContent = ` /* 头像呼吸动画 */ .avatar-breathe { animation: avatarBreathe 3s ease-in-out infinite; } @keyframes avatarBreathe { 0%, 100% { transform: scale(1); box-shadow: 0 2px 6px rgba(255, 255, 255, 0.2); } 50% { transform: scale(1.02); box-shadow: 0 3px 8px rgba(255, 255, 255, 0.35); } } /* 下拉按钮呼吸动画 */ .dropdown-breathe { animation: dropdownBreathe 3s ease-in-out infinite; } @keyframes dropdownBreathe { 0%, 100% { background: rgba(255, 255, 255, 0.9); box-shadow: 0 2px 6px rgba(0, 0, 0, 0.1); transform: scale(1); } 50% { background: rgba(255, 255, 255, 0.95); box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12); transform: scale(1.02); } } /* 悬停时停止动画 */ .avatar-breathe:hover, .dropdown-breathe:hover { animation: none !important; } /* 下拉菜单样式 */ .user-dropdown-menu { display: none; position: absolute; right: -8px; top: calc(100% + 8px); background: white; border-radius: 8px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.12); min-width: 160px; z-index: 1000; border: 1px solid rgba(0, 0, 0, 0.08); overflow: hidden; transform: translateY(-8px); opacity: 0; transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); } .user-dropdown-menu.show { display: block; transform: translateY(0); opacity: 1; } .user-dropdown-header { padding: 12px 16px; border-bottom: 1px solid #eee; background: linear-gradient(135deg, #f8f9fa, #e9ecef); } .user-dropdown-item { padding: 10px 16px; display: block; color: #333; text-decoration: none; transition: all 0.2s ease; font-size: 13px; border: none; background: none; width: 100%; text-align: left; cursor: pointer; } .user-dropdown-item:hover { background: #f8f9fa; color: #333; text-decoration: none; } .user-dropdown-divider { height: 1px; background: #eee; margin: 8px 0; } .user-dropdown-icon { width: 16px; margin-right: 10px; display: inline-block; text-align: center; } /* SVG图标动画 */ .dropdown-arrow { transition: transform 0.3s ease; } .dropdown-arrow.rotated { transform: rotate(180deg); } `; document.head.appendChild(styles); // 创建容器元素,稍后通过JavaScript动态更新 document.write("
"); // 页面加载完成后检查登录状态 document.addEventListener('DOMContentLoaded', function() { updateHeaderLoginStatus(); // 添加全局点击事件,点击外部区域关闭下拉菜单 document.addEventListener('click', function(e) { var dropdown = document.getElementById('userDropdownMenu'); var container = document.getElementById('headerLoginArea'); if (dropdown && container && !container.contains(e.target)) { hideUserDropdown(); } }); }); function updateHeaderLoginStatus() { // 检查登录状态 var userid = getCookie('adyrxmluserid') || getCookie('ecmsmluserid'); if (userid) { // 从API获取用户信息(昵称从数据库读取,避免编码问题) fetchHeaderUserInfo(userid); } // 未登录状态保持原有显示 } // XSS防护:HTML转义函数 function escapeHtml(text) { var div = document.createElement('div'); div.textContent = text; return div.innerHTML; } function fetchHeaderUserInfo(userid) { // 计算相对路径 var avatarPath; var basePath; if (currentPath.includes('/zhuanyeyinzhang/')) { avatarPath = '../../e/data/images/01.png'; basePath = '../../'; } else if (currentPath.includes('/aikoutu/')) { avatarPath = '../e/data/images/01.png'; basePath = '../'; } else { avatarPath = 'e/data/images/01.png'; basePath = ''; } // 从API获取用户昵称 fetch(basePath + 'backend/api/simple-user-api.php?userid=' + userid) .then(function(response) { return response.json(); }) .then(function(data) { if (data.success && data.user) { renderUserHeader(data.user.nickname || '用户', avatarPath, basePath); // 更新price.html页面的到期时间显示 updatePricePageExpiryTime(data.user); } else { // API失败时用cookie中的昵称作为备用 var username = getCookie('adyrxmlusername') || '用户'; renderUserHeader(username, avatarPath, basePath); } }) .catch(function() { var username = getCookie('adyrxmlusername') || '用户'; renderUserHeader(username, avatarPath, basePath); }); } // 更新price.html页面的到期时间显示 function updatePricePageExpiryTime(user) { var expiryElement = document.getElementById('dqsj'); if (!expiryElement) return; // 不是price.html页面 if (user.vip_level === 3) { // 终身VIP expiryElement.textContent = '永久有效'; } else if (user.vip_level > 0 && user.vip_end_time) { // 有VIP且有到期时间 try { var endDate = new Date(user.vip_end_time); var options = { year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', timeZone: 'Asia/Shanghai' }; var formattedTime = endDate.toLocaleString('zh-CN', options); expiryElement.textContent = formattedTime; } catch (e) { expiryElement.textContent = user.vip_end_time; } } else { // 普通用户或无VIP expiryElement.textContent = '未开通会员'; } } function renderUserHeader(username, avatarPath, basePath) { var safeUsername = escapeHtml(username); var headerArea = document.getElementById('headerLoginArea'); if (headerArea) { headerArea.innerHTML = `
用户头像
${safeUsername}
点击查看更多功能
👤 个人中心 👑 开通会员
`; } } // 跳转到用户中心 function goToUserCenter() { var basePath; if (currentPath.includes('/zhuanyeyinzhang/')) { basePath = '../../'; } else if (currentPath.includes('/aikoutu/')) { basePath = '../'; } else { basePath = ''; } window.location.href = basePath + 'user-center.html'; } // 切换下拉菜单 function toggleUserDropdown() { var menu = document.getElementById('userDropdownMenu'); var arrow = document.getElementById('dropdownArrow'); if (menu && arrow) { var isVisible = menu.classList.contains('show'); if (isVisible) { hideUserDropdown(); } else { showUserDropdown(); } } } // 显示下拉菜单 function showUserDropdown() { var menu = document.getElementById('userDropdownMenu'); var arrow = document.getElementById('dropdownArrow'); if (menu && arrow) { menu.classList.add('show'); arrow.classList.add('rotated'); } } // 隐藏下拉菜单 function hideUserDropdown() { var menu = document.getElementById('userDropdownMenu'); var arrow = document.getElementById('dropdownArrow'); if (menu && arrow) { menu.classList.remove('show'); arrow.classList.remove('rotated'); } } // 执行退出登录 function performLogout() { if (confirm('确定要退出登录吗?')) { // 清除cookie document.cookie = 'adyrxmlusername=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; document.cookie = 'adyrxmluserid=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; document.cookie = 'ecmsmlusername=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; document.cookie = 'ecmsmluserid=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; document.cookie = 'user_login_data=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;'; // 刷新页面 window.location.reload(); } } function getCookie(name) { var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); if (arr = document.cookie.match(reg)) { var value = arr[2]; // 循环解码直到值不再变化(处理多重编码) try { var decoded = value; var prev = ''; while (decoded !== prev && decoded.indexOf('%') !== -1) { prev = decoded; decoded = decodeURIComponent(decoded); } return decoded; } catch (e) { return value; } } return null; } // 暴露函数到全局作用域 window.goToUserCenter = goToUserCenter; window.toggleUserDropdown = toggleUserDropdown; window.showUserDropdown = showUserDropdown; window.hideUserDropdown = hideUserDropdown; window.performLogout = performLogout; })();