WeChat applet authorization location logic

 // 微信授权信息位置逻辑
    getAuthLocation() {
    
    
      // 获取用户的授权设置信息
      uni.getSetting({
    
    
        success: (res) => {
    
    
          // 检查用户是否已经授权了位置信息
          if (res.authSetting["scope.userLocation"]) {
    
    
            // 用户已经授权位置信息,执行后续逻辑
            this.getUserLocalhost();
          } else {
    
    
            // 用户未授权位置信息,请求授权
            uni.authorize({
    
    
              scope: "scope.userLocation",
              success: () => {
    
    
                // 用户授权位置信息成功,执行后续逻辑
                this.getUserLocalhost();
              },
              fail: () => {
    
    
                // 用户拒绝授权位置信息,显示提示框或其他处理
                uni.showModal({
    
    
                  title: "铅理智行",
                  content: "小程序需要您的位置授权信息,以方便定位显示周围商户",
                  success: (res) => {
    
    
                    wx.openSetting({
    
    
                      success: (res) => {
    
    
                        // 检查用户的授权设置信息
                        if (res.authSetting["scope.userLocation"]) {
    
    
                          // 用户重新授权位置信息成功,执行后续逻辑
                          this.getUserLocalhost();
                        } else {
    
    
                          // 用户依然未授权位置信息,可以显示提示框或其他处理
                          wx.showToast({
    
    
                            title: "需要授权位置信息才能正常使用",
                            icon: "none",
                            duration: 2000,
                          });
                        }
                      },
                    });
                  },
                  fail: (res) => {
    
    },
                });
              },
            });
          }
        },
      });
    },
    // 获取当前位置授权成功后 获取当前的点位坐标在地图上画点以及执行其他逻辑
    getUserLocalhost() {
    
    
      uni.getLocation({
    
    
        type: "gcj02",
        success: function (res) {
    
    
          uni.setStorageSync(keys.mylocalhost, res);
          that.longitude = res.longitude;
          that.latitude = res.latitude;
          that.$forceUpdate();
          that.circles = [];
          that.covers.length = 0;
          //画圈
          that.circles.push({
    
    
            longitude: res.longitude,
            latitude: res.latitude,
            radius: 20,
            fillColor: "#7cb5ec88",
            level: "1",
          });
          that.myPosition = [
            {
    
    
              id: -1,
              longitude: res.longitude,
              latitude: res.latitude,
              iconPath:
                "",
              width: 40,
              height: 40,
            },
          ];
          let tempPosition = [
            {
    
    
              id: -1,
              longitude: res.longitude,
              latitude: res.latitude,
              iconPath:
                "",
              width: 40,
              height: 40,
            },
          ];
          that.covers = that.myStoreList.concat(tempPosition);
          that.$system
            .showLocalhostAddress(res.longitude, res.latitude)
            .then((res) => {
    
    
              if (res.regeocode) {
    
    
                uni.setStorageSync(keys.myaddressinfo, res.regeocode);
              }
            });
          that.isRefresh = false;
          that.isRefresh = true;
          console.log(that.covers);
        },
        fail: (res) => {
    
    
          // 微信官方规定该接口调用间隔时间须大于30秒
          // 如果小于30秒则会直接走失败回调,该失败回调无法刷新用户位置信息
          console.log("fail 分支");
          let tempPosition = [
            {
    
    
              id: -1,
              longitude: that.longitude,
              latitude: that.latitude,
              iconPath:
                "",
              width: 40,
              height: 40,
            },
          ];
          that.myPosition = [
            {
    
    
              id: -1,
              longitude: that.longitude,
              latitude: that.latitude,
              iconPath:
                "",
              width: 40,
              height: 40,
            },
          ];
          that.covers = that.myStoreList.concat(tempPosition);
        },
      });
    },

Guess you like

Origin blog.csdn.net/LRQQHM/article/details/132753787