class SystemLog extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
}
connectedCallback() {
this.render();
this.addEventListeners();
this.loadLogs();
}
render() {
this.shadowRoot.innerHTML = `
`;
}
addEventListeners() {
// 搜索框事件
const searchInput = this.shadowRoot.querySelector('.search-box input');
searchInput.addEventListener('input', this.debounce(() => {
this.filterLogs();
}, 300));
// 级别筛选事件
const levelFilter = this.shadowRoot.querySelector('#levelFilter');
levelFilter.addEventListener('change', () => {
this.filterLogs();
});
// 时间筛选事件
const timeFilter = this.shadowRoot.querySelector('#timeFilter');
timeFilter.addEventListener('change', () => {
this.filterLogs();
});
// 刷新按钮事件
const refreshBtn = this.shadowRoot.querySelector('#refreshBtn');
refreshBtn.addEventListener('click', () => {
this.loadLogs();
});
// 分页按钮事件
const prevPage = this.shadowRoot.querySelector('#prevPage');
const nextPage = this.shadowRoot.querySelector('#nextPage');
prevPage.addEventListener('click', () => {
this.changePage(-1);
});
nextPage.addEventListener('click', () => {
this.changePage(1);
});
}
async loadLogs() {
try {
const response = await fetch('/api/system-log/logs');
if (!response.ok) {
throw new Error('网络响应失败');
}
const logs = await response.json();
this.renderLogs(logs);
} catch (error) {
console.error('加载日志失败:', error);
}
}
renderLogs(logs) {
const logList = this.shadowRoot.querySelector('#logList');
logList.innerHTML = logs.map(log => `
${log.time || ''}
${this.getLevelText(log.level)}
${log.source || ''}
${log.user || ''}
${log.log || ''}
`).join('');
}
getLevelText(level) {
const levelMap = {
'info': '信息',
'warning': '警告',
'error': '错误',
'debug': '调试'
};
return levelMap[level] || level;
}
filterLogs() {
const searchText = this.shadowRoot.querySelector('.search-box input').value.toLowerCase();
const levelFilter = this.shadowRoot.querySelector('#levelFilter').value;
const timeFilter = this.shadowRoot.querySelector('#timeFilter').value;
// 这里应该根据筛选条件调用后端API获取过滤后的日志
this.loadLogs();
}
changePage(delta) {
// 实现分页功能
console.log('切换页面:', delta);
}
debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
}
customElements.define('system-log', SystemLog);