qieping.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <view></view>
  3. </template>
  4. <script setup>
  5. import {
  6. ref,
  7. onUnmounted,
  8. nextTick,
  9. computed
  10. } from "vue";
  11. import {
  12. getClientQiepingCheat,getClientQiepingTimes
  13. } from "@/api/kaoshi.js"
  14. import {
  15. useZhuapaiStore
  16. } from "@/store/zhuapai.js"
  17. const zhuapaiStore = useZhuapaiStore();
  18. const emits = defineEmits(['zhuapai', 'forceSubmit'])
  19. const zhuapaiFlag = ref(false);
  20. const zhuapaixiaoshi = ref(0);
  21. const leaveTime = ref('');
  22. const toggleScreenSecond = ref(0);
  23. const toggleScreenFlag = ref(0);
  24. const ksId = ref('')
  25. function cheatingFun() {
  26. // 用户离开了当前页面
  27. if (document.visibilityState === 'hidden') {
  28. console.log('页面不可见');
  29. if (zhuapaiFlag.value) {
  30. // 此时 切出后台 发生抓拍情况下 要传递固定图片
  31. console.log('切出去zhuapaixiaoshi 恢复正常');
  32. zhuapaixiaoshi.value = 1
  33. zhuapaiStore.setStatus(1)
  34. }
  35. // 计时
  36. leaveTime.value = new Date().getTime();
  37. } else if (document.visibilityState === 'visible') {
  38. console.log('切回来 恢复正常');
  39. // 用户打开或回到页面
  40. if (zhuapaiFlag.value) {
  41. zhuapaixiaoshi.value = 0
  42. zhuapaiStore.setStatus(0)
  43. emits('zhuapai') // 重置抓拍
  44. }
  45. zhuapaixiaoshi.value = 0
  46. zhuapaiStore.setStatus(0)
  47. console.log('页面可见');
  48. let nowTime = new Date().getTime();
  49. if (Number(nowTime) - Number(leaveTime.value) > toggleScreenSecond.value + '000') {
  50. let req = {
  51. ksId: ksId.value,
  52. };
  53. getClientQiepingCheat(req).then(res => {
  54. //cutScreenDialog 是否超限 true:超过限制 false:未超过限制 ,
  55. if (res.code === 0 && res.data.flag) {
  56. emits('forceSubmit') // 强制交卷
  57. } else {
  58. emits('qiepingToast', res.data.times) // 提示警告
  59. }
  60. });
  61. }
  62. }
  63. }
  64. function zhuapaiFun() {
  65. if (document.visibilityState === 'hidden') {
  66. console.log('页面不可见');
  67. zhuapaixiaoshi.value = 1
  68. zhuapaiStore.setStatus(1)
  69. } else if (document.visibilityState === 'visible') {
  70. zhuapaixiaoshi.value = 0
  71. zhuapaiStore.setStatus(0)
  72. emits('zhuapai')
  73. }
  74. }
  75. function init(options) {
  76. console.log('init', options)
  77. toggleScreenFlag.value = options.toggleScreenFlag;
  78. toggleScreenSecond.value = options.toggleScreenSecond;
  79. zhuapaiFlag.value = options.zhuapaiFlag;
  80. ksId.value = options.ksId;
  81. // #ifdef H5
  82. if (toggleScreenFlag.value !== 0) {
  83. console.log("有切屏");
  84. document.addEventListener('visibilitychange', cheatingFun, false);
  85. cheatingNumberSearch();
  86. }
  87. if (zhuapaiFlag.value && toggleScreenFlag.value == 0) {
  88. console.log("有抓拍 无切屏");
  89. // 有抓拍 没有切屏 此方法是 解决切到后台,抓拍停留一帧的问题
  90. document.addEventListener('visibilitychange', zhuapaiFun, false);
  91. }
  92. // #endif
  93. }
  94. function cheatingNumberSearch() {
  95. let req = {
  96. ksId: ksId.value,
  97. };
  98. getClientQiepingTimes(req).then(res => {
  99. if (res.code === 0) {
  100. if (res.data.times > 0 && res.data.times <= res.data.toggleScreenFlag) {
  101. emits('qiepingToast', res.data.times) // 提示警告
  102. } else if (res.data.times > 0 && res.data.times >= res.data.toggleScreenFlag) {
  103. emits('forceSubmit') // 强制交卷
  104. }
  105. }
  106. });
  107. }
  108. function stopListen() {
  109. if (toggleScreenFlag.value !== 0) {
  110. console.log("有切屏 销毁");
  111. document.removeEventListener('visibilitychange', cheatingFun, false);
  112. }
  113. if (zhuapaiFlag.value && toggleScreenFlag.value == 0) {
  114. console.log("有抓拍 无切屏 销毁");
  115. document.removeEventListener('visibilitychange', zhuapaiFun, false);
  116. }
  117. }
  118. onUnmounted(() => {
  119. // 组件销毁时移除监听
  120. stopListen()
  121. })
  122. defineExpose({
  123. init
  124. })
  125. </script>
  126. <style>
  127. </style>