小程序:需要授权的功能报错,例如定位授权报错

  1. 检查授权情况
  2. false 需要打开授权页面
  3. 这个授权页面需要点击事件才能触发,一般用小程序的 modal 确定事件代替,也可以自己写一个button触发。

下面是定位授权的判断,wepy 例子

    // 判断授权
    getSetting () {
        let that = this;
        wx.getSetting({
            success (res) {
                console.log('setting-suc', res.authSetting);
                // 已授权或者未请求过授权
                if (res.authSetting['scope.userLocation'] || res.authSetting['scope.userLocation'] === undefined) {
                    that.getLocation();
                } else if (!res.authSetting['scope.userLocation']) {
                    // 拒绝授权,showModal 为了触发点击事件,里面的title,content自定义
                    wx.showModal({
                        title: '小程序授权',
                        content: '请授权定位功能,否则无法顺利体验小程序功能',
                        success: res => {
                            if (res.confirm) {
                                that.openSetting();
                            }
                        }
                    });
                }
            }
        });
    }
    // 打开授权
    openSetting () {
        wx.openSetting({
            success (res) {
                console.log('open-suc', res.authSetting);
            },
            fail (err) {
                console.log('open-err', err);
            }
        });
    }
    // 获取位置
    getLocation () {
        let that = this;
        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                console.log('location-suc', res);
                that.latitude = res.latitude;
                that.longitude = res.longitude;
                that.$apply();
                that.getCityList();
            },
            fail: function (err) {
                console.log('location-err', err);
                // 有些手机定位功能无法使用,例如华为手机,需要打开手机定位,据说是安全策略的设置。
                if (err.errMsg.indexOf('ERROR_SERVER_NOT_LOCATION') > -1) {
                    wx.showToast({
                        title: '请打开手机定位功能,下拉页面重新获取数据',
                        icon: 'none'
                    });
                } else if (err.errMsg.indexOf('auth deny') > -1) {
                    wx.showToast({
                        title: '请打开授权定位功能,否则无法顺利使用小程序',
                        icon: 'none'
                    });
                }
            }
        });
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,528评论 1 11
  • 简介 微信小程序入门门槛低、开发周期短、代码编写灵活、传播速度快等优点让微信小程序迅速火爆,开发者纷纷涌入,任何语...
    大公爵阅读 4,227评论 0 10
  • wepy+vant 微信小程序开发总结 前言 本次小程序开发选择使用 wepy作为小程序第三方框架,在UI组件库方...
    嗨姑娘_大个子阅读 1,243评论 0 0
  • 进入伏天以来,就一直闷热,要洇出水的天气却没有下过一场透雨。 在这闷热的天气里,素素只想躺着吹吹空调,特别是在步行...
    真冉阅读 430评论 0 0
  • 好吧,我困了,睡觉睡觉~zZ明天早起 仅仅是个开始的周一都有点累,怎么办,调整状态 数老今天讲的被我听的有点糊涂,...
    说到做到的奇女子阅读 202评论 0 0