mirror of
https://github.com/jxxghp/MoviePilot-Frontend.git
synced 2026-09-05 07:28:37 +08:00
优化离线状态管理逻辑
This commit is contained in:
+1
-1
@@ -137,7 +137,7 @@
|
|||||||
|
|
||||||
<div class="status-badge">
|
<div class="status-badge">
|
||||||
<span class="status-dot"></span>
|
<span class="status-dot"></span>
|
||||||
<span>离线模式</span>
|
<span>离线状态</span>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
+4
-2
@@ -39,8 +39,9 @@ const globalOfflineStatus = useGlobalOfflineStatus()
|
|||||||
// 添加响应拦截器
|
// 添加响应拦截器
|
||||||
api.interceptors.response.use(
|
api.interceptors.response.use(
|
||||||
response => {
|
response => {
|
||||||
// 成功响应时,清除应用离线状态
|
// 成功响应时,清除应用离线状态并重置连续错误计数
|
||||||
globalOfflineStatus.setAppOffline(false)
|
globalOfflineStatus.setAppOffline(false)
|
||||||
|
globalOfflineStatus.resetConsecutiveErrors()
|
||||||
return response.data
|
return response.data
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
@@ -57,7 +58,8 @@ api.interceptors.response.use(
|
|||||||
if (error.code === 'ECONNABORTED') {
|
if (error.code === 'ECONNABORTED') {
|
||||||
reason = 'Request timeout'
|
reason = 'Request timeout'
|
||||||
}
|
}
|
||||||
globalOfflineStatus.setAppOffline(true, reason)
|
// 记录网络错误,只有连续三次才会设置为离线模式
|
||||||
|
globalOfflineStatus.recordNetworkError(reason)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (error.code === 'NETWORK_ERROR' || error.code === 'ERR_NETWORK') {
|
if (error.code === 'NETWORK_ERROR' || error.code === 'ERR_NETWORK') {
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ import { useOnline } from '@vueuse/core'
|
|||||||
// 全局状态
|
// 全局状态
|
||||||
const isAppOffline = ref(false)
|
const isAppOffline = ref(false)
|
||||||
const appOfflineReason = ref('')
|
const appOfflineReason = ref('')
|
||||||
|
const consecutiveNetworkErrors = ref(0)
|
||||||
|
const MAX_CONSECUTIVE_ERRORS = 3
|
||||||
|
|
||||||
// 全局离线状态管理
|
// 全局离线状态管理
|
||||||
export function useGlobalOfflineStatus() {
|
export function useGlobalOfflineStatus() {
|
||||||
@@ -19,6 +21,26 @@ export function useGlobalOfflineStatus() {
|
|||||||
const setAppOffline = (offline: boolean, reason?: string) => {
|
const setAppOffline = (offline: boolean, reason?: string) => {
|
||||||
isAppOffline.value = offline
|
isAppOffline.value = offline
|
||||||
appOfflineReason.value = reason || ''
|
appOfflineReason.value = reason || ''
|
||||||
|
|
||||||
|
// 如果设置为在线状态,重置连续错误计数
|
||||||
|
if (!offline) {
|
||||||
|
consecutiveNetworkErrors.value = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 记录网络错误
|
||||||
|
const recordNetworkError = (reason?: string) => {
|
||||||
|
consecutiveNetworkErrors.value++
|
||||||
|
|
||||||
|
// 只有连续出现三次网络错误时才设置为离线模式
|
||||||
|
if (consecutiveNetworkErrors.value >= MAX_CONSECUTIVE_ERRORS) {
|
||||||
|
setAppOffline(true, reason || `连续${MAX_CONSECUTIVE_ERRORS}次网络错误`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 重置连续错误计数
|
||||||
|
const resetConsecutiveErrors = () => {
|
||||||
|
consecutiveNetworkErrors.value = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取离线消息
|
// 获取离线消息
|
||||||
@@ -37,7 +59,10 @@ export function useGlobalOfflineStatus() {
|
|||||||
isOffline,
|
isOffline,
|
||||||
canPerformNetworkAction,
|
canPerformNetworkAction,
|
||||||
setAppOffline,
|
setAppOffline,
|
||||||
|
recordNetworkError,
|
||||||
|
resetConsecutiveErrors,
|
||||||
getOfflineMessage,
|
getOfflineMessage,
|
||||||
|
consecutiveNetworkErrors: computed(() => consecutiveNetworkErrors.value),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -144,9 +144,9 @@ export default {
|
|||||||
restartTip: 'After restart, you will be logged out and need to log in again.',
|
restartTip: 'After restart, you will be logged out and need to log in again.',
|
||||||
restartTimeout: 'Restart timeout, the system may need more time to recover, please refresh the page manually later',
|
restartTimeout: 'Restart timeout, the system may need more time to recover, please refresh the page manually later',
|
||||||
restartFailed: 'Restart failed, please check system status',
|
restartFailed: 'Restart failed, please check system status',
|
||||||
offline: 'Offline Mode',
|
offline: 'Application Offline',
|
||||||
offlineMessage: 'Network connection lost, some features may be limited',
|
offlineMessage: 'Network connection lost, some features may be limited',
|
||||||
online: 'Online Mode',
|
online: 'Application Online',
|
||||||
onlineMessage: 'Network connection restored',
|
onlineMessage: 'Network connection restored',
|
||||||
},
|
},
|
||||||
pwa: {
|
pwa: {
|
||||||
|
|||||||
@@ -144,9 +144,9 @@ export default {
|
|||||||
restartTip: '重启后,您将被注销并需要重新登录。',
|
restartTip: '重启后,您将被注销并需要重新登录。',
|
||||||
restartTimeout: '重启超时,系统可能需要更长时间恢复,请稍后手动刷新页面',
|
restartTimeout: '重启超时,系统可能需要更长时间恢复,请稍后手动刷新页面',
|
||||||
restartFailed: '重启失败,请检查系统状态',
|
restartFailed: '重启失败,请检查系统状态',
|
||||||
offline: '离线模式',
|
offline: '应用已离线',
|
||||||
offlineMessage: '网络连接已断开,部分功能可能受限',
|
offlineMessage: '网络连接已断开,部分功能可能受限',
|
||||||
online: '在线模式',
|
online: '应用在线',
|
||||||
onlineMessage: '网络连接已恢复',
|
onlineMessage: '网络连接已恢复',
|
||||||
},
|
},
|
||||||
pwa: {
|
pwa: {
|
||||||
|
|||||||
@@ -145,9 +145,9 @@ export default {
|
|||||||
restartTip: '重啟後,您將被註銷並需要重新登錄。',
|
restartTip: '重啟後,您將被註銷並需要重新登錄。',
|
||||||
restartTimeout: '重啟超時,系統可能需要更長時間恢復,請稍後手動刷新頁面',
|
restartTimeout: '重啟超時,系統可能需要更長時間恢復,請稍後手動刷新頁面',
|
||||||
restartFailed: '重啟失敗,請檢查系統狀態',
|
restartFailed: '重啟失敗,請檢查系統狀態',
|
||||||
offline: '離線模式',
|
offline: '應用已離線',
|
||||||
offlineMessage: '網絡連接已斷開,部分功能可能受限',
|
offlineMessage: '網絡連接已斷開,部分功能可能受限',
|
||||||
online: '在線模式',
|
online: '應用在線',
|
||||||
onlineMessage: '網絡連接已恢復',
|
onlineMessage: '網絡連接已恢復',
|
||||||
},
|
},
|
||||||
pwa: {
|
pwa: {
|
||||||
|
|||||||
Reference in New Issue
Block a user