Browse Source

fix: 指令轮询防重入并保留 HTTP 状态码

main
xiaosi 3 weeks ago
parent
commit
d979d60ec6
  1. 5
      src/utils/http.js
  2. 15
      src/views/MonitorView/MonitorView.vue

5
src/utils/http.js

@ -41,7 +41,10 @@ request.interceptors.response.use(
} else {
const message = data?.msg || error.message || '网络错误'
useUiStore().toast(message)
return Promise.reject(new Error(message))
const err = new Error(message)
err.status = status
err.response = error.response
return Promise.reject(err)
}
return Promise.reject(error)
}

15
src/views/MonitorView/MonitorView.vue

@ -1390,6 +1390,8 @@ const commandProgress = reactive({
let commandPollTimer = null
let commandAutoCloseTimer = null
let commandPollFails = 0
let commandPollInFlight = false
let commandProgressEpoch = 0
function buttonStatusText(title) {
if (commandProgress.buttonTitle !== title || !commandProgress.status) return '状态:待执行'
@ -1410,6 +1412,7 @@ function clearCommandTimers() {
function closeCommandProgress() {
clearCommandTimers()
commandProgressEpoch += 1
commandProgress.open = false
}
@ -1431,9 +1434,12 @@ function scheduleAutoClose() {
}
async function pollCommandOnce() {
if (!commandProgress.commandId) return
if (!commandProgress.commandId || commandPollInFlight) return
const epoch = commandProgressEpoch
commandPollInFlight = true
try {
const cmd = await request.get(urls.COMMAND(commandProgress.commandId))
if (epoch !== commandProgressEpoch) return
commandPollFails = 0
const meta = applyCommandStatus(cmd)
if (meta.terminal) {
@ -1442,8 +1448,9 @@ async function pollCommandOnce() {
scheduleAutoClose()
}
} catch (e) {
if (epoch !== commandProgressEpoch) return
commandPollFails += 1
const status = e?.response?.status
const status = e?.status ?? e?.response?.status
if (status === 403 || status === 404 || commandPollFails >= 3) {
clearInterval(commandPollTimer)
commandPollTimer = null
@ -1455,11 +1462,15 @@ async function pollCommandOnce() {
: (e.message || '进度查询失败')
scheduleAutoClose()
}
} finally {
commandPollInFlight = false
}
}
function startCommandProgress(cmd, title, deviceName) {
clearCommandTimers()
commandPollInFlight = false
commandProgressEpoch += 1
commandPollFails = 0
commandProgress.open = true
commandProgress.title = title

Loading…
Cancel
Save