柒零 1 rok pred
rodič
commit
b66946cee8

+ 59 - 1
componets/authorized-login-dialog/authorizedLoginDialog.js

@@ -63,6 +63,8 @@ Component({
     unionid: "",
     openid: "",
     session_key: "",
+    latitude: null,
+    longitude: null,
   },
 
   /**
@@ -243,6 +245,61 @@ Component({
         return;
       }
       var that = this;
+      that.getLocation(e);
+    },
+    getLocation(e) {
+      const that = this;
+      wx.getSetting({
+        success(res) {
+          if (res.authSetting['scope.userLocation']) {
+            // 用户已经授权  
+            wx.getLocation({
+              type: 'gcj02', // 返回可以用于wx.openLocation的经纬度  
+              success(locationRes) {
+                that.setData({
+                  latitude: locationRes.latitude,
+                  longitude: locationRes.longitude,
+                  error: null
+                });
+                console.log('纬度:', locationRes.latitude);
+                console.log('经度:', locationRes.longitude);
+                that.authorizedLogin(e);
+              },
+              fail(err) {
+                that.setData({
+                  latitude: null,
+                  longitude: null,
+                  error: err.message
+                });
+                console.error('获取位置失败:', err.message);
+                that.authorizedLogin(e);
+              }
+            });
+          } else {
+            // 用户未授权  
+            wx.authorize({
+              scope: 'scope.userLocation',
+              success() {
+                // 用户同意授权后再次获取位置  
+                that.getLocation();
+              },
+              fail() {
+                // 用户拒绝授权  
+                that.setData({
+                  latitude: null,
+                  longitude: null,
+                  error: '用户拒绝授权获取地理位置'
+                });
+                console.error('用户拒绝授权获取地理位置');
+                that.authorizedLogin(e);
+              }
+            });
+          }
+        }
+      });
+    },
+    authorizedLogin(e) {
+      const that = this;
       if (e.detail.userInfo) {
         // 用户点击允许,获取到用户信息  
         this.setData({
@@ -258,7 +315,8 @@ Component({
             openid: wx.getStorageSync("openid"),
             avatarUrl: that.data.avatarUrl,
             nickname: that.data.nickName,
-            encryptphone: that.data.encryptphone
+            encryptphone: that.data.encryptphone,
+            longandlat: that.data.longitude + "|" + that.data.latitude
           },
           method: 'POST',
           success: function (loginRes) {