index.tsx 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. import styles from "./index.module.less";
  2. import { Snackbar } from "@varlet/ui";
  3. import { closeToast, showLoadingToast, showToast, Popup } from "vant";
  4. import { defineComponent, onMounted, onUnmounted, reactive, ref, watch } from "vue";
  5. import { getLeveByScore, getLeveByScoreMeasure, IEvaluatings } from "./evaluatResult";
  6. import {
  7. cancelEvaluating,
  8. endEvaluating,
  9. endSoundCheck,
  10. getEarphone,
  11. api_proxyServiceMessage,
  12. removeResult,
  13. sendResult,
  14. startEvaluating,
  15. startSoundCheck,
  16. api_openWebView,
  17. api_startRecording,
  18. api_startRecordingCb,
  19. api_stopRecording,
  20. api_recordStartTime,
  21. api_remove_recordStartTime,
  22. api_startCapture,
  23. api_endCapture,
  24. api_getDeviceDelay,
  25. hideComplexButton,
  26. api_checkSocketStatus,
  27. addAccompanyError,
  28. removeAccompanyError,
  29. addSocketStatus,
  30. removeSocketStatus,
  31. api_disconnectSocket,
  32. api_midiMicDelay,
  33. api_cloudSetCurrentTime,
  34. api_cloudChangeSpeed,
  35. api_startDelayCheck,
  36. api_closeDelayCheck,
  37. } from "/src/helpers/communication";
  38. import state, { IPlayState, clearSelection, handleStopPlay, onPlay, resetPlaybackToStart, togglePlay, initSetPlayRate } from "/src/state";
  39. import { IPostMessage } from "/src/utils/native-message";
  40. import { usePageVisibility } from "@vant/use";
  41. import { browser } from "/src/utils";
  42. import { getAudioCurrentTime, toggleMutePlayAudio, audioListStart, audioData } from "../audio-list";
  43. import { handleStartTick, closeTick } from "../tick";
  44. import AbnormalPop from "../abnormal-pop";
  45. import { storeData } from "../../store";
  46. import icon_bg from "../abnormal-pop/icon_bg.svg";
  47. import icon_close from "../abnormal-pop/icon_close.svg";
  48. import icon_btn from "../abnormal-pop/icon_btn.svg";
  49. import icon_success from "../abnormal-pop/icon_success.svg";
  50. import { data } from "../../page-instrument/custom-plugins/work-index";
  51. import { startCountdown } from "/src/page-instrument/evaluat-model/countdown";
  52. const browserInfo = browser();
  53. let socketStartTime = 0;
  54. export const popImgs = {
  55. icon_bg,
  56. icon_close,
  57. icon_btn,
  58. icon_success,
  59. };
  60. export const evaluatingData = reactive({
  61. /** 评测数据 */
  62. contentData: {} as any,
  63. /** 评测模块是否加载完成 */
  64. rendered: false,
  65. earphone: false, // 是否插入耳机
  66. soundEffect: false, // 是否效音
  67. soundEffectFrequency: 0, // 效音频率
  68. checkStep: 0, // 执行步骤
  69. checkEnd: false, // 检测结束
  70. earphoneMode: false, // 耳机弹窗
  71. earPhoneType: "" as "" | "有线耳机" | "蓝牙耳机", // 耳机类型
  72. soundEffectMode: false, // 效音弹窗
  73. websocketState: false, // websocket连接状态
  74. /**是否开始播放 */
  75. startBegin: false, // 开始
  76. backtime: 0, // 延迟时间
  77. /** 已经评测的数据 */
  78. evaluatings: {} as IEvaluatings,
  79. /** 评测结果 */
  80. resultData: {} as any,
  81. /** 评测结果弹窗 */
  82. resulstMode: false,
  83. /** 是否是完整评测 */
  84. isComplete: false,
  85. /** */
  86. isDisabledPlayMusic: false,
  87. /** socket异常状态弹窗 */
  88. socketErrorPop: false,
  89. /** 异常提示 */
  90. errorContents: '',
  91. /** socket异常状态弹窗的状态值 */
  92. socketErrorStatus: 0,
  93. /** 延迟检测,socket状态异常 */
  94. delayCheckSocketError: false,
  95. /** 异常状态,不生成评测记录,不调用保存接口 */
  96. isErrorState: false,
  97. /** accompanyError,错误类型 */
  98. accompanyErrorType: '',
  99. /** app播放结束状态,重新评测需要重置为 */
  100. isAudioPlayEnd: false,
  101. preloadJson: true, // 预加载延迟检测的资源
  102. jsonLoading: true, // 延迟检测的资源加载中状态
  103. jsonLoadDone: true, // 延迟检测的动画dom加载完成状态
  104. hideResultModal: false, // 评测作业,如果不是完整评测,需要隐藏评测结果弹窗
  105. oneselfCancleEvaluating: false, // 是否是自主取消评测,自主取消评测,不生产评测记录
  106. isBeginMask: false, // 倒计时和系统节拍器时候的遮罩,防止用户点击,
  107. recordingTime: 0, // 调用startRecording的时间
  108. endEvaluatingTime: 0, // 调用endEvaluating的时间
  109. evaluatSpeed: 0, // 评测记录的速度
  110. needReplayEvaluat: false, // 手动取消评测,需要自动开始评测
  111. });
  112. const sendOffsetTime = async (offsetTime: number) => {
  113. const delayData = await api_getDeviceDelay();
  114. api_midiMicDelay({
  115. header: {
  116. commond: "audioPlayStart",
  117. type: "SOUND_COMPARE",
  118. },
  119. body: {
  120. offsetTime,
  121. micDelay: delayData?.content?.value,
  122. },
  123. });
  124. };
  125. /** 点击开始评测按钮 */
  126. export const handleStartEvaluat = async () => {
  127. if (state.modeType === "evaluating") {
  128. handleCancelEvaluat();
  129. // 放下面会在异步之后执行 旋律线可能在会隐藏不了
  130. state.modeType = "practise";
  131. } else {
  132. // 放下面会在异步之后执行 旋律线可能在会隐藏不了
  133. state.modeType = "evaluating";
  134. if (state.platform !== "PC") {
  135. // 评测前先检查APP端的websocket状态
  136. const res = await api_checkSocketStatus();
  137. if (res?.content?.status === "connected") {
  138. handleStopPlay();
  139. } else {
  140. // socket未连接
  141. // evaluatingData.socketErrorPop = true
  142. }
  143. } else {
  144. handleStopPlay();
  145. }
  146. }
  147. //state.modeType = state.modeType === "evaluating" ? "practise" : "evaluating";
  148. if (state.modeType !== "evaluating") {
  149. // 切换到练习模式,卸载评测模块
  150. evaluatingData.rendered = false;
  151. }
  152. };
  153. /** 开始评测 & 延迟检测开始按钮 */
  154. export const startCheckDelay = async () => {
  155. // 评测前先检查APP端的websocket状态
  156. const res = await api_checkSocketStatus();
  157. if (res?.content?.status === "connected") {
  158. //
  159. return new Promise((resolve) => {
  160. resolve({ checked: true });
  161. });
  162. } else {
  163. /**
  164. * socket未连接,记录此时的时间,以便于和收到socket成功链接,进行对比,对比时间小于500ms时,则连接中的状态默认显示500ms持续时间
  165. *
  166. * */
  167. socketStartTime = +new Date();
  168. evaluatingData.socketErrorPop = true;
  169. evaluatingData.socketErrorStatus = 1;
  170. return new Promise((resolve) => {
  171. resolve({ checked: false });
  172. });
  173. }
  174. };
  175. const check_currentTime = () => {
  176. let preTime = 0;
  177. // 选段评测模式
  178. if (state.isSelectMeasureMode) {
  179. preTime = state.section[0].time * 1000;
  180. }
  181. const currentTime = getAudioCurrentTime() * 1000 - preTime;
  182. // console.log('播放进度music', currentTime, 'preTime:' + preTime)
  183. if (currentTime >= 500) {
  184. sendEvaluatingOffsetTime(500);
  185. return;
  186. }
  187. setTimeout(() => {
  188. check_currentTime();
  189. }, 10);
  190. };
  191. /** 开始播放发送延迟时间 */
  192. export const sendEvaluatingOffsetTime = async (currentTime: number) => {
  193. // 没有开始时间点, 不处理
  194. if (!evaluatingData.backtime) return;
  195. const nowTime = Date.now();
  196. const delayTime = nowTime - evaluatingData.backtime - currentTime;
  197. console.error("真正播放延迟", delayTime, "currentTime:", currentTime);
  198. await api_proxyServiceMessage({
  199. header: {
  200. commond: "audioPlayStart",
  201. type: "SOUND_COMPARE",
  202. },
  203. body: {
  204. offsetTime: delayTime < 0 ? 0 : delayTime,
  205. micDelay: 0,
  206. },
  207. });
  208. };
  209. /** 检测耳机 */
  210. export const checkUseEarphone = async () => {
  211. const res = await getEarphone();
  212. return res?.content?.checkIsWired || false;
  213. };
  214. /**
  215. * 开始录音
  216. */
  217. const handleStartSoundCheck = () => {
  218. startSoundCheck();
  219. };
  220. /** 结束录音 */
  221. export const handleEndSoundCheck = () => {
  222. endSoundCheck();
  223. };
  224. /** 连接websocket */
  225. export const connectWebsocket = async (content: any) => {
  226. evaluatingData.contentData = content;
  227. evaluatingData.websocketState = true;
  228. };
  229. /**
  230. * 执行检测
  231. */
  232. export const handlePerformDetection = async () => {
  233. // 检测完成不检测了
  234. if (evaluatingData.checkEnd) return;
  235. // 延迟检测
  236. if (evaluatingData.checkStep === 0) {
  237. evaluatingData.checkStep = 5;
  238. // 没有设备延迟数据 或 开启了效音 显示检测组件,并持续检测耳机状态
  239. if (state.setting.soundEffect) {
  240. evaluatingData.soundEffectMode = true;
  241. return;
  242. }
  243. const delayTime = await api_getDeviceDelay();
  244. console.log("🚀 ~ delayTime:", delayTime);
  245. if (!delayTime) {
  246. evaluatingData.soundEffectMode = true;
  247. return;
  248. }
  249. handlePerformDetection();
  250. return;
  251. }
  252. // 检测耳机
  253. if ((evaluatingData.checkStep = 5)) {
  254. evaluatingData.checkStep = 10;
  255. const erji = await checkUseEarphone();
  256. if (!erji) {
  257. evaluatingData.earphoneMode = true;
  258. return;
  259. }
  260. handlePerformDetection();
  261. return;
  262. }
  263. // 效音
  264. // if (evaluatingData.checkStep === 7) {
  265. // // 是否需要开启效音
  266. // evaluatingData.checkStep = 10;
  267. // if (state.setting.soundEffect && !state.isPercussion) {
  268. // evaluatingData.soundEffectMode = true;
  269. // handleStartSoundCheck();
  270. // return
  271. // }
  272. // handlePerformDetection();
  273. // return;
  274. // }
  275. // 效验完成
  276. if (evaluatingData.checkStep === 10) {
  277. evaluatingData.checkEnd = true;
  278. }
  279. };
  280. /** 记录小节分数 */
  281. export const addMeasureScore = (measureScore: any, show = true) => {
  282. // #8720 bug修复
  283. for (let idx in evaluatingData.evaluatings) {
  284. evaluatingData.evaluatings[idx].show = false;
  285. }
  286. evaluatingData.evaluatings[measureScore.measureRenderIndex] = {
  287. ...measureScore,
  288. leve: getLeveByScoreMeasure(measureScore.score),
  289. show,
  290. };
  291. // console.log("🚀 ~ measureScore:", evaluatingData.evaluatings)
  292. };
  293. const handleScoreResult = (res?: IPostMessage) => {
  294. console.log("返回", res, evaluatingData.oneselfCancleEvaluating);
  295. // 如果是手动取消评测,不生成评测记录
  296. // if (evaluatingData.oneselfCancleEvaluating) {
  297. // return;
  298. // }
  299. if (res?.content) {
  300. const { header, body } = res.content;
  301. // 效音返回
  302. if (header.commond === "checking") {
  303. evaluatingData.soundEffectFrequency = body.frequency;
  304. }
  305. // 小节评分返回
  306. if (header?.commond === "measureScore") {
  307. console.log("🚀 ~ 评测返回:", res);
  308. addMeasureScore(body);
  309. }
  310. // 评测结束返回
  311. if (header?.commond === "overall") {
  312. console.log("🚀 ~ 评测返回:", res);
  313. console.log("评测结束", body);
  314. state.isHideEvaluatReportSaveBtn = false;
  315. setTimeout(() => {
  316. // 评测作业,如果不是完整评测,不展示评测弹窗
  317. if (data.trainingType === "EVALUATION" && !evaluatingData.isComplete) {
  318. evaluatingData.hideResultModal = true;
  319. } else {
  320. evaluatingData.hideResultModal = false;
  321. }
  322. // 手动取消评测,不展示评测弹窗
  323. if (evaluatingData.oneselfCancleEvaluating) {
  324. evaluatingData.hideResultModal = true;
  325. }
  326. evaluatingData.resulstMode = evaluatingData.isErrorState ? false : true;
  327. }, 200);
  328. evaluatingData.resultData = {
  329. ...body,
  330. ...getLeveByScore(body.score),
  331. };
  332. // console.log("🚀 ~ evaluatingData.resultData:", evaluatingData.resultData)
  333. closeToast();
  334. }
  335. }
  336. };
  337. /** 开始评测 */
  338. export const handleStartBegin = async (preTimes?: number) => {
  339. if (state.isAppPlay) {
  340. await api_cloudSetCurrentTime({
  341. currentTime: 0,
  342. songID: state.examSongId,
  343. })
  344. }
  345. evaluatingData.isComplete = false;
  346. evaluatingData.evaluatings = {};
  347. evaluatingData.resultData = {};
  348. evaluatingData.backtime = 0;
  349. evaluatingData.isAudioPlayEnd = false;
  350. const res = await startEvaluating(evaluatingData.contentData);
  351. if (res?.api !== "startEvaluating") {
  352. Snackbar.error("请在APP端进行评测");
  353. evaluatingData.startBegin = false;
  354. return;
  355. }
  356. if (res?.content?.reson) {
  357. // #10984,APP端会有弹窗提示,不需要再次提示
  358. // showToast(res.content?.des);
  359. evaluatingData.startBegin = false;
  360. return;
  361. }
  362. resetPlaybackToStart();
  363. evaluatingData.startBegin = true;
  364. if (evaluatingData.isDisabledPlayMusic) {
  365. evaluatingData.isBeginMask = true
  366. // 先播放倒计时
  367. await startCountdown()
  368. state.playState = state.playState === "paused" ? "play" : "paused";
  369. // 设置为开始播放时, 如果需要节拍,先播放节拍器
  370. if (state.playState === "play" && (state.playType==="play"&&state.needTick)||(state.playType==="sing"&&state.needSingTick)) {
  371. // 如果是系统节拍器 等系统节拍器播完了再播,如果是mp3节拍器 直接播
  372. if((state.playType==="play" && !state.isOpenMetronome)||(state.playType==="sing" && !state.isSingOpenMetronome)){
  373. const tickend = await handleStartTick();
  374. console.log("🚀 ~ tickend:", tickend)
  375. // 节拍器返回false, 取消播放
  376. if (!tickend) {
  377. state.playState = "paused";
  378. evaluatingData.startBegin = false;
  379. evaluatingData.isBeginMask = false
  380. return;
  381. }
  382. }else{
  383. handleStartTick()
  384. }
  385. }
  386. evaluatingData.isBeginMask = false
  387. onPlay();
  388. }
  389. if (evaluatingData.isErrorState) return
  390. //开始录音
  391. // await api_startRecording({
  392. // accompanimentState: state.setting.enableAccompaniment ? 1 : 0,
  393. // firstNoteTime: preTimes || 0,
  394. // });
  395. let rate = state.speed / state.originSpeed;
  396. rate = parseFloat(rate.toFixed(2));
  397. await api_startRecordingCb({
  398. // accompanimentState: state.setting.enableAccompaniment ? 1 : 0,
  399. accompanimentState: !state.accompany ? 0 : 1, // 评测没有伴奏时,静音播放
  400. firstNoteTime: preTimes || 0,
  401. speedRate: rate, // 播放倍率
  402. }, () => {
  403. if (state.isAppPlay) {
  404. setTimeout(() => {
  405. sendOffsetTime(0)
  406. }, 300);
  407. }
  408. })
  409. /**
  410. * 安卓端,快速点击开始评测和结束评测,间隔太小了时,会出现异常情况(音频没有停止播放)
  411. * 记录一下startRecord的开始时间
  412. */
  413. evaluatingData.recordingTime = +new Date();
  414. // 如果开启了摄像头, 开启录制视频
  415. if (state.setting.camera) {
  416. console.log("开始录制视频");
  417. await api_startCapture();
  418. }
  419. // 如果是midi音频评测,需要调用cloudPlay
  420. if (state.isAppPlay) {
  421. await api_cloudChangeSpeed({
  422. speed: state.originSpeed,
  423. originalSpeed: state.originSpeed,
  424. songID: state.examSongId,
  425. });
  426. audioData.progress = 0
  427. audioListStart(state.playState);
  428. }
  429. evaluatingData.oneselfCancleEvaluating = false;
  430. };
  431. /** 播放音乐 */
  432. const playMusic = async () => {
  433. const playState = await togglePlay("play");
  434. // 取消播放,停止播放
  435. if (!playState) {
  436. evaluatingData.startBegin = false;
  437. handleCancelEvaluat();
  438. return;
  439. }
  440. // 检测播放进度, 计算延迟
  441. check_currentTime();
  442. // 如果开启了摄像头, 开启录制视频
  443. if (state.setting.camera) {
  444. console.log("开始录制视频");
  445. api_startCapture();
  446. }
  447. };
  448. let _audio: HTMLAudioElement;
  449. /** 录音开始,记录开始时间点 */
  450. const recordStartTimePoint = async (res?: IPostMessage) => {
  451. console.error("开始录音");
  452. // 没有开始评测,不处理
  453. if (!evaluatingData.startBegin) return;
  454. let inteveral = res?.content?.inteveral || 0;
  455. if (browserInfo.ios) {
  456. inteveral *= 1000;
  457. }
  458. evaluatingData.backtime = inteveral || Date.now();
  459. console.log("🚀 ~ 开始时间点:", evaluatingData.backtime, "已经录的时间:", Date.now() - inteveral, "记录时间点:", Date.now());
  460. // 是否禁播
  461. if (evaluatingData.isDisabledPlayMusic) {
  462. return;
  463. }
  464. // 开始播放
  465. playMusic();
  466. };
  467. /**
  468. * 结束评测
  469. * @param isComplete 是否完整评测
  470. * @param endType 结束类型,selfCancel:是否是自己取消本次评测
  471. * @returns
  472. */
  473. export const handleEndEvaluat = (isComplete = false, endType?: string) => {
  474. // 没有开始评测 , 不是评测模式 , 不评分
  475. if (!evaluatingData.startBegin || state.modeType !== "evaluating") return;
  476. // 结束录音
  477. // api_stopRecording();
  478. // 结束评测
  479. console.log("评测结束1");
  480. endEvaluating({
  481. musicScoreId: state.examSongId,
  482. });
  483. // 评测作业如果不是完整评测,给出提示
  484. if (!isComplete && data.trainingType === "EVALUATION") {
  485. showToast({
  486. message: "完整演奏结束才算作业分数!",
  487. });
  488. } else {
  489. if (!endType) {
  490. showLoadingToast({
  491. message: "评分中",
  492. duration: 0,
  493. overlay: true,
  494. overlayClass: styles.scoreMode,
  495. });
  496. }
  497. }
  498. setTimeout(() => {
  499. evaluatingData.startBegin = false;
  500. }, 500);
  501. evaluatingData.isComplete = isComplete;
  502. // 如果开启了摄像头, 结束录制视频
  503. if (state.setting.camera) {
  504. console.log("结束录制视频");
  505. api_endCapture();
  506. }
  507. };
  508. // 校验评测结束最小的时间间隔
  509. export const checkMinInterval = () => {
  510. const currentTime = +new Date();
  511. // 开始评测和结束评测的间隔时间小于800毫秒,则不处理
  512. if (currentTime - evaluatingData.recordingTime < 800) {
  513. return;
  514. }
  515. }
  516. /**
  517. * 结束评测(手动结束评测)
  518. */
  519. export const handleEndBegin = () => {
  520. handleEndEvaluat();
  521. handleStopPlay();
  522. };
  523. /**
  524. * 取消评测
  525. */
  526. export const handleCancelEvaluat = (cancelType?: string) => {
  527. evaluatingData.evaluatings = {};
  528. evaluatingData.startBegin = false;
  529. // 关闭提示
  530. closeToast();
  531. // 取消记录
  532. api_proxyServiceMessage({
  533. header: {
  534. commond: "recordCancel",
  535. type: "SOUND_COMPARE",
  536. status: 200,
  537. },
  538. });
  539. /**
  540. * 异常状态是取消评测(cancelEvaluating),正常结束时结束评测(endEvaluating)
  541. */
  542. // if (cancelType === "cancel") {
  543. // // 取消评测
  544. // cancelEvaluating();
  545. // } else {
  546. // endEvaluating({
  547. // musicScoreId: state.examSongId,
  548. // });
  549. // }
  550. cancelEvaluating();
  551. // 停止播放
  552. handleStopPlay();
  553. console.log("评测结束2");
  554. endEvaluating({
  555. musicScoreId: state.examSongId,
  556. });
  557. // 如果开启了摄像头, 结束录制视频
  558. if (state.setting.camera) {
  559. console.log("结束录制视频");
  560. api_endCapture();
  561. }
  562. };
  563. /** 查看报告 */
  564. export const handleViewReport = (key: "recordId" | "recordIdStr", type: "gym" | "colexiu" | "orchestra" | "instrument") => {
  565. const id = evaluatingData.resultData?.[key] || "";
  566. let url = "";
  567. switch (type) {
  568. case "gym":
  569. url = location.origin + location.pathname + "#/report/" + id;
  570. break;
  571. case "orchestra":
  572. url = location.origin + location.pathname + "report-share.html?id=" + id;
  573. break;
  574. case "instrument":
  575. url = location.origin + location.pathname + "#/evaluat-report?id=" + id + "&musicRenderType=" + state.musicRenderType;
  576. break;
  577. default:
  578. url = location.origin + location.pathname + "report-share.html?id=" + id;
  579. break;
  580. }
  581. api_openWebView({
  582. url,
  583. orientation: 0,
  584. isHideTitle: true, // 此处兼容安卓,意思为隐藏全部头部
  585. statusBarTextColor: false,
  586. isOpenLight: true,
  587. c_orientation: 0,
  588. });
  589. };
  590. // 隐藏存演奏按钮
  591. const handleComplexButton = (res?: IPostMessage) => {
  592. console.log("监听是否隐藏保存按钮", res);
  593. if (res?.content) {
  594. const { header, body } = res.content;
  595. state.isHideEvaluatReportSaveBtn = true;
  596. }
  597. };
  598. // 检测到APP发送的异常信息
  599. const handleAccompanyError = (res?: IPostMessage) => {
  600. console.log("异常信息返回", res);
  601. if (res?.content) {
  602. const { type, reson } = res.content;
  603. switch (type) {
  604. case "enterBackground":
  605. // App退到后台
  606. case "playError":
  607. // 播放异常
  608. case "socketError":
  609. // socket连接断开,评测中,则取消评测
  610. // 延迟检测中
  611. if (evaluatingData.soundEffectMode) {
  612. evaluatingData.socketErrorStatus = 0;
  613. evaluatingData.delayCheckSocketError = true;
  614. evaluatingData.socketErrorPop = type === "socketError" ? true : false;
  615. evaluatingData.accompanyErrorType = type;
  616. // api_checkSocketStatus()
  617. return;
  618. }
  619. // 评测中
  620. if (state.modeType === "evaluating" && evaluatingData.startBegin) {
  621. handleCancelEvaluat("cancel");
  622. }
  623. // 关闭节拍器
  624. closeTick();
  625. // socketerrror,才发送关闭延迟检测的消息
  626. if (type === "socketError") {
  627. api_closeDelayCheck({});
  628. }
  629. evaluatingData.socketErrorStatus = 0;
  630. evaluatingData.socketErrorPop = type === "socketError" ? true : false;
  631. evaluatingData.isErrorState = true;
  632. evaluatingData.accompanyErrorType = type;
  633. resetPlaybackToStart();
  634. break;
  635. case "recordError":
  636. // 录音异常
  637. break;
  638. default:
  639. break;
  640. }
  641. }
  642. };
  643. // 监测socket状态,是否已经成功连接
  644. const handleSocketStatus = (res?: IPostMessage) => {
  645. if (res?.content?.status === "connected") {
  646. const currentTime = +new Date();
  647. evaluatingData.delayCheckSocketError = false;
  648. const diffTime = currentTime - socketStartTime;
  649. if (diffTime < 1000) {
  650. const remainingTime = 1000 - diffTime;
  651. setTimeout(() => {
  652. evaluatingData.socketErrorStatus = 2;
  653. }, remainingTime);
  654. }
  655. }
  656. };
  657. // 评测出现异常,再试一次
  658. export const hanldeConfirmPop = async () => {
  659. api_checkSocketStatus();
  660. evaluatingData.socketErrorStatus = 1;
  661. socketStartTime = +new Date();
  662. };
  663. // 关闭异常弹窗
  664. export const hanldeClosePop = () => {
  665. evaluatingData.socketErrorPop = false;
  666. evaluatingData.socketErrorStatus = 0;
  667. };
  668. export default defineComponent({
  669. name: "evaluating",
  670. setup() {
  671. const pageVisibility = usePageVisibility();
  672. // 需要记录的数据
  673. const record_old_data = reactive({
  674. /** 指法 */
  675. finger: false,
  676. /** 原音伴奏 */
  677. play_mode: "" as IPlayState,
  678. /** 评测是否要伴奏 */
  679. enableAccompaniment: true,
  680. });
  681. /** 记录状态 */
  682. const hanlde_record = () => {
  683. // 取消指法
  684. record_old_data.finger = state.setting.displayFingering;
  685. state.setting.displayFingering = false;
  686. // 切换为伴奏
  687. record_old_data.play_mode = state.playSource;
  688. record_old_data.enableAccompaniment = state.setting.enableAccompaniment;
  689. // 如果关闭伴奏,评测静音
  690. if (!record_old_data.enableAccompaniment) {
  691. console.log("关闭伴奏");
  692. toggleMutePlayAudio(record_old_data.play_mode === "music" ? "music" : record_old_data.play_mode === "background" ? "background" : "mingSong", true);
  693. }
  694. };
  695. /** 还原状态 */
  696. const handle_reduction = () => {
  697. // 还原指法
  698. state.setting.displayFingering = record_old_data.finger;
  699. state.playSource = record_old_data.play_mode;
  700. // 如果关闭伴奏, 结束评测取消静音
  701. if (!record_old_data.enableAccompaniment) {
  702. toggleMutePlayAudio(record_old_data.play_mode === "music" ? "music" : record_old_data.play_mode === "background" ? "background" : "mingSong", false);
  703. }
  704. };
  705. watch(pageVisibility, (value) => {
  706. if (value == "hidden" && evaluatingData.startBegin) {
  707. // handleEndBegin();
  708. }
  709. });
  710. watch(
  711. () => evaluatingData.socketErrorStatus,
  712. () => {
  713. if (evaluatingData.socketErrorStatus === 2) {
  714. setTimeout(() => {
  715. evaluatingData.socketErrorPop = false;
  716. // evaluatingData.socketErrorStatus = 0
  717. }, 1000);
  718. }
  719. }
  720. );
  721. watch(
  722. () => evaluatingData.socketErrorPop,
  723. () => {
  724. if (evaluatingData.socketErrorPop && state.setting.soundEffect) {
  725. // 监听到socket状态异常,需要关闭延迟检测
  726. api_closeDelayCheck({});
  727. }
  728. }
  729. );
  730. onMounted(() => {
  731. resetPlaybackToStart();
  732. hanlde_record();
  733. evaluatingData.resultData = {};
  734. // evaluatingData.resulstMode = true;
  735. // evaluatingData.resultData = {...getLeveByScore(10), score: 10, intonation: 10, cadence: 30, integrity: 40}
  736. // console.log("🚀 ~ evaluatingData.resultData:", evaluatingData.resultData)
  737. evaluatingData.evaluatings = {};
  738. evaluatingData.soundEffectFrequency = 0;
  739. evaluatingData.checkStep = 0;
  740. evaluatingData.rendered = true;
  741. sendResult(handleScoreResult);
  742. hideComplexButton(handleComplexButton, true);
  743. api_recordStartTime(recordStartTimePoint);
  744. addAccompanyError(handleAccompanyError);
  745. addSocketStatus(handleSocketStatus);
  746. // 不是选段模式评测, 就清空已选段
  747. if (!state.isSelectMeasureMode) {
  748. clearSelection();
  749. }
  750. console.log("加载评测模块成功");
  751. });
  752. onUnmounted(() => {
  753. evaluatingData.checkEnd = false;
  754. evaluatingData.rendered = false;
  755. resetPlaybackToStart();
  756. removeResult(handleScoreResult);
  757. hideComplexButton(() => {}, false);
  758. api_remove_recordStartTime(recordStartTimePoint);
  759. handle_reduction();
  760. removeAccompanyError(handleAccompanyError);
  761. if (evaluatingData.socketErrorPop && state.setting.soundEffect) {
  762. console.log('延迟检测出错')
  763. } else {
  764. removeSocketStatus(handleSocketStatus);
  765. }
  766. api_disconnectSocket();
  767. console.log("卸载评测模块成功");
  768. });
  769. return () => (
  770. <div>
  771. {/** 预加载一下断网需要用到的图片 */}
  772. <div class={styles.hiddenPop}>
  773. <img src={popImgs.icon_bg} />
  774. <img src={popImgs.icon_btn} />
  775. <img src={popImgs.icon_success} />
  776. <img src={popImgs.icon_close} />
  777. </div>
  778. <Popup teleport="body" closeOnClickOverlay={false} class={["popup-custom", "van-scale"]} transition="van-scale" v-model:show={evaluatingData.socketErrorPop}>
  779. <AbnormalPop onConfirm={hanldeConfirmPop} onClose={hanldeClosePop} />
  780. </Popup>
  781. </div>
  782. );
  783. },
  784. });