zhuapai.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. <view v-show="showVideo" @click="noShowVideoBtn" class="shiti-video-hidden-btn">
  12. <icon :style="{ backgroundImage: 'url(' + iconsArr.videoCloseIcon + ')' }"></icon>
  13. </view>
  14. <view v-show="!showVideo" @click="showVideoBtn" class="shiti-video-show-btn">
  15. <icon :style="{ backgroundImage: 'url(' + iconsArr.videoPlayIcon + ')' }"></icon>
  16. </view>
  17. </view>
  18. </template>
  19. <script setup>
  20. import {ref,onUnmounted,nextTick,computed,onMounted,reactive} from "vue";
  21. import cacheManager from '@/utils/cacheManager.js';
  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 = '';
  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: "17vh",
  42. right: "0",
  43. });
  44. const iconsArr = reactive({
  45. noKaoshiImg: '',
  46. videoCloseIcon: '',
  47. videoPlayIcon: '',
  48. })
  49. const myClass = computed(() => {
  50. return {
  51. 'show-video': showVideo.value,
  52. 'hidden-video': !showVideo.value
  53. }
  54. })
  55. const emits = defineEmits(['init', 'success', 'error', 'cancel', 'progress'])
  56. function noShowVideoBtn() {
  57. showVideo.value = false
  58. }
  59. function showVideoBtn() {
  60. showVideo.value = true;
  61. }
  62. function touchmove(event) {
  63. // 2,获取手指移动的实时位置 需要减去位置差
  64. let moveX = event.touches[0].pageX - disX.value;
  65. let moveY = event.touches[0].pageY - disY.value;
  66. const systemInfo = uni.getAppBaseInfo();
  67. const windowHeight = systemInfo.windowHeight; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  68. const windowWidth = systemInfo.windowWidth; // 可视区域高度 ‌:ml-citation{ref="1,3" data="citationList"}
  69. // 3,获取容器的宽高和拖动元素的宽高 每一次移动都会获取一次 ,建议放在外面进行获取
  70. // let dragHeight = DropRef.value.$el.offsetHeight;
  71. // let dragWidth = DropRef.value.$el.offsetWidth;
  72. let dragHeight = 0;
  73. let dragWidth = 0;
  74. // 4,控制范围:在元素 被拖拽的过程中 判断 元素的定位值 是否到达边界 如果到了 就不能在走了
  75. if (moveX <= 0) {
  76. moveX = 0;
  77. }
  78. // 上边界
  79. if (moveY <= 0) {
  80. moveY = 0;
  81. }
  82. //下边界 容器高度 - 拖动元素高度
  83. if (moveY >= windowHeight - dragHeight - 150) {
  84. moveY = windowHeight - dragHeight - 150;
  85. }
  86. //右边界 容器宽度 - 拖动元素宽度
  87. if (moveX >= windowWidth - dragWidth) {
  88. moveX = 0;
  89. }
  90. // 5,开始移动
  91. style.value.top = moveY + "px";
  92. }
  93. function touchstart(event) {
  94. // disX.value = event.touches[0].pageX - DropRef.value.$el.offsetLeft;
  95. // disY.value = event.touches[0].pageY - DropRef.value.$el.offsetTop;
  96. disX.value = 0;
  97. disY.value = 0;
  98. }
  99. function init(options) {
  100. zhuapai.value = options.zhuapai;
  101. operId.value = options.operId;
  102. if (zhuapai.value > 0) {
  103. // 启动摄像头
  104. nextTick(() => {
  105. startCamera()
  106. // 设定计时器
  107. setInterval(() => handleZhua(), zhuapai.value * 60 * 1000)
  108. })
  109. }
  110. }
  111. function startCamera() {
  112. // 请求摄像头权限并获取流
  113. cameraContext.value = uni.createCameraContext();
  114. }
  115. function urlToBase64(url) {
  116. return new Promise((resolve, reject) => {
  117. // #ifdef MP-WEIXIN
  118. // 微信小程序平台
  119. uni.getFileSystemManager().readFile({
  120. filePath: url, // 图片临时路径
  121. encoding: 'base64', // 指定编码格式
  122. success: (res) => {
  123. // 拼接Data URL前缀,注意图片类型可能需要根据实际情况调整(如png)
  124. let base64 = 'data:image/jpeg;base64,' + res.data;
  125. resolve(base64);
  126. },
  127. fail: (err) => {
  128. reject(err);
  129. }
  130. });
  131. // #endif
  132. });
  133. }
  134. function getSnapShotImage(data) {
  135. urlToBase64(data).then((d1) => {
  136. const imgData = d1.split(';base64,');
  137. if (!imgData.length) {
  138. console.error('【源 :拍照数据异常,未找到图片二进制数据分割节点: `;base64,`】');
  139. return;
  140. }
  141. const opt = {
  142. data: imgData[1],
  143. prefix: 'kaoshi/zhuapai',
  144. suffix: 'png',
  145. };
  146. getFileUpload(opt).then(res => {
  147. const dOption = {
  148. operId:operId.value,
  149. url: res.data
  150. }
  151. ksApi.getClientZhuaPaiUpdate(dOption)
  152. .then(res => {
  153. console.log('【源 : 获取抓拍数据】');
  154. })
  155. .catch(err => {
  156. console.error('源 :抓拍接口异常', err);
  157. uni.showToast({
  158. icon: 'none',
  159. title: '抓拍图片异常!'
  160. })
  161. uni.redirectTo({
  162. url: "/pages/client/Kaoshi/list"
  163. })
  164. });
  165. }).catch(err => {
  166. uni.showToast({
  167. icon: 'none',
  168. title: '当前网络可能存在异常,请稍后重试,如持续异常,请联系管理员。注:若异常未联系管理员,可能会影响考试结果。'
  169. })
  170. uni.redirectTo({
  171. url: "/pages/client/Kaoshi/list"
  172. })
  173. })
  174. })
  175. }
  176. function doZhuaiPai(ImageFile) {
  177. try {
  178. if (zhuapaiStore.status == 0) {
  179. getSnapShotImage(ImageFile);
  180. } else {
  181. const ImageFile1 = imgUrl.value;
  182. getSnapShotImage(ImageFile1);
  183. }
  184. } catch (err) {
  185. console.error('源 :绘图失败', err);
  186. }
  187. }
  188. function handleZhua() {
  189. cameraContext.value.takePhoto({
  190. quality: 'high',
  191. success: (res) => {
  192. doZhuaiPai(res.tempImagePath)
  193. },
  194. fail: (err) => {
  195. console.error('拍照失败:', err);
  196. }
  197. });
  198. }
  199. function onVideoSuccess() {
  200. // setTimeout(() => {
  201. // // 首次运行进行抓拍一次
  202. // handleZhua();
  203. // }, 3000);
  204. }
  205. function onVideoError() {
  206. emits('error')
  207. }
  208. function handleError() {
  209. emits('error')
  210. }
  211. function handleStop() {
  212. emits('error')
  213. }
  214. // 针对视频通话的监听处理
  215. function onTimeupdate() {
  216. if (isBuffer.value) {
  217. console.log('buffer')
  218. return;
  219. }
  220. if (!stopTimer.value) {
  221. return;
  222. }
  223. console.log('onTimeupdate')
  224. clearTimeout(stopTimer.value);
  225. stopTimer.value = null;
  226. }
  227. function onProgress() {
  228. if (stopTimer.value) {
  229. return;
  230. }
  231. isBuffer.value = true;
  232. console.log('onProgress')
  233. // buffer时间增大到3秒 过滤掉后续的onTimeupdate
  234. setTimeout(() => {isBuffer.value = false}, 3000)
  235. // 视频中途暂停被占用
  236. stopTimer.value = setTimeout(() => {
  237. emits('progress', false);
  238. console.log('结束')
  239. }, 10 * 1000)
  240. }
  241. onMounted(() => {
  242. iconsArr.videoCloseIcon = cacheManager.get('projectImg').video_close_icon;
  243. iconsArr.videoPlayIcon = cacheManager.get('projectImg').video_play_icon;
  244. });
  245. defineExpose({
  246. init,
  247. showVideoBtn
  248. })
  249. </script>
  250. <style lang="scss">
  251. .zhuapai-drop-container {
  252. width: 180rpx;
  253. height: 400rpx;
  254. margin: 0;
  255. padding: 0;
  256. z-index: 10;
  257. position: absolute;
  258. overflow: hidden;
  259. .phone-camera-box-zhuapai {
  260. width: 100%;
  261. height: 240rpx;
  262. position: absolute;
  263. overflow: hidden;
  264. .uni-video-container {
  265. background-color: transparent;
  266. pointer-events: none;
  267. }
  268. .canvas-view-box,
  269. .hidden-video {
  270. transform: translateY(10000rpx);
  271. }
  272. }
  273. .video-view-box {
  274. width: 100%;
  275. height: 240rpx;
  276. position: absolute;
  277. }
  278. .shiti-video-hidden-btn,
  279. .shiti-video-show-btn {
  280. position: absolute;
  281. top: 0;
  282. icon{
  283. width: 32rpx;
  284. height: 32rpx;
  285. display: block;
  286. background-size: cover;
  287. background-repeat: no-repeat;
  288. background-position: center;
  289. }
  290. }
  291. .shiti-video-hidden-btn {
  292. width: 60rpx;
  293. height: 60rpx;
  294. left: 0;
  295. icon {
  296. margin: 6rpx auto 6rpx 6rpx;
  297. }
  298. }
  299. .shiti-video-show-btn {
  300. background-color: #dcfbf1;
  301. padding: 20rpx;
  302. border-radius: 8rpx;
  303. right: 0;
  304. }
  305. }
  306. </style>