小程序_连接蓝牙设备根据平台去打开定位权限
侧边栏壁纸
  • 累计撰写 35 篇文章
  • 累计收到 1 条评论

小程序_连接蓝牙设备根据平台去打开定位权限

逸曦穆泽
2022-10-15 / 0 评论 / 64 阅读 / 正在检测是否收录...

全局定义和使用:

全局获取一次,适用单个或多个页面去使用,避免多次频繁去获取接口信息。

app.js
onLaunch() {
  const res = wx.getSystemInfoSync();
  this.globalData.platform = res.platform != "ios" ? true : false;
},
globalData: {
  platform: false, // 默认 ios,其他平台需要权限
}
xxx.js 使用
const app = getApp();
var platform = false; // 平台:iOS_false,其他_true
Page({
  onLoad: function (options) {
    let that = this;
    platform = app.globalData.platform
    if(platform){
      that.getLocation();
    }
  }
})

在单独页面使用:

单个页面获取与使用,在单页可以单次或多次使用,方便直接。

const app = getApp();
var platform = false; // 平台:iOS_false,其他_true
Page({
  onLoad: function (options) {
    let that = this;
    const res = wx.getSystemInfoSync()
    platform = res.platform != "ios" ? true : false;
    if(platform){
      that.getLocation();
    }
  },
 
  /** 获取定位信息 */
  getLocation: function () {
    // 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限
    wx.getSetting({
      success: function (res) {
        if(res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true){
          wx.showModal({
            title: '是否授权当前位置',
            content: '需要获取你的地理位置,请确认授权,否则无法获蓝牙',
            success: function(mres){
              if(mres.confirm){
                wx.openSetting({
                  success (authData) {
                    if(authData.authSetting['scope.userLocation'] == true){
                      wx.showToast({
                        title: '授权成功',
                        icon: "success",
                        duration: 1000
                      })
                    }else{
                      wx.showToast({
                        title: '授权失败',
                        icon: "error",
                        duration: 1000
                      })
                    }
                  }
                })
              }else if(mres.cancel){
                  wx.showToast({
                    title: '授权失败',
                    icon: "error",
                    duration: 1000
                  })
              }
            }
          })
        }else{
        // 当前位置 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标
          wx.getLocation({
            type: 'wgs84', 
            success: function (res) {
              app.hintLog('打开地理位置','')
            },
          })
        }
      },
    })
  },
})

可能有很多人有疑惑,为什么Android手机需要打开定位权限,其实不是的,Android手机需要打开定位权限的只是部分手机而已,并不是全部,如果可以,你可以根据部分手机去过滤掉也是可行的。

0

评论 (0)

取消