微信小程序获取授权

只关注获取权限成功后的逻辑

使用

import {getSetting} from './permissions'

getSetting("userLocation").then(() => {
      console.log("userLocation: ok")
    
    }).catch(() => wx.showToast({title: '请获取授权后使用', icon: 'none', duration: 1500}) );

引入permissions.js

//
// import {getSetting} from './permissions'
// tap触发判断调用
// getSetting("userLocation").then(() => {
//  console.log("userLocation: ok")
// }).catch(() => wx.showToast({title: '请获取授权后使用', icon: 'none', duration: 1500}) );
//
// 被动弹窗
// wx.showModal({
//       title: '提示:需要获取您的位置信息',
//       // content: '请获取位置授权后使用',
//       success (res) {
//         if (res.confirm) {
//           console.log('用户点击确定')
//           getSetting("userLocation").then(() => {
//             console.log("userLocation: ok")
//           }).catch(() =>{
//             that.getlocation();
//           });
//
//         } else if (res.cancel) {
//           console.log('用户点击取消')
//           that.getlocation();
//         }
//       }
//     })

const LOG = true

// 权限列表
const scopeList = {
    "userInfo": "scope.userInfo",                                       // wx.getUserInfo   用户信息
    "userLocation": "scope.userLocation",                               // wx.getLocation, wx.chooseLocation    地理位置
    "userLocationBackground": "scope.userLocationBackground",           // wx.startLocationUpdateBackground 后台定位
    "werun": "scope.werun",                                             // wx.getWeRunData  微信运动步数
    "record": "scope.record",                                           // wx.startRecord   录音功能
    "writePhotosAlbum": "scope.writePhotosAlbum",                       // wx.saveImageToPhotosAlbum, wx.saveVideoToPhotosAlbum 保存到相册
    "camera": "scope.camera",                                           //camera 组件 摄像头
}

/**
 * 检查权限
 * @param key   scopeList -> key
 * @returns {Promise<unknown>}
 */
function getSetting(key) {
    const scope = scopeList[key]
    return new Promise((resolve, reject) => {
        wx.getSetting({
            success: (getSettingRes) => {
                LOG && console.log("申请授权过的信息", JSON.stringify(getSettingRes))
                // 如果未申请第一次 && 已经同意了
                if (getSettingRes.authSetting[`${scope}`] != undefined && getSettingRes.authSetting[`${scope}`] != true) {
                    // 申请过,已拒绝 -> 打开设置权限
                    LOG && console.log("打开设置")
                    wx.openSetting({
                        success: (openSettingRes) => {
                            if (openSettingRes.authSetting[`${scope}`] == true) {
                                LOG && console.log("P: 打开设置授权成功")
                                resolve()
                            } else {
                                LOG && console.log("P: 打开设置授权失败")
                                reject()
                            }
                        },
                        fail(e) {
                            LOG && console.log("P: 打开设置", e)
                            reject()
                        }
                    })
                } else {
                    // 设置界面只会出现小程序已经向用户请求过的权限。*未申请第一次 && 已经同意了
                    LOG && console.log("P: 检查权限")
                    // 授权
                    authorize(scope).then(() => {
                        resolve()
                    }).catch(() => {
                        reject()
                    })

                }
            }
        })
    })

}

// 首次尝试申请授权
function authorize(scope) {
    return new Promise((resolve, reject) => {
        wx.authorize({
            scope: `${scope}`,
            success() {
                // 首次申请权限同意
                LOG && console.log("P: 权限同意")
                resolve()
            },
            fail() {
                // 首次申请权限拒绝
                LOG && console.log("P: 权限拒绝")
                reject()
            }
        })
    })
}


export {
    getSetting
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容