zhuapai.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <template>
  2. <view class="zhuapai-drop-container" id="Drop" ref="DropRef" :style="style" @touchmove="touchmove($event)"
  3. @touchstart="touchstart($event)">
  4. <view class="phone-camera-box-zhuapai">
  5. <video ref="videoRef" class="video-view-box" :class="myClass" id="videoZhaPai" :controls="false"></video>
  6. <!-- 隐藏抓拍绘制图片 -->
  7. <canvas id="canvasZhuaPai" class="video-view-box canvas-view-box" :class="myClass"></canvas>
  8. <!-- 用于抓拍切出去传递固定img-->
  9. <!-- #ifdef H5 -->
  10. <img :src="imgUrl" alt="" id="gdImg" v-show="false">
  11. <!-- #endif -->
  12. <!-- 测试抓拍使用 -->
  13. <!-- <button @click="handleZhua">抓拍</button> -->
  14. </view>
  15. <span v-show="showVideo" @click="noShowVideoBtn" class="shiti-video-hidden-btn"><i></i></span>
  16. <span v-show="!showVideo" @click="showVideoBtn" class="shiti-video-show-btn"><i></i></span>
  17. </view>
  18. </template>
  19. <script setup>
  20. import {
  21. ref,
  22. onUnmounted,
  23. nextTick,
  24. computed
  25. } from "vue";
  26. import {
  27. useH5Camera
  28. } from "@/components/zhuapaiConfirm/useCamera";
  29. import * as ksApi from "@/api/kaoshi.js"
  30. import {
  31. getStaticUrl
  32. } from "@/utils/common.js"
  33. const imgUrl = getStaticUrl('static/images/exam/nokaoshi.png')
  34. let zhuapaiFun = null;
  35. let stopCamera = null;
  36. let playVideoFun = null;
  37. const DropRef = ref(null);
  38. const DropContainerRef = ref(null);
  39. const zhuapai = ref(0); // 单位分
  40. const operId = ref(null); // 单位分
  41. const disX = ref(0); // 移动x
  42. const disY = ref(0); // 移动y
  43. const showVideo = ref(true);
  44. const isBuffer = ref(false);
  45. const stopTimer = ref(null);
  46. const style = ref({
  47. top: "10vh",
  48. right: "0",
  49. });
  50. const myClass = computed(() => {
  51. return {
  52. 'show-video': showVideo.value,
  53. 'hidden-video': !showVideo.value
  54. }
  55. })
  56. const emits = defineEmits(['init', 'success', 'error', 'cancel', 'progress'])
  57. function noShowVideoBtn() {
  58. showVideo.value = false
  59. }
  60. function showVideoBtn() {
  61. showVideo.value = true;
  62. playVideoFun && playVideoFun();
  63. }
  64. function touchmove(event) {
  65. // 2,获取手指移动的实时位置 需要减去位置差
  66. let moveX = event.touches[0].pageX - disX.value;
  67. let moveY = event.touches[0].pageY - disY.value;
  68. const systemInfo = uni.getSystemInfoSync();
  69. const windowHeight = systemInfo.windowHeight; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  70. const windowWidth = systemInfo.windowWidth; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  71. // 3,获取容器的宽高和拖动元素的宽高 每一次移动都会获取一次 ,建议放在外面进行获取
  72. let dragHeight = DropRef.value.$el.offsetHeight;
  73. let dragWidth = DropRef.value.$el.offsetWidth;
  74. // 4,控制范围:在元素 被拖拽的过程中 判断 元素的定位值 是否到达边界 如果到了 就不能在走了
  75. if (moveX <= 0) {
  76. moveX = 0;
  77. }
  78. // 上边界
  79. if (moveY <= 0) {
  80. moveY = 0;
  81. }
  82. //下边界 容器高度 - 拖动元素高度
  83. if (moveY >= windowHeight - dragHeight - 150) {
  84. moveY = windowHeight - dragHeight - 150;
  85. }
  86. //右边界 容器宽度 - 拖动元素宽度
  87. if (moveX >= windowWidth - dragWidth) {
  88. moveX = 0;
  89. }
  90. // 5,开始移动
  91. style.value.top = moveY + "px";
  92. }
  93. function touchstart(event) {
  94. disX.value = event.touches[0].pageX - DropRef.value.$el.offsetLeft;
  95. disY.value = event.touches[0].pageY - DropRef.value.$el.offsetTop;
  96. }
  97. function init(options) {
  98. zhuapai.value = options.zhuapai;
  99. operId.value = options.operId;
  100. if (zhuapai.value > 0) {
  101. // 启动摄像头
  102. nextTick(() => {
  103. startCamera()
  104. // 设定计时器
  105. setInterval(() => handleZhua(), zhuapai.value * 60 * 1000)
  106. })
  107. }
  108. }
  109. function startCamera() {
  110. // 请求摄像头权限并获取流
  111. // #ifdef H5
  112. console.log('navigator', navigator)
  113. const {
  114. startH5Camera,
  115. handlePaiZhao,
  116. stopH5Camera,
  117. playVideo
  118. } = useH5Camera({
  119. elVideoId: '#videoZhaPai',
  120. elCanvasId: '#canvasZhuaPai',
  121. onVideoSuccess,
  122. onVideoError,
  123. zhuapaiHttp: ksApi.getClientZhuaPaiUpdate,
  124. operId: operId.value
  125. })
  126. startH5Camera();
  127. zhuapaiFun = handlePaiZhao;
  128. stopCamera = stopH5Camera;
  129. playVideoFun = playVideo;
  130. // #endif
  131. }
  132. function handleZhua() {
  133. zhuapaiFun && zhuapaiFun()
  134. }
  135. function onVideoSuccess() {
  136. setTimeout(() => {
  137. // 首次运行进行抓拍一次
  138. handleZhua();
  139. }, 3000);
  140. addVideoListener();
  141. }
  142. function onVideoError() {
  143. emits('error')
  144. removeVideoListener()
  145. }
  146. // 针对视频通话的监听处理
  147. function onTimeupdate() {
  148. if (isBuffer.value) {
  149. console.log('buffer')
  150. return;
  151. }
  152. if (!stopTimer.value) {
  153. return;
  154. }
  155. console.log('onTimeupdate')
  156. clearTimeout(stopTimer.value);
  157. stopTimer.value = null;
  158. }
  159. function onProgress() {
  160. if (stopTimer.value) {
  161. return;
  162. }
  163. isBuffer.value = true;
  164. console.log('onProgress')
  165. // buffer时间增大到3秒 过滤掉后续的onTimeupdate
  166. setTimeout(() => {isBuffer.value = false}, 3000)
  167. // 视频中途暂停被占用
  168. stopTimer.value = setTimeout(() => {
  169. emits('progress', false);
  170. console.log('结束')
  171. }, 10 * 1000)
  172. }
  173. function addVideoListener() {
  174. let video = document.querySelector(`#videoZhaPai .uni-video-video`)
  175. // 判定有流
  176. video.addEventListener('progress', onProgress);
  177. video.addEventListener('timeupdate', onTimeupdate);
  178. }
  179. function removeVideoListener() {
  180. let video =document.querySelector(`#videoZhaPai .uni-video-video`)
  181. video && video.removeEventListener('progress', onProgress);
  182. video && video.removeEventListener('timeupdate', onTimeupdate);
  183. }
  184. onUnmounted(() => {
  185. // 组件销毁时停止摄像头
  186. stopCamera && stopCamera();
  187. removeVideoListener();
  188. })
  189. defineExpose({
  190. init,
  191. showVideoBtn
  192. })
  193. </script>
  194. <style lang="scss">
  195. .zhuapai-drop-container {
  196. width: 180rpx;
  197. height: 400rpx;
  198. margin: 0;
  199. padding: 0;
  200. z-index: 10;
  201. position: absolute;
  202. overflow: hidden;
  203. .phone-camera-box-zhuapai {
  204. width: 100%;
  205. height: 240rpx;
  206. position: absolute;
  207. overflow: hidden;
  208. .uni-video-container {
  209. background-color: transparent;
  210. pointer-events: none;
  211. }
  212. .canvas-view-box,
  213. .hidden-video {
  214. transform: translateY(10000rpx);
  215. }
  216. }
  217. .video-view-box {
  218. width: 100%;
  219. height: 240rpx;
  220. position: absolute;
  221. }
  222. .shiti-video-hidden-btn,
  223. .shiti-video-show-btn {
  224. position: absolute;
  225. top: 0;
  226. i {
  227. width: 32rpx;
  228. height: 32rpx;
  229. display: block;
  230. background-size: cover;
  231. background-repeat: no-repeat;
  232. background-position: center;
  233. }
  234. }
  235. .shiti-video-hidden-btn {
  236. width: 60rpx;
  237. height: 60rpx;
  238. left: 0;
  239. i {
  240. background-image: url("@/static/images/exam/video-close-icon.svg");
  241. margin: 6rpx auto 6rpx 6rpx;
  242. }
  243. }
  244. .shiti-video-show-btn {
  245. background-color: #dcfbf1;
  246. padding: 20rpx;
  247. border-radius: 8rpx;
  248. right: 0;
  249. i {
  250. background-image: url("@/static/images/exam/video-play-icon.svg");
  251. }
  252. }
  253. }
  254. </style>