uniapp获取当前位置及检测授权状态——支持App、微信小程序

uniapp获取当前位置检测及定位权限——支持App、微信小程序

首先,祝天下母亲,节日快乐~



Tips:


效果图

在这里插入图片描述


在这里插入图片描述


新增 兼容小程序方法

  • location.js 新增 兼容小程序方法
  • 新增方法:
    • getSetting
    • showConfirm
    • openSetting
// #ifdef APP-PLUS
import permision from '@/common/js/permission.js'
// #endif
import store from '@/store/index.js'

var modalInfo = {
	content: '为了您更好的体验App蓝牙功能,需要获取位置信息,请点击设置开启定位权限',
	confirmText: '设置'
}

export async function getLocation(cb) {
	// #ifdef APP-PLUS
	let status = await checkPermission();
	if (status !== 1) {
		return status;
	}
	// #endif
	
	// #ifdef MP-WEIXIN
	let status = await getSetting();
	if (status === 2) {
	    showConfirm();
	    return;
	}
	// #endif
	
	if (typeof cb == 'function') cb && typeof cb == 'function' && cb(status)
	doGetLocation();
}

export const doGetLocation = () => {
	uni.getLocation({
		success: (res) => {
			store.commit('SET_LOCATION', {
				lng: res.longitude,
				lat: res.latitude
			});
			console.log('当前位置:', res.longitude, res.latitude);
		},
		fail: (err) => {
			if (err.errMsg.indexOf("auth deny") >= 0) {
				uni.showToast({
					title: '访问位置被拒绝',
					icon: 'none'
				})
			} else {
				uni.showToast({
					title: err.errMsg,
					icon: 'none'
				})
			}
		}
	})
}

async function checkPermission() {
	let status = permision.isIOS ? await permision.requestIOS('location') :
		await permision.requestAndroid('android.permission.ACCESS_FINE_LOCATION');

	if (status === null || status === 1) {
		status = 1;
	} else if (status === 2) {
		uni.showModal({
			content: '系统定位已关闭',
			showCancel: false,
			success: function(res) {}
		})
	} else if (status.code) {
		uni.showModal({
			content: status.message
		})
	} else {
		uni.showModal({
			content: modalInfo.content,
			confirmText: modalInfo.confirmText,
			success: function(res) {
				if (res.confirm) {
					permision.gotoAppSetting();
				}
			}
		})
	}

	return status;
}

/**
 * 获取用户的当前设置
 */
function getSetting() {
	return new Promise((resolve, reject) => {
		uni.getSetting({
			success: (res) => {
				if (res.authSetting['scope.userLocation'] === undefined) {
					resolve(0);
					return;
				}
				if (res.authSetting['scope.userLocation']) {
					resolve(1);
				} else {
					resolve(2);
				}
			}
		});
	});
}

/**
 * 拒绝授权:弹窗可以和App共用,根据平台调用方法,自行修改下即可;
 */
function showConfirm() {
	uni.showModal({
		content: modalInfo.content,
		confirmText: modalInfo.confirmText,
		showCancel: true,
		success: (res) => {
			if (res.confirm) {
				openSetting();
			}
		}
	})
}

/**
 * 如果拒绝授权,调起微信小程序设置界面
 */
function openSetting() {
	uni.openSetting({
		success: (res) => {
			if (res.authSetting && res.authSetting['scope.userLocation']) {
				doGetLocation();
			}
		},
		fail: (err) => {}
	})
}


const location = {
	getLocation
}
export default location;

manifest

  • manifest.js 中,添加 "requiredPrivateInfos" : [ "getLocation" ]
"mp-weixin" : {
	"appid" : "xxxx",
	"setting" : {
		"urlCheck" : false,
		"es6" : true,
		"postcss" : true,
		"minified" : true
	},
	"usingComponents" : true,
	"lazyCodeLoading" : "requiredComponents",
	"permission" : {
		"scope.userLocation" : {
			"desc" : "你的位置信息将用于为了更好体验蓝牙功能"
		}
	},
	"libVersion" : "latest",
	"requiredPrivateInfos" : [ "getLocation" ]
},
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值