赞
踩
因为华为上架需要添加申请权限时的使用说明,否则就无法通过审核,就封装了申请权限时弹出权限说明窗口,利用了本地存储如果权限已经通过就不弹权限说明窗口直接调用回调,如果没有通过或没有就弹出申请权限弹窗。
1.页面组件
- <template>
- <view>
- <view v-if="showTips" class="fixed bgFF radio30 absolute tips fs28 color02">
- <view class="lh50">
- {{actionPermission.title}}使用说明:
- </view>
- <view class="lh50">
- {{actionPermission.tips}}
- </view>
- </view>
- <u-popup :show="show" @close="close" mode="center" round="30rpx">
- <view class="padding50 bgFF content text-center radio20 fs32 color">
- <view class="wh100">
- <view class="lh50 marginB40">
- 您已禁止{{actionPermission.title}},如需要使用请设置!
- </view>
- <view @click="handelAllow" class="fw600 paddingT40 borderT1">
- 去设置
- </view>
- </view>
- </view>
- </u-popup>
- </view>
- </template>
-
- <script>
- import {
- tipsConfig
- } from './index.js'
- import permision from './permission.js'
- export default {
- data() {
- return {
- show: false,
- key: 'CAMERA',
- // isSetting: false,
- showTips: false
- }
- },
- computed: {
- actionPermission() {
- return tipsConfig[this.key]
- }
- },
- methods: {
- async showView(key = 'CAMERA', callback) {
- // #ifdef APP-PLUS
- // 获取系统信息
- const systemInfo = uni.getSystemInfoSync();
- // 判断是否为ios
- const isIos = systemInfo?.platform === 'ios';
- if (isIos) {
- return callback && callback()
- }
- this.key = key
- // 如果返回1 则是允许权限 直接调用回调就行 如果是0则询问 -1则永久拒绝
- const permission = uni.getStorageSync('permission') || {}
- const keyStatus = permission[this.actionPermission?.key] || null
- if (keyStatus == 1) {
- return callback && callback()
- }
- // else if (keyStatus == -1) {
- // this.show = true
- // return
- // }
- const result = await this.requestAndroidPermission()
- permission[this.actionPermission?.key] = result
- uni.setStorageSync('permission', permission)
- if (result == 1) {
- callback && callback()
- } else {
- this.show = true
- }
- //#endif
- // #ifndef APP-PLUS
- callback && callback()
- //#endif
- },
- close() {
- this.show = false
- this.showTips = false
- },
- /**
- * 禁止
- */
- handelProhibited(status) {
- this.close()
- },
- /**
- * 允许
- */
- handelAllow() {
- this.show = false
- permision.gotoAppPermissionSetting()
- },
- async requestAndroidPermission() {
- this.showTips = true
- return await permision.requestAndroidPermission(this.actionPermission.permission, () => {
- this.showTips = false
- })
- },
- }
- }
- </script>
-
- <style lang="scss" scoped>
- .tips-box {
- left: 0;
- top: 0;
- width: 100vw;
- height: 100vh;
- }
-
- .tips {
- width: 80vw;
- padding: 30rpx 70rpx;
- left: 50%;
- top: 15vh;
- transform: translateX(-50%);
- z-index: 10076;
- }
-
- /deep/ .u-popup__content {
- background-color: transparent;
- }
-
- .content {
- width: 80vw;
- }
-
- .color {
- color: #4994F6;
- }
-
- .action::before {
- content: '';
- width: 4rpx;
- height: 100%;
- position: absolute;
- left: 50%;
- top: 0;
- transform: translateX(-50%);
- background-color: #D9D9D9;
- }
- </style>
2.权限相关配置说明
- export const tipsConfig = {
- LOCATION: {
- title: '位置权限',
- tips: '用于获取当前位置信息、考勤打卡等场景',
- describe: '获取位置信息',
- key: 'LOCATION',
- permission:'android.permission.ACCESS_FINE_LOCATION'
- },
- CAMERA: {
- title: '相机/拍摄权限',
- tips: '用于拍照、录制视频等场景',
- describe: '拍摄照片和录制视频',
- key: 'CAMERA',
- permission:'android.permission.CAMERA'
- },
- CAMERA_STORAGE: {
- title: '相机/拍摄/存储空间权限',
- tips: '用于拍照、上传照片等场景',
- describe: '拍摄照片、上传照片',
- key: 'STORAGE',
- permission:'android.permission.CAMERA'
- },
- CAMERA_VIDEO_STORAGE: {
- title: '相机/拍摄/存储空间权限',
- tips: '用于拍摄视频、上传视频等场景',
- describe: '拍摄视频、上传视频',
- key: 'STORAGE',
- permission:'android.permission.CAMERA'
- },
- STORAGE: {
- title: '存储空间/照片权限',
- tips: '用于上传文件、上传照片和视频等场景',
- describe: '上传文件和照片、视频',
- key: 'STORAGE',
- permission:'android.permission.READ_EXTERNAL_STORAGE'
- },
- RECORD_AUDIO: {
- title: '麦克风权限',
- tips: '用于发送语音等场景',
- describe: '录制音频',
- key: 'RECORD_AUDIO',
- permission:'android.permission.RECORD_AUDIO'
- },
- CALL_PHONE: {
- title: '拨打电话权限',
- tips: '用于拨打电话等场景',
- describe: '拨打电话',
- key: 'CALL_PHONE',
- permission:'android.permission.CALL_PHONE'
- }
- }
- export const clearPermissionStorageKey = (key,err,callback) => {
- if(err?.errMsg?.includes('No Permission')){
- const permission = uni.getStorageSync('permission') || {}
- if(permission[key] == 1){
- delete permission[key]
- uni.setStorageSync('permission',permission)
- callback && callback()
- }
- }
- }
3.查询安卓权限是否授权代码(网上抄的)
- /**
- * 本模块封装了Android、iOS的应用权限判断、打开应用权限设置界面、以及位置系统服务是否开启
- */
-
- var isIos
- // #ifdef APP-PLUS
- isIos = (plus.os.name == "iOS")
- // #endif
-
- // 判断推送权限是否开启
- function judgeIosPermissionPush() {
- var result = false;
- var UIApplication = plus.ios.import("UIApplication");
- var app = UIApplication.sharedApplication();
- var enabledTypes = 0;
- if (app.currentUserNotificationSettings) {
- var settings = app.currentUserNotificationSettings();
- enabledTypes = settings.plusGetAttribute("types");
- console.log("enabledTypes1:" + enabledTypes);
- if (enabledTypes == 0) {
- console.log("推送权限没有开启");
- } else {
- result = true;
- console.log("已经开启推送功能!")
- }
- plus.ios.deleteObject(settings);
- } else {
- enabledTypes = app.enabledRemoteNotificationTypes();
- if (enabledTypes == 0) {
- console.log("推送权限没有开启!");
- } else {
- result = true;
- console.log("已经开启推送功能!")
- }
- console.log("enabledTypes2:" + enabledTypes);
- }
- plus.ios.deleteObject(app);
- plus.ios.deleteObject(UIApplication);
- return result;
- }
-
- // 判断定位权限是否开启
- function judgeIosPermissionLocation() {
- var result = false;
- var cllocationManger = plus.ios.import("CLLocationManager");
- var status = cllocationManger.authorizationStatus();
- result = (status != 2)
- console.log("定位权限开启:" + result);
- // 以下代码判断了手机设备的定位是否关闭,推荐另行使用方法 checkSystemEnableLocation
- /* var enable = cllocationManger.locationServicesEnabled();
- var status = cllocationManger.authorizationStatus();
- console.log("enable:" + enable);
- console.log("status:" + status);
- if (enable && status != 2) {
- result = true;
- console.log("手机定位服务已开启且已授予定位权限");
- } else {
- console.log("手机系统的定位没有打开或未给予定位权限");
- } */
- plus.ios.deleteObject(cllocationManger);
- return result;
- }
-
- // 判断麦克风权限是否开启
- function judgeIosPermissionRecord() {
- var result = false;
- var avaudiosession = plus.ios.import("AVAudioSession");
- var avaudio = avaudiosession.sharedInstance();
- var permissionStatus = avaudio.recordPermission();
- console.log("permissionStatus:" + permissionStatus);
- if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
- console.log("麦克风权限没有开启");
- } else {
- result = true;
- console.log("麦克风权限已经开启");
- }
- plus.ios.deleteObject(avaudiosession);
- return result;
- }
-
- // 判断相机权限是否开启
- function judgeIosPermissionCamera() {
- var result = false;
- var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
- var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
- console.log("authStatus:" + authStatus);
- if (authStatus == 3) {
- result = true;
- console.log("相机权限已经开启");
- } else {
- console.log("相机权限没有开启");
- }
- plus.ios.deleteObject(AVCaptureDevice);
- return result;
- }
-
- // 判断相册权限是否开启
- function judgeIosPermissionPhotoLibrary() {
- var result = false;
- var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
- var authStatus = PHPhotoLibrary.authorizationStatus();
- console.log("authStatus:" + authStatus);
- if (authStatus == 3) {
- result = true;
- console.log("相册权限已经开启");
- } else {
- console.log("相册权限没有开启");
- }
- plus.ios.deleteObject(PHPhotoLibrary);
- return result;
- }
-
- // 判断通讯录权限是否开启
- function judgeIosPermissionContact() {
- var result = false;
- var CNContactStore = plus.ios.import("CNContactStore");
- var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
- if (cnAuthStatus == 3) {
- result = true;
- console.log("通讯录权限已经开启");
- } else {
- console.log("通讯录权限没有开启");
- }
- plus.ios.deleteObject(CNContactStore);
- return result;
- }
-
- // 判断日历权限是否开启
- function judgeIosPermissionCalendar() {
- var result = false;
- var EKEventStore = plus.ios.import("EKEventStore");
- var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
- if (ekAuthStatus == 3) {
- result = true;
- console.log("日历权限已经开启");
- } else {
- console.log("日历权限没有开启");
- }
- plus.ios.deleteObject(EKEventStore);
- return result;
- }
-
- // 判断备忘录权限是否开启
- function judgeIosPermissionMemo() {
- var result = false;
- var EKEventStore = plus.ios.import("EKEventStore");
- var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
- if (ekAuthStatus == 3) {
- result = true;
- console.log("备忘录权限已经开启");
- } else {
- console.log("备忘录权限没有开启");
- }
- plus.ios.deleteObject(EKEventStore);
- return result;
- }
-
- // Android权限查询
- function requestAndroidPermission(permissionID,callback) {
- return new Promise((resolve, reject) => {
- plus.android.requestPermissions(
- [permissionID], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装
- function(resultObj) {
- var result = 0;
- for (var i = 0; i < resultObj.granted.length; i++) {
- var grantedPermission = resultObj.granted[i];
- console.log('已获取的权限:' + grantedPermission);
- result = 1
- }
- for (var i = 0; i < resultObj.deniedPresent.length; i++) {
- var deniedPresentPermission = resultObj.deniedPresent[i];
- console.log('拒绝本次申请的权限:' + deniedPresentPermission);
- result = 0
- }
- for (var i = 0; i < resultObj.deniedAlways.length; i++) {
- var deniedAlwaysPermission = resultObj.deniedAlways[i];
- console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);
- result = -1
- }
- callback && callback()
- resolve(result);
- // 若所需权限被拒绝,则打开APP设置界面,可以在APP设置界面打开相应权限
- // if (result != 1) {
- // gotoAppPermissionSetting()
- // }
- },
- function(error) {
- console.log('申请权限错误:' + error.code + " = " + error.message);
- callback && callback()
- resolve({
- code: error.code,
- message: error.message
- });
- }
- );
- });
- }
-
- // 使用一个方法,根据参数判断权限
- function judgeIosPermission(permissionID) {
- if (permissionID == "location") {
- return judgeIosPermissionLocation()
- } else if (permissionID == "camera") {
- return judgeIosPermissionCamera()
- } else if (permissionID == "photoLibrary") {
- return judgeIosPermissionPhotoLibrary()
- } else if (permissionID == "record") {
- return judgeIosPermissionRecord()
- } else if (permissionID == "push") {
- return judgeIosPermissionPush()
- } else if (permissionID == "contact") {
- return judgeIosPermissionContact()
- } else if (permissionID == "calendar") {
- return judgeIosPermissionCalendar()
- } else if (permissionID == "memo") {
- return judgeIosPermissionMemo()
- }
- return false;
- }
-
- // 跳转到**应用**的权限页面
- function gotoAppPermissionSetting() {
- if (isIos) {
- var UIApplication = plus.ios.import("UIApplication");
- var application2 = UIApplication.sharedApplication();
- var NSURL2 = plus.ios.import("NSURL");
- // var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
- var setting2 = NSURL2.URLWithString("app-settings:");
- application2.openURL(setting2);
-
- plus.ios.deleteObject(setting2);
- plus.ios.deleteObject(NSURL2);
- plus.ios.deleteObject(application2);
- } else {
- // console.log(plus.device.vendor);
- var Intent = plus.android.importClass("android.content.Intent");
- var Settings = plus.android.importClass("android.provider.Settings");
- var Uri = plus.android.importClass("android.net.Uri");
- var mainActivity = plus.android.runtimeMainActivity();
- var intent = new Intent();
- intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
- var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
- intent.setData(uri);
- mainActivity.startActivity(intent);
- }
- }
-
- // 检查系统的设备服务是否开启
- // var checkSystemEnableLocation = async function () {
- function checkSystemEnableLocation() {
- if (isIos) {
- var result = false;
- var cllocationManger = plus.ios.import("CLLocationManager");
- var result = cllocationManger.locationServicesEnabled();
- console.log("系统定位开启:" + result);
- plus.ios.deleteObject(cllocationManger);
- return result;
- } else {
- var context = plus.android.importClass("android.content.Context");
- var locationManager = plus.android.importClass("android.location.LocationManager");
- var main = plus.android.runtimeMainActivity();
- var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
- var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
- console.log("系统定位开启:" + result);
- return result
- }
- }
-
-
- module.exports = {
- judgeIosPermission: judgeIosPermission,
- requestAndroidPermission: requestAndroidPermission,
- checkSystemEnableLocation: checkSystemEnableLocation,
- gotoAppPermissionSetting: gotoAppPermissionSetting
- }
4.使用方法:
1.页面引入组件,申请权限前调用组件showView方法 传入对应的权限值和回调函数
2.如果用户拒绝或取消就调用 clearPermissionStorageKey 方法删除本地是否已经授权的权限值
- 1.引入组件、注册组件
- import Authorize from '@/components/authorize/index.vue'
- <Authorize ref="authorize" />
- import {
- clearPermissionStorageKey
- } from '@/components/authorize/index.js'
- 2.方法调用
- handelAuthorize() {
- this.$refs?.authorize?.showView('CAMERA_VIDEO_STORAGE', this.handelVideo)
- },
- 3.回调函数
- // 视频
- handelVideo() {
- uni.chooseVideo({
- sourceType: ['camera', 'album'],
- success: async (res) => {
- },
- fail: (err) => {
- clearPermissionStorageKey('CAMERA_VIDEO_STORAGE', err, () => {
- this.handelVideo()
- })
- }
- });
- },
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。