鉴权代码如下
// 安卓拨打电话权限
const androidPermissions = {
'android.permission.CAMERA': {
title: '相机权限使用说明',
content: '需要获取相机权限,以便拍照上传图片',
},
'android.permission.READ_EXTERNAL_STORAGE': {
title: '存储空间/照片权限申请说明',
content: '需要获取访问设备上的照片、文件权限, 以便选择图片', // 存储读取权限
},
'android.permission.CALL_PHONE': {
title: '拨打电话权限申请说明',
content: '需要获取电话权限,以便拨打电话',
},
}
export function checkPermission(permissionIds: string[]): Promise<number> {
return new Promise((resolve, reject) => {
if (plus) {
const ids = permissionIds
plus.android.requestPermissions(
ids,
(resultObj) => {
let result = 0
let deniedPermissionInfo = ''
for (let i = 0; i < resultObj.granted.length; i++) {
const grantedPermission = resultObj.granted[i]
console.log('已获取的权限:' + grantedPermission)
result = 1
}
for (let i = 0; i < resultObj.deniedPresent.length; i++) {
const deniedPresentPermission = resultObj.deniedPresent[i]
console.log('拒绝本次申请的权限:' + deniedPresentPermission)
result = 0
}
for (let i = 0; i < resultObj.deniedAlways.length; i++) {
deniedPermissionInfo = resultObj.deniedAlways[i]
console.log('永久拒绝申请的权限:' + deniedPermissionInfo)
result = -1
}
if (result === -1) {
showPermissionDialog(deniedPermissionInfo)
}
resolve(result)
},
(error) => {
console.log('申请权限错误:' + error.code + ' = ' + error.message)
reject(error)
},
)
}
})
}
function showPermissionDialog(deniedPermissionInfo: string) {
const permissionInfo = androidPermissions[deniedPermissionInfo]
if (permissionInfo) {
uni.showModal({
title: permissionInfo.title,
content: permissionInfo.content,
showCancel: true,
confirmText: '打开应用设置',
success: (res) => {
if (res.confirm) {
gotoAppPermissionSetting()
}
},
})
}
}
function gotoAppPermissionSetting() {
const mainActivity = plus.android.runtimeMainActivity()
const Intent = plus.android.importClass('android.content.Intent')
const Settings = plus.android.importClass('android.provider.Settings')
const Uri = plus.android.importClass('android.net.Uri')
const intent = new Intent()
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
const uri = Uri.fromParts('package', mainActivity.getPackageName(), null)
intent.setData(uri)
mainActivity.startActivity(intent)
}