<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 }); } }) } }) } |

<buttonbindtap="openSetting"wx:if="{{ver}}">高版本授权流程</button><buttonopen-type="openSetting"bindopensetting="openSetting2"wx:else>低版本授权</button>已解决,通过判断版本,高版本继续保持原来的授权方式,第二次授权点击按钮后仍然弹出框提示,确认授权后,跳转到授权按钮;
低版本采用第一次提示授权,后面不提示授权,直接跳转到授权按钮