收藏
回答

兼容低于基础库2.3.2的地理位置授权

框架类型 问题类型 API/组件名称 终端类型 微信版本 基础库版本
小程序 需求 wx.openSetting(Object object) 工具 6.5.3 2.3.2以下


<button bindtap="openSetting">授权流程</button>

在基础库低于2.3.2时候,采用该方法报错 openSetting:fail can only be invoked by user TAP gesture.

在基础库2.0.9时候,采用该方法报错 openSetting:fail 此接口已废弃,请使用 OpenSetting 组件

index.js

openSetting() {
       app.getPermission();
}

app.js

getPermission: function(fn) {
        let _this = this;
        let customTip = false;
        wx.getStorage({
            key: 'customTip',
            success(res) {
                customTip = true;
            },
            fail() {
                wx.setStorageSync('customTip', customTip);
            }
        });
        wx.getLocation({
            type: 'wgs84',
            success: function(res) {
                console.log("成功时候调用")
                if (typeof fn == "function") fn();
            },
            fail: function() {
                if (typeof fn == "function") fn();
                wx.getSetting({
                    success(res) {
                        console.log(res)
                        if (customTip) {
                            if (!res.authSetting['scope.userLocation']) {
                                wx.showModal({
                                    title: "地理位置未授权",
                                    content: "如需使用,请开启您手机中的定位授权,开启后重新打开小程序。",
                                    success(tip) {
                                        if (tip.confirm) {
                                            wx.openSetting({
                                                success(data) {
                                                    console.log("重新加载")
                                                    console.log(data)
                                                    console.log(data.authSetting['scope.userLocation'])
                                                    if (data.authSetting['scope.userLocation']) {
                                                        wx.showToast({
                                                            title: '授权成功',
                                                            icon: 'success',
                                                            duration: 1000
                                                        });
                                                    } else {
                                                        wx.showToast({
                                                            title: '授权失败',
                                                            icon: 'none',
                                                            duration: 1000
                                                        });
                                                    }
                                                },
                                                fail(errset) {
                                                    console.log("重新加载")
                                                    wx.showToast({
                                                        title: '调用设置窗口失败',
                                                        icon: 'none',
                                                        duration: 1000
                                                    });
                                                }
                                            })
                                        } else {
                                            console.log("取消了地理位置授权")
                                        }
                                    }
                                })
                            }
                        }
                        customTip = true;
                        wx.setStorageSync('customTip', customTip);
                    },
                    fail(err) {
                        wx.showToast({
                            title: '调用授权窗口失败',
                            icon: 'none',
                            duration: 1000
                        });
                    }
                })
            }
        })
    }


最后一次编辑于  2019-01-02
回答关注问题邀请回答
收藏

1 个回答

  • cc
    cc
    2019-01-02
    <button bindtap="openSetting" wx:if="{{ver}}">高版本授权流程</button>
    <button open-type="openSetting" bindopensetting="openSetting2" wx:else>低版本授权</button>

    已解决,通过判断版本,高版本继续保持原来的授权方式,第二次授权点击按钮后仍然弹出框提示,确认授权后,跳转到授权按钮;

    低版本采用第一次提示授权,后面不提示授权,直接跳转到授权按钮

    2019-01-02
    有用
    回复
登录 后发表内容