Jelajahi Sumber

feat: 摄像头权限判断

chenlei 2 bulan lalu
induk
melakukan
be60136904
1 mengubah file dengan 58 tambahan dan 4 penghapusan
  1. 58 4
      subPackages/pages/ar/index.js

+ 58 - 4
subPackages/pages/ar/index.js

@@ -18,18 +18,72 @@ Component({
     forzen: false,
     showVideo: false,
     markerIndex: 1, // 使用的 marker 索引
+    hasCameraPermission: true, // 是否有摄像头权限
   },
   hintInfo: undefined, // 提示框信息
   pageLifetimes: {
     show() {
-      setTimeout(() => {
-        this.setData({
-          forzen: false
+      this.checkCameraPermission()
+        .then(hasPermission => {
+          if (hasPermission) {
+            setTimeout(() => {
+              this.setData({
+                forzen: false
+              })
+            }, 1000)
+          }
         })
-      }, 1000)
     }
   },
   methods: {
+    checkCameraPermission() {
+      return new Promise((resolve) => {
+        wx.getSetting({
+          success: (res) => {
+            // 检查是否有摄像头权限
+            if (!res.authSetting['scope.camera']) {
+              this.setData({ hasCameraPermission: false })
+              // 询问用户是否授权
+              wx.showModal({
+                title: '权限申请',
+                content: '需要获取摄像头权限以进行AR识别',
+                confirmText: '去开启',
+                cancelText: '取消',
+                success: (modalRes) => {
+                  if (modalRes.confirm) {
+                    // 打开权限设置页
+                    wx.openSetting({
+                      success: (settingRes) => {
+                        const hasPermission = !!settingRes.authSetting['scope.camera']
+                        this.setData({ hasCameraPermission: hasPermission })
+                        resolve(hasPermission)
+                        // 如果获得权限,初始化相关功能
+                        if (hasPermission) {
+                          this.initIfNeeded()
+                        }
+                      }
+                    })
+                  } else {
+                    resolve(false)
+                  }
+                }
+              })
+            } else {
+              this.setData({ hasCameraPermission: true })
+              resolve(true)
+            }
+          },
+          fail: () => {
+            resolve(false)
+          }
+        })
+      })
+    },
+    initIfNeeded() {
+      if (!this.session) {
+        this.init()
+      }
+    },
     handleEnded() {
       wx.navigateTo({
         url: '/pages/webview/index?url=' + encodeURIComponent(`https://sit-kelamayi.4dage.com/zuan/#/info/${this.data.markerIndex}`),