zhuapai.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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="front" flash="off" style="width: 200px;height: 200px" :class="myClass" @initdone="onVideoSuccess" @error="handleError" @stop="handleStop"></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. function handleError() {
  204. emits('error')
  205. }
  206. function handleStop() {
  207. emits('error')
  208. }
  209. // 针对视频通话的监听处理
  210. function onTimeupdate() {
  211. if (isBuffer.value) {
  212. console.log('buffer')
  213. return;
  214. }
  215. if (!stopTimer.value) {
  216. return;
  217. }
  218. console.log('onTimeupdate')
  219. clearTimeout(stopTimer.value);
  220. stopTimer.value = null;
  221. }
  222. function onProgress() {
  223. if (stopTimer.value) {
  224. return;
  225. }
  226. isBuffer.value = true;
  227. console.log('onProgress')
  228. // buffer时间增大到3秒 过滤掉后续的onTimeupdate
  229. setTimeout(() => {isBuffer.value = false}, 3000)
  230. // 视频中途暂停被占用
  231. stopTimer.value = setTimeout(() => {
  232. emits('progress', false);
  233. console.log('结束')
  234. }, 10 * 1000)
  235. }
  236. defineExpose({
  237. init,
  238. showVideoBtn
  239. })
  240. </script>
  241. <style lang="scss">
  242. .zhuapai-drop-container {
  243. width: 180rpx;
  244. height: 400rpx;
  245. margin: 0;
  246. padding: 0;
  247. z-index: 10;
  248. position: absolute;
  249. overflow: hidden;
  250. .phone-camera-box-zhuapai {
  251. width: 100%;
  252. height: 240rpx;
  253. position: absolute;
  254. overflow: hidden;
  255. .uni-video-container {
  256. background-color: transparent;
  257. pointer-events: none;
  258. }
  259. .canvas-view-box,
  260. .hidden-video {
  261. transform: translateY(10000rpx);
  262. }
  263. }
  264. .video-view-box {
  265. width: 100%;
  266. height: 240rpx;
  267. position: absolute;
  268. }
  269. .shiti-video-hidden-btn,
  270. .shiti-video-show-btn {
  271. position: absolute;
  272. top: 0;
  273. i {
  274. width: 32rpx;
  275. height: 32rpx;
  276. display: block;
  277. background-size: cover;
  278. background-repeat: no-repeat;
  279. background-position: center;
  280. }
  281. }
  282. .shiti-video-hidden-btn {
  283. width: 60rpx;
  284. height: 60rpx;
  285. left: 0;
  286. i {
  287. background-image: url("@/static/images/exam/video-close-icon.svg");
  288. margin: 6rpx auto 6rpx 6rpx;
  289. }
  290. }
  291. .shiti-video-show-btn {
  292. background-color: #dcfbf1;
  293. padding: 20rpx;
  294. border-radius: 8rpx;
  295. right: 0;
  296. i {
  297. background-image: url("@/static/images/exam/video-play-icon.svg");
  298. }
  299. }
  300. }
  301. </style>