微信小程序定位权限封装

微信小程序定位权限封装

getUserOrSystemPermissions.js

//如需动态获取权限,修改res.authSetting['scope.userLocation']与scope: 'scope.userLocation'的值。
const getLocation = () => {
  return new Promise((resolve, reject) => {
    wx.getSystemInfo({
      success(res) {
        var isSystemLocatioon = res.locationAuthorized;
        if (isSystemLocatioon == false) {
          wx.showModal({
            title: '提示',
            content: '请授权微信定位权限,\n前往【设置】中开启',
            showCancel: false,
            success(res) {
              if (res.confirm) {
                reject()
              }
            }
          })
        } else {
          wx.getSetting({
            success(res) {
              if (!res.authSetting['scope.userLocation']) {
                wx.authorize({
                  scope: 'scope.userLocation',
                  success() {
                    resolve()
                  },
                  fail(e) {
                    wx.showModal({
                      title: '提示',
                      content: '请前往设置打开权限',
                      success(res) {
                        if (res.confirm) {
                          wx.openSetting({
                            success(res) {
                              resolve()
                            },
                            fail(err) {
                              reject()
                            }
                          })
                        } else if (res.cancel) {
                          wx.showModal({
                            title: '提示',
                            content: '请授权小程序定位权限,\n「右上角」-「设置」中开启',
                            showCancel: false,
                            success(res) {
                              if (res.confirm) {
                                reject()
                              }
                            }
                          })
                        }
                      }
                    })
                  }
                })
              } else {
                resolve()
              }
            }
          })
        }
      }
    })
  })
}
module.exports = {
  getLocation,
}

get_address.js

var getUOSPermissions = require("../../utils/getUserOrSystemPermissions.js")
onLoad: function (options) {
	getUOSPermissions.getLocation().then(res => {
      	that.setData({})
      	wx.getLocation({
        	type: 'wgs84', //返回可以用于wx.openLocation的经纬度
        	success(res) {
          	that.setData({
            	latitude: res.latitude,
            	longitude: res.longitude
          	})
        	}
      	})
    	}).catch(e => {
      	console.log("异常:", e);
    	})
  	}
}

app.json

	"permission": {
   	 	"scope.userLocation": {
      		"desc": "你的位置信息将用于小程序位置接口"
    	}
  }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值