()
| 1500 | |
| 1501 | // 取消任务 |
| 1502 | async function handleCancelTask() { |
| 1503 | // 禁用取消按钮,防止重复点击 |
| 1504 | elements.cancelBtn.disabled = true; |
| 1505 | addLog('info', '[系统] 正在提交取消请求...'); |
| 1506 | |
| 1507 | try { |
| 1508 | // 批量任务取消(包括普通批量模式和 Outlook 批量模式) |
| 1509 | if (currentBatch && (isBatchMode || isOutlookBatchMode || isAutoMode)) { |
| 1510 | // 优先通过 WebSocket 取消 |
| 1511 | if (batchWebSocket && batchWebSocket.readyState === WebSocket.OPEN) { |
| 1512 | batchWebSocket.send(JSON.stringify({ type: 'cancel' })); |
| 1513 | addLog('warning', '[警告] 批量任务取消请求已提交'); |
| 1514 | toast.info('任务取消请求已提交'); |
| 1515 | } else { |
| 1516 | // 降级到 REST API |
| 1517 | const endpoint = isOutlookBatchMode |
| 1518 | ? `/registration/outlook-batch/${currentBatch.batch_id}/cancel` |
| 1519 | : `/registration/batch/${currentBatch.batch_id}/cancel`; |
| 1520 | |
| 1521 | await api.post(endpoint); |
| 1522 | addLog('warning', '[警告] 批量任务取消请求已提交'); |
| 1523 | toast.info('任务取消请求已提交'); |
| 1524 | if (!isAutoMode) { |
| 1525 | stopBatchPolling(); |
| 1526 | resetButtons(); |
| 1527 | } |
| 1528 | } |
| 1529 | } |
| 1530 | // 单次任务取消 |
| 1531 | else if (currentTask) { |
| 1532 | // 优先通过 WebSocket 取消 |
| 1533 | if (webSocket && webSocket.readyState === WebSocket.OPEN) { |
| 1534 | webSocket.send(JSON.stringify({ type: 'cancel' })); |
| 1535 | addLog('warning', '[警告] 任务取消请求已提交'); |
| 1536 | toast.info('任务取消请求已提交'); |
| 1537 | } else { |
| 1538 | // 降级到 REST API |
| 1539 | await api.post(`/registration/tasks/${currentTask.task_uuid}/cancel`); |
| 1540 | addLog('warning', '[警告] 任务已取消'); |
| 1541 | toast.info('任务已取消'); |
| 1542 | stopLogPolling(); |
| 1543 | resetButtons(); |
| 1544 | } |
| 1545 | } |
| 1546 | // 没有活动任务 |
| 1547 | else { |
| 1548 | addLog('warning', '[警告] 没有活动的任务可以取消'); |
| 1549 | toast.warning('没有活动的任务'); |
| 1550 | resetButtons(); |
| 1551 | } |
| 1552 | } catch (error) { |
| 1553 | addLog('error', `[错误] 取消失败: ${error.message}`); |
| 1554 | toast.error(error.message); |
| 1555 | // 恢复取消按钮,允许重试 |
| 1556 | elements.cancelBtn.disabled = false; |
| 1557 | } |
| 1558 | } |
| 1559 |
nothing calls this directly
no test coverage detected