欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 创投人物 > 微信小程序权限授权工具类

微信小程序权限授权工具类

2025/6/1 2:44:36 来源:https://blog.csdn.net/shgg2917/article/details/144334849  浏览:    关键词:微信小程序权限授权工具类

最近写微信小程序的时候需要在页面获取设备权限,又不想每个页面都写,就写了一个工具类方便管理

/*** 权限工具类用于获取授权* 权限工具类使用方法,注意调用时函数需要定义为异步函数 async* import PermissionUtils from "./permissionUtils"* * try {*     const hasPermission = await PermissionUtils.requestPermission('CAMERA', '自定义提示信息,可以不传入,默认值已经定义');*     console.log(hasPermission)*   } catch (error) {*     console.error('权限请求失败:', error);*   }* */
// permissionUtils.js
class PermissionUtils {// 权限枚举,包含默认提示信息static Permissions = {USER_LOCATION: {scope: 'scope.userLocation',defaultMessage: '您未开启精确地理位置权限,将影响相关功能使用,请在设置中手动开启。'},USER_FUZZY_LOCATION: {scope: 'scope.userFuzzyLocation',defaultMessage: '您未开启模糊地理位置权限,将影响相关功能使用,请在设置中手动开启。'},USER_LOCATION_BACKGROUND: {scope: 'scope.userLocationBackground',defaultMessage: '您未开启后台定位权限,将影响相关功能使用,请在设置中手动开启。'},RECORD: {scope: 'scope.record',defaultMessage: '您未开启麦克风权限,将影响相关功能使用,请在设置中手动开启。'},CAMERA: {scope: 'scope.camera',defaultMessage: '您未开启摄像头权限,将影响相关功能使用,请在设置中手动开启。'},BLUETOOTH: {scope: 'scope.bluetooth',defaultMessage: '您未开启蓝牙权限,将影响相关功能使用,请在设置中手动开启。'},WRITE_PHOTOS_ALBUM: {scope: 'scope.writePhotosAlbum',defaultMessage: '您未开启添加到相册权限,将影响相关功能使用,请在设置中手动开启。'},ADD_PHONE_CONTACT: {scope: 'scope.addPhoneContact',defaultMessage: '您未开启添加到联系人权限,将影响相关功能使用,请在设置中手动开启。'},ADD_PHONE_CALENDAR: {scope: 'scope.addPhoneCalendar',defaultMessage: '您未开启添加到日历权限,将影响相关功能使用,请在设置中手动开启。'},WERUN: {scope: 'scope.werun',defaultMessage: '您未开启微信运动步数权限,将影响相关功能使用,请在设置中手动开启。'},ADDRESS: {scope: 'scope.address',defaultMessage: '您未开启通讯地址权限,将影响相关功能使用,请在设置中手动开启。'},INVOICE_TITLE: {scope: 'scope.invoiceTitle',defaultMessage: '您未开启发票抬头权限,将影响相关功能使用,请在设置中手动开启。'},INVOICE: {scope: 'scope.invoice',defaultMessage: '您未开启获取发票权限,将影响相关功能使用,请在设置中手动开启。'},USER_INFO: {scope: 'scope.userInfo',defaultMessage: '您未开启用户信息权限,将影响相关功能使用,请在设置中手动开启。'}};static async requestPermission(permissionKey, customMessage = null) {const permission = PermissionUtils.Permissions[permissionKey];if (!permission) {throw new Error(`未知权限类型: ${permissionKey}`);}// 获取当前权限状态const setting = await new Promise((resolve, reject) => {wx.getSetting({success: (res) => resolve(res),fail: (err) => reject(err)});});const authSetting = setting.authSetting || {};if (!authSetting[permission.scope]) {// 用户尚未授权const authorizeResult = await new Promise((resolve, reject) => {wx.authorize({scope: permission.scope,success: (res) => resolve(res),fail: (err) => resolve({ errMsg: 'authorize:fail auth deny' }) // 处理授权失败的情况});});if (authorizeResult.errMsg === 'authorize:ok') {// 用户同意授权return true;} else {// 用户拒绝授权const message = customMessage || permission.defaultMessage;const modalResult = await new Promise((resolve) => {wx.showModal({title: '提示',content: message,showCancel: true,cancelText: '残忍拒绝',confirmText: '立即前往',success: (res) => {if (res.confirm) {// 用户点击了确定wx.openSetting({success: (res) => {if (res.authSetting[permission.scope]) {// 用户在设置中开启了权限resolve(true);} else {// 用户在设置中仍然拒绝权限resolve(false);}},fail: (err) => {console.error('打开设置页面失败:', err);resolve(false);}});} else {// 用户点击了取消resolve(false);}}});});return modalResult;}} else {// 用户已经授权return true;}}
}module.exports = PermissionUtils;

示例代码

import PermissionUtils from "../../../../utils/permissionUtils"
//其它结构...async test() {try {const hasPermission = await PermissionUtils.requestPermission('CAMERA');//....你的代码} catch (error) {// console.error('权限请求失败:', error);}}//其它结构...

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词