index.vue 2.4 KB

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