123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <template>
- <uni-popup ref="commonPopup" :animation="false" :is-mask-click="false" mask-background-color="rgba(0, 0, 0, 0.4)">
- <view class="phone-common-dialog">
- <view class="common-body-box">
- <view class="common-title">{{title}}</view>
- <view class="common-content" :class="dialogContentClass">
- <view v-if="showCamera" class="camera-modal">
- <view class="mask" @click="closeCamera"></view>
- <view class="modal-content">
- <camera class="camera-view" device-position="front"></camera>
- <button class="not-confirm-btn" @click="closeCamera">关闭</button>
- </view>
- </view>
- </view>
- <view class="common-btn-box">
- <view class="not-confirm-btn" @click="handleClose">{{notBtn}}</view>
- <view class="confirm-btn" @click="confirmBtn">{{okBtn}}</view>
- </view>
- </view>
- </view>
- </uni-popup>
- </template>
- <script setup>
- import {
- ref
- } from 'vue';
- const props = defineProps({
- title: {
- type: String,
- default: ''
- },
- content: {
- type: String,
- require: true,
- default: ''
- },
- dialogContentClass: {
- type: String,
- require: true,
- default: 'content-center-class'
- },
- notBtn: {
- type: String,
- require: true,
- default: '取消'
- },
- okBtn: {
- type: String,
- require: true,
- default: '确认'
- },
- });
- const commonPopup = ref(null);
- let showCamera = ref(false)
- let cameraContext = ref(null)
- let listener = ref(null)
- const $emit = defineEmits(['confirm-btn'])
- function handleShow() {
- commonPopup.value.open();
- handleCheck()
- }
- function handleClose() {
- commonPopup.value.close();
- showCamera.value = false;
- }
- function confirmBtn() {
- $emit('confirm-btn');
- commonPopup.value.close();
- showCamera.value = false;
- }
- function handleCheck() {
- try {
- const systemInfo = uni.getSystemInfo();
- // if (!systemInfo.cameraAuthorized) {
- // uni.showToast({
- // title: '设备不支持摄像头',
- // duration: 2000
- // });
- // }
- uni.getSetting({
- success(res) {
- const authSetting = res.authSetting || {}
- if (!authSetting['scope.camera']) {
- const {
- errMsg
- } = uni.authorize({
- scope: 'scope.camera'
- });
- // if (errMsg !== 'authorize:ok') {
- // throw new Error('权限被拒绝');
- // }
- }
- showCamera.value = true;
- setTimeout(() => {
- cameraContext.value = uni.createCameraContext();
- console.log('cameraContext.value', cameraContext.value);
- listener.value = cameraContext.value.onCameraFrame((frame) => {
- // console.log(frame.data instanceof ArrayBuffer, frame.width, frame.height)
- })
- // console.log('listener.value',listener.value);
- listener.value.start()
- }, 500)
- },
- fail(err) {
- handleError(err)
- }
- })
- } catch (err) {
- handleError(err);
- }
- }
- function closeCamera() {
- if (listener.value) {
- listener.value.stop();
- }
- showCamera.value = false;
- handleClose()
- }
- function handleError(err) {
- uni.showModal({
- title: '权限验证失败',
- content: err.message,
- confirmText: '去设置',
- success: (res) => {
- if (res.confirm) {
- // #ifdef MP-WEIXIN
- wx.openSetting();
- // #endif
- }
- }
- });
- }
- defineExpose({
- handleShow,
- handleCheck,
- handleClose
- })
- </script>
- <style>
- </style>
|