ai_career_interview_chat.vue 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. <template>
  2. <div class="ai_career_interview_chat">
  3. <div class="left-box">
  4. <div class="profile">
  5. <img src="@/assets/images/environment/ai-career-profile.png" alt="404">
  6. </div>
  7. <div class="introduce-text">
  8. 在生涯访谈中,通过与不同职业人物对话,可以了解不同职业的发展前景、职业要求,为尽早规划职业发展 xxxx
  9. <p>访谈时你可以提出以下问题:</p>
  10. </div>
  11. <div class="question-list">
  12. <p class="question-list-item">您为什么要选择这一职业?</p>
  13. <p class="question-list-item">这项职业每天的核心任务是什么?</p>
  14. <p class="question-list-item">这项职业的工作环境如何?(室内/室外,办公室/工厂,团队工作/独自工作等)</p>
  15. <p class="question-list-item">从事这项职业需要哪些学历、证书、经验或关键能力?</p>
  16. <p class="question-list-item">这项职业在招聘或晋升时,更看重哪些个人特质或阶段特征?</p>
  17. <p class="question-list-item">大学里哪些专业与这项职业直接相关?</p>
  18. <p class="question-list-item">为了从事这项职业,在校期间应做哪些具体准备?</p>
  19. <p class="question-list-item">请分享一个最有成就感的项目/瞬间,以及一次印象深刻的挫折,您从中学到的关键经验是什么?</p>
  20. <p class="question-list-item">这项职业未来的发展前景如何?</p>
  21. <p class="question-list-item">这项职业进一步提升的途径有哪些?(培训、证书、项目、学历等)</p>
  22. <p class="question-list-item">如果未来的我将选择从事该职业,您会给我哪些具体建议?</p>
  23. <p class="question-list-item">请用时间轴描述一个典型工作日从上班到下班的详细流程?</p>
  24. </div>
  25. </div>
  26. <div class="center-box">
  27. <div class="center-header">
  28. <div style="margin-left: 30px"></div>
  29. <div>小新老师</div>
  30. <div class="header-exit-btn">退出</div>
  31. </div>
  32. <div v-loading="loadingHistoryRecord" class="chat-container">
  33. <div class="message-list" ref="chatContainerRef">
  34. <div v-for="(item, index) in chatRecordList" :key="item.id">
  35. <div v-if="item.isUser == 1" class="user-message">
  36. <img src="@/assets/images/common/ai/user.png" alt="icon" style="margin-left: 4px"/>
  37. <span class="user-message-content">{{item.content}}</span>
  38. </div>
  39. <div v-else class="bot-message">
  40. <img src="@/assets/images/common/ai/system.png" alt="icon" style="margin-left: 4px"/>
  41. <div style="margin-left: 4px;">
  42. <div class="bot-message-content">
  43. <div v-if="index === chatRecordList.length-1">
  44. <div v-if="item.isTyping">
  45. <div v-html="item.content"></div>
  46. <span class="loading-dots" style="display: flex;justify-content: center">
  47. <span class="dot"></span>
  48. <span class="dot"></span>
  49. </span>
  50. </div>
  51. <div v-else>
  52. <div v-html="item.content"></div>
  53. </div>
  54. </div>
  55. <div v-else>
  56. <div v-show="!expandIndexList.includes(index)" v-html="item.content.substring(0, 80)"></div>
  57. <div v-show="expandIndexList.includes(index)" v-html="item.content"></div>
  58. </div>
  59. </div>
  60. <div style="margin: 4px">
  61. <el-text>{{item.createTime}}</el-text>
  62. </div>
  63. </div>
  64. <div v-if="index !== chatRecordList.length-1">
  65. <el-button v-if="item.content.length>80" style="margin-left: 4px"
  66. @click="foldOrExpandMessage(index, expandIndexList.includes(index))">
  67. {{expandIndexList.includes(index)?'折叠':'展开'}}
  68. </el-button>
  69. </div>
  70. <el-button @click="ttsStartPlay(item.content, index)">{{playActiveIndex === index && playButtonFlag ?'停止':'播放'}}</el-button>
  71. </div>
  72. </div>
  73. </div>
  74. </div>
  75. <div class="input-container">
  76. <el-input
  77. v-model="inputMessage"
  78. type="textarea"
  79. placeholder="请输入消息"
  80. @keyup.enter="sendMessage"
  81. ></el-input>
  82. <el-button @click="sendMessage" :disabled="isSending" type="primary">发送</el-button>
  83. </div>
  84. </div>
  85. </div>
  86. </template>
  87. <script setup>
  88. import headComponent from '@/views/xjc-integratedmachine/components/head_component.vue'
  89. import Drag_component from "@/views/xjc-integratedmachine/components/drag_component.vue";
  90. import {onMounted, ref, nextTick} from "vue";
  91. import { getToken } from '@/utils/auth'
  92. // md转换为html
  93. import { marked } from 'marked'
  94. // 语音识别
  95. import CryptoJS from 'crypto-js';
  96. import * as RecorderManager from "/public/ai/iat/dist/index.umd.js"
  97. // 语音播放
  98. import {Base64} from 'js-base64'
  99. import * as AudioPlayer from "/public/ai/tts/dist/index.umd.js"
  100. const router = useRouter()
  101. const route = useRoute()
  102. const param = route.query
  103. const loadingHistoryRecord = ref(false)
  104. // 聊天记录
  105. let chatRecordList = ref([])
  106. // md流式数据
  107. const streamMarkdownData = ref('');
  108. // html流式数据
  109. let streamHtmlData = ref('');
  110. // 流式加载状态
  111. const isLoading = ref(false);
  112. // 读取器实例
  113. let reader = null;
  114. // AbortController用于中止请求
  115. let controller = null;
  116. // 输入的问题
  117. let inputMessage = ref('')
  118. // 发送标识
  119. let isSending = ref(false)
  120. // 选中的消息,用于展开折叠
  121. let expandIndexList = ref([])
  122. // 播放按钮状态
  123. let playActiveIndex = ref(0)
  124. let playButtonFlag = ref(false)
  125. // 查看所有聊天记录
  126. function list() {
  127. loadingHistoryRecord.value = true
  128. let queryForm = {
  129. pageNum: 1,
  130. pageSize: 10000
  131. }
  132. aiChatRecordList(queryForm).then(resp =>{
  133. chatRecordList.value = resp.rows;
  134. loadingHistoryRecord.value = false
  135. setTimeout(()=>{
  136. scrollToBottom();
  137. }, 100);
  138. if(resp.total == 0){
  139. sayHi();
  140. }
  141. })
  142. }
  143. function addRecord(content) {
  144. let queryForm = {
  145. content: content,
  146. isUser: 0
  147. }
  148. aiChatRecordAdd(queryForm).then(resp =>{
  149. console.log(resp)
  150. })
  151. }
  152. function sayHi() {
  153. const botMsg = {
  154. isUser: false,
  155. content: '你好,我是来帮助你进行学习规划、选科辅导以及志愿填报的生涯教育专家。如果你有关于这些方面的问题,欢迎随时向我咨询!', // 增量填充
  156. isTyping: false, // 显示加载动画
  157. }
  158. chatRecordList.value.push(botMsg)
  159. }
  160. const sendMessage = () => {
  161. if (inputMessage.value.trim()) {
  162. sendRequest(inputMessage.value.trim())
  163. inputMessage.value = ''
  164. }
  165. }
  166. const sendRequest = async(message) => {
  167. // 用户信息
  168. const userMessage = {
  169. isUser: true,
  170. content: message,
  171. isTyping: false
  172. }
  173. // 消息加入聊天记录
  174. chatRecordList.value.push(userMessage)
  175. const botMsg = {
  176. isUser: false,
  177. content: '', // 增量填充
  178. isTyping: true, // 显示加载动画
  179. }
  180. chatRecordList.value.push(botMsg)
  181. const lastMsg = chatRecordList.value[chatRecordList.value.length - 1]
  182. try{
  183. isLoading.value = true;
  184. streamMarkdownData.value = ''; // 清空之前的数据
  185. // 创建AbortController以便可以中止请求
  186. controller = new AbortController();
  187. // 请求体
  188. let form = {
  189. "content": message? message: "你是谁?"
  190. }
  191. // 发送fetch请求
  192. const response = await fetch('/dev-api/ai/chat/record/stream', {
  193. method: 'POST',
  194. headers: {
  195. 'Content-Type': 'application/json',
  196. 'Authorization': 'Bearer ' + getToken()
  197. },
  198. body: JSON.stringify(form)
  199. });
  200. // 获取可读流的读取器
  201. reader = response.body.getReader();
  202. const decoder = new TextDecoder('utf-8');
  203. // 循环读取流数据
  204. while (true) {
  205. const { done, value } = await reader.read();
  206. if (done) break; // 如果流读取完成则退出循环
  207. // 解码并追加数据
  208. let chunk = decoder.decode(value, { stream: true });
  209. chunk = chunk.replace(/\n\n/g, '').replace(/data:/g, '')
  210. streamMarkdownData.value += chunk;
  211. streamHtmlData.value = marked(streamMarkdownData.value)
  212. lastMsg.content = marked(streamMarkdownData.value)
  213. }
  214. }catch (error) {
  215. // 如果是手动中止,不显示错误
  216. if (error.name !== 'AbortError') {
  217. console.error('流式读取失败:', error);
  218. streamHtmlData.value = 'Error: ' + error.message;
  219. }
  220. } finally {
  221. isLoading.value = false;
  222. let htmlData = marked(streamMarkdownData.value);
  223. addRecord(htmlData);
  224. streamHtmlData.value = htmlData;
  225. lastMsg.content = marked(streamMarkdownData.value)
  226. lastMsg.isTyping = false
  227. // 组件挂载后自动滚动到底部
  228. scrollToBottom();
  229. }
  230. }
  231. const stopMessage = () => {
  232. if (reader) {
  233. // 取消读取
  234. reader.cancel().catch(() => {});
  235. reader = null;
  236. }
  237. if (controller) {
  238. // 中止请求
  239. controller.abort();
  240. controller = null;
  241. }
  242. isLoading.value = false;
  243. }
  244. function foldOrExpandMessage(index, flag){
  245. if(flag){
  246. expandIndexList.value = expandIndexList.value.filter((item)=>{
  247. return item !== index;
  248. })
  249. }else{
  250. expandIndexList.value.push(index)
  251. }
  252. }
  253. /* 语音识别 ↓*/
  254. // 控制录音弹窗
  255. let iatVoiceOpen = false
  256. // 是否开始录音
  257. let iatStartVoiceStatus = false
  258. // 识别中状态
  259. let iatIdentifyStatus = false
  260. // 录音对象
  261. let iatRecorder = null
  262. let iatTranscription = ''
  263. let iatBtnStatus = ''
  264. let iatResultText = ''
  265. let iatResultTextTemp = ''
  266. let iatCountdownInterval = null
  267. let iatWS = null
  268. let iatRecognition = null
  269. let iatButtonDisabled = true
  270. let iatLoading = false
  271. let xfIatKeys = {
  272. APPID: '5a2643f4',
  273. APISecret: 'MTg4MWIzY2VmYTg2YTEwMjliMDY1N2Iz',
  274. APIKey: '8b1a53486bec887eb817b4410aa743ed',
  275. }
  276. // 初始化语音识别
  277. function initSpeechRecognition(){
  278. // 初始化录音
  279. iatRecorder = new window.RecorderManager('/ai/iat/dist');
  280. // 开始录音
  281. iatRecorder.onStart = () => {
  282. iatChangeBtnStatus('OPEN');
  283. };
  284. iatRecorder.onFrameRecorded = ({ isLastFrame, frameBuffer }) => {
  285. if (iatWS.readyState === iatWS.OPEN) {
  286. iatWS.send(
  287. JSON.stringify({
  288. data: {
  289. status: isLastFrame ? 2 : 1,
  290. format: 'audio/L16;rate=16000',
  291. encoding: 'raw',
  292. audio: toBase64(frameBuffer),
  293. },
  294. }),
  295. );
  296. if (isLastFrame) {
  297. iatChangeBtnStatus('CLOSING');
  298. }
  299. }
  300. };
  301. iatRecorder.onStop = () => {
  302. console.log('录音结束,停止定时器');
  303. clearInterval(iatCountdownInterval);
  304. iatStartVoiceStatus = false;
  305. };
  306. }
  307. async function iatStartVoice() {
  308. if (iatLoading) {
  309. return;
  310. }
  311. iatVoiceOpen = true;
  312. await iatPlayIatVoice();
  313. }
  314. async function iatPlayIatVoice() {
  315. if (iatLoading) {
  316. return;
  317. }
  318. iatStartVoiceStatus = !iatStartVoiceStatus;
  319. // 浏览器自带的识别
  320. if (iatRecognition) {
  321. if (iatStartVoiceStatus) {
  322. iatRecognition.start();
  323. } else {
  324. iatRecognition.stop();
  325. }
  326. return;
  327. }
  328. if (iatStartVoiceStatus) {
  329. iatConnectWebSocket();
  330. } else {
  331. iatRecorder.stop();
  332. }
  333. }
  334. /**
  335. * 关闭录音弹窗
  336. */
  337. function iatCloseVoiceOpen() {
  338. iatVoiceOpen = false;
  339. iatStartVoiceStatus = false;
  340. if (iatRecorder) {
  341. iatRecorder.stop();
  342. }
  343. if (iatRecognition) {
  344. iatRecognition.stop();
  345. }
  346. iatTranscription = '';
  347. }
  348. function iatRenderResult(resultData) {
  349. // 识别结束
  350. const jsonData = JSON.parse(resultData);
  351. if (jsonData.data && jsonData.data.result) {
  352. const data = jsonData.data.result;
  353. let str = '';
  354. const { ws } = data;
  355. for (let i = 0; i < ws.length; i += 1) {
  356. str += ws[i].cw[0].w;
  357. }
  358. // 开启wpgs会有此字段(前提:在控制台开通动态修正功能)
  359. // 取值为 "apd"时表示该片结果是追加到前面的最终结果;取值为"rpl" 时表示替换前面的部分结果,替换范围为rg字段
  360. if (data.pgs) {
  361. if (data.pgs === 'apd') {
  362. // 将resultTextTemp同步给resultText
  363. iatResultText = iatResultTextTemp;
  364. }
  365. // 将结果存储在resultTextTemp中
  366. iatResultTextTemp = iatResultText + str;
  367. } else {
  368. iatResultText += str;
  369. }
  370. inputMessage.value = iatResultTextTemp || iatResultText || '';
  371. }
  372. if (jsonData.code === 0 && jsonData.data.status === 2) {
  373. iatWS.close();
  374. }
  375. if (jsonData.code !== 0) {
  376. iatWS.close();
  377. console.error(jsonData);
  378. }
  379. }
  380. function iatConnectWebSocket() {
  381. const websocketUrl = iatGetWebSocketUrl();
  382. if ('WebSocket' in window) {
  383. iatWS = new window.WebSocket(websocketUrl);
  384. } else if ('MozWebSocket' in window) {
  385. iatWS = new window.MozWebSocket(websocketUrl);
  386. } else {
  387. message.error('浏览器不支持WebSocket');
  388. return;
  389. }
  390. iatChangeBtnStatus('CONNECTING');
  391. iatWS.onopen = e => {
  392. console.log('iatWS.onopen', e);
  393. // 开始录音
  394. iatRecorder.start({
  395. sampleRate: 16000,
  396. frameSize: 1280,
  397. });
  398. const params = {
  399. common: {
  400. app_id: xfIatKeys.APPID,
  401. },
  402. business: {
  403. language: 'zh_cn',
  404. domain: 'iat',
  405. accent: 'mandarin',
  406. vad_eos: 5000,
  407. dwa: 'wpgs',
  408. nbest: 1,
  409. wbest: 1,
  410. },
  411. data: {
  412. status: 0,
  413. format: 'audio/L16;rate=16000',
  414. encoding: 'raw',
  415. },
  416. };
  417. iatWS.send(JSON.stringify(params));
  418. };
  419. iatWS.onmessage = e => {
  420. iatRenderResult(e.data);
  421. };
  422. iatWS.onerror = e => {
  423. console.error(e);
  424. iatRecorder.stop();
  425. iatChangeBtnStatus('CLOSED');
  426. };
  427. iatWS.onclose = e => {
  428. console.log(e);
  429. iatRecorder.stop();
  430. iatChangeBtnStatus('CLOSED');
  431. };
  432. }
  433. function iatGetWebSocketUrl() {
  434. const { APIKey, APISecret } = xfIatKeys;
  435. if (!APIKey) {
  436. message.error('语音识别配置未生效');
  437. return null;
  438. }
  439. // 请求地址根据语种不同变化
  440. let url = 'wss://iat-api.xfyun.cn/v2/iat';
  441. const host = 'iat-api.xfyun.cn';
  442. const apiKey = APIKey;
  443. const apiSecret = APISecret;
  444. const date = new Date().toGMTString();
  445. const algorithm = 'hmac-sha256';
  446. const headers = 'host date request-line';
  447. const signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2/iat HTTP/1.1`;
  448. const signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret);
  449. const signature = CryptoJS.enc.Base64.stringify(signatureSha);
  450. const authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`;
  451. const authorization = btoa(authorizationOrigin);
  452. url = `${url}?authorization=${authorization}&date=${date}&host=${host}`;
  453. return url;
  454. }
  455. function iatCountdown() {
  456. let seconds = 60;
  457. console.log(`录音中(${seconds}s)`);
  458. iatCountdownInterval = setInterval(() => {
  459. seconds -= 1;
  460. if (seconds <= 0) {
  461. clearInterval(iatCountdownInterval);
  462. iatRecorder.stop();
  463. } else {
  464. console.log(`录音中(${seconds}s)`);
  465. }
  466. }, 1000);
  467. }
  468. function iatChangeBtnStatus(status) {
  469. iatBtnStatus = status;
  470. if (status === 'CONNECTING') {
  471. console.log('建立连接中');
  472. iatResultText = '';
  473. iatResultTextTemp = '';
  474. } else if (status === 'OPEN') {
  475. if (iatRecorder) {
  476. iatCountdown();
  477. }
  478. } else if (status === 'CLOSING') {
  479. console.log('关闭连接中');
  480. } else if (status === 'CLOSED') {
  481. console.log('开始录音');
  482. }
  483. }
  484. function toBase64(buffer) {
  485. let binary = '';
  486. const bytes = new Uint8Array(buffer);
  487. const len = bytes.byteLength;
  488. for (let i = 0; i < len; i += 1) {
  489. binary += String.fromCharCode(bytes[i]);
  490. }
  491. return window.btoa(binary);
  492. }
  493. /* 语音识别 ↑*/
  494. /*语音合成 ↓*/
  495. // 合成对象
  496. let audioPlayer = null
  497. // 按钮状态:"UNDEFINED" "CONNECTING" "PLAY" "STOP"
  498. let btnStatus = "UNDEFINED";
  499. //
  500. let ttsWS = null;
  501. function initSpeechSynthesis(){
  502. // 初始化语音播放
  503. audioPlayer = new window.AudioPlayer("/ai/tts/dist");
  504. // 开始播放
  505. audioPlayer.onPlay = () => {
  506. changeBtnStatus("PLAY");
  507. };
  508. audioPlayer.onStop = (audioDatas) => {
  509. console.log('停止播放');
  510. // 修改为undefined,下次播放重头开始
  511. btnStatus === "PLAY" && changeBtnStatus("UNDEFINED");
  512. };
  513. }
  514. function changeBtnStatus(status) {
  515. btnStatus = status;
  516. if (status === "UNDEFINED") {
  517. console.log("立即合成");
  518. } else if (status === "CONNECTING") {
  519. console.log("正在合成");
  520. } else if (status === "PLAY") {
  521. console.log("停止播放");
  522. } else if (status === "STOP") {
  523. console.log("重新播放");
  524. }
  525. }
  526. function getWebSocketUrl() {
  527. const { APIKey, APISecret } = xfIatKeys;
  528. if (!APIKey) {
  529. message.error('语音合成配置未生效');
  530. return null;
  531. }
  532. let url = "wss://tts-api.xfyun.cn/v2/tts";
  533. let host = "tts-api.xfyun.cn";
  534. const apiKey = APIKey;
  535. const apiSecret = APISecret;
  536. let date = new Date().toGMTString();
  537. let algorithm = "hmac-sha256";
  538. let headers = "host date request-line";
  539. let signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v2/tts HTTP/1.1`;
  540. let signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret);
  541. let signature = CryptoJS.enc.Base64.stringify(signatureSha);
  542. let authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`;
  543. let authorization = btoa(authorizationOrigin);
  544. url = `${url}?authorization=${authorization}&date=${date}&host=${host}`;
  545. return url;
  546. }
  547. function encodeText(text, type) {
  548. if (type === "unicode") {
  549. let buf = new ArrayBuffer(text.length * 4);
  550. let bufView = new Uint16Array(buf);
  551. for (let i = 0, strlen = text.length; i < strlen; i++) {
  552. bufView[i] = text.charCodeAt(i);
  553. }
  554. let binary = "";
  555. let bytes = new Uint8Array(buf);
  556. let len = bytes.byteLength;
  557. for (let i = 0; i < len; i++) {
  558. binary += String.fromCharCode(bytes[i]);
  559. }
  560. return window.btoa(binary);
  561. } else {
  562. return Base64.encode(text);
  563. }
  564. }
  565. function ttsStartPlay(text, index) {
  566. playActiveIndex.value = index
  567. playButtonFlag.value = !playButtonFlag.value;
  568. if (btnStatus === "UNDEFINED") {
  569. // 开始合成
  570. connectWebSocket(text.replace(/<\/?.+?\/?>/g,''));
  571. } else if (btnStatus === "CONNECTING") {
  572. // 停止合成
  573. changeBtnStatus("UNDEFINED");
  574. ttsWS?.close();
  575. audioPlayer.reset();
  576. return;
  577. } else if (btnStatus === "PLAY") {
  578. audioPlayer.stop();
  579. } else if (btnStatus === "STOP") {
  580. audioPlayer.play();
  581. }
  582. };
  583. function connectWebSocket(text) {
  584. const url = getWebSocketUrl();
  585. if ("WebSocket" in window) {
  586. ttsWS = new WebSocket(url);
  587. } else if ("MozWebSocket" in window) {
  588. ttsWS = new MozWebSocket(url);
  589. } else {
  590. alert("浏览器不支持WebSocket");
  591. return;
  592. }
  593. changeBtnStatus("CONNECTING");
  594. ttsWS.onopen = (e) => {
  595. audioPlayer.start({
  596. autoPlay: true,
  597. sampleRate: 16000,
  598. resumePlayDuration: 1000
  599. });
  600. changeBtnStatus("PLAY");
  601. let tte = "UTF8";
  602. let params = {
  603. common: {
  604. app_id: xfIatKeys.APPID,
  605. },
  606. business: {
  607. aue: "raw",
  608. auf: "audio/L16;rate=16000",
  609. vcn: "xiaoyan",
  610. // vcn: "aisbabyxu",
  611. // vcn: "aisjinger",
  612. // vcn: "aisxping",
  613. // vcn: "aisjinger",
  614. // vcn: "x4_lingxiaoyao_em",
  615. // vcn: "x4_lingxiaoyao_en",
  616. speed: +50,
  617. volume: +50,
  618. pitch: +50,
  619. bgs: 1,
  620. tte,
  621. },
  622. data: {
  623. status: 2,
  624. text: encodeText(text, tte),
  625. },
  626. };
  627. ttsWS.send(JSON.stringify(params));
  628. };
  629. ttsWS.onmessage = (e) => {
  630. let jsonData = JSON.parse(e.data);
  631. // 合成失败
  632. if (jsonData.code !== 0) {
  633. console.error(jsonData);
  634. changeBtnStatus("UNDEFINED");
  635. return;
  636. }
  637. audioPlayer.postMessage({
  638. type: "base64",
  639. data: jsonData.data.audio,
  640. isLastData: jsonData.data.status === 2,
  641. });
  642. if (jsonData.code === 0 && jsonData.data.status === 2) {
  643. ttsWS.close();
  644. }
  645. };
  646. ttsWS.onerror = (e) => {
  647. console.error(e);
  648. };
  649. ttsWS.onclose = (e) => {
  650. // console.log(e);
  651. };
  652. }
  653. /*语音合成 ↑*/
  654. let chatContainerRef = ref(null)
  655. function scrollToBottom(){
  656. if(chatContainerRef.value.scrollHeight){
  657. chatContainerRef.value.scrollTop = chatContainerRef.value.scrollHeight+20;
  658. }
  659. }
  660. onMounted(()=>{
  661. nextTick(()=>{
  662. initSpeechRecognition();
  663. initSpeechSynthesis();
  664. list();
  665. })
  666. })
  667. </script>
  668. <style scoped lang="scss">
  669. p{
  670. margin: 0;
  671. padding: 0;
  672. }
  673. .ai_career_interview_chat{
  674. background: url('@/assets/images/environment/ai-career-background.png') no-repeat;
  675. background-size: 1920px 1080px;
  676. z-index:10;
  677. width: 100%;
  678. height: 1080px;
  679. display: flex;
  680. .left-box{
  681. /*position: absolute;*/
  682. margin-top: 18px;
  683. margin-left: 44px;
  684. width: 428px;
  685. height: 1044px;
  686. background: rgba(255,255,255,0.4);
  687. border-radius: 10px 10px 10px 10px;
  688. .profile {
  689. img{
  690. width: 117px;
  691. height: 136px;
  692. margin-top: 48px;
  693. margin-left: 145px;
  694. }
  695. }
  696. .introduce-text{
  697. width: 392px;
  698. margin-left: 14px;
  699. font-weight: 400;
  700. font-size: 28px;
  701. color: #7D7C7C;
  702. line-height: 40px;
  703. text-align: left;
  704. font-style: normal;
  705. text-transform: none;
  706. }
  707. .question-list{
  708. width: 392px;
  709. height: 500px;
  710. overflow: auto;
  711. margin-left: 14px;
  712. font-weight: 400;
  713. font-size: 28px;
  714. color: #7D7C7C;
  715. line-height: 40px;
  716. text-align: left;
  717. font-style: normal;
  718. text-transform: none;
  719. .question-list-item{
  720. cursor: pointer;
  721. }
  722. }
  723. }
  724. .center-box{
  725. width: 1390px;
  726. height: 1044px;
  727. margin-top: 18px;
  728. margin-left: 14px;
  729. background: rgba(255,255,255,0.2);
  730. border-radius: 10px 10px 10px 10px;
  731. .center-header{
  732. width: 1390px;
  733. height: 95px;
  734. background: #D8F9F2;
  735. border-radius: 10px 10px 0px 0px;
  736. font-weight: bold;
  737. font-size: 30px;
  738. color: #333333;
  739. line-height: 52px;
  740. letter-spacing: 3px;
  741. text-align: left;
  742. font-style: normal;
  743. text-transform: none;
  744. display: flex;
  745. justify-content: space-between;
  746. align-items: center;
  747. .header-exit-btn{
  748. width: 136px;
  749. height: 74px;
  750. background: #FB5451;
  751. border-radius: 10px 10px 10px 10px;
  752. font-weight: 400;
  753. font-size: 24px;
  754. color: #FFFFFF;
  755. /*text-align: center;*/
  756. font-style: normal;
  757. text-transform: none;
  758. display: flex;
  759. align-items: center;
  760. justify-content: center;
  761. margin-right: 30px;
  762. }
  763. }
  764. }
  765. }
  766. </style>