fix: 轮询按运营商账户限流
All checks were successful
构建并部署前端到测试环境 / deploy (push) Successful in 55s

This commit is contained in:
luo
2026-10-26 14:31:15 +08:00
parent 049f8257fb
commit 06684e1fc0
88 changed files with 7919 additions and 486 deletions

View File

@@ -0,0 +1,125 @@
import { ref, onBeforeUnmount, onDeactivated, onMounted, watch } from 'vue'
import { PollingConfigService } from '@/api/modules/pollingConfig'
import type { PollingConfig } from '@/types/api/pollingConfig'
import type { PollingRecalculateTask } from '@/types/api/pollingConfigExtensions'
import { gatewayData, gatewayError, requireGatewayAccess } from '@/utils/business/gateway'
import { pollingTaskActive } from '@/utils/business/pollingConfig'
export function usePollingConfigCandidates() {
const candidates = ref<PollingConfig[]>([])
const loading = ref(false)
const error = ref('')
let sequence = 0
async function search(query = '') {
const current = ++sequence
loading.value = true
error.value = ''
try {
const data = gatewayData(
await PollingConfigService.getPollingConfigs({
page: 1,
page_size: 20,
config_name: query.trim()
})
)
if (current === sequence) candidates.value = data.items || []
} catch (cause) {
if (current === sequence) {
error.value = gatewayError(cause)
candidates.value = []
}
} finally {
if (current === sequence) loading.value = false
}
}
onMounted(() => search())
onBeforeUnmount(() => {
++sequence
})
return { candidates, loading, error, search }
}
/** 单一任务、非重叠查询;失败、终态、关闭、切换任务和卸载都撤销旧请求。 */
export function usePollingRecalculateProgress(allowed: () => boolean) {
const taskId = ref('')
const task = ref<PollingRecalculateTask | null>(null)
const loading = ref(false)
const error = ref('')
const autoRefresh = ref(false)
let sequence = 0
let timer: ReturnType<typeof setTimeout> | undefined
let disposed = false
function stop() {
++sequence
if (timer) clearTimeout(timer)
timer = undefined
loading.value = false
}
async function query() {
if (disposed || loading.value) return
stop()
const current = sequence
const id = taskId.value.trim()
error.value = ''
loading.value = true
try {
requireGatewayAccess(allowed())
if (!id) throw new Error('请输入任务 ID')
const data = gatewayData(await PollingConfigService.getRecalculateTask(id))
if (current !== sequence || disposed) return
requireGatewayAccess(allowed())
if (data.task_id !== id) throw new Error('返回的任务 ID 与查询任务不一致')
task.value = data
if (pollingTaskActive(data.status) && autoRefresh.value)
timer = setTimeout(() => {
void query()
}, 3000)
} catch (cause) {
if (current === sequence) error.value = gatewayError(cause)
} finally {
if (current === sequence) loading.value = false
}
}
function adopt(data: PollingRecalculateTask) {
stop()
taskId.value = data.task_id
task.value = data
error.value = ''
if (allowed() && autoRefresh.value && pollingTaskActive(data.status))
timer = setTimeout(() => {
void query()
}, 3000)
}
// 同步 watch:修改输入的这一刻即使旧网络请求未结束也不再允许其回填。
watch(
taskId,
() => {
stop()
task.value = null
error.value = ''
},
{ flush: 'sync' }
)
watch(
autoRefresh,
(enabled) => {
if (!enabled) stop()
else if (!loading.value && taskId.value.trim() && pollingTaskActive(task.value?.status))
void query()
},
{ flush: 'sync' }
)
watch(allowed, (enabled) => {
if (!enabled) {
stop()
task.value = null
error.value = ''
}
})
onDeactivated(stop)
onBeforeUnmount(() => {
disposed = true
stop()
})
return { taskId, task, loading, error, autoRefresh, query, stop, adopt }
}

View File

@@ -0,0 +1,56 @@
import { ref, shallowRef, onBeforeUnmount, onDeactivated, onActivated, watch } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { gatewayError, requireGatewayAccess } from '@/utils/business/gateway'
import { pollingScope } from '@/utils/business/pollingScheduler'
export function useSchedulerRequest<T>(request: () => Promise<T>, allowed: () => boolean) {
const user = useUserStore()
const data = shallowRef<T | null>(null),
loading = ref(false),
error = ref('')
let sequence = 0,
active = true
function clear() {
++sequence
data.value = null
loading.value = false
error.value = ''
}
async function load() {
if (!active) return
const current = ++sequence,
scope = pollingScope(user)
loading.value = true
error.value = ''
data.value = null
try {
requireGatewayAccess(allowed())
const result = await request()
if (active && current === sequence && scope === pollingScope(user) && allowed())
data.value = result
} catch (cause) {
if (active && current === sequence) {
data.value = null
error.value = gatewayError(cause)
}
} finally {
if (current === sequence) loading.value = false
}
}
watch(() => [pollingScope(user), allowed()], clear, { flush: 'sync' })
onDeactivated(() => {
active = false
clear()
})
onActivated(() => {
if (!active) {
active = true
void load()
}
})
onBeforeUnmount(() => {
active = false
clear()
})
return { data, loading, error, load }
}