'commit'
This commit is contained in:
@@ -51,6 +51,16 @@ const UIController = {
|
||||
this.toggleElement('stopRecordBtn', isRecording);
|
||||
},
|
||||
|
||||
// 禁用/启用帮我讲题按钮
|
||||
setStartRecordButtonEnabled(enabled) {
|
||||
const startBtn = document.getElementById('startRecordBtn');
|
||||
if (startBtn) {
|
||||
startBtn.disabled = !enabled;
|
||||
startBtn.style.opacity = enabled ? '1' : '0.5';
|
||||
startBtn.style.cursor = enabled ? 'pointer' : 'not-allowed';
|
||||
}
|
||||
},
|
||||
|
||||
// 更新播放按钮图标
|
||||
updatePlayButton(isPlaying) {
|
||||
const btn = document.getElementById('playAudioBtn');
|
||||
@@ -150,6 +160,12 @@ const ASRProcessor = {
|
||||
async processAudio(audioBlob) {
|
||||
console.log('开始上传音频到服务器');
|
||||
UIController.toggleElement('thinkingIndicator', true);
|
||||
// 禁用帮我讲题按钮,防止在思考过程中重复点击
|
||||
UIController.setStartRecordButtonEnabled(false);
|
||||
|
||||
// 创建AbortController用于超时控制
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), 120000); // 120秒超时
|
||||
|
||||
try {
|
||||
const formData = new FormData();
|
||||
@@ -157,14 +173,20 @@ const ASRProcessor = {
|
||||
|
||||
const response = await fetch('/api/xueban/upload-audio', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
body: formData,
|
||||
signal: controller.signal // 添加超时信号
|
||||
});
|
||||
|
||||
// 请求成功,清除超时定时器
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
if (!response.ok) throw new Error('服务器响应错误');
|
||||
|
||||
const data = await response.json();
|
||||
console.log('处理结果:', data);
|
||||
UIController.toggleElement('thinkingIndicator', false);
|
||||
// 思考结束,重新启用帮我讲题按钮
|
||||
UIController.setStartRecordButtonEnabled(true);
|
||||
|
||||
if (data.success) {
|
||||
ResultDisplay.showResults(data.data);
|
||||
@@ -172,9 +194,20 @@ const ASRProcessor = {
|
||||
alert('音频处理失败: ' + data.message);
|
||||
}
|
||||
} catch (error) {
|
||||
// 清除超时定时器
|
||||
clearTimeout(timeoutId);
|
||||
|
||||
console.error('上传音频失败:', error);
|
||||
UIController.toggleElement('thinkingIndicator', false);
|
||||
alert('上传音频失败: ' + error.message);
|
||||
// 发生错误时也要重新启用按钮
|
||||
UIController.setStartRecordButtonEnabled(true);
|
||||
|
||||
// 判断是否是超时错误
|
||||
if (error.name === 'AbortError') {
|
||||
alert('请求超时,服务器响应时间过长,请稍后再试');
|
||||
} else {
|
||||
alert('上传音频失败: ' + error.message);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user