uniapp about WeChat applet and Android APP authorization verification package

Only the authorization of the applet and Android is considered here. After calling, the authorization pop-up window will pop up automatically. If the authorization has been rejected, the pop-up window will jump to the permission setting pop-up window.

Android needs to introduce permission.js

// #ifdef APP-PLUS
import permision from "@/utils/permission.js";
// #endif

const isAuthorize = function(scopeStr, permissionToast) {
    
    
	return new Promise((resolve, reject) => {
    
    
		// #ifdef MP-WEIXIN
		// 微信小程序验证是否开启授权
		uni.getSetting({
    
    
			success(res) {
    
    
				// 如果权限未开启
				if (!res.authSetting[scopeStr]) {
    
    
					uni.authorize({
    
    
						scope: scopeStr,
						// 允许授权
						success() {
    
    
							resolve(true)
						},
						// 拒绝授权
						fail() {
    
    
							uni.showModal({
    
    
								title: permissionToast.title,
								content: permissionToast.content,
								confirmText: "开启授权",
								cancelText: '取消',
								success: (res2) => {
    
    
									if (res2.confirm) {
    
    
										// 开启授权设置
										uni.openSetting({
    
    
											success: (res3) => {
    
    
												//设置后返回,不管开没开
												// 有回调触发回调
												if(permissionToast.callback){
    
    
													permissionToast.callback();
												}
											}
										})
										reject(false)
									} else {
    
    
										reject(false)
										return false;
									}
								},
								fail: () => {
    
    
									reject(false)
								}
							})
						}
					})
				} else{
    
    
					// 已开启权限
					resolve(true)
				}
			}
		})
		// #endif
		// #ifdef APP-PLUS
		// APP权限验证
		permision.requestAndroidPermission(scopeStr).then(res=>{
    
    
			// 1已授权 2 未授权 -1 永久拒绝授权
			// 2会自动调取弹窗 -1必须手动调取弹窗
			if(res == -1) {
    
    
				// 永久拒绝授权相册
				uni.showModal({
    
    
					title:  permissionToast.title,
					content: permissionToast.content,
					confirmText: "开启权限",
					cancelText: '取消',
					success: (res) => {
    
    
						if (res.confirm) {
    
    
							permision.gotoAppPermissionSetting()
						} else {
    
    
							return false;
						}
					}
				})
			}
			resolve(res)
		}).catch(err=>{
    
    
			reject(false)
		})
		// #endif
	})
}

export default isAuthorize

use

// #ifdef MP-WEIXIN
const toastData = {
    
    
	title: '请开启定位',
	content: '请允许使用您的定位',
	callback:()=>{
    
    
		//小程序需要设置完成后再次触发的方法如this.getLocation()
	}
}
isAuthorize('scope.userLocation',toastData ).then(() => {
    
    
	// 已开启权限
	
})
// #endif
// #ifdef APP-PLUS
isAuthorize('android.permission.ACCESS_FINE_LOCATION', toastData).then(res => {
    
    
	if (res == 1) {
    
    
		//已开启权限
	}
})
// #endif

Wechat applet only needs to replace uni with wx.
Wechat applet permission code.
Android APP permission code.

2023/5/31

Note that the above method will have problems when it is used as async await . For detailed reasons, refer to another article , so a second modification was made

// #ifdef APP-PLUS
import permision from "@/utils/permission.js";
// #endif

