From fa3aed0194fc70d68b24b9978832ecf2730e8194 Mon Sep 17 00:00:00 2001 From: wumengyu <2683447714@qq.com> Date: Wed, 23 Jan 2019 15:35:00 +0800 Subject: [PATCH] app配置地理位置 --- app.js | 2 +- app.json | 5 +++++ pages/index/index.js | 215 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------- 3 files changed, 170 insertions(+), 52 deletions(-) diff --git a/app.js b/app.js index b22a362..32e77a4 100644 --- a/app.js +++ b/app.js @@ -11,7 +11,7 @@ App({ self.globalData.isLogin = true; wx.setStorageSync('isLogin', true) // wx.redirectTo({ - // url: '/pages/index/index', //跳转到授权页面 + // url: '/pages/index/index', //跳转到首页 // }); } else { console.log('授权失效到授权页'); diff --git a/app.json b/app.json index 118c1f2..bf5153b 100644 --- a/app.json +++ b/app.json @@ -32,5 +32,10 @@ "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "火柴西路", "navigationBarTextStyle": "black" + }, + "permission": { + "scope.userLocation": { + "desc": "你的位置信息将用于获取附近的活动" + } } } \ No newline at end of file diff --git a/pages/index/index.js b/pages/index/index.js index 5859244..460c884 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -3,10 +3,30 @@ const app = getApp(); import mock from './mock.js'//拿到的数据 +var getLocation = function (self) {//当第一次未授权时需要重新调用此接口 + wx.getLocation({ + type: 'wgs84', + success: function (res) { + console.log('再次授权结果', res); + self.setData({ + lat: res.latitude, + lng: res.longitude + }); + }, + fail: function () { + wx.showToast({ + title: '授权失败', + icon: 'none', + duration: 1000 + }) + } + }) +} + Page({ data: { - is_show_city:false, + is_show_city: false, items: [], slide: [], cate: [], @@ -22,7 +42,7 @@ Page({ current_swiper: 0, page_number: 1, cityList: [], - addr:'', //切换地址 + addr: '', //切换地址 img_heights: [], //图片宽度 img_width: 750, //默认 @@ -37,10 +57,10 @@ Page({ }, //关闭金火柴勋章弹框 close() { - this.setData({show_gold_modal:false,show_normal_modal:false}); + this.setData({show_gold_modal: false, show_normal_modal: false}); try { wx.removeStorageSync('score'); - console.log('删除后score',wx.getStorageSync('score')); + console.log('删除后score', wx.getStorageSync('score')); } catch (e) { // Do something when catch error } @@ -84,7 +104,7 @@ Page({ is_showAnswer: false, is_showUserInfo: false, is_showRelease: false, - is_show_city:false, + is_show_city: false, }) }, //进入分类详情 @@ -92,10 +112,10 @@ Page({ const id = +e.currentTarget.dataset.id; const name = e.currentTarget.dataset.name; let city = null; - if(this.data.is_city_change) { + if (this.data.is_city_change) { city = JSON.stringify({ // name:this.data.cityList[this.data.current_city].title, - name:this.data.addr, + name: this.data.addr, lat: this.data.lat, lng: this.data.lng, }) @@ -114,10 +134,10 @@ Page({ //查看更多 goMore() { let city = null; - if(this.data.is_city_change) { + if (this.data.is_city_change) { city = JSON.stringify({ // name:this.data.cityList[this.data.current_city].title, - name:this.data.addr, + name: this.data.addr, lat: this.data.lat, lng: this.data.lng, }) @@ -149,7 +169,7 @@ Page({ }) }, bannerChange(e) { - console.log(e,'current', current); + console.log(e, 'current', current); const current = e.detail.current; this.setData({currentBannerIndex: current}) }, @@ -217,21 +237,22 @@ Page({ }, //发布按钮 release(e) { - console.log('发布',app.globalData.isLogin); + console.log('发布', app.globalData.isLogin); const self = this; // self.setData({ // is_showAnswer: true, //弹出 去答题弹框 // is_showRelease:false, // }); - if(!app.globalData.isLogin) { - wx.showToast({title: '还没登录,先去登录吧~', icon: 'none', - success:function () { + if (!app.globalData.isLogin) { + wx.showToast({ + title: '还没登录,先去登录吧~', icon: 'none', + success: function () { setTimeout(function () { - wx.redirectTo({url:'/pages/my/my'}) - },500) + wx.redirectTo({url: '/pages/my/my'}) + }, 500) } }); - }else { + } else { if (+self.data.is_write === 2 && wx.getStorageSync('is_canSend') === 2) { //没填写个人信息并且不可以发布 =>去填写 self.setData({ is_showUserInfo: true //弹出 去填写个人信息弹框 @@ -240,7 +261,7 @@ Page({ } else if (+self.data.is_write === 1 && wx.getStorageSync('is_answer') === 2) { //已填写且不能发布=>去答题 self.setData({ is_showAnswer: true, //弹出 去答题弹框 - is_showRelease:false, + is_showRelease: false, }); } else if (+self.data.is_write === 1 && wx.getStorageSync('is_canSend') === 1 && wx.getStorageSync('is_answer') === 1) { //已填写且可以发布 =>去发布 self.setData({ @@ -288,7 +309,7 @@ Page({ //打开城市列表页面 chooseCity() { // this.setData({is_show_city:!this.data.is_show_city}) - wx.navigateTo({url:'/pages/index/city-list/city-list'}) + wx.navigateTo({url: '/pages/index/city-list/city-list'}) }, //切换城市 // cityPickerChange(e) { @@ -302,35 +323,99 @@ Page({ // // console.log('lat', 'lng',lat,lng); // this.getIndex(); // }, + getPosition() { + const self = this; + console.log('2获取当前经纬度'); + wx.getLocation({ //获取当前经纬度 + type: 'wgs84', //返回可以用于wx.openLocation的经纬度,官方提示bug: iOS 6.3.30 type 参数不生效,只会返回 wgs84 类型的坐标信息 + success: function (res) { + console.log('获取当前经纬度', res); + self.setData({lat: res.latitude, lng: res.longitude}); + // app.loginCallback(); + // app.loginCallback = res => { + // console.log('show-callback-res', res); + app.getLogin().then(function (res) { + console.log('res', res); + console.log('1'); + self.getIndex(res.token);//获取首页 + self.getIsWrite(); //判断是否已填写个人信息 + }); + // }; + }, + fail: function (res) { + console.log('定位失败', res) + console.log('重新调起授权,获取当前城市'); + wx.getSetting({ + success: (res) => { + // console.log('成功'); + if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权 + console.log('非初始化进入该页面,且未授权'); + wx.showModal({ + title: '是否授权当前位置', + content: '需要获取您的地理位置,请确认授权,否则无法加载内容', + success: function (res) { + if (res.cancel) { + self.setData({ + isshowCIty: false + }) + wx.showToast({ + title: '授权失败', + icon: 'none', + duration: 1000 + }) + } else if (res.confirm) { + wx.openSetting({ + success: function (dataAu) { + if (dataAu.authSetting["scope.userLocation"] == true) { + wx.showToast({ + title: '授权成功', + icon: 'success', + duration: 1000 + }) + //再次授权,调用getLocationt的API + getLocation(self); + } else { + wx.showToast({ + title: '授权失败', + icon: 'none', + duration: 1000 + }) + } + } + }) + } + } + }) + } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入 + console.log('初始化进入'); + getLocation(self); + } else { //授权后默认加载 + console.log('授权后默认加载'); + getLocation(self); + } + } + }) + } + }) + }, //获取经纬度 getLat() { const self = this; - if(wx.getStorageSync('lat') === '' && - wx.getStorageSync('lng') === '' && - wx.getStorageSync('addr') === '') { - wx.getLocation({ //获取当前经纬度 - type: 'wgs84', //返回可以用于wx.openLocation的经纬度,官方提示bug: iOS 6.3.30 type 参数不生效,只会返回 wgs84 类型的坐标信息 - success: function (res) { - console.log('获取当前经纬度', res); - self.setData({lat: res.latitude, lng: res.longitude}); - // app.loginCallback(); - // app.loginCallback = res => { - // console.log('show-callback-res', res); - app.getLogin().then(function (res) { - console.log('res', res); - self.getIndex(res.token);//获取首页 - self.getIsWrite(); //判断是否已填写个人信息 - }); - // }; - } - }); + console.log('没有经纬度', wx.getStorageSync('lat') === ''); + if (wx.getStorageSync('lat') !== '' && + wx.getStorageSync('lng') !== '' && + wx.getStorageSync('addr') !== '') { + return + } else { + console.log('1获取当前经纬度'); + self.getPosition() } // wx.setStorageSync('score',30); - console.log('score',wx.getStorageSync('score')); - if(wx.getStorageSync('score') !== '' && wx.getStorageSync('score')>=80) { - self.setData({show_gold_modal:true,}); - }else if(wx.getStorageSync('score') !== '' && wx.getStorageSync('score')<80) { - self.setData({show_normal_modal:true}) + console.log('score', wx.getStorageSync('score')); + if (wx.getStorageSync('score') !== '' && wx.getStorageSync('score') >= 80) { + self.setData({show_gold_modal: true,}); + } else if (wx.getStorageSync('score') !== '' && wx.getStorageSync('score') < 80) { + self.setData({show_normal_modal: true}) } }, //获取首页内容 @@ -393,21 +478,23 @@ Page({ onLoad: function () { // this.getCityList(); // this.getAbroadList(); - this._doRefreshMasonry(this.data.items) + this._doRefreshMasonry(this.data.items); + // this.getPosition() + // this.getLat();//获取经纬度 }, onShow() { - if(wx.getStorageSync('isLogin') === true) { + if (wx.getStorageSync('isLogin')) { this.getLat();//获取经纬度 } - if(wx.getStorageSync('lat') !== '' && + if (wx.getStorageSync('lat') !== '' && wx.getStorageSync('lng') !== '' && wx.getStorageSync('addr') !== '') { console.log('已选择城市'); this.setData({ - is_city_change:true, - lat:wx.getStorageSync('lat'), + is_city_change: true, + lat: wx.getStorageSync('lat'), lng: wx.getStorageSync('lng'), - addr:wx.getStorageSync('addr'), + addr: wx.getStorageSync('addr'), }); this.getIndex() } @@ -423,7 +510,7 @@ Page({ // name: "", // address: "" // }) - console.log('index-show-isLogin',wx.getStorageSync('isLogin')); + console.log('index-show-isLogin', wx.getStorageSync('isLogin')); // console.log(app.globalData.userInfo); }, onPageScroll(e) { @@ -439,7 +526,7 @@ Page({ */ onShareAppMessage: function () { let self = this; - self.setData({show_gold_modal:false}); + self.setData({show_gold_modal: false}); // 设置菜单中的转发按钮触发转发事件时的转发内容 var shareObj = { title: '火柴西路', // 默认是小程序的名称(可以写slogan等) @@ -462,4 +549,30 @@ Page({ } } }, + // onReady() { + // console.log('进入onReady'); + // if (wx.getStorageSync('isLogin')) { + // this.getLat();//获取经纬度 + // } + // wx.openSetting({ + // success: function (dataAu) { + // console.log('进入onReady拉起授权',dataAu); + // if (dataAu.authSetting["scope.userLocation"]) { + // wx.showToast({ + // title: '授权成功', + // icon: 'success', + // duration: 1000 + // }) + // //再次授权,调用getLocationt的API + // getLocation(self); + // } else { + // wx.showToast({ + // title: '授权失败', + // icon: 'none', + // duration: 1000 + // }) + // } + // } + // }) + // }, }); -- libgit2 0.24.0