zhuapai.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <template>
  2. <view class="zhuapai-drop-container" id="Drop" ref="DropRef" :style="style" @touchmove="touchmove($event)"
  3. @touchstart="touchstart($event)">
  4. <view class="phone-camera-box-zhuapai">
  5. <camera class="camera" device-position="back" flash="off" style="width: 200px;height: 200px" :class="myClass" @initdone="onVideoSuccess"></camera>
  6. <!-- 用于抓拍切出去传递固定img-->
  7. <img :src="imgUrl" alt="" v-show="false">
  8. <!-- 测试抓拍使用 -->
  9. <!-- <button @click="handleZhua">抓拍</button> -->
  10. </view>
  11. <span v-show="showVideo" @click="noShowVideoBtn" class="shiti-video-hidden-btn"><i>隐藏</i></span>
  12. <span v-show="!showVideo" @click="showVideoBtn" class="shiti-video-show-btn"><i></i></span>
  13. </view>
  14. </template>
  15. <script setup>
  16. import {
  17. ref,
  18. onUnmounted,
  19. nextTick,
  20. computed,
  21. } from "vue";
  22. import * as ksApi from "@/api/kaoshi.js"
  23. import {
  24. getStaticUrl
  25. } from "@/utils/common.js"
  26. import {useZhuapaiStore} from "@/store/zhuapai.js"
  27. import {getFileUpload} from "@/api/kaoshi.js"
  28. const zhuapaiStore = useZhuapaiStore();
  29. const imgUrl = getStaticUrl('static/images/exam/nokaoshi.png')
  30. const cameraContext = ref(null);
  31. const DropRef = ref(null);
  32. const DropContainerRef = ref(null);
  33. const zhuapai = ref(0); // 单位分
  34. const operId = ref(null); // 单位分
  35. const disX = ref(0); // 移动x
  36. const disY = ref(0); // 移动y
  37. const showVideo = ref(true);
  38. const isBuffer = ref(false);
  39. const stopTimer = ref(null);
  40. const style = ref({
  41. top: "10vh",
  42. right: "0",
  43. });
  44. const myClass = computed(() => {
  45. return {
  46. 'show-video': showVideo.value,
  47. 'hidden-video': !showVideo.value
  48. }
  49. })
  50. const emits = defineEmits(['init', 'success', 'error', 'cancel', 'progress'])
  51. function noShowVideoBtn() {
  52. showVideo.value = false
  53. }
  54. function showVideoBtn() {
  55. showVideo.value = true;
  56. }
  57. function touchmove(event) {
  58. // 2,获取手指移动的实时位置 需要减去位置差
  59. let moveX = event.touches[0].pageX - disX.value;
  60. let moveY = event.touches[0].pageY - disY.value;
  61. const systemInfo = uni.getAppBaseInfo();
  62. const windowHeight = systemInfo.windowHeight; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  63. const windowWidth = systemInfo.windowWidth; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  64. // 3,获取容器的宽高和拖动元素的宽高 每一次移动都会获取一次 ,建议放在外面进行获取
  65. // let dragHeight = DropRef.value.$el.offsetHeight;
  66. // let dragWidth = DropRef.value.$el.offsetWidth;
  67. let dragHeight = 0;
  68. let dragWidth = 0;
  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. disX.value = 0;
  92. disY.value = 0;
  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. cameraContext.value = uni.createCameraContext();
  109. }
  110. function urlToBase64(url) {
  111. return new Promise((resolve, reject) => {
  112. // #ifdef MP-WEIXIN
  113. // 微信小程序平台
  114. uni.getFileSystemManager().readFile({
  115. filePath: url, // 图片临时路径
  116. encoding: 'base64', // 指定编码格式
  117. success: (res) => {
  118. // 拼接Data URL前缀,注意图片类型可能需要根据实际情况调整(如png)
  119. let base64 = 'data:image/jpeg;base64,' + res.data;
  120. resolve(base64);
  121. },
  122. fail: (err) => {
  123. reject(err);
  124. }
  125. });
  126. // #endif
  127. });
  128. }
  129. function getSnapShotImage(data) {
  130. urlToBase64(data).then((d1) => {
  131. const imgData = d1.split(';base64,');
  132. if (!imgData.length) {
  133. console.error('【源 :拍照数据异常,未找到图片二进制数据分割节点: `;base64,`】');
  134. return;
  135. }
  136. const opt = {
  137. data: imgData[1],
  138. prefix: 'kaoshi/zhuapai',
  139. suffix: 'png',
  140. };
  141. getFileUpload(opt).then(res => {
  142. const dOption = {
  143. operId:operId.value,
  144. url: res.data
  145. }
  146. ksApi.getClientZhuaPaiUpdate(dOption)
  147. .then(res => {
  148. console.log('【源 : 获取抓拍数据】');
  149. })
  150. .catch(err => {
  151. console.error('源 :抓拍接口异常', err);
  152. uni.showToast({
  153. icon: 'none',
  154. title: '抓拍图片异常!'
  155. })
  156. uni.redirectTo({
  157. url: "/pages/client/Kaoshi/list"
  158. })
  159. });
  160. }).catch(err => {
  161. uni.showToast({
  162. icon: 'none',
  163. title: '当前网络可能存在异常,请稍后重试,如持续异常,请联系管理员。注:若异常未联系管理员,可能会影响考试结果。'
  164. })
  165. uni.redirectTo({
  166. url: "/pages/client/Kaoshi/list"
  167. })
  168. })
  169. })
  170. }
  171. function doZhuaiPai(ImageFile) {
  172. try {
  173. if (zhuapaiStore.status == 0) {
  174. getSnapShotImage(ImageFile);
  175. } else {
  176. const ImageFile1 = imgUrl.value;
  177. getSnapShotImage(ImageFile1);
  178. }
  179. } catch (err) {
  180. console.error('源 :绘图失败', err);
  181. }
  182. }
  183. function handleZhua() {
  184. cameraContext.value.takePhoto({
  185. quality: 'high',
  186. success: (res) => {
  187. doZhuaiPai(res.tempImagePath)
  188. },
  189. fail: (err) => {
  190. console.error('拍照失败:', err);
  191. }
  192. });
  193. }
  194. function onVideoSuccess() {
  195. setTimeout(() => {
  196. // 首次运行进行抓拍一次
  197. handleZhua();
  198. }, 3000);
  199. }
  200. function onVideoError() {
  201. emits('error')
  202. }
  203. // 针对视频通话的监听处理
  204. function onTimeupdate() {
  205. if (isBuffer.value) {
  206. console.log('buffer')
  207. return;
  208. }
  209. if (!stopTimer.value) {
  210. return;
  211. }
  212. console.log('onTimeupdate')
  213. clearTimeout(stopTimer.value);
  214. stopTimer.value = null;
  215. }
  216. function onProgress() {
  217. if (stopTimer.value) {
  218. return;
  219. }
  220. isBuffer.value = true;
  221. console.log('onProgress')
  222. // buffer时间增大到3秒 过滤掉后续的onTimeupdate
  223. setTimeout(() => {isBuffer.value = false}, 3000)
  224. // 视频中途暂停被占用
  225. stopTimer.value = setTimeout(() => {
  226. emits('progress', false);
  227. console.log('结束')
  228. }, 10 * 1000)
  229. }
  230. defineExpose({
  231. init,
  232. showVideoBtn
  233. })
  234. </script>
  235. <style lang="scss">
  236. .zhuapai-drop-container {
  237. width: 180rpx;
  238. height: 400rpx;
  239. margin: 0;
  240. padding: 0;
  241. z-index: 10;
  242. position: absolute;
  243. overflow: hidden;
  244. .phone-camera-box-zhuapai {
  245. width: 100%;
  246. height: 240rpx;
  247. position: absolute;
  248. overflow: hidden;
  249. .uni-video-container {
  250. background-color: transparent;
  251. pointer-events: none;
  252. }
  253. .canvas-view-box,
  254. .hidden-video {
  255. transform: translateY(10000rpx);
  256. }
  257. }
  258. .video-view-box {
  259. width: 100%;
  260. height: 240rpx;
  261. position: absolute;
  262. }
  263. .shiti-video-hidden-btn,
  264. .shiti-video-show-btn {
  265. position: absolute;
  266. top: 0;
  267. i {
  268. width: 32rpx;
  269. height: 32rpx;
  270. display: block;
  271. background-size: cover;
  272. background-repeat: no-repeat;
  273. background-position: center;
  274. }
  275. }
  276. .shiti-video-hidden-btn {
  277. width: 60rpx;
  278. height: 60rpx;
  279. left: 0;
  280. i {
  281. background-image: url("@/static/images/exam/video-close-icon.svg");
  282. margin: 6rpx auto 6rpx 6rpx;
  283. }
  284. }
  285. .shiti-video-show-btn {
  286. background-color: #dcfbf1;
  287. padding: 20rpx;
  288. border-radius: 8rpx;
  289. right: 0;
  290. i {
  291. background-image: url("@/static/images/exam/video-play-icon.svg");
  292. }
  293. }
  294. }
  295. </style>