zhuapai.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 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: 160rpx;height: 300rpx;margin: 0;padding: 0;z-index: 10;position: absolute;right: 0;top: 30%;overflow: hidden;
  148. .phone-camera-box-zhuapai{
  149. width: 100%;height: 200rpx;position: absolute;overflow: hidden;
  150. .uni-video-container{background-color: transparent;}
  151. .hidden-video{transform: translateY(500rpx);}
  152. }
  153. .video-view-box{width:100%;-height: 200rpx;position: absolute;}
  154. .shiti-video-hidden-btn,.shiti-video-show-btn{
  155. background-color: red;width: 50rpx;height: 50rpx;display: block;position: absolute;bottom:50px;}
  156. }
  157. /* .dropContainer {
  158. height: 200rpx;
  159. }
  160. #Drop {
  161. width: 150rpx;
  162. height: 200rpx;
  163. margin: 0;
  164. padding: 0;
  165. z-index: 10;
  166. position: absolute;
  167. right: 0;
  168. top: 30%;
  169. .uni-video-container{background-color: transparent; overflow: hidden;}
  170. .phone-camera-box-zhuapai{
  171. .hidden-video{transform: translateX(500rpx);}
  172. }
  173. .video-view-box {
  174. height: 10vh;
  175. width: 100%
  176. }
  177. ::v-deep .uni-video-container {
  178. position: absolute;
  179. }
  180. ::v-deep .uni-video-video {
  181. position: absolute;
  182. }
  183. ::v-deep .uni-canvas-canvas {
  184. position: absolute;
  185. }
  186. ::v-deep .show-video {
  187. z-index: 10;
  188. .uni-video-video,
  189. .uni-canvas-canvas {
  190. z-index: 10;
  191. }
  192. }
  193. ::v-deep .uni-video-container {}
  194. ::v-deep .hidden-video {
  195. z-index: -10;
  196. .uni-video-video,
  197. .uni-canvas-canvas,
  198. {
  199. z-index: -10;
  200. }
  201. }
  202. } */
  203. </style>