CSAPP/src/index.ts

132 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-04-30 00:05:32 +08:00
// Webview元素接口
interface WebviewElement extends HTMLElement {
src: string;
addEventListener(event: string, listener: (event: any) => void): void;
}
// 菜单项接口
interface MenuItem {
ShowName: string;
Url: string;
Icon: string;
Children: MenuItem[] | null;
}
2025-04-29 18:36:29 +08:00
2025-04-30 00:05:32 +08:00
// API响应接口
interface ApiResponse<T> {
status: number;
code: number;
msg: string;
data: T;
}
2025-04-29 18:36:29 +08:00
// 检查登录状态
function checkLoginStatus() {
const auth = document.cookie.split('; ').find(row => row.startsWith('EACToken=') || row.startsWith('MssSsoToken='));
if (!auth) {
window.location.href = 'login.html';
return;
}
2025-04-30 00:05:32 +08:00
2025-04-29 18:36:29 +08:00
// 显示用户信息
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';
}
2025-04-30 00:05:32 +08:00
// 获取菜单列表
async function getMenuList(): Promise<MenuItem[]> {
try {
const response = await fetch('http://1.12.73.211:8848/EIAC_Desktop_Api/api/Menu/GetMenu', {
method: 'POST'
2025-04-29 18:36:29 +08:00
});
2025-04-30 00:05:32 +08:00
const result = await response.json() as ApiResponse<MenuItem[]>;
if (result.status === 0) {
return result.data;
} else {
throw new Error(result.msg || '获取菜单失败');
2025-04-29 18:36:29 +08:00
}
2025-04-30 00:05:32 +08:00
} catch (error) {
console.error('获取菜单失败:', error);
throw error;
2025-04-29 18:36:29 +08:00
}
}
2025-04-30 00:05:32 +08:00
// 创建菜单项
function createMenuItem(item: MenuItem): HTMLLIElement {
const li = document.createElement('li');
li.className = 'menu-item';
2025-04-29 18:36:29 +08:00
2025-04-30 00:05:32 +08:00
const icon = document.createElement('img');
//icon.src = `./assets/${item.Icon}`;
icon.src = `./assets/list.png`;
icon.alt = item.ShowName;
icon.className = 'menu-icon';
const span = document.createElement('span');
span.textContent = item.ShowName;
li.appendChild(icon);
li.appendChild(span);
if (item.Url) {
li.addEventListener('click', () => {
(document.querySelector("webview") as WebviewElement).src = item.Url;
//(document.querySelector("webview") as WebviewElement).src = "https://www.baidu.com";
});
2025-04-29 18:36:29 +08:00
}
2025-04-30 00:05:32 +08:00
return li;
}
// 渲染菜单
function renderMenu(menuList: MenuItem[]) {
const menuContainer = document.getElementById('menuList');
if (!menuContainer) return;
menuList.forEach(item => {
const menuItem = createMenuItem(item);
menuContainer.appendChild(menuItem);
if (item.Children) {
const subMenu = document.createElement('ul');
subMenu.className = 'submenu';
item.Children.forEach(child => {
const childItem = createMenuItem(child);
subMenu.appendChild(childItem);
});
menuContainer.appendChild(subMenu);
}
});
2025-04-29 18:36:29 +08:00
}
// 初始化
2025-04-30 00:05:32 +08:00
async function initialize() {
2025-04-29 18:36:29 +08:00
// 检查登录状态
checkLoginStatus();
2025-04-30 00:05:32 +08:00
try {
const menuList = await getMenuList();
renderMenu(menuList);
// 绑定退出登录事件
const logoutBtn = document.getElementById('btnLogout');
if (logoutBtn) {
logoutBtn.addEventListener('click', handleLogout);
}
} catch (error) {
console.error('初始化失败:', error);
2025-04-29 18:36:29 +08:00
}
}
2025-04-30 00:05:32 +08:00
// 页面加载完成后初始化
2025-04-29 18:36:29 +08:00
document.addEventListener('DOMContentLoaded', initialize);