中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
检查某个权限是否已经授权。
微信扫码分享
/** * 检查是否授权 * @param permission 待检查权限 * @returns 授权状态 */ async checkAccessToken(permission: Permissions): Promise<abilityAccessCtrl.GrantStatus> { let atManager: abilityAccessCtrl.AtManager = abilityAccessCtrl.createAtManager(); let grantStatus: abilityAccessCtrl.GrantStatus = abilityAccessCtrl.GrantStatus.PERMISSION_DENIED; let tokenId: number = 0; //获取应用程序的accessTokenID try { let bundleInfo: bundleManager.BundleInfo = await bundleManager.getBundleInfoForSelf(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION); let appInfo: bundleManager.ApplicationInfo = bundleInfo.appInfo; tokenId = appInfo.accessTokenId; } catch (error) { const err: BusinessError = error as BusinessError; // ToastUtil.showToast('获取应用信息失败:' + err.message); } //校验应用是否被授予权限 try { grantStatus = await atManager.checkAccessToken(tokenId, permission); } catch (error) { const err: BusinessError = error as BusinessError; // ToastUtil.showToast('校验授权信息失败:' + err.message); } return grantStatus; } async checkPermissions(permissions: Permissions): Promise<boolean> { let grantStatus: abilityAccessCtrl.GrantStatus = await this.checkAccessToken(permissions); if (grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) { //判断是否授权 return true; //已经授权 } else { return false; //未授权 } }