多个页面需要判断是否授权地理位置,怎么封装授权地理位置好?
发布于 5 年前 作者 taoliu 10652 次浏览 来自 官方Issues

?1、是不是通过如下判断是否授权

// 1 地理位置授权
   wx.getSetting({
     success(res) {
       if (!res.authSetting['scope.userLocation']) {
         console.log("1-没有授权《地理位置》权限");
         // 接口调用询问 
         wx.authorize({
           scope: 'scope.userLocation',
           success() {
             console.log("2-授权《地理位置》权限成功");
             //获取地理位置信息
             that.getLocation();
           },
           fail() {// 用户拒绝了授权 
             console.log("2-授权《地理位置》权限失败");

           }

}

?2、如果我5个页面都要判断是否授权,这样写就太麻烦,怎么提出去呀

描述不是很清楚,望大神解答,或者讲解一下设计思路,非常感谢!

2 回复

封装到你的 util.js 里边岂不美哉?

/** 检测是否有定位权限BY 小程序 **/
function checkHasLocationPermissionByMP() {
  return new Promise(function(resolve, reject) {
    wx.getSetting({
      success(sd) {
        if (!sd.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success(e) {
              resolve()
            },
            fail(e) {
              reject()
            }
          })
        } else {
          resolve()
        }
      }
    })
  })

}

module.exports = {

checkHasLocationPermissionByMP

}



app.js里写,我们是做用户如果跑到其他城市了 会有提示 是否更换城市

回到顶部