# 获取用户位置 用户拒绝后的重新获取位置的方法


//  在manifest.json文件中,mp-weixin属性下配置permission获取地理位置的权限
"permission": {
    // 获取当前的地理位置、速度 配置
    "scope.userLocation": {
        "desc": "你的位置信息将用于小程序位置接口的效果展示"
    }
}

// js 方法
getLocation () {
  let that = this
  // 获取用户是否开启 授权获取当前的地理位置、速度的权限。
  uni.getSetting({
    success (res) {
      console.log(res)

      // 如果没有授权
      if (!res.authSetting['scope.userLocation']) {
        // 则拉起授权窗口
        uni.authorize({
          scope: 'scope.userLocation',
          success () {
            //点击允许后--就一直会进入成功授权的回调 就可以使用获取的方法了
            uni.getLocation({
              type: 'wgs84',
              success: function (res) {
                that.x = res.longitude
                that.y = res.latitude
                console.log(res)
                console.log('当前位置的经度:' + res.longitude)
                console.log('当前位置的纬度:' + res.latitude)
                uni.showToast({
                  title: '当前位置的经纬度:' + res.longitude + ',' + res.latitude,
                  icon: 'success',
                  mask: true
                })
              }, fail (error) {
                console.log('失败', error)
              }
            })
          },
          fail (error) {
            //点击了拒绝授权后--就一直会进入失败回调函数--此时就可以在这里重新拉起授权窗口
            console.log('拒绝授权', error)

            uni.showModal({
              title: '提示',
              content: '若点击不授权,将无法使用位置功能',
              cancelText: '不授权',
              cancelColor: '#999',
              confirmText: '授权',
              confirmColor: '#f94218',
              success (res) {
                console.log(res)
                if (res.confirm) {
                  // 选择弹框内授权
                  uni.openSetting({
                    success (res) {
                      console.log(res.authSetting)
                    }
                  })
                } else if (res.cancel) {
                  // 选择弹框内 不授权
                  console.log('用户点击不授权')
                }
              }
            })
          }
        })
      } else {
        // 有权限则直接获取
        uni.getLocation({
          type: 'wgs84',
          success: function (res) {
            that.x = res.longitude
            that.y = res.latitude
            console.log(res)
            console.log('当前位置的经度:' + res.longitude)
            console.log('当前位置的纬度:' + res.latitude)
            uni.showToast({
              title: '当前位置的经纬度:' + res.longitude + ',' + res.latitude,
              icon: 'success',
              mask: true
            })
          }, fail (error) {
            uni.showToast({
              title: '请勿频繁调用!',
              icon: 'none',
            })
            console.log('失败', error)
          }
        })
      }
    }
  })
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

# JS处理富文本返回的数据


richTextHandle(strValue) {
  if (!strValue) return;
  strValue = strValue.replace(/&&/g, '&');
  strValue = strValue.replace(/&/g, '&');
  strValue = strValue.replace(/&lt;/g, '<');
  strValue = strValue.replace(/&gt;/g, '>');
  strValue = strValue.replace(/&quot;/g, '"');
  strValue = strValue.replace(/&#39;/g, "'");
  strValue = strValue.replace(/<table([\s\w"-=\/\.:;]+)/gi, '<table$1 class="table"');
  strValue = strValue.replace(/<td([\s\w"-=\/\.:;]+)/gi, '<td$1 class="td"');
  strValue = strValue.replace(/<td\s*/gi, `<td class="td"`);
  strValue = strValue.replace(/<th\s*/gi, '<th class="th" ');
  return strValue;
},

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Last Updated: 2/15/2023, 9:51:53 AM