// static/js/web-components.js
// Web工作台公共组件(头部、底部)
// 渲染工作台头部
function renderWebHeader(activeNav) {
const logoPath = (typeof SITE_CONFIG !== 'undefined') ? SITE_CONFIG.path : '/static/images/logo.png';
const siteName = (typeof SITE_CONFIG !== 'undefined') ? SITE_CONFIG.name : '简记';
const navItems = [
{ path: '/web/calendar', label: '日程', id: 'calendar' },
{ path: '/web/growth', label: '成长中心', id: 'growth' },
{ path: '/web/profile', label: '个人中心', id: 'profile' }
];
let navHtml = '';
navItems.forEach(item => {
const isActive = item.id === activeNav;
navHtml += `${item.label}`;
});
const nickname = localStorage.getItem('user_nickname') || '用户';
const avatar = localStorage.getItem('user_avatar');
const avatarHtml = avatar
? `
`
: nickname.charAt(0).toUpperCase();
return `
`;
}
// 渲染工作台底部
function renderWebFooter() {
return `
`;
}
// 退出登录
function handleWebLogout() {
if (typeof $toast !== 'undefined' && $toast.confirm) {
$toast.confirm('确定退出登录?').then(function(confirmed) {
if (confirmed) {
doLogout();
}
});
} else {
if (confirm('确定退出登录?')) {
doLogout();
}
}
}
function doLogout() {
localStorage.clear();
window.location.href = '/login';
}
// 检查登录状态
function checkWebLogin() {
const token = localStorage.getItem('user_token');
if (!token) {
window.location.href = '/login';
return false;
}
// 检查token是否过期
const expiresAt = localStorage.getItem('user_token_expires');
if (expiresAt && Date.now() > parseInt(expiresAt)) {
localStorage.clear();
window.location.href = '/login';
return false;
}
return true;
}
// 初始化Web页面
function initWebPage(activeNav) {
if (!checkWebLogin()) return;
// 插入头部
const headerContainer = document.getElementById('webHeader');
if (headerContainer) {
headerContainer.innerHTML = renderWebHeader(activeNav);
}
// 插入底部
const footerContainer = document.getElementById('webFooter');
if (footerContainer) {
footerContainer.innerHTML = renderWebFooter();
}
}