index.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. nextTick(() => {
  58. startCamera()
  59. })
  60. }
  61. function handleClose() {
  62. emits('cancel')
  63. popupRef.value.close();
  64. stopCamera && stopCamera()
  65. }
  66. function handleConfirm() {
  67. emits('success')
  68. popupRef.value.close();
  69. stopCamera && stopCamera()
  70. }
  71. defineExpose({
  72. showDialog
  73. })
  74. onUnmounted(() => {
  75. // 组件销毁时停止摄像头
  76. stopCamera && stopCamera();
  77. })
  78. </script>
  79. <style lang="scss">
  80. .phone-camera-box {
  81. width: 100%;
  82. }
  83. .uni-video-container {
  84. width: 100%;
  85. }
  86. .uni-video-video {
  87. width: 100%;
  88. }
  89. .my-dialog-cc .uni-dialog-button-group .uni-border-left .uni-button-color {
  90. color: #3fd2a1;
  91. }
  92. </style>