var app = getApp(); module.exports = Behavior({ data: { address_a: "", point: { lat: "", lng: "", }, city: "", }, methods: { _getLocation(callback,errCall) { if (app.globalData.isLocationIng) { // 防止同时获取定位信息弹窗 setTimeout(() => { this._getLocation(callback); }, 100); return; } app.globalData.isLocationIng = true; wx.getSetting({ success: (resData) => { if (!resData.authSetting["scope.userLocation"]) { wx.authorize({ scope: "scope.userLocation", success() {}, fail(msgData) {}, complete: () => { this.getlocaldata(callback,errCall); }, }); } else { this.getlocaldata(callback,errCall); } }, fail(msgData) { console.log(msgData); } }); }, getlocaldata(callback,errCall) { if (wx.getStorageSync("address-point")) { let point = wx.getStorageSync("address-point").point; this._setaddressData({ lat: point.lat, lng: point.lng, from: 'storage' }, callback ); } else { wx.getLocation({ type: "gcj02", success: (res) => { const latitude = res.latitude; const longitude = res.longitude; // const speed = res.speed; // const accuracy = res.accuracy; this._setaddressData({ lat: latitude, lng: longitude, }, callback ); }, fail: (err) => { if (err.errMsg === "getLocation:fail auth deny") { console.log("当初用户拒绝,再次发起授权"); wx.openSetting({ success: (settingdata) => { if (settingdata.authSetting["scope.userLocation"]) { wx.showToast({ icon: "none", title: "获取权限成功", duration: 1500, }); this.getlocaldata(callback); } else { app.globalData.isLocationIng = false; wx.showToast({ icon: "none", title: "获取权限失败,定位需要用户授权", duration: 1500, }); } }, fail:(err)=>{ try { this.is_errCall(errCall) } catch (error) { console.log(error) } } }); }else{ app.globalData.isLocationIng = false; } }, }); } }, is_errCall(errCall){ errCall(false) }, _setaddressData(e, callback) { app .getReverseGeocoder(e.lat, e.lng) .then((res) => { let mapdata = res; this.setData({ "point.lat": e.lat||'', "point.lng": e.lng||'', address_a: mapdata.address, title: mapdata.formatted_addresses&&mapdata.formatted_addresses.recommend?mapdata.formatted_addresses.recommend:'', city: !app._isTextEmpty(mapdata.address_component.city) ? mapdata.address_component.city :(!app._isTextEmpty(mapdata.address_component.province)?mapdata.address_component.province:'') , }); if (e.from === 'storage') { // 缓存获取就不重新设置 app.globalData.isLocationIng = false; callback && callback(mapdata, e); } else { wx.setStorage({ key: "address-point", data: { point: this.data.point, city: this.data.city, address_component: mapdata.address_component }, success: (res) => { //异步缓存 app.globalData.isLocationIng = false; callback && callback(mapdata, e); }, }); } }) .catch((err) => { app.globalData.isLocationIng = false; console.log(err); }); }, }, });