index.vue 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <view v-show="!showConfirmBtn" class="sxt-tip-box">{{textMess}}</view>
  3. <camera v-show="showConfirmBtn" class="zp-camera" device-position="front" flash="off" @initdone="onVideoSuccess"></camera>
  4. </template>
  5. <script setup>
  6. import {
  7. ref,
  8. onUnmounted,
  9. onMounted,
  10. nextTick
  11. } from "vue"
  12. import {
  13. useH5Camera
  14. } from "./useCamera.js"
  15. const emits = defineEmits(['success', 'error', 'cancel'])
  16. const popupRef = ref(null)
  17. const showConfirmBtn = ref(false);
  18. const cameraContext = ref(null);
  19. let zhuapaiFun = null;
  20. let stopCamera = null;
  21. let errorFunCompleteFn = null;
  22. const textMess = ref('摄像头正在初始化...')
  23. function startCamera() {
  24. // 请求摄像头权限并获取流
  25. cameraContext.value = uni.createCameraContext();
  26. }
  27. function handleZhua() {
  28. zhuapaiFun()
  29. }
  30. function onVideoSuccess() {
  31. showConfirmBtn.value = true;
  32. }
  33. function onVideoError(e) {
  34. showConfirmBtn.false = true;
  35. textMess.value = '摄像头初始化失败!'
  36. emits('error')
  37. errorFunCompleteFn && errorFunCompleteFn(e)
  38. }
  39. function showDialog() {
  40. textMess.value = '摄像头正在初始化...'
  41. setTimeout(() => {
  42. showConfirmBtn.value = true;
  43. nextTick(() => {
  44. startCamera()
  45. })
  46. },1000)
  47. }
  48. function handleClose() {
  49. emits('cancel')
  50. stopCamera && stopCamera()
  51. }
  52. function handleConfirm() {
  53. emits('success')
  54. stopCamera && stopCamera()
  55. }
  56. defineExpose({
  57. showDialog
  58. })
  59. onUnmounted(() => {
  60. // 组件销毁时停止摄像头
  61. stopCamera && stopCamera();
  62. })
  63. </script>