qieping.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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/exam.js"
  14. import {
  15. useZhuapaiStore
  16. } from "@/store/zhuapai.js"
  17. import {onHide,onShow} from "@dcloudio/uni-app"
  18. const zhuapaiStore = useZhuapaiStore();
  19. const emits = defineEmits(['zhuapai', 'forceSubmit'])
  20. const zhuapaiFlag = ref(false);
  21. const zhuapaixiaoshi = ref(0);
  22. const leaveTime = ref('');
  23. const toggleScreenSecond = ref(0);
  24. const toggleScreenFlag = ref(0);
  25. const ksId = ref('')
  26. function cheatingFun(code) {
  27. // 用户离开了当前页面
  28. if (code === 'hidden') {
  29. console.log('页面不可见');
  30. if (zhuapaiFlag.value) {
  31. // 此时 切出后台 发生抓拍情况下 要传递固定图片
  32. console.log('切出去zhuapaixiaoshi 恢复正常');
  33. zhuapaixiaoshi.value = 1
  34. zhuapaiStore.setStatus(1)
  35. }
  36. // 计时
  37. leaveTime.value = new Date().getTime();
  38. } else if (code === 'visible') {
  39. console.log('切回来 恢复正常');
  40. // 用户打开或回到页面
  41. if (zhuapaiFlag.value) {
  42. zhuapaixiaoshi.value = 0
  43. zhuapaiStore.setStatus(0)
  44. emits('zhuapai') // 重置抓拍
  45. }
  46. zhuapaixiaoshi.value = 0
  47. zhuapaiStore.setStatus(0)
  48. console.log('页面可见');
  49. let nowTime = new Date().getTime();
  50. if (Number(nowTime) - Number(leaveTime.value) > toggleScreenSecond.value + '000') {
  51. let req = {
  52. ksId: ksId.value,
  53. };
  54. getClientQiepingCheat(req).then(res => {
  55. //cutScreenDialog 是否超限 true:超过限制 false:未超过限制 ,
  56. if (res.code === 0 && res.data.flag) {
  57. emits('forceSubmit') // 强制交卷
  58. } else {
  59. emits('qiepingToast', res.data.times) // 提示警告
  60. }
  61. });
  62. }
  63. }
  64. }
  65. function zhuapaiFun(code) {
  66. if (code === 'hidden') {
  67. console.log('页面不可见');
  68. zhuapaixiaoshi.value = 1
  69. zhuapaiStore.setStatus(1)
  70. } else if (code === 'visible') {
  71. zhuapaixiaoshi.value = 0
  72. zhuapaiStore.setStatus(0)
  73. emits('zhuapai')
  74. }
  75. }
  76. function init(options) {
  77. console.log('切屏初始化完成',options)
  78. toggleScreenFlag.value = options.toggleScreenFlag;
  79. toggleScreenSecond.value = options.toggleScreenSecond;
  80. zhuapaiFlag.value = options.zhuapaiFlag;
  81. ksId.value = options.ksId;
  82. }
  83. function cheatingNumberSearch() {
  84. let req = {
  85. ksId: ksId.value,
  86. };
  87. getClientQiepingTimes(req).then(res => {
  88. if (res.code === 0) {
  89. if (res.data.times > 0 && res.data.times <= res.data.toggleScreenFlag) {
  90. emits('qiepingToast', res.data.times) // 提示警告
  91. } else if (res.data.times > 0 && res.data.times >= res.data.toggleScreenFlag) {
  92. emits('forceSubmit') // 强制交卷
  93. }
  94. }
  95. });
  96. }
  97. function stopListen() {
  98. if (toggleScreenFlag.value !== 0) {
  99. console.log("有切屏 销毁");
  100. document.removeEventListener('visibilitychange', cheatingFun, false);
  101. }
  102. if (zhuapaiFlag.value && toggleScreenFlag.value == 0) {
  103. console.log("有抓拍 无切屏 销毁");
  104. document.removeEventListener('visibilitychange', zhuapaiFun, false);
  105. }
  106. }
  107. onHide(() => {
  108. nextTick(() => {
  109. // 切出屏幕
  110. if (toggleScreenFlag.value !== 0) {
  111. cheatingFun('hidden');
  112. cheatingNumberSearch();
  113. }
  114. if (zhuapaiFlag.value && toggleScreenFlag.value == 0) {
  115. zhuapaiFun('hidden');
  116. }
  117. })
  118. })
  119. onShow(() => {
  120. nextTick(() => {
  121. // 切回屏幕
  122. if (toggleScreenFlag.value !== 0) {
  123. cheatingFun('visit');
  124. cheatingNumberSearch();
  125. }
  126. if (zhuapaiFlag.value && toggleScreenFlag.value == 0) {
  127. zhuapaiFun('visit');
  128. }
  129. })
  130. })
  131. onUnmounted(() => {
  132. // 组件销毁时移除监听
  133. // stopListen()
  134. })
  135. defineExpose({
  136. init
  137. })
  138. </script>
  139. <style>
  140. </style>