zhuapai.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <template>
  2. <view id="Drop" :style="style" @touchmove="touchmove($event)" @touchstart="touchstart($event)" class="dropContainer">
  3. <view class="phone-camera-box-zhuapai">
  4. <video ref="videoRef" :class="{
  5. 'show-video': showVideo,
  6. 'hidden-video': !showVideo
  7. }" style="width:100%; height: 150rpx;" id="videoZhaPai" :controls="false"></video>
  8. <!-- 隐藏抓拍绘制图片 -->
  9. <canvas id="canvasZhuaPai" :class="{
  10. 'show-video': showVideo,
  11. 'hidden-video': !showVideo
  12. }" style="width:100%; height: 150rpx;"></canvas>
  13. <!-- 用于抓拍切出去传递固定img-->
  14. <!-- #ifdef H5 -->
  15. <img :src="imgUrl" alt="" ref="gudingImg" v-show="false">
  16. <!-- #endif -->
  17. <!-- 测试抓拍使用 -->
  18. <!-- <button @click="handleZhua">抓拍</button> -->
  19. </view>
  20. <span v-show="showVideo" @click="noShowVideoBtn" class="shiti-video-hidden-btn">开<i></i></span>
  21. <span v-show="!showVideo" @click="showVideoBtn" class="shiti-video-show-btn">关<i></i></span>
  22. </view>
  23. </template>
  24. <script setup>
  25. import {
  26. ref,
  27. onUnmounted,
  28. nextTick
  29. } from "vue";
  30. import {
  31. useH5Camera
  32. } from "@/components/zhuapaiConfirm/useCamera";
  33. import * as ksApi from "@/api/kaoshi.js"
  34. import {
  35. getStaticUrl
  36. } from "@/utils/common.js"
  37. const imgUrl = getStaticUrl('static/images/exam/nokaoshi.png')
  38. let zhuapaiFun = null;
  39. let stopCamera = null;
  40. const zhuapai = ref(0); // 单位分
  41. const showVideo = ref(true);
  42. const style = ref({
  43. top: "10vh",
  44. right: "0",
  45. });
  46. const emits = defineEmits(['init', 'success', 'error', 'cancel'])
  47. function noShowVideoBtn() {
  48. showVideo.value = false
  49. }
  50. function showVideoBtn() {
  51. showVideo.value = true
  52. }
  53. function touchmove(event) {
  54. console.log(4444, event)
  55. }
  56. function touchstart(event) {
  57. console.log(3333, event)
  58. }
  59. function init(options) {
  60. zhuapai.value = options.zhuapai;
  61. if (zhuapai.value > 0) {
  62. // 启动摄像头
  63. nextTick(() => {
  64. startCamera()
  65. })
  66. // 设定计时器
  67. setInterval(() => handleZhua(), zhuapai.value * 60 * 1000 || 6000000)
  68. }
  69. }
  70. function startCamera() {
  71. // 请求摄像头权限并获取流
  72. // #ifdef H5
  73. console.log('navigator', navigator)
  74. const {
  75. startH5Camera,
  76. handlePaiZhao,
  77. stopH5Camera
  78. } = useH5Camera({
  79. elVideoId: '#videoZhaPai',
  80. elCanvasId: '#canvasZhuaPai',
  81. onVideoSuccess,
  82. onVideoError,
  83. zhuapaiHttp: ksApi.getClientZhuaPaiUpdate
  84. })
  85. startH5Camera();
  86. zhuapaiFun = handlePaiZhao;
  87. stopCamera = stopH5Camera;
  88. // #endif
  89. }
  90. function handleZhua() {
  91. zhuapaiFun && zhuapaiFun()
  92. }
  93. function onVideoSuccess() {
  94. setTimeout(() => {
  95. // 首次运行进行抓拍一次
  96. // handleZhua();
  97. }, 3000);
  98. }
  99. function onVideoError() {
  100. emits('error')
  101. }
  102. onUnmounted(() => {
  103. // 组件销毁时停止摄像头
  104. stopCamera && stopCamera();
  105. })
  106. defineExpose({
  107. init
  108. })
  109. </script>
  110. <style lang="scss" scoped>
  111. .dropContainer {
  112. width: 30vw;
  113. height: 150rpx;
  114. margin: 0;
  115. padding: 0;
  116. // position: absolute;
  117. // top: 0;
  118. // left: 0;
  119. z-index: 10;
  120. position: absolute;
  121. right: 0;
  122. top: 30%;
  123. .show-video {
  124. z-index: 10
  125. }
  126. .hidden-video {
  127. z-index: -10
  128. }
  129. }
  130. </style>