Browse Source

获取位置

刘洋 1 year ago
parent
commit
2f1e1d863e
3 changed files with 77 additions and 20 deletions
  1. 10 2
      src/manifest.json
  2. 66 17
      src/pages/home/home.vue
  3. 1 1
      src/utils/upgrade.js

+ 10 - 2
src/manifest.json

@@ -95,7 +95,7 @@
   },
   "quickapp": {},
   "mp-weixin": {
-    "appid": "wx3564271a274bd400",
+    "appid": "wx8b4c77de1b6f387b",
     "setting": {
       "urlCheck": true,
       "es6": true,
@@ -105,7 +105,15 @@
     "optimization": {
       "subPackages": true
     },
-    "usingComponents": true
+    "usingComponents": true,
+    "requiredPrivateInfos": [
+      "getLocation"
+    ],
+    "permission": {
+      "scope.userLocation": {
+        "desc": "定位打卡服务需要获取您的位置信息"
+      }
+    }
   },
   "mp-alipay": {
     "usingComponents": true,

+ 66 - 17
src/pages/home/home.vue

@@ -1,6 +1,6 @@
 <template>
   <view class="home">
-    <camera class="camera" device-position="front" flash="off"></camera>
+    <!-- <camera class="camera" device-position="front" flash="off"></camera> -->
     <image v-if="tempImg" mode="widthFix" :src="tempImg" />
   </view>
 </template>
@@ -12,16 +12,15 @@
     data() {
       return {
         cameraAuth: false,
+        locationAuth: false,
         tempImg: '',
         VKSession: null,
         videoCtx: null,
         listener: null
       }
     },
-
-    mounted() {
-      this.getAuth()
-      this.initData()
+    onShow() {
+      this.getLocationAuth()
     },
     onUnload() {
       this.VKSession?.destroy()
@@ -30,8 +29,21 @@
           console.log('listener.stop', res)
         }
       })
+      this.VKSession = null
+      this.listener = null
     },
     methods: {
+      getMyLocation() {
+        console.log('getMyLocation:')
+        uni.getLocation({
+          success(res) {
+            console.log('位置信息', res)
+          },
+          fail(err) {
+            console.log('fail:', err)
+          }
+        })
+      },
       async openSetting() {
         const _this = this
         let promise = new Promise((resolve, reject) => {
@@ -57,38 +69,72 @@
                   }
                 })
               } else if (res.cancel) {
-                setTimeout(() => {
-                  _this.openSetting().then((res) => {
-                    resolve(true)
-                  })
-                }, 3000)
+                // setTimeout(() => {
+                //   _this.openSetting().then((res) => {
+                //     resolve(true)
+                //   })
+                // }, 3000)
               }
             }
           })
         })
         return promise
       },
-      async getAuth() {
+      async getLocationAuth() {
+        const _this = this
+        uni.getSetting({
+          success(res) {
+            console.log('authSetting:', res.authSetting)
+
+            if (res.authSetting['scope.userLocation']) {
+              // 用户已经授权
+              _this.locationAuth = true
+              _this.getMyLocation()
+            } else {
+              uni.authorize({
+                scope: 'scope.userLocation',
+                success() {
+                  // 用户同意授权
+                  _this.locationAuth = true
+                  _this.getMyLocation()
+                },
+                fail() {
+                  // 用户不同意授权
+                  // _this.openSetting().then((res) => {
+                  //   console.log('终于授权了')
+                  //   _this.cameraAuth = true
+                  // })
+                  _this.locationAuth = false
+                }
+              })
+            }
+          }
+        })
+      },
+      //获取摄像头权限,按需调用,如果该用户被配置为需要人脸打卡,再引导用户开启
+      getCameraAuth() {
         const _this = this
         uni.getSetting({
           success(res) {
-            console.log('sss', res.authSetting)
             if (res.authSetting['scope.camera']) {
               // 用户已经授权
               _this.cameraAuth = true
+              _this.initFaceData()
             } else {
               uni.authorize({
                 scope: 'scope.camera',
                 success() {
                   // 用户同意授权
                   _this.cameraAuth = true
+                  _this.initFaceData()
                 },
                 fail() {
                   // 用户不同意授权
-                  _this.openSetting().then((res) => {
-                    console.log('终于授权了')
-                    _this.cameraAuth = true
-                  })
+                  // _this.openSetting().then((res) => {
+                  //   console.log('终于授权了')
+                  //   _this.cameraAuth = true
+                  // })
+                  _this.cameraAuth = false
                 }
               })
             }
@@ -105,7 +151,10 @@
           modelMode: 1
         })
       },
-      initData() {
+      initFaceData() {
+        if (this.VKSession && this.listener) {
+          return
+        }
         this.videoCtx = wx.createCameraContext()
         console.log('videoCtx:', this.videoCtx)
         let count = 0

+ 1 - 1
src/utils/upgrade.js

@@ -4,7 +4,7 @@
 export const wxUpgrade = () => {
   const updateManager = uni.getUpdateManager()
   updateManager.onCheckForUpdate((res) => {
-    console.log(res)
+    console.log('onCheckForUpdate', res)
   })
 
   updateManager.onUpdateReady((res) => {