index.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. import { Button, Popup } from "vant";
  2. import styles from "./index.module.less";
  3. import { defineComponent, onMounted, onUnmounted, reactive, ref, watch } from "vue";
  4. import { api_toggleTune, getEarphone, removeResult, sendResult, addCheckPlayEnd, removeCheckPlayEnd } from "/src/helpers/communication";
  5. import { Vue3Lottie } from "vue3-lottie";
  6. import bg from "./json/bg.json";
  7. import bg1 from "./json/bg1.json";
  8. import l1 from "./json/l1.json";
  9. import f1 from "./json/f1.json";
  10. import f2 from "./json/f2.json";
  11. import f3 from "./json/f3.json";
  12. import f4 from "./json/f4.json";
  13. import f5 from "./json/f5.json";
  14. import f6 from "./json/f6.json";
  15. import f7 from "./json/f7.json";
  16. import r1 from "./image/r1.png";
  17. import icon_1 from "./image/icon_1.png";
  18. import icon_1_1 from "./image/icon_1_1.png";
  19. import icon_2_1 from "./image/icon_2_1.png";
  20. import icon_2_2 from "./image/icon_2_2.png";
  21. import icon_2_3 from "./image/icon_2_3.png";
  22. import icon_2_4 from "./image/icon_2_4.png";
  23. import icon_4_0 from "./image/icon_4_0.png";
  24. import icon_4_1 from "./image/icon_4_1.png";
  25. import icon_4_2 from "./image/icon_4_2.png";
  26. import icon_3_0 from "./image/icon_3_0.png";
  27. import icon_3_1 from "./image/icon_3_1.png";
  28. import icon_3_2 from "./image/icon_3_2.png";
  29. import iconBack from "./image/icon-back.png";
  30. import { evaluatingData, startCheckDelay } from "/src/view/evaluating";
  31. import { IPostMessage } from "/src/utils/native-message";
  32. import state from "/src/state";
  33. export default defineComponent({
  34. name: "delay-check",
  35. emits: ["close", "back"],
  36. setup(props, { emit }) {
  37. // startTune定时器
  38. let startTuneTimer: any = null
  39. let checkErjiTimer: any = null
  40. const anim = ref();
  41. const data = reactive({
  42. show: false,
  43. /** 检测状态 */
  44. checkStatus: "init" as "init" | "ing" | "finish" | "end",
  45. count: 0,
  46. step: 1,
  47. startTimer: null as any,
  48. stopTimer: null as any,
  49. volumeTimer: null as any,
  50. earPhoneType: "" as "" | "有线耳机" | "蓝牙耳机",
  51. startAbnormalTimer: null as any, // 发送startTune后,超过10秒没有收到sendResult回调,处理这类异常的定时器
  52. startTuneResult: false, // 发送startTune后,能否收到正常的result回调
  53. abnormalPopShow: false, // 异常弹窗
  54. });
  55. // 调用'isWiredHeadsetOn'最小时间间隔,1秒
  56. let minInterval = 0;
  57. /** 获取耳机状态 */
  58. const getEarphoneState = async () => {
  59. const res = await getEarphone();
  60. data.earPhoneType = res?.content?.type || "";
  61. return res?.content?.checkIsWired || false;
  62. };
  63. /** 持续检测耳机状态 */
  64. const keepCheckEarphone = async () => {
  65. if (data.step >= 7 || !data.show) return;
  66. let momentTime = +new Date()
  67. // console.log('间隔123',momentTime - minInterval)
  68. if (momentTime - minInterval < 1000) {
  69. return
  70. }
  71. minInterval = momentTime
  72. evaluatingData.earphone = await getEarphoneState();
  73. // console.log('erji',evaluatingData.earphone,data.step)
  74. if (evaluatingData.earphone) {
  75. clearTimeout(data.startTimer);
  76. clearTimeout(data.stopTimer);
  77. clearTimeout(startTuneTimer);
  78. if (data.step <= 5) {
  79. data.checkStatus = "init"
  80. data.step = 3
  81. }
  82. } else {
  83. if (data.step === 3) {
  84. data.step = 2
  85. data.checkStatus = "init"
  86. }
  87. }
  88. checkErjiTimer = setTimeout(() => {
  89. keepCheckEarphone();
  90. }, 1000);
  91. };
  92. const listenerResult = (res?: IPostMessage) => {
  93. console.log("🚀 ~ res:", res);
  94. if (res?.content) {
  95. const { header, body } = res.content;
  96. if (header?.commond === "recordEnd") {
  97. if (data.checkStatus !== "ing") return;
  98. data.count++;
  99. if (data.count >= 2) {
  100. toggleTune("finishTune");
  101. return;
  102. }
  103. data.startTimer = setTimeout(() => {
  104. toggleTune("start");
  105. }, 100);
  106. }
  107. }
  108. };
  109. // 监听到校音音频播放完成
  110. const checkAudioPlayEnd = (res?: IPostMessage) => {
  111. console.log("🚀 ~ res:校音音频", res);
  112. // startTune效音通过返回后,调用endTune
  113. if (res) {
  114. clearTimeout(data.startAbnormalTimer);
  115. data.startTuneResult = true
  116. toggleTune("stop");
  117. }
  118. }
  119. onMounted(() => {
  120. clearTimeout(checkErjiTimer)
  121. data.show = true;
  122. sendResult(listenerResult);
  123. addCheckPlayEnd(checkAudioPlayEnd);
  124. keepCheckEarphone();
  125. });
  126. onUnmounted(() => {
  127. clearTimeout(checkErjiTimer)
  128. data.show = false;
  129. removeResult(listenerResult);
  130. removeCheckPlayEnd(checkAudioPlayEnd);
  131. });
  132. const handleStartTune = async () => {
  133. // 检测APP端socket状态
  134. const res: any = await startCheckDelay();
  135. if (res?.checked) {
  136. // data.step = evaluatingData.earphone ? 4 : 3;
  137. if (evaluatingData.earphone) {
  138. if (data.step <= 5) {
  139. if (data.checkStatus === "ing") {
  140. data.count = 0;
  141. clearTimeout(data.startTimer);
  142. clearTimeout(data.stopTimer);
  143. // toggleTune("stop");
  144. }
  145. data.step = 3;
  146. }
  147. } else {
  148. if (data.step === 2 || data.step === 3) {
  149. data.step = 4;
  150. startTuneTimer = setTimeout(() => {
  151. data.step = 5;
  152. data.checkStatus = "ing";
  153. data.count = 0;
  154. toggleTune("start");
  155. }, 2000);
  156. }
  157. }
  158. }
  159. };
  160. const hanldeEndTune = () => {
  161. console.log('设置soundEffect')
  162. data.step = 7;
  163. anim.value?.play();
  164. // state.setting.soundEffect = false;
  165. };
  166. const toggleTune = async (state: "start" | "stop" | "finishTune") => {
  167. if (state === "start") {
  168. const res = await api_toggleTune("start", data.count);
  169. // 用户没有授权,需要重置状态
  170. if (res?.content?.reson) {
  171. data.step = 2
  172. data.checkStatus = 'init'
  173. data.count = 0
  174. } else {
  175. // data.stopTimer = setTimeout(() => {
  176. // api_toggleTune("stop");
  177. // }, 2000);
  178. data.startTuneResult = false
  179. data.startAbnormalTimer = setTimeout(() => {
  180. /** startTune发出后,超过10秒没有收到回调,异常处理 */
  181. if (!data.startTuneResult) {
  182. api_toggleTune("stop");
  183. // 给出异常提示,再来一次
  184. data.abnormalPopShow = true
  185. }
  186. }, 10000);
  187. }
  188. } else if (state === "finishTune") {
  189. data.checkStatus = "finish";
  190. data.step = 6;
  191. api_toggleTune("finishTune");
  192. } else if (state === "stop") {
  193. api_toggleTune("stop");
  194. }
  195. };
  196. const resetCheck = () => {
  197. api_toggleTune("stop");
  198. clearTimeout(startTuneTimer)
  199. clearTimeout(data.startAbnormalTimer);
  200. data.abnormalPopShow = false
  201. data.step = 2
  202. data.checkStatus = 'init'
  203. data.count = 0
  204. data.startTuneResult = false
  205. }
  206. watch(
  207. () => evaluatingData.delayCheckSocketError,
  208. () => {
  209. // 监听到网络异常,重置延迟检测状态
  210. if (evaluatingData.delayCheckSocketError) {
  211. resetCheck()
  212. }
  213. }
  214. );
  215. return () => (
  216. <Popup
  217. teleport="body"
  218. overlay={false}
  219. class={["popup-custom", styles.popup]}
  220. transition="van-fade"
  221. v-model:show={data.show}
  222. >
  223. <div class={styles.delayBox}>
  224. {/*返回按钮*/}
  225. <img class={styles.delayBackBtn} src={iconBack} onClick={() => {
  226. clearTimeout(startTuneTimer)
  227. api_toggleTune("stop");
  228. emit("back")
  229. }} />
  230. {/* 异常提示弹窗 */}
  231. {
  232. data.abnormalPopShow &&
  233. <div class={styles.tipBox}>
  234. <div class={styles.tipContent}>检测失败,请确保设备麦克风、扬声器正常,重新开始检测</div>
  235. <div class={styles.tipBtn} onClick={resetCheck}>再来一次</div>
  236. </div>
  237. }
  238. <div class={[styles.item, data.step !== 7 && styles.show]}>
  239. <Vue3Lottie animationData={bg}></Vue3Lottie>
  240. </div>
  241. <div class={[styles.item, data.step === 7 && styles.show]}>
  242. <Vue3Lottie animationData={bg1}></Vue3Lottie>
  243. </div>
  244. <div class={[styles.l1]}>
  245. <Vue3Lottie animationData={l1}></Vue3Lottie>
  246. </div>
  247. <img class={styles.r1} src={r1} />
  248. <div
  249. style={{ left: "10px", top: "-11px", transform: "rotate(-2deg)" }}
  250. class={[styles.item, data.step == 1 && styles.show]}
  251. >
  252. <Vue3Lottie
  253. animationData={f1}
  254. loop={false}
  255. onOnComplete={() => {
  256. data.step = 2;
  257. }}
  258. ></Vue3Lottie>
  259. </div>
  260. <div
  261. style={{ left: "10px", top: "-11px", transform: "rotate(-2deg)" }}
  262. class={[styles.item, data.step === 2 && styles.show]}
  263. >
  264. <Vue3Lottie animationData={f2}></Vue3Lottie>
  265. </div>
  266. <div class={[styles.item, data.step === 3 && styles.show]}>
  267. <Vue3Lottie animationData={f3}></Vue3Lottie>
  268. </div>
  269. <div class={[styles.item, data.step === 4 && styles.show]}>
  270. <Vue3Lottie animationData={f4}></Vue3Lottie>
  271. </div>
  272. <div class={[styles.item, data.step === 5 && styles.show]}>
  273. <Vue3Lottie animationData={f5}></Vue3Lottie>
  274. </div>
  275. <div class={[styles.item, data.step === 6 && styles.show]}>
  276. <Vue3Lottie animationData={f6}></Vue3Lottie>
  277. </div>
  278. <div class={[styles.item, data.step === 7 && styles.show]}>
  279. <Vue3Lottie
  280. ref={anim}
  281. animationData={f7}
  282. autoPlay={false}
  283. loop={false}
  284. onOnComplete={() => {
  285. emit("close");
  286. }}
  287. ></Vue3Lottie>
  288. </div>
  289. <div style={{ opacity: data.step > 1 ? "" : "0" }} class={styles.btnBox}>
  290. <div class={[styles.baseBtn, styles.btn1, data.step <= 2 ? "" : styles.btnDisabled]}>
  291. <img src={icon_2_2} />
  292. <img class={styles.btn0} src={icon_1} onClick={() => handleStartTune()} />
  293. </div>
  294. <div class={[styles.baseBtn, styles.btn2, data.step == 3 ? "" : styles.btnDisabled]}>
  295. <img src={icon_2_1} />
  296. </div>
  297. <div class={[styles.baseBtn, styles.btn3, data.step == 4 ? "" : styles.btnDisabled]}>
  298. <img src={icon_2_3} />
  299. </div>
  300. <div class={[styles.baseBtn, styles.btn4, data.step == 5 ? "" : styles.btnDisabled]}>
  301. <img src={icon_2_4} />
  302. </div>
  303. <div
  304. class={[
  305. styles.baseBtn,
  306. styles.btn5,
  307. data.step >= 6 && !evaluatingData.earphone ? "" : styles.btnDisabled,
  308. ]}
  309. >
  310. <img src={icon_3_0} />
  311. <img class={styles.btn0} src={icon_1_1} onClick={() => hanldeEndTune()} />
  312. </div>
  313. <div
  314. class={[
  315. styles.baseBtn,
  316. styles.btn6,
  317. data.step >= 6 && evaluatingData.earphone && data.earPhoneType !== "有线耳机"
  318. ? ""
  319. : styles.btnDisabled,
  320. ]}
  321. >
  322. <img src={icon_3_1} />
  323. <img class={styles.btn0} src={icon_1_1} onClick={() => hanldeEndTune()} />
  324. </div>
  325. <div
  326. class={[
  327. styles.baseBtn,
  328. styles.btn7,
  329. data.step >= 6 && evaluatingData.earphone && data.earPhoneType === "有线耳机"
  330. ? ""
  331. : styles.btnDisabled,
  332. ]}
  333. >
  334. <img src={icon_3_2} />
  335. <img class={styles.btn0} src={icon_1_1} onClick={() => hanldeEndTune()} />
  336. </div>
  337. <div class={styles.dotbox}>
  338. <div class={styles.dotLine}>
  339. <div class={styles.dot}>
  340. <img
  341. style={{
  342. display:
  343. data.step >= 3 && evaluatingData.earphone && data.checkStatus !== "finish"
  344. ? ""
  345. : "none",
  346. }}
  347. class={styles.dotWraning}
  348. src={icon_4_1}
  349. />
  350. <img
  351. style={{
  352. display:
  353. data.step >= 3 && (!evaluatingData.earphone || data.checkStatus === "finish")
  354. ? ""
  355. : "none",
  356. }}
  357. src={icon_4_2}
  358. />
  359. </div>
  360. <div class={styles.dot}>
  361. <img style={{ display: data.step >= 4 ? "" : "none" }} src={icon_4_2} />
  362. </div>
  363. <div class={styles.dot}>
  364. <img
  365. style={{ display: data.step >= 5 && data.checkStatus === "ing" ? "" : "none" }}
  366. class={styles.dotWraning}
  367. src={icon_4_1}
  368. />
  369. <img
  370. style={{ display: data.step >= 5 && data.checkStatus === "finish" ? "" : "none" }}
  371. src={icon_4_2}
  372. />
  373. </div>
  374. <div class={styles.dot}>
  375. <img
  376. style={{ display: data.step >= 6 && !evaluatingData.earphone ? "" : "none" }}
  377. class={styles.dotWraning}
  378. src={icon_4_1}
  379. />
  380. <img
  381. style={{ display: data.step >= 6 && evaluatingData.earphone ? "" : "none" }}
  382. src={icon_4_2}
  383. />
  384. </div>
  385. </div>
  386. </div>
  387. </div>
  388. </div>
  389. </Popup>
  390. );
  391. },
  392. });