wujingjing
2025-04-09 2b8b2cac4fe3f05474459a034bc4034f2d7aa0cb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<template>
    <div class="fixed top-0 left-0 w-screen h-screen bg-[rgb(0,0,0,.8)] backdrop-blur-[20px] z-10">
        <div
            class="absolute w-[414px] h-[752px] bg-black left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-xl text-white flex flex-col"
        >
            <div class="mx-auto text-lg text-gray-300 mt-4 flex-0">Chat-4.0</div>
            <div class="flex flex-col flex-auto relative items-center">
                <div class="chat mt-[130px] mb-[110px]">
                    <span :style="{ 'animation-play-state': animationPlayState }"
                        ><i :style="{ 'animation-play-state': animationPlayState }"></i
                        ><i :style="{ 'animation-play-state': animationPlayState }"></i
                        ><i :style="{ 'animation-play-state': animationPlayState }"></i
                    ></span>
                    <div class="chatPop size-72" :style="{ 'animation-play-state': animationPlayState }">
                        <span class="size-32" :style="{ 'animation-play-state': animationPlayState }"></span>
                    </div>
                </div>
                <div class="flex items-center">
                    <div class="sound-animate relative">
                        <i class="ywifont ywicon-maikefeng-filled !text-[26px] absolute -left-10 top-[5px]"></i>
                        <span :style="{ 'animation-play-state': animationPlayState }"></span
                        ><span :style="{ 'animation-play-state': animationPlayState }"></span
                        ><span :style="{ 'animation-play-state': animationPlayState }"></span
                        ><span :style="{ 'animation-play-state': animationPlayState }"></span>
                    </div>
                </div>
                <div class="mt-5" :class="{ 'cursor-pointer': currentVoiceType === VoiceTipType.Speak }" @click="voiceTipClick">
                    {{ voiceTipMap[currentVoiceType] }}
                </div>
 
                <div class="flex items-center justify-between bottom-16 absolute left-1/2 -translate-x-1/2 space-x-16">
                    <div class="size-[35px] flex items-center justify-center bg-[#292929] rounded-full cursor-pointer" @click="togglePlayClick">
                        <i class="ywifont !text-[16px]" :class="playIcon"></i>
                    </div>
                    <div class="size-[56px] flex items-center justify-center bg-red-500 rounded-full cursor-pointer" @click="closeClick">
                        <i class="ywifont ywicon-guanbi !text-[26px]"></i>
                    </div>
                    <div class="size-[35px] flex items-center justify-center bg-[#292929] rounded-full cursor-pointer">
                        <i class="ywifont ywicon-gengduo !text-[23px]"></i>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
 
<script setup lang="ts">
import { computed, nextTick, ref, watch } from 'vue';
import type { ChatContent } from '../../../model/types';
import { AnswerType } from '../../../model/types';
import { VoiceRecognitionErrorType, VoiceTipType, voiceTipMap } from './types';
import router from '/@/router';
import { setRoomConfig } from '/@/stores/chatRoom';
 
const animationPlayState = ref<'paused' | 'running'>('running');
 
const playIcon = computed(() => (animationPlayState.value === 'running' ? 'ywicon-zanting' : 'ywicon-bofang'));
const isSpeak = ref(false);
const togglePlayClick = () => {
    animationPlayState.value = animationPlayState.value === 'running' ? 'paused' : 'running';
    if (currentVoiceType.value === VoiceTipType.Speak) {
        if (isSpeak.value) {
            window.speechSynthesis.pause();
        } else {
            window.speechSynthesis.resume();
        }
        isSpeak.value = !isSpeak.value;
    }
};
 
const props = defineProps(['isHome']);
const emit = defineEmits(['submit', 'updateInputValue']);
const isShow = defineModel('isShow', {
    type: Boolean,
});
 
const resetToListenVoice = () => {
    currentVoiceType.value = VoiceTipType.NoSpeech;
    audioChangeWord();
};
const isListening = ref(false);
 