const isAuthorize = function(scopeStr, permissionToast, subscriptionsSetting = false) {
    
    
	return new Promise((resolve, reject) => {
    
    
		// #ifdef MP-WEIXIN
		// 微信小程序验证是否开启授权
		uni.getSetting({
    
    
			withSubscriptions: subscriptionsSetting,
			success(res) {
    
    
				if(subscriptionsSetting){
    
    
					// 订阅消息权限判断
					if(res.subscriptionsSetting&&!res.subscriptionsSetting.mainSwitch){
    
    
						uni.showModal({
    
    
							title: '温馨提示',
							content: '您没有开启订阅消息的权限,是否去设置?',
							confirmText: "去设置",
							cancelText: '取消',
							success: (res2) => {
    
    
								if (res2.confirm) {
    
    
									// 开启授权设置
									uni.openSetting({
    
    
										success: (res3) => {
    
    
											//设置后返回,不管开没开
										}
									})
								} else {
    
    
								}
							},
							fail: () => {
    
    
							}
						})
						resolve(true)
					}else{
    
    
						resolve(true)
					}
				}else if (!res.authSetting[scopeStr]) {
    
    
					//权限判断
					// 如果权限未开启
					uni.authorize({
    
    
						scope: scopeStr,
						// 允许授权
						success() {
    
    
							resolve(true)
						},
						// 拒绝授权
						fail() {
    
    
							uni.showModal({
    
    
								title: permissionToast.title,
								content: permissionToast.content,
								confirmText: "去设置",
								cancelText: '取消',
								success: (res2) => {
    
    
									if (res2.confirm) {
    
    
										// 开启授权设置
										uni.openSetting({
    
    
											success: (res3) => {
    
    
												//设置后返回,不管开没开
												// 有回调触发回调
												if(permissionToast.callback){
    
    
													permissionToast.callback();
												}
											}
										})
										resolve(false)
									} else {
    
    
										resolve(false)
									}
								},
								fail: () => {
    
    
									resolve(false)
								}
							})
							resolve(false)
						}
					})
				} else{
    
    
					// 已开启权限
					resolve(true)
				}
			},
			fail(err){
    
    
				resolve(false)
			}
		})
		// #endif
		// #ifdef APP-PLUS
		// APP权限验证
		permision.requestAndroidPermission(scopeStr).then(res=>{
    
    
			// 1已授权 2 未授权 -1 永久拒绝授权
			if(res == -1) {
    
    
				// 永久拒绝授权相册
				uni.showModal({
    
    
					title:  permissionToast.title,
					content: permissionToast.content,
					confirmText: "去设置",
					cancelText: '取消',
					success: (res) => {
    
    
						if (res.confirm) {
    
    
							permision.gotoAppPermissionSetting()
						} else {
    
    
							resolve(false)
						}
					}
				})
			}
			resolve(res)
		}).catch(err=>{
    
    
			resolve(false)
		})
		// #endif
	})
}

export default isAuthorize
//异步使用
const toastData = {
    
    
	title: '位置权限已关闭',
	content: '开启位置权限后,',
	callback: () => {
    
    
		this.getDefualtLocation()
	}
}
// #ifndef APP-PLUS
isAuthorize('scope.userLocation', toastData).then((res) => {
    
    
	// 已开启权限
	if(res){
    
    
		this.getDefualtLocation()
	}
})
// #endif
// #ifdef APP-PLUS
isAuthorize('android.permission.ACCESS_FINE_LOCATION', toastData, ).then(res => {
    
    
	if (res == 1) {
    
    
		this.getDefualtLocation()
	}
})


// 同步使用
// 鉴位置权限
async checkAuthor() {
    
    
	const toastData = {
    
    
		title: '位置权限已关闭',
		content: '开启位置权限后',
		callback: () => {
    
    }
	}
	// #ifndef APP-PLUS
	const res = await isAuthorize('scope.userLocation', toastData)
	return res
	// #endif
	// #ifdef APP-PLUS
	const res = await isAuthorize('android.permission.ACCESS_FINE_LOCATION', toastData)
	return res
	// #endif
}


async CreatOrder() {
    
    
	const authorRes = await this.checkAuthor();
	// #ifndef APP-PLUS
	if (!authorRes) {
    
    
		return;
	}
	// #endif
	// #ifdef APP-PLUS
	// 1已授权 2 未授权 -1 永久拒绝授权
	if (authorRes != 1) {
    
    
		return;
	};
	// #endif
}
// #endif

Guess you like

Origin blog.csdn.net/weixin_38566069/article/details/130228861
Recommended