Files
WeChatDataAnalysis/frontend/plugins/api-check.client.js
2025-07-25 20:21:26 +08:00

26 lines
777 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// 客户端插件检查API连接状态
export default defineNuxtPlugin(async (nuxtApp) => {
const { healthCheck } = useApi()
const appStore = useAppStore()
// 检查API连接
const checkApiConnection = async () => {
try {
const result = await healthCheck()
if (result.status === 'healthy') {
appStore.setApiStatus('connected', '已连接到后端API')
} else {
appStore.setApiStatus('error', 'API响应异常')
}
} catch (error) {
appStore.setApiStatus('error', '无法连接到后端API请确保后端服务已启动')
console.error('API连接失败:', error)
}
}
// 初始检查
await checkApiConnection()
// 定期检查每30秒
setInterval(checkApiConnection, 30000)
})