Skip to content
目录

获取授权和定位封装

common.js

javascript
// common.js
function getLocation() {
  return new Promise(function (resolve,reject) {
    wx.getSetting({
      success: res => {
        // scope.userLocation  == undefined代表用户未授权且第一次登陆
        console.log('检查地理位置信息是否授权', res.authSetting['scope.userLocation'])
        if (res.authSetting['scope.userLocation'] === undefined) {
          //如果用户是第一次登陆且未授权的情况,会直接弹窗请求授权
          // 使用 getlocation 获取用户 经纬度位置
          wx.getLocation({
            //这里我们要指定定位类型是gcj02,因为不填默认是wgs84,定位精确度会相较于gcj02有几百到一千米的偏差,如果对精确度要求较高的请务必加上type:'gcj02'
            type: 'gcj02',
            isHighAccuracy: true,
            highAccuracyExpireTime: '3100ms',
            success: res => {
              //获取用户位置成功后,将会返回 latitude, longitude 两个字段,代表用户的经纬度位置
              resolve(res);
              // //给经纬度定义为全局变量,可以在全局调用
              // app.latitude=res.latitude;
              // app.longitude=res.longitude;
            },
            fail: err => {
              console.log(err)
              //用户已授权,但是获取地理位置失败,会弹框提示用户去系统设置中打开定位
              wx.showModal({
                title: '',
                content: '请在手机系统设置中打开定位服务,重新进入小程序',
                confirmText: '确定',
                success: res => {
                }
              })
            }
          })
        }
        //小程序检测到用户不是第一次进入该页面,且未授权
        else if (res.authSetting['scope.userLocation'] !== undefined && res.authSetting['scope.userLocation'] !== true) {
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取您的地理位置,请确认授权,否则无法获取您所需数据',
            success: res => {
              //如果点击取消则显示授权失败
              if (res.cancel) {
                wx.showToast({
                  title: '授权失败',
                  icon: 'error',
                  duration: 1000
                })
                wx.openSetting()
              }
              //如果点击确定会打开授权页请求二次授权
              else if (res.confirm) {
                wx.openSetting({
                  success: dataAu => {
                    if (dataAu.authSetting["scope.userLocation"] === true) {
                      wx.navigateBack({delta: 1});
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权,调用getLocationt的API
                      wx.getLocation({
                        type: 'gcj02',
                        isHighAccuracy: true,
                        highAccuracyExpireTime: '3100ms',
                        success: res => {
                          resolve(res);
                        },
                        fail: err => {
                          //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
                          wx.showModal({
                            title: '',
                            content: '请在系统设置中打开定位服务,否则无法正常使用',
                            confirmText: '确定',
                            success: res => {}
                          })
                        }
                      })
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'error',
                        duration: 1000
                      })
                      wx.openSetting()
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] === true) {
          //授权后默认加载,直接获取定位
          wx.getLocation({
            type: 'gcj02',
            success: res => {
              resolve(res);
            },
            fail(err) {
              if (err.errMsg !== 'getLocation:fail 频繁调用会增加电量损耗,可考虑使用 wx.onLocationChange 监听地理位置变化') {
                //用户已授权,但是获取地理位置失败,提示用户去系统设置中打开定位
                wx.showModal({
                  title: '',
                  content: '请在手机系统设置中打开定位服务,重新进入小程序',
                  confirmText: '确定',
                  success: res => {
                    // 未授权强制退出
                    wx.exitMiniProgram({})
                  }
                })
              }
            }
          })
        }
      }
    })
  })
}

const common = {
  getLocation,
};
export default common;