JLBA202505130001_关于吉林银行新建AI智能培训系统的需求_继续开发语音录制按钮

This commit is contained in:
田岩
2026-03-03 09:46:44 +08:00
parent f6b1ef16c3
commit 7ec7a8942e
4 changed files with 57 additions and 35 deletions
+2 -2
View File
@@ -4,10 +4,10 @@ ENV = 'development'
VITE_APP_BASE_API_Url = 'https://aits.jlbank.com.cn:7001'
#VITE_APP_BASE_API_Url = 'https://aits.jlbank.com.cn:7001'
#VITE_APP_BASE_API_Url = 'https://aitstest.jlbank.com.cn:7001'
VITE_APP_BASE_API_Url = 'https://aitstest.jlbank.com.cn:7001'
# VITE_APP_BASE_API_Url = 'https://aitstest.jlbank.com.cn:7002'
# VITE_APP_BASE_API_Url = 'http://192.168.247.200'
+22 -8
View File
@@ -1,3 +1,4 @@
import { ErrorCode, ShowErrorTipOptions } from './touch-btn.types';
import { get_base_url } from '@/api/request.js';
import { getToken } from '@/common/common.js';
// const url = '/trapractice/voiceCallSocket/voiceCall'
@@ -70,13 +71,26 @@ export const initWebSocket = (wsUrl: string): Promise<UniApp.SocketTask> => {
});
};
/**
* 错误提示统一处理函数(极简版)
* @param errorCode 错误码(枚举类型,直接对应提示文本)
* @param options 可选参数:延迟显示时间
* @returns 固定返回false(保持原有返回值类型)
*/
export function showErrorTip(
errorCode: ErrorCode,
options: ShowErrorTipOptions = {}
): boolean {
// 解构可选参数,设置默认延迟时间为200ms
const { delayTime = 200 } = options;
// 延迟显示对应的错误提示
setTimeout(() => {
uni.showToast({
title: errorCode, // 直接使用枚举值作为提示文本
icon: 'none'
});
}, delayTime); // 使用传入的延迟时间(或默认200ms)
// 方式2Promise.then/catch 调用
// initWebSocket('wss://your-real-ws-url.com')
// .then((socketTask) => {
// console.log('WS连接成功', socketTask);
// })
// .catch((error) => {
// console.error('WS连接失败', error);
// });
return false;
}
+21 -6
View File
@@ -11,9 +11,24 @@ export type StatusType = 'default' | 'voice_recording' | 'text_input' | 'voice_t
/**
* 语音转文字的子状态(仅当主状态为 voice_to_text 时生效)
*/
export type VoiceToTextSubStatus =
| '' // 纯空
| 'init' // 初始化(刚进入语音转文字页面)
| 'loading' // 转换中(正在处理语音→文字)
| 'success' // 转换完成(成功得到文字结果)
| 'failed'; // 转换失败(网络/识别错误等)
export type VoiceToTextSubStatus =
| '' // 纯空
| 'init' // 初始化(刚进入语音转文字页面)
| 'loading' // 转换中(正在处理语音→文字)
| 'success' // 转换完成(成功得到文字结果)
| 'failed'; // 转换失败(网络/识别错误等)
/** 错误码枚举 - 直接映射提示文本 */
export enum ErrorCode {
// 网络错误提示
NetworkError = '网络连接失败,请稍后再试!',
// 录音时长过短提示
ShortRecord = '说话时间太短,没有听清!'
}
/** 错误提示函数的可选参数类型 */
export interface ShowErrorTipOptions {
// 提示框延迟显示时间(毫秒),默认200ms
delayTime?: number;
}
+12 -19
View File
@@ -71,8 +71,8 @@
<script setup lang="ts">
// 导出图片
import { cancelBtn, cancelActiveBtn, translateBtn, translateActiveBtn, voiceWhiteBtn, icKeyboardSvg, iconMicSvg, iconSendSvg, iconRecognizeErrorSvg } from './touch-btn.images';
import { StatusType, VoiceToTextSubStatus } from './touch-btn.types';
import { wsUrl, bottomAreaTopBoundary, horizontalSplitX, initWebSocket } from './touch-btn';
import { StatusType, VoiceToTextSubStatus,ErrorCode } from './touch-btn.types';
import { wsUrl, bottomAreaTopBoundary, horizontalSplitX, initWebSocket, showErrorTip } from './touch-btn';
import { ref, computed, onMounted, nextTick, watch, reactive } from 'vue';
import common from '@/common/common';
import { onHide, onLaunch } from '@dcloudio/uni-app';
@@ -214,6 +214,7 @@
}
});
}).catch(() => {
stopRecord('networkError')
console.log('网络出现问题');
})
pressTimer.value = setTimeout(() => {
@@ -259,13 +260,12 @@
const closePopup = () => {
popupRef.value.close();
}
const stopRecord = async () => {
const stopRecord = async (errrStatus='') => {
clearTimeout(pressTimer.value);
if (!isRecording.value) return; // 未处于录音状态
if (isOutStatus.value === '03') { // 转换文字状态
console.log('已发送', '03转文字');
switchFromVoiceToText()
}
const recordDurationMs = Date.now() - recordStartTime.value;
if (isOutStatus.value !== '02') { // 取消的时候,不校验最低时间
@@ -279,8 +279,8 @@
}
}
isRecording.value = false;
if (isOutStatus.value === '02' && ws.value) { // 取消的时候自己主动关闭websocket,其他的时候需要等待后端的消息关闭
ws.value.close({
if (isOutStatus.value === '02' || errrStatus === 'networkError') { // 取消的时候自己主动关闭websocket,其他的时候需要等待后端的消息关闭
ws.value && ws.value.close({
code: 1000,
reason: '挂断',
complete: () => {
@@ -290,21 +290,15 @@
});
}
stopAudioRecord(); // 关掉录音
if (isOutStatus.value !== '03') { // 转文字时候不关遮罩层
if (isOutStatus.value !== '03' || errrStatus === 'networkError') { // 转文字时候不关遮罩层,网络错误必须关
popupRef.value.close();
if (isOutStatus.value !== '02') { // 取消的时候,不校验最低时间
if (recordDurationMs <= 1200) {
setTimeout(() => {
uni.showToast({
title: '说话时间太短,没有听清!',
icon: 'none'
});
}, 200)
return
}
// 关掉弹窗后的提示
if(errrStatus === 'networkError') {
return showErrorTip(ErrorCode.NetworkError)
} else if (isOutStatus.value !== '02' && recordDurationMs <= 1200) { // 取消的时候,不校验最低时间
return showErrorTip(ErrorCode.ShortRecord)
}
}
};
// 从录音状态切换成转文字状态
@@ -313,7 +307,6 @@
ws.value && ws.value.send({ data: ProtocolCodec.pack(MessageType.CONTROL_CMD, { type: ControlCode.FRNOT_TO_SERVER_OVER, ossID: '', text: '' }) });
voice_to_text_status.value = 'init'
// 执行动画
11
}
// 发送文本