zhuapai.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <view class="zhuapai-drop-container" id="Drop" ref="DropRef" :style="style" @touchmove="touchmove($event)" @touchstart="touchstart($event)">
  3. <view class="phone-camera-box-zhuapai">
  4. <video ref="videoRef" class="video-view-box" :class="myClass" id="videoZhaPai" :controls="false"></video>
  5. <!-- 隐藏抓拍绘制图片 -->
  6. <canvas id="canvasZhuaPai" class="video-view-box canvas-view-box" :class="myClass"></canvas>
  7. <!-- 用于抓拍切出去传递固定img-->
  8. <!-- #ifdef H5 -->
  9. <img :src="imgUrl" alt="" id="gdImg" v-show="false">
  10. <!-- #endif -->
  11. <!-- 测试抓拍使用 -->
  12. <!-- <button @click="handleZhua">抓拍</button> -->
  13. </view>
  14. <span v-show="showVideo" @click="noShowVideoBtn" class="shiti-video-hidden-btn"><i></i></span>
  15. <span v-show="!showVideo" @click="showVideoBtn" class="shiti-video-show-btn"><i></i></span>
  16. </view>
  17. </template>
  18. <script setup>
  19. import {
  20. ref,
  21. onUnmounted,
  22. nextTick,
  23. computed
  24. } from "vue";
  25. import {
  26. useH5Camera
  27. } from "@/components/zhuapaiConfirm/useCamera";
  28. import * as ksApi from "@/api/kaoshi.js"
  29. import {
  30. getStaticUrl
  31. } from "@/utils/common.js"
  32. const imgUrl = getStaticUrl('static/images/exam/nokaoshi.png')
  33. let zhuapaiFun = null;
  34. let stopCamera = null;
  35. const DropRef = ref(null);
  36. const DropContainerRef = ref(null);
  37. const zhuapai = ref(0); // 单位分
  38. const operId = ref(null); // 单位分
  39. const disX = ref(0); // 移动x
  40. const disY = ref(0); // 移动y
  41. const showVideo = ref(true);
  42. const style = ref({
  43. top: "10vh",
  44. right: "0",
  45. });
  46. const myClass = computed(() => {
  47. return {
  48. 'show-video': showVideo.value,
  49. 'hidden-video': !showVideo.value
  50. }
  51. })
  52. const emits = defineEmits(['init', 'success', 'error', 'cancel'])
  53. function noShowVideoBtn() {
  54. showVideo.value = false
  55. }
  56. function showVideoBtn() {
  57. showVideo.value = true
  58. }
  59. function touchmove(event) {
  60. // 2,获取手指移动的实时位置 需要减去位置差
  61. let moveX = event.touches[0].pageX - disX.value;
  62. let moveY = event.touches[0].pageY - disY.value;
  63. const systemInfo = uni.getSystemInfoSync();
  64. const windowHeight = systemInfo.windowHeight; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  65. const windowWidth = systemInfo.windowWidth; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  66. // 3,获取容器的宽高和拖动元素的宽高 每一次移动都会获取一次 ,建议放在外面进行获取
  67. let dragHeight = DropRef.value.$el.offsetHeight;
  68. let dragWidth = DropRef.value.$el.offsetWidth;
  69. // 4,控制范围:在元素 被拖拽的过程中 判断 元素的定位值 是否到达边界 如果到了 就不能在走了
  70. if (moveX <= 0) {
  71. moveX = 0;
  72. }
  73. // 上边界
  74. if (moveY <= 0) {
  75. moveY = 0;
  76. }
  77. //下边界 容器高度 - 拖动元素高度
  78. if (moveY >= windowHeight - dragHeight - 150) {
  79. moveY = windowHeight - dragHeight - 150;
  80. }
  81. //右边界 容器宽度 - 拖动元素宽度
  82. if (moveX >= windowWidth - dragWidth) {
  83. moveX = 0;
  84. }
  85. // 5,开始移动
  86. style.value.top = moveY + "px";
  87. }
  88. function touchstart(event) {
  89. disX.value = event.touches[0].pageX - DropRef.value.$el.offsetLeft;
  90. disY.value = event.touches[0].pageY - DropRef.value.$el.offsetTop;
  91. }
  92. function init(options) {
  93. zhuapai.value = options.zhuapai;
  94. operId.value = options.operId;
  95. if (zhuapai.value > 0) {
  96. // 启动摄像头
  97. nextTick(() => {
  98. startCamera()
  99. // 设定计时器
  100. setInterval(() => handleZhua(), zhuapai.value * 60 * 1000)
  101. })
  102. }
  103. }
  104. function startCamera() {
  105. // 请求摄像头权限并获取流
  106. // #ifdef H5
  107. console.log('navigator', navigator)
  108. const {
  109. startH5Camera,
  110. handlePaiZhao,
  111. stopH5Camera
  112. } = useH5Camera({
  113. elVideoId: '#videoZhaPai',
  114. elCanvasId: '#canvasZhuaPai',
  115. onVideoSuccess,
  116. onVideoError,
  117. zhuapaiHttp: ksApi.getClientZhuaPaiUpdate,
  118. operId: operId.value
  119. })
  120. startH5Camera();
  121. zhuapaiFun = handlePaiZhao;
  122. stopCamera = stopH5Camera;
  123. // #endif
  124. }
  125. function handleZhua() {
  126. zhuapaiFun && zhuapaiFun()
  127. }
  128. function onVideoSuccess() {
  129. setTimeout(() => {
  130. // 首次运行进行抓拍一次
  131. handleZhua();
  132. }, 3000);
  133. }
  134. function onVideoError() {
  135. emits('error')
  136. }
  137. onUnmounted(() => {
  138. // 组件销毁时停止摄像头
  139. stopCamera && stopCamera();
  140. })
  141. defineExpose({
  142. init,showVideoBtn
  143. })
  144. </script>
  145. <style lang="scss">
  146. .zhuapai-drop-container {
  147. width: 180rpx;height: 400rpx;margin: 0;padding: 0;z-index: 10;position: absolute;overflow: hidden;
  148. .phone-camera-box-zhuapai{
  149. width: 100%;height: 240rpx;position: absolute;overflow: hidden;
  150. .uni-video-container{background-color: transparent; pointer-events: none;}
  151. .canvas-view-box,.hidden-video{transform: translateY(10000rpx);}
  152. }
  153. .video-view-box{width:100%;height: 240rpx;position: absolute;}
  154. .shiti-video-hidden-btn,.shiti-video-show-btn{
  155. position: absolute;top:0;
  156. i{width: 32rpx;height: 32rpx;display: block;background-size: cover;background-repeat: no-repeat;background-position:center;}
  157. }
  158. .shiti-video-hidden-btn{
  159. width: 60rpx;height: 60rpx;left:0;
  160. i{background-image: url("@/static/images/exam/video-close-icon.svg");margin:6rpx auto 6rpx 6rpx;}
  161. }
  162. .shiti-video-show-btn{
  163. background-color: #dcfbf1;padding:20rpx;border-radius:8rpx;right:0;
  164. i{background-image: url("@/static/images/exam/video-play-icon.svg");}
  165. }
  166. }
  167. </style>