feat(webui): 新增webui和状态查看接口
This commit is contained in:
@@ -0,0 +1,194 @@
|
||||
* {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
min-height: 100vh;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.container {
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
header {
|
||||
text-align: center;
|
||||
color: white;
|
||||
margin-bottom: 30px;
|
||||
}
|
||||
|
||||
header h1 {
|
||||
font-size: 2.5rem;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
header p {
|
||||
font-size: 1.2rem;
|
||||
opacity: 0.9;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: rgba(255, 255, 255, 0.95);
|
||||
border-radius: 15px;
|
||||
padding: 25px;
|
||||
margin-bottom: 25px;
|
||||
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
|
||||
backdrop-filter: blur(10px);
|
||||
}
|
||||
|
||||
.card h2 {
|
||||
color: #333;
|
||||
margin-bottom: 20px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.form-group {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 15px;
|
||||
}
|
||||
|
||||
.form-group label {
|
||||
font-weight: 600;
|
||||
color: #555;
|
||||
}
|
||||
|
||||
.form-group input {
|
||||
padding: 12px 15px;
|
||||
border: 2px solid #ddd;
|
||||
border-radius: 8px;
|
||||
font-size: 16px;
|
||||
transition: border-color 0.3s;
|
||||
}
|
||||
|
||||
.form-group input:focus {
|
||||
outline: none;
|
||||
border-color: #667eea;
|
||||
}
|
||||
|
||||
.btn-primary, .btn-secondary, .btn-danger {
|
||||
padding: 12px 20px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
font-size: 16px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-danger {
|
||||
background: linear-gradient(135deg, #ff6b6b 0%, #ee5a24 100%);
|
||||
color: white;
|
||||
padding: 15px 30px;
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.btn-primary:hover, .btn-secondary:hover, .btn-danger:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
|
||||
.status-container {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.timer-display {
|
||||
font-size: 3rem;
|
||||
font-weight: 700;
|
||||
color: #333;
|
||||
margin: 20px 0;
|
||||
font-family: 'Courier New', monospace;
|
||||
}
|
||||
|
||||
.progress-bar {
|
||||
width: 100%;
|
||||
height: 20px;
|
||||
background-color: #eee;
|
||||
border-radius: 10px;
|
||||
overflow: hidden;
|
||||
margin: 20px 0;
|
||||
}
|
||||
|
||||
.progress-fill {
|
||||
height: 100%;
|
||||
background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
|
||||
width: 0%;
|
||||
transition: width 0.5s ease;
|
||||
}
|
||||
|
||||
.controls {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.hint {
|
||||
color: #666;
|
||||
margin-top: 15px;
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
footer {
|
||||
text-align: center;
|
||||
color: white;
|
||||
margin-top: 30px;
|
||||
opacity: 0.8;
|
||||
}
|
||||
|
||||
.notification {
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
padding: 15px 25px;
|
||||
border-radius: 8px;
|
||||
color: white;
|
||||
font-weight: 600;
|
||||
transform: translateX(400px);
|
||||
transition: transform 0.3s ease;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.notification.show {
|
||||
transform: translateX(0);
|
||||
}
|
||||
|
||||
.notification.success {
|
||||
background: linear-gradient(135deg, #56ab2f 0%, #a8e6cf 100%);
|
||||
}
|
||||
|
||||
.notification.error {
|
||||
background: linear-gradient(135deg, #ff416c 0%, #ff4b2b 100%);
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.container {
|
||||
padding: 10px;
|
||||
}
|
||||
|
||||
header h1 {
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
.card {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.timer-display {
|
||||
font-size: 2rem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
class TimerController {
|
||||
constructor() {
|
||||
this.secret = localStorage.getItem('timerSecret') || '';
|
||||
this.baseURL = window.location.origin;
|
||||
this.initElements();
|
||||
this.bindEvents();
|
||||
this.updateSecretDisplay();
|
||||
|
||||
// 自动刷新状态
|
||||
this.startAutoRefresh();
|
||||
}
|
||||
|
||||
initElements() {
|
||||
this.secretInput = document.getElementById('secret');
|
||||
this.saveSecretBtn = document.getElementById('save-secret');
|
||||
this.refreshStatusBtn = document.getElementById('refresh-status');
|
||||
this.sendSignalBtn = document.getElementById('send-signal');
|
||||
this.remainingTimeEl = document.getElementById('remaining-time');
|
||||
this.progressFillEl = document.getElementById('progress-fill');
|
||||
this.notificationEl = document.getElementById('notification');
|
||||
}
|
||||
|
||||
bindEvents() {
|
||||
this.saveSecretBtn.addEventListener('click', () => this.saveSecret());
|
||||
this.refreshStatusBtn.addEventListener('click', () => this.fetchStatus());
|
||||
this.sendSignalBtn.addEventListener('click', () => this.sendSignal());
|
||||
|
||||
// 回车键保存密钥
|
||||
this.secretInput.addEventListener('keypress', (e) => {
|
||||
if (e.key === 'Enter') {
|
||||
this.saveSecret();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
saveSecret() {
|
||||
const secret = this.secretInput.value.trim();
|
||||
if (!secret) {
|
||||
this.showNotification('请输入密钥', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
this.secret = secret;
|
||||
localStorage.setItem('timerSecret', secret);
|
||||
this.updateSecretDisplay();
|
||||
this.showNotification('密钥已保存', 'success');
|
||||
this.fetchStatus(); // 保存后自动刷新状态
|
||||
}
|
||||
|
||||
updateSecretDisplay() {
|
||||
if (this.secret) {
|
||||
this.secretInput.placeholder = '密钥已保存';
|
||||
this.secretInput.value = '';
|
||||
} else {
|
||||
this.secretInput.placeholder = '输入您的访问密钥';
|
||||
}
|
||||
}
|
||||
|
||||
async fetchStatus() {
|
||||
try {
|
||||
const response = await fetch(`${this.baseURL}/timer/status`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.code === 200) {
|
||||
this.displayTime(data.remaining_time);
|
||||
} else {
|
||||
this.showNotification('获取状态失败', 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching status:', error);
|
||||
this.showNotification('网络错误,请稍后重试', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
displayTime(seconds) {
|
||||
const hours = Math.floor(seconds / 3600);
|
||||
const minutes = Math.floor((seconds % 3600) / 60);
|
||||
const secs = Math.floor(seconds % 60);
|
||||
|
||||
this.remainingTimeEl.textContent =
|
||||
`${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
|
||||
|
||||
// 更新进度条(假设最大时间为24小时)
|
||||
const maxTime = 24 * 3600; // 24小时
|
||||
const progress = Math.min(100, (seconds / maxTime) * 100);
|
||||
this.progressFillEl.style.width = `${100 - progress}%`;
|
||||
}
|
||||
|
||||
async sendSignal() {
|
||||
if (!this.secret) {
|
||||
this.showNotification('请先设置密钥', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${this.baseURL}/signal?secret=${encodeURIComponent(this.secret)}`);
|
||||
const data = await response.json();
|
||||
|
||||
if (data.code === 200) {
|
||||
this.showNotification('信号发送成功!', 'success');
|
||||
setTimeout(() => this.fetchStatus(), 1000); // 1秒后刷新状态
|
||||
} else {
|
||||
this.showNotification(`操作失败: ${data.message}`, 'error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error sending signal:', error);
|
||||
this.showNotification('网络错误,请稍后重试', 'error');
|
||||
}
|
||||
}
|
||||
|
||||
showNotification(message, type) {
|
||||
this.notificationEl.textContent = message;
|
||||
this.notificationEl.className = `notification ${type} show`;
|
||||
|
||||
setTimeout(() => {
|
||||
this.notificationEl.classList.remove('show');
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
startAutoRefresh() {
|
||||
// 每30秒自动刷新一次状态
|
||||
setInterval(() => {
|
||||
if (this.secret) {
|
||||
this.fetchStatus();
|
||||
}
|
||||
}, 30000);
|
||||
|
||||
// 页面加载完成后立即获取一次状态
|
||||
setTimeout(() => this.fetchStatus(), 1000);
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化应用
|
||||
document.addEventListener('DOMContentLoaded', () => {
|
||||
new TimerController();
|
||||
});
|
||||
Reference in New Issue
Block a user