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

全局定义和使用:

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

// 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('打开地理位置','')
            },
          })
        }
      },
    })
  },

})

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸曦穆泽

您的鼓励是我的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值