nofx13 / web /src /components /NetworkStatus.vue
bisheng13's picture
Update web/src/components/NetworkStatus.vue
640d303 verified
Raw History Blame Contribute Delete
7.88 kB
<template>
<div class="network-monitor">
<div class="header">
<h2>网络状态监控</h2>
<div class="controls">
<button @click="refreshAll" class="btn-refresh" :disabled="loading">
<span class="icon">🔄</span>
刷新状态
</button>
</div>
</div>
<div class="status-grid">
<!-- 网络连接状态 -->
<div class="status-card" :class="networkStatusClass">
<div class="card-header">
<h3>网络连接</h3>
<div class="status-indicator">
<span class="dot"></span>
<span class="status-text">{{ networkStatusText }}</span>
</div>
</div>
<div class="card-content">
<div class="info-item">
<span class="label">连接类型:</span>
<span class="value">{{ networkInfo.effectiveType || '未知' }}</span>
</div>
<div class="info-item">
<span class="label">下行速度:</span>
<span class="value">{{ networkInfo.downlink ? networkInfo.downlink + ' Mbps' : '未知' }}</span>
</div>
<div class="info-item">
<span class="label">网络延迟:</span>
<span class="value">{{ networkInfo.rtt ? networkInfo.rtt + ' ms' : '未知' }}</span>
</div>
</div>
</div>
<!-- 服务器状态 -->
<div class="status-card" :class="serverStatusClass">
<div class="card-header">
<h3>服务器状态</h3>
<div class="status-indicator">
<span class="dot"></span>
<span class="status-text">{{ serverStatus }}</span>
</div>
</div>
<div class="card-content">
<div class="info-item">
<span class="label">服务状态:</span>
<span class="value">{{ serverHealth.status || '检查中...' }}</span>
</div>
<div class="info-item">
<span class="label">版本:</span>
<span class="value">{{ serverHealth.version || '未知' }}</span>
</div>
<div class="info-item">
<span class="label">最后响应:</span>
<span class="value">{{ serverHealth.timestamp ? formatTime(serverHealth.timestamp) : '未知' }}</span>
</div>
</div>
</div>
</div>
<!-- 加载状态 -->
<div v-if="loading" class="loading-overlay">
<div class="loading-spinner"></div>
<span>检查状态中...</span>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, onUnmounted } from 'vue'
// 响应式数据
const isOnline = ref(navigator.onLine)
const networkInfo = ref<any>({})
const serverHealth = ref<any>({})
const loading = ref(false)
// 计算属性
const networkStatusClass = computed(() => ({
'online': isOnline.value,
'offline': !isOnline.value
}))
const networkStatusText = computed(() =>
isOnline.value ? '在线' : '离线'
)
const serverStatusClass = computed(() => ({
'healthy': serverHealth.value.status === 'healthy',
'checking': !serverHealth.value.status,
'error': serverHealth.value.status && serverHealth.value.status !== 'healthy'
}))
const serverStatus = computed(() => {
if (!serverHealth.value.status) return '检查中...'
return serverHealth.value.status === 'healthy' ? '运行正常' : '服务异常'
})
// 方法
const updateNetworkInfo = () => {
const connection = (navigator as any).connection
if (connection) {
networkInfo.value = {
effectiveType: connection.effectiveType,
downlink: connection.downlink,
rtt: connection.rtt,
saveData: connection.saveData
}
}
}
const checkServerHealth = async () => {
try {
loading.value = true
const response = await fetch('/api/health')
if (response.ok) {
serverHealth.value = await response.json()
} else {
serverHealth.value = { status: 'error' }
}
} catch (error) {
serverHealth.value = { status: 'error' }
console.error('服务器健康检查失败:', error)
} finally {
loading.value = false
}
}
const refreshAll = () => {
updateNetworkInfo()
checkServerHealth()
}
const formatTime = (timestamp: string) => {
return new Date(timestamp).toLocaleTimeString()
}
const handleOnline = () => {
isOnline.value = true
updateNetworkInfo()
}
const handleOffline = () => {
isOnline.value = false
}
// 生命周期
onMounted(() => {
// 事件监听
window.addEventListener('online', handleOnline)
window.addEventListener('offline', handleOffline)
// 初始检查
refreshAll()
onUnmounted(() => {
window.removeEventListener('online', handleOnline)
window.removeEventListener('offline', handleOffline)
})
})
</script>
<style scoped>
.network-monitor {
max-width: 800px;
margin: 0 auto;
padding: 20px;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 30px;
padding-bottom: 20px;
border-bottom: 2px solid #e2e8f0;
}
.header h2 {
color: #2d3748;
margin: 0;
font-size: 28px;
font-weight: 600;
}
.btn-refresh {
display: flex;
align-items: center;
gap: 8px;
padding: 10px 20px;
background: #3b82f6;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 14px;
font-weight: 500;
transition: all 0.2s;
}
.btn-refresh:hover:not(:disabled) {
background: #2563eb;
}
.btn-refresh:disabled {
opacity: 0.6;
cursor: not-allowed;
}
.status-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
margin-bottom: 30px;
}
.status-card {
background: white;
border-radius: 12px;
padding: 20px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
border: 2px solid transparent;
}
.status-card.online {
border-color: #10b981;
}
.status-card.offline {
border-color: #ef4444;
}
.status-card.healthy {
border-color: #10b981;
}
.status-card.error {
border-color: #ef4444;
}
.status-card.checking {
border-color: #f59e0b;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.card-header h3 {
margin: 0;
color: #2d3748;
font-size: 18px;
font-weight: 600;
}
.status-indicator {
display: flex;
align-items: center;
gap: 8px;
}
.dot {
width: 10px;
height: 10px;
border-radius: 50%;
animation: pulse 2s infinite;
}
.online .dot,
.healthy .dot {
background-color: #10b981;
}
.offline .dot,
.error .dot {
background-color: #ef4444;
}
.checking .dot {
background-color: #f59e0b;
}
.status-text {
font-weight: 600;
font-size: 14px;
}
.online .status-text,
.healthy .status-text {
color: #10b981;
}
.offline .status-text,
.error .status-text {
color: #ef4444;
}
.checking .status-text {
color: #f59e0b;
}
.card-content {
space-y: 12px;
}
.info-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px 0;
border-bottom: 1px solid #f1f5f9;
}
.info-item:last-child {
border-bottom: none;
}
.label {
color: #64748b;
font-size: 14px;
font-weight: 500;
}
.value {
color: #1e293b;
font-weight: 600;
font-size: 14px;
}
.loading-overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(255, 255, 255, 0.9);
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
z-index: 1000;
}
.loading-spinner {
width: 40px;
height: 40px;
border: 4px solid #e2e8f0;
border-left: 4px solid #3b82f6;
border-radius: 50%;
animation: spin 1s linear infinite;
margin-bottom: 16px;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.5; }
100% { opacity: 1; }
}
</style>