zhuapai.vue 5.5 KB

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