解决uni-app授权弹框华为审核拒绝

news/2025/3/19 10:25:04/

背景:

在使用定位、相机、文件、电话,需要用户同意授权时,华为和vivo需要告知用户使用权限目的。

方案:

在uni授权时,弹框告诉授权目的,效果如下:

代码:

const perListener = {// 初始化状态state: {// 处理应用程序权限请求WRITE_EXTERNAL_STORAGE: false,ACCESS_FINE_LOCATION: false,CALL_PHONE: false,/* #ifdef APP-PLUS */isIos: plus.os.name == "iOS",/* #endif */mapping: {'WRITE_EXTERNAL_STORAGE': {title: "拍摄/相册权限说明",content: "用于司机在车辆发生事故时,可以快速上传事故图片等材料,用于协助司机处理出险问题。",methods: 'SET_WRITE_EXTERNAL_STORAGE'},'ACCESS_FINE_LOCATION': {title: "定位权限说明",content: "用于获取用户位置,以便为用户提供有地域特点的服务。",methods: 'SET_ACCESS_FINE_LOCATION'},'CALL_PHONE': {title: "拨打电话权限说明",content: "允许应用获取本机号码,以便用户得到更便捷的服务。",methods: 'SET_CALL_PHONE'}}},mutations: {// 管理权限告知目的SET_WRITE_EXTERNAL_STORAGE(state, val) {state.WRITE_EXTERNAL_STORAGE = val},SET_CALL_PHONE(state, val) {state.CALL_PHONE = val},SET_ACCESS_FINE_LOCATION(state, val) {state.ACCESS_FINE_LOCATION = val}},actions: {//权限获取async requestPermissions({state,dispatch,commit}, permissionID) {try {if (!state[permissionID] && !state.isIos) {var viewObj = await dispatch('nativeObjView', permissionID);viewObj.show();}console.log('android.permission.' + permissionID, '当前手机权限');let model = uni.getSystemInfoSync().brand.toLowerCase();return new Promise(async (resolve, reject) => {//苹果不需要这个if (state.isIos) {resolve(1);return}if (model != 'vivo' && model != 'huawei') {resolve(1);return;}// Android权限查询function requestAndroidPermission(permissionID_) {return new Promise((resolve, reject) => {plus.android.requestPermissions([permissionID_], // 理论上支持多个权限同时查询,但实际上本函数封装只处理了一个权限的情况。有需要的可自行扩展封装function(resultObj) {console.log('resultObj', resultObj)var result = 0;for (var i = 0; i < resultObj.granted.length; i++) {// var grantedPermission = resultObj.granted[i];console.log('已获取的权限:');result = 1}for (var i = 0; i < resultObj.deniedPresent.length; i++) {// var deniedPresentPermission = resultObj.deniedPresent[i];console.log('拒绝本次申请的权限:');result = 0}for (var i = 0; i < resultObj.deniedAlways.length; i++) {// var deniedAlwaysPermission = resultObj.deniedAlways[i];// console.log('永久拒绝申请的权限:' + deniedAlwaysPermission);result = -1}resolve(result);},function(error) {console.log('申请权限错误:' + error.code + " = " + error.message);resolve({code: error.code,message: error.message});});});}const result = await requestAndroidPermission('android.permission.' + permissionID);console.log('result', result)if (result === 1) {//'已获得授权'commit(state.mapping[permissionID].methods, true)} else if (result === 0) {//'未获得授权'commit(state.mapping[permissionID].methods, false)} else {commit(state.mapping[permissionID].methods, true)uni.showModal({title: '提示',content: '操作权限已被拒绝,请手动前往设置',confirmText: "立即设置",success: (res) => {if (res.confirm) {dispatch('gotoAppPermissionSetting')}}})}if (viewObj) viewObj.close()resolve(result);});} catch (error) {console.log(error);reject(error);}},//提示框nativeObjView({state}, permissionID) {const systemInfo = uni.getSystemInfoSync();const statusBarHeight = systemInfo.statusBarHeight;const navigationBarHeight = systemInfo.platform === 'android' ? 48 :44; // Set the navigation bar height based on the platformconst totalHeight = statusBarHeight + navigationBarHeight;let view = new plus.nativeObj.View('per-modal', {top: '0px',left: '0px',width: '100%',backgroundColor: '#444',//opacity: .5;})view.drawRect({color: '#fff',radius: '5px'}, {top: totalHeight + 'px',left: '5%',width: '90%',height: "100px",})view.drawText(state.mapping[permissionID].title, {top: totalHeight + 5 + 'px',left: "8%",height: "30px"}, {align: "left",color: "#000",}, {onClick: function(e) {console.log(e);}})view.drawText(state.mapping[permissionID].content, {top: totalHeight + 35 + 'px',height: "60px",left: "8%",width: "84%"}, {whiteSpace: 'normal',size: "14px",align: "left",color: "#656563"})function show() {view = plus.nativeObj.View.getViewById('per-modal');view.show()view = null //展示的时候也得清空,不然影响下次的关闭,不知道为啥}function close() {view = plus.nativeObj.View.getViewById('per-modal');view.close();view = null}return {show,close}},// 跳转到**应用**的权限页面gotoAppPermissionSetting({state}) {if (state.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);}}}}
export default perListener

使用:

async handleCall(phone) {// #ifdef APP-PLUSlet result = await this.$store.dispatch("requestPermissions",'CALL_PHONE')if (result !== 1) return// #endifuni.makePhoneCall({phoneNumber: phone})
},


http://www.ppmy.cn/news/1580292.html

相关文章

AI爬虫 :Crawl4AI的安装和详细使用案例(开源 LLM 友好型网络爬虫)

更多内容请见: 爬虫和逆向教程-专栏介绍和目录 文章目录 1. Crawl4AI概述1.1 Crawl4AI 介绍1.2 Crawl4AI 做什么?1.3 Crawl4AI 的核心理念1.4 Crawl4AI v0.5.0 新功能2. Crawl4AI的安装和第一个案例2.1 Crawl4AI 的安装2.2 初始设置2.3 诊断2.4 第一个案例2.5 高级安装(可选…

Linux top 命令详解:从入门到高级用法

Linux top 命令详解&#xff1a;从入门到高级用法 在 Linux 系统中&#xff0c;top 是一个强大的实时监控工具&#xff0c;用于查看系统资源使用情况和进程状态。它可以帮助你快速了解 CPU、内存、负载等信息&#xff0c;是系统管理员和开发者的日常利器。本文将从基本用法开始…

Netty基础—8.Netty实现私有协议栈一

大纲 1.私有协议介绍 2.私有协议的通信模型 3.私有协议栈的消息定义 4.私有协议栈链路的建立 5.私有协议栈链路的关闭 6.私有协议栈的心跳机制 7.私有协议栈的重连机制 8.私有协议栈的重复登录保护 9.私有协议栈核心的ChannelHandler 10.私有协议栈的客户端和服务端 …

The Rust Programming Language 学习 (六)

包和crate和模块 包和crate crate 是一个二进制项或者库。crate root 是一个源文件&#xff0c;Rust 编译器以它为起始点&#xff0c;并构成你的 crate 的根模块,包&#xff08;package&#xff09;是提供一系列功能的一个或者多个 crate。一个包会包含有一个 Cargo.toml 文件…

LeetCode[142] 环形链表 II

哈希表匹配法 set存储遍历过的节点每次遍历查询set中是否有该节点 有&#xff0c;则代表该节点为环的起点无&#xff0c;则插入set中&#xff0c;继续遍历链表 /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNo…

【软件工程】01_软件工程的概述

1. 定义 软件是计算机系统中与硬件相互依存的另一部分&#xff0c;它是包括程序&#xff0c;数据及其相关文档的完整集合。 2. 软硬件失效 3. 软件危机 软件危机&#xff08;Software Crisis&#xff09;&#xff1a;指由于落后的软件生产方式无法满足迅速增长的计算机软件需求…

函数模板

一.什么是函数模板呢&#xff1f; 我们拿交换函数来举个例子。 我们要实现这个交换功能&#xff0c;我们可能得写两个函数。 使用函数重载虽然可以实现&#xff0c;但是有一下几个不好的地方&#xff1a; 1. 重载的函数仅仅是类型不同&#xff0c;代码复用率比较低&#xff0…

【Git学习笔记】Git分支管理策略及其结构原理分析

【Git学习笔记】Git分支管理策略及其结构原理分析 &#x1f525;个人主页&#xff1a;大白的编程日记 &#x1f525;专栏&#xff1a;Git学习笔记 文章目录 【Git学习笔记】Git分支管理策略及其结构原理分析前言一.合并冲突二. 分支管理策略2.1 分支策略2.2 bug分支2.3 删除临…