index.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="phone-camera-box">
  3. <view v-show="!showConfirmBtn" class="sxt-tip-box" style="text-align: center;">{{textMess}}</view>
  4. <camera v-show="showConfirmBtn" class="camera" device-position="front" flash="off" @initdone="onVideoSuccess"
  5. style="width:100%; height: 320rpx;"></camera>
  6. </view>
  7. </template>
  8. <script setup>
  9. import {
  10. ref,
  11. onUnmounted,
  12. onMounted,
  13. nextTick
  14. } from "vue"
  15. import {
  16. useH5Camera
  17. } from "./useCamera.js"
  18. const emits = defineEmits(['success', 'error', 'cancel'])
  19. const popupRef = ref(null)
  20. const showConfirmBtn = ref(false);
  21. const cameraContext = ref(null);
  22. let zhuapaiFun = null;
  23. let stopCamera = null;
  24. let errorFunCompleteFn = null;
  25. const textMess = ref('摄像头正在初始化...')
  26. function startCamera() {
  27. // 请求摄像头权限并获取流
  28. cameraContext.value = uni.createCameraContext();
  29. }
  30. function handleZhua() {
  31. zhuapaiFun()
  32. }
  33. function onVideoSuccess() {
  34. showConfirmBtn.value = true;
  35. }
  36. function onVideoError(e) {
  37. showConfirmBtn.false = true;
  38. textMess.value = '摄像头初始化失败!'
  39. emits('error')
  40. errorFunCompleteFn && errorFunCompleteFn(e)
  41. }
  42. function showDialog() {
  43. textMess.value = '摄像头正在初始化...'
  44. setTimeout(() => {
  45. showConfirmBtn.value = true;
  46. nextTick(() => {
  47. startCamera()
  48. })
  49. },1000)
  50. }
  51. function handleClose() {
  52. emits('cancel')
  53. stopCamera && stopCamera()
  54. }
  55. function handleConfirm() {
  56. emits('success')
  57. stopCamera && stopCamera()
  58. }
  59. defineExpose({
  60. showDialog
  61. })
  62. onUnmounted(() => {
  63. // 组件销毁时停止摄像头
  64. stopCamera && stopCamera();
  65. })
  66. </script>
  67. <style lang="scss">
  68. .phone-camera-box {
  69. margin-top: 60px;
  70. width: 100%;
  71. }
  72. .uni-video-container {
  73. width: 100%;
  74. }
  75. .uni-video-video {
  76. width: 100%;
  77. }
  78. .my-dialog-cc .uni-dialog-button-group .uni-border-left .uni-button-color {
  79. color: #3fd2a1;
  80. }
  81. </style>