CSAPP/src/index.ts

101 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-04-29 18:36:29 +08:00
console.log('👋 This message is being logged by "index.ts", included via Vite');
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
// 检查登录状态
function checkLoginStatus() {
const auth = document.cookie.split('; ').find(row => row.startsWith('EACToken=') || row.startsWith('MssSsoToken='));
if (!auth) {
window.location.href = 'login.html';
return;
}
// 显示用户信息
const userInfo = document.getElementById('userInfo');
if (userInfo) {
userInfo.textContent = '欢迎使用';
}
}
// 处理退出登录
function handleLogout() {
document.cookie = 'expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;';
window.location.href = 'login.html';
}
// 处理菜单切换
function handleMenuClick(event: Event) {
const target = event.target as HTMLElement;
const menuItem = target.closest('.menu-item');
if (menuItem) {
// 移除所有菜单项的active类
document.querySelectorAll('.menu-item').forEach(item => {
item.classList.remove('active');
});
// 添加active类到当前点击的菜单项
menuItem.classList.add('active');
// 获取页面标识
const page = menuItem.getAttribute('data-page');
if (page) {
loadPageContent(page);
}
}
}
// 加载页面内容
async function loadPageContent(page: string) {
const contentArea = document.getElementById('pageContent');
if (!contentArea) return;
try {
// 这里可以根据不同的页面加载不同的内容
switch (page) {
case 'dashboard':
contentArea.innerHTML = '<h2>仪表盘</h2><p>欢迎使用中国电信桌面应用</p>';
break;
case 'tasks':
contentArea.innerHTML = '<h2>任务管理</h2><p>任务管理功能开发中...</p>';
break;
case 'reports':
contentArea.innerHTML = '<h2>报表统计</h2><p>报表统计功能开发中...</p>';
break;
case 'settings':
contentArea.innerHTML = '<h2>系统设置</h2><p>系统设置功能开发中...</p>';
break;
default:
contentArea.innerHTML = '<h2>页面不存在</h2>';
}
} catch (error) {
console.error('加载页面内容失败:', error);
contentArea.innerHTML = '<h2>加载失败</h2><p>请稍后重试</p>';
}
}
// 初始化
function initialize() {
// 检查登录状态
checkLoginStatus();
// 绑定退出登录事件
const logoutBtn = document.getElementById('btnLogout');
if (logoutBtn) {
logoutBtn.addEventListener('click', handleLogout);
}
// 绑定菜单点击事件
const menuList = document.querySelector('.menu-list');
if (menuList) {
menuList.addEventListener('click', handleMenuClick);
}
// 加载默认页面(仪表盘)
loadPageContent('dashboard');
}
// 当DOM加载完成后初始化
document.addEventListener('DOMContentLoaded', initialize);