const closeClick = () => {
    isShow.value = false;
};
 
let recognition = null;
let speech = null;
const currentVoiceType = ref<VoiceTipType>(VoiceTipType.NoSpeech);
const handleAnswerRes = (res: ChatContent) => {
    if (!res) {
        return;
    }
    if (!isShow.value) {
        return;
    }
    let text = '';
 
    if (res.type === AnswerType.Text || res.type === AnswerType.Knowledge) {
        if (res.type === AnswerType.Knowledge) {
            text = res.values?.map((item) => item.answer) ?? '';
        } else {
            text = res.values;
        }
    } else {
        text = '抱歉,我无法口述回答此问题的,需要查看请关闭此语音对话界面';
    }
 
    currentVoiceType.value = VoiceTipType.Speak;
    isSpeak.value = true;
    var speech = new SpeechSynthesisUtterance();
    speech.text = text; // 内容
    speech.lang = 'zh-cn'; // 语言
    speech.voiceURI = 'Microsoft Huihui - Chinese (Simplified, PRC)'; // 声音和服务
    // eslint-disable-next-line no-irregular-whitespace
    speech.volume = 0.7; // 声音的音量区间范围是​​0​​​到​​1默认是​​1​​
    // eslint-disable-next-line no-irregular-whitespace
    speech.rate = 1; // 语速,数值,默认值是​​1​​​,范围是​​0.1​​​到​​10​​​,表示语速的倍数,例如​​2​​表示正常语速的两倍
    // eslint-disable-next-line no-irregular-whitespace
    speech.pitch = 1; // 表示说话的音高,数值,范围从​​0​​​(最小)到​​2​​​(最大)。默认值为​​1​​。
 
    speech.onend = () => {
        resetToListenVoice();
    };
    window.speechSynthesis.speak(speech);
    setRoomConfig(router.currentRoute.value.query.id as string, 'firstResCb', undefined);
};
 
const voiceTipClick = () => {
    switch (currentVoiceType.value) {
        case VoiceTipType.Speak:
            window.speechSynthesis.cancel();
            setTimeout(() => {
                resetToListenVoice();
            }, 0);
 
            break;
        default:
            break;
    }
    window.speechSynthesis.cancel();
};
const audioChangeWord = () => {
    emit('updateInputValue', '');
    // 创建SpeechRecognition对象
    // eslint-disable-next-line no-undef
    recognition = new webkitSpeechRecognition();
    if (!recognition) {
        // eslint-disable-next-line no-undef
        recognition = new SpeechRecognition();
    }
    console.log('recognition2', recognition);
    console.log(11);
    // 设置语言
    recognition.lang = 'zh-CN';
    console.log(22);
    // 开始语音识别
    recognition.start();
    isListening.value = true;
    console.log(33);
    // 监听识别结果
    recognition.onresult = function (event) {
        var result = event.results[0][0].transcript;
        console.log('监听结果:', result);
 
        emit('updateInputValue', result);
        currentVoiceType.value = VoiceTipType.Think;
        if (!props.isHome) {
            emit('submit', handleAnswerRes);
        } else {
            setRoomConfig(router.currentRoute.value.query.id as string, 'firstResCb', handleAnswerRes);
            emit('submit');
        }
    };
    recognition.onspeechstart = (event) => {
        currentVoiceType.value = VoiceTipType.Speech;
    };
 
    // 监听错误事件
    recognition.onerror = function (event) {
        isListening.value = false;
        // ElMessage.error('监听语音失败');
        console.error(event.error);
        switch (event.error) {
            case VoiceRecognitionErrorType.NoSpeech:
                if (isShow.value) {
                    resetToListenVoice();
                }
                break;
 
            default:
                break;
        }
    };
    // 监听结束事件(包括识别成功、识别错误和用户停止)
    recognition.onend = function () {
        isListening.value = false;
        console.log('语音识别停止');
    };
};
 
