cameraCommon.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <uni-popup ref="commonPopup" :animation="false" :is-mask-click="false" mask-background-color="rgba(0, 0, 0, 0.4)">
  3. <view class="phone-common-dialog">
  4. <view class="common-body-box">
  5. <view class="common-title">{{title}}</view>
  6. <view class="common-content" :class="dialogContentClass">
  7. <view v-if="showCamera" class="camera-modal">
  8. <view class="mask" @click="closeCamera"></view>
  9. <view class="modal-content">
  10. <camera class="camera-view" device-position="front"></camera>
  11. <button class="not-confirm-btn" @click="closeCamera">关闭</button>
  12. </view>
  13. </view>
  14. </view>
  15. <view class="common-btn-box">
  16. <view class="not-confirm-btn" @click="handleClose">{{notBtn}}</view>
  17. <view class="confirm-btn" @click="confirmBtn">{{okBtn}}</view>
  18. </view>
  19. </view>
  20. </view>
  21. </uni-popup>
  22. </template>
  23. <script setup>
  24. import {
  25. ref
  26. } from 'vue';
  27. const props = defineProps({
  28. title: {
  29. type: String,
  30. default: ''
  31. },
  32. content: {
  33. type: String,
  34. require: true,
  35. default: ''
  36. },
  37. dialogContentClass: {
  38. type: String,
  39. require: true,
  40. default: 'content-center-class'
  41. },
  42. notBtn: {
  43. type: String,
  44. require: true,
  45. default: '取消'
  46. },
  47. okBtn: {
  48. type: String,
  49. require: true,
  50. default: '确认'
  51. },
  52. });
  53. const commonPopup = ref(null);
  54. let showCamera = ref(false)
  55. let cameraContext = ref(null)
  56. let listener = ref(null)
  57. const $emit = defineEmits(['confirm-btn'])
  58. function handleShow() {
  59. commonPopup.value.open();
  60. handleCheck()
  61. }
  62. function handleClose() {
  63. commonPopup.value.close();
  64. showCamera.value = false;
  65. }
  66. function confirmBtn() {
  67. $emit('confirm-btn');
  68. commonPopup.value.close();
  69. showCamera.value = false;
  70. }
  71. function handleCheck() {
  72. try {
  73. const systemInfo = uni.getSystemInfo();
  74. // if (!systemInfo.cameraAuthorized) {
  75. // uni.showToast({
  76. // title: '设备不支持摄像头',
  77. // duration: 2000
  78. // });
  79. // }
  80. uni.getSetting({
  81. success(res) {
  82. const authSetting = res.authSetting || {}
  83. if (!authSetting['scope.camera']) {
  84. const {
  85. errMsg
  86. } = uni.authorize({
  87. scope: 'scope.camera'
  88. });
  89. // if (errMsg !== 'authorize:ok') {
  90. // throw new Error('权限被拒绝');
  91. // }
  92. }
  93. showCamera.value = true;
  94. setTimeout(() => {
  95. cameraContext.value = uni.createCameraContext();
  96. console.log('cameraContext.value', cameraContext.value);
  97. listener.value = cameraContext.value.onCameraFrame((frame) => {
  98. // console.log(frame.data instanceof ArrayBuffer, frame.width, frame.height)
  99. })
  100. // console.log('listener.value',listener.value);
  101. listener.value.start()
  102. }, 500)
  103. },
  104. fail(err) {
  105. handleError(err)
  106. }
  107. })
  108. } catch (err) {
  109. handleError(err);
  110. }
  111. }
  112. function closeCamera() {
  113. if (listener.value) {
  114. listener.value.stop();
  115. }
  116. showCamera.value = false;
  117. handleClose()
  118. }
  119. function handleError(err) {
  120. uni.showModal({
  121. title: '权限验证失败',
  122. content: err.message,
  123. confirmText: '去设置',
  124. success: (res) => {
  125. if (res.confirm) {
  126. // #ifdef MP-WEIXIN
  127. wx.openSetting();
  128. // #endif
  129. }
  130. }
  131. });
  132. }
  133. defineExpose({
  134. handleShow,
  135. handleCheck,
  136. handleClose
  137. })
  138. </script>
  139. <style>
  140. </style>