zhuapai.vue 5.4 KB

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