const resetStatus = () => {
    currentVoiceType.value = VoiceTipType.NoSpeech;
    animationPlayState.value = 'running';
    recognition?.abort();
    window.speechSynthesis.cancel();
};
 
watch(
    () => isShow.value,
    (val) => {
        if (!val) {
            resetStatus();
        } else {
            resetToListenVoice();
        }
    }
);
</script>
<style scoped lang="scss">
@keyframes balls {
    0% {
        top: 0;
        left: 0;
    }
 
    to {
        opacity: 0;
        top: 0;
        left: 100%;
    }
}
 
@keyframes chat-voices {
    0% {
        opacity: 1;
        -webkit-transform: scale(1);
        transform: scale(1);
    }
 
    50% {
        opacity: 1;
        -webkit-transform: scale(1.5);
        transform: scale(1.5);
    }
 
    to {
        opacity: 1;
        -webkit-transform: scale(1);
        transform: scale(1);
    }
}
@keyframes change-size {
    0% {
        -webkit-transform: scale(1);
        transform: scale(1);
    }
 
    50% {
        -webkit-transform: scale(1.2);
        transform: scale(1.2);
    }
 
    to {
        -webkit-transform: scale(1);
        transform: scale(1);
    }
}
.chat {
    position: relative;
    width: 100%;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    > span {
        width: 50%;
        position: absolute;
        left: 0;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -webkit-box-align: center;
        -ms-flex-align: center;
        align-items: center;
        top: 50%;
        -webkit-transform: translateY(-50%);
        -ms-transform: translateY(-50%);
        transform: translateY(-50%);
    }
}
.chatPop {
    border-radius: 50%;
    background-color: #fff;
    -webkit-animation: chat-voice-ad71603e 1s ease 0.5s forwards;
    animation: chat-voice-ad71603e 1s ease 0.5s forwards;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -webkit-box-pack: center;
    -ms-flex-pack: center;
    justify-content: center;
    -webkit-box-align: center;
    -ms-flex-align: center;
    align-items: center;
    > span {
        border-radius: 50%;
        opacity: 0;
        background-color: #000;
        -webkit-animation: chat-voices 1.5s linear 0.9s infinite;
        animation: chat-voices 1.5s linear 0.9s infinite;
        > i[data-v-ad71603e]:first-of-type {
            width: 16px;
            height: 16px;
            border-radius: 50%;
            background-color: #fff;
            position: relative;
            -webkit-animation: balls 1s ease 0.3s forwards;
            animation: balls 1s ease 0.3s forwards;
            margin-left: 23px;
        }
 
        > i[data-v-ad71603e]:nth-of-type(2) {
            width: 23px;
            height: 23px;
            border-radius: 50%;
            background-color: #fff;
            position: relative;
            -webkit-animation: balles-ad71603e 1s ease 0.3s forwards;
            animation: balles-ad71603e 1s ease 0.3s forwards;
            margin-left: 7px;
        }
 
        > i[data-v-ad71603e]:nth-of-type(3) {
            width: 38px;
            height: 38px;
            border-radius: 50%;
            background-color: #fff;
            position: relative;
            -webkit-animation: ball-ad71603e 1s ease 0.3s forwards;
            animation: ball-ad71603e 1s ease 0.3s forwards;
            margin-left: 7px;
        }
    }
}
 
.sound-animate > span {
    -webkit-box-sizing: content-box;
    box-sizing: content-box;
    display: inline-block;
    width: 27px;
    height: 27px;
    border-radius: 50%;
    background-color: #fff;
    -webkit-animation: change-size var(--animation-duration) linear infinite alternate;
    animation: change-size var(--animation-duration) linear infinite alternate;
}
 
.sound-animate span:first-child {
    --animation-duration: 1s;
}
 
.sound-animate span:nth-child(2) {
    --animation-duration: 1.2s;
}
 
.sound-animate span:nth-child(3) {
    --animation-duration: 0.8s;
}
 
.sound-animate span:nth-child(4) {
    --animation-duration: 1.4s;
}
</style>