小程序授权js封装

2020-03-17 09:49:40 浏览数 (1)

小程序的js封装,不是很全面,不过大部分的授权,做的产品是对接腾讯云的即时通讯IM

代码语言:javascript复制
自建一个js文件放进去
const deviceAuthorSeting=function(author){
    return new Promise((resolve, reject) => {
        uni.getSetting({
            success(res) {
                //授权是否存在
                if (res.authSetting[author] == undefined) { //从未授权
                    uni.authorize({
                        scope: author,
                        success(authorizeResponse) {
                            resolve(authorizeResponse)
                        },
                        fail(authorizeErr) {//拒绝授权
                            reject(authorizeErr)
                        }
                    })
                } else { //已经存在
                    if (res.authSetting[author]) { //已授权
                        resolve(res)
                    } else {
                        uni.openSetting({
                            success(openResponse) {
                                if(openResponse.authSetting[author]){
                                    resolve(true)
                                }else{
                                    reject(openResponse)
                                }

                            }
                        });
                    }
                }
            }
        })
    })
}
export default deviceAuthorSeting
代码语言:javascript复制
引入:import deviceAuthorSeting from "@/common/deviceAuthorSeting.js"
代码语言:javascript复制
调用:
 deviceAuthorSeting('scope.record').then(function(data) {
    _this.openFun(3)
}).catch(error => {
    console.log(error)
})

0 人点赞