useQieping.js 4.2 KB

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