uniapp获取权限弹窗时同时告知获取权限目的

实现如图效果

// 先创建提示的内容
this.popview = new plus.nativeObj.View('pop',{
    top:'0px',
	left:'0px',
	width:'100%',
	backgroundColor:'#444'
})
this.popview.drawText('相机权限',{
	top:'0px',
	height:'80%',
	// height:'wrap_content'
},{
	color:'#ffffff',
	size:'25px'
})
			
this.popview.drawText('为了给您提供扫描二维码、图片,用于扫码排队,核销,修改头像功能',{
	top:'35px',
	width:'100%',
	height:'80%',
	// height:'wrap_content'
},{
	color:'#ffffff',
	whiteSpace:'normal',
})

 判断是否有某个权限的函数

// 判断是否有权限
requestPermissions(scope){
		let that = this
		return new Promise((resolve,reject)=>{
			plus.android.requestPermissions(
			['android.permission.'+scope], 
			function(resultObj) {
		        console.log(resultObj,'resultObj')
			    resolve(resultObj)
			    if(resultObj.deniedPresent.length>0 || resultObj.deniedAlways.length>0){
				    uni.showToast({
					    icon:'none',
					    title:'有必要权限未获得,请在设置中打开'
				    })
			    }
			},
			function(error) {
				uni.showToast({
					icon:'none',
					title:'获取权限失败,请在设置中打开'
				})
			}
		})
},

在调用入扫码前调用改函数

async scanCode(){
	let scopeFlag = this.$store.getters.permissions[scope] || false
	if(!scopeFlag){
		this.popview.show()
	}
	let scope = 'CAMERA'
	let res =  await this.requestPermissions(scope).then()
	let data = {}
	if(res.granted[0]){
		data[scope] = true
	}else{
		data[scope] = false
	}
	this.globalData.popview.hide()
	console.log(res,'权限')
	this.$store.commit('setpermissions',data)
	uni.scanCode()
}

 在store记录相册权限是否被允许

state:{
    permissions:{}
},
getters: {
    permissions:state=>{
		return state.permissions
	}
},
mutations: {
	setpermissions(state,scope){
	  	state.permissions={...state.permissions,...scope}
	}
}

猜你喜欢

转载自blog.csdn.net/xiyan_yu/article/details/128970460