当前位置: 首页 > news >正文

uniapp小程序授权统一处理

1.使用

1.将工具代码引入到utils中

const authorize = (scope, isOne = false, isMust = false) => {if (!scope || !authorizeObj[scope]) {return console.error('请传输需要获取权限的 scope,详见','https://uniapp.dcloud.net.cn/api/other/authorize.html#scope-%E5%88%97%E8%A1%A8')}return new Promise(async (resolve, reject) => {let hasAuthorize = await getAuthorize(scope)if (hasAuthorize) {resolve()} else {uni.authorize({scope: `scope.${scope}`,success: (authorizeRes) => {resolve()},fail: (authorizeErr) => {if (!isOne) {showModal(resolve, reject, scope, isMust)} else {reject()}}})}})
}
const showModal = (resolve, reject, scope, isMust) => {uni.showModal({title: '温馨提示',content: `获取${authorizeObj[scope]}失败,是否进入设置页开启您的权限?`,success: (showModalRes) => {if (showModalRes.confirm) {uni.openSetting({success: (openSettingRes) => {if (openSettingRes.authSetting[`scope.${scope}`]) {resolve()} else {if (isMust) {showModal(resolve, reject, scope, isMust)} else {reject()}}}});} else {reject()}}})
}//查看是否授权
const getAuthorize = (scope) => {return new Promise((resolve, reject) => {uni.getSetting({success(res) {resolve(res.authSetting[`scope.${scope}`])}})})
}
let authorizeObj = {'userLocation': '精确地理位置','userFuzzyLocation': '模糊地理位置','userLocationBackground': '后台定位','record': '麦克风权限','camera': '摄像头权限','bluetooth': '蓝牙权限','writePhotosAlbum': '添加到相册权限','addPhoneContact': '添加到联系人权限','addPhoneCalendar': '添加到日历权限','werun': '微信运动步数权限','address': '通讯地址权限','invoiceTitle': '发票抬头权限','invoice': '获取发票权限','userInfo': '用户信息权限'
}export default authorize

2.打开main.js 将方法挂载到全局

//授权方法全局挂载
import authorize from '@/utils/authorize.js'
Vue.prototype.$authorize = authorize

3.页面中使用

this.$authorize('writePhotosAlbum',true).then(()=>{//成功授权后相应逻辑
}).catch(()=>{//拒绝授权后相应逻辑
}).finally(() => {//无论授权成功或者失败都执行的逻辑
});

2.参数详解

const authorize = (scope, isOne = false, isMust = false) => {}

参数名默认值参考值备注
scopeuserLocation、userFuzzyLocation…权限标识目前支持参数如下
isOnefalsetrue、false授权是否只能一次(是否支持弹窗跳转设置页)
isMustfalsetrue、false是否必须授权(如果必须授权,在设置页如果没有开启权限返回页面还会弹提示)

scope :权限标识 参考 文档

目前支持参数

'userLocation': '精确地理位置',
'userFuzzyLocation': '模糊地理位置',
'userLocationBackground': '后台定位',
'record': '麦克风权限',
'camera': '摄像头权限',
'bluetooth': '蓝牙权限',
'writePhotosAlbum': '添加到相册权限',
'addPhoneContact': '添加到联系人权限',
'addPhoneCalendar': '添加到日历权限',
'werun': '微信运动步数权限',
'address': '通讯地址权限',
'invoiceTitle': '发票抬头权限',
'invoice': '获取发票权限',
'userInfo': '用户信息权限'

isOne :是否只弹一次授权 默认不是只弹一次

isMust :是否强制授权 true强制授权 false不强制授权

亲测好用

http://www.lryc.cn/news/215158.html

相关文章:

  • 光学仿真|优化汽车内部照明体验
  • Spring XML使用CASE WHEN处理SELECT字段
  • 关于C#中使用多线程的讨论
  • 工程机械数字孪生可视化平台建设,推动大型装备智能化数字化转型升级
  • Linux 网络流量监控利器 iftop命令详解及实战
  • protected by SourceGuardian and requires a SourceGuardian loader ‘ixed.8解决方案
  • KWin、libdrm、DRM从上到下全过程 —— drmModeAddFBxxx(14)
  • 2023-macOS下安装anaconda,终端自动会出现(base)字样,如何取消
  • Nginx搭载负载均衡及前端项目部署
  • 深度学习——炼丹
  • Matlab中的app设计
  • 曾经遇到过的无法解释的问题
  • 基于uniapp与uview做一个按拼音首字母排序的通讯录页面
  • 网络工程师-入门基础课:华为HCIA认证课程介绍
  • 玻色量子成功研制光量子计算专用光纤恒温控制设备——“量晷”
  • 力扣:147. 对链表进行插入排序(Python3)
  • OpenCV4(C++)——形态学(腐蚀、膨胀)
  • C++设计模式_24_Visitor 访问器
  • el-tabel表格加个多选框
  • Go语言集成开发环境(IDE):GoLand 2023中文
  • opencv c++ canny 实现 以及与halcon canny的对比
  • 阿里云无影升级2.0 云电脑解决方案时代到来
  • 【案例展示】多物理场仿真软件介绍
  • k8s的RBAC中,clusterrole, rolebinding 是什么关系谁先谁后
  • myabtis流式查询
  • K8S的pod创建过程
  • java修仙传之海岛奇遇
  • 电子商务平台对接电商供应链,不得不说的开放平台电商API接口
  • 【JAVA学习笔记】 57 - 本章作业
  • 【题解】[GenshinOI Round 3] P9816 少项式复合幂