作者 wumengyu

app配置地理位置

@@ -11,7 +11,7 @@ App({ @@ -11,7 +11,7 @@ App({
11 self.globalData.isLogin = true; 11 self.globalData.isLogin = true;
12 wx.setStorageSync('isLogin', true) 12 wx.setStorageSync('isLogin', true)
13 // wx.redirectTo({ 13 // wx.redirectTo({
14 - // url: '/pages/index/index', //跳转到授权页面 14 + // url: '/pages/index/index', //跳转到首页
15 // }); 15 // });
16 } else { 16 } else {
17 console.log('授权失效到授权页'); 17 console.log('授权失效到授权页');
@@ -32,5 +32,10 @@ @@ -32,5 +32,10 @@
32 "navigationBarBackgroundColor": "#fff", 32 "navigationBarBackgroundColor": "#fff",
33 "navigationBarTitleText": "火柴西路", 33 "navigationBarTitleText": "火柴西路",
34 "navigationBarTextStyle": "black" 34 "navigationBarTextStyle": "black"
  35 + },
  36 + "permission": {
  37 + "scope.userLocation": {
  38 + "desc": "你的位置信息将用于获取附近的活动"
  39 + }
35 } 40 }
36 } 41 }
@@ -3,10 +3,30 @@ @@ -3,10 +3,30 @@
3 const app = getApp(); 3 const app = getApp();
4 4
5 import mock from './mock.js'//拿到的数据 5 import mock from './mock.js'//拿到的数据
  6 +var getLocation = function (self) {//当第一次未授权时需要重新调用此接口
  7 + wx.getLocation({
  8 + type: 'wgs84',
  9 + success: function (res) {
  10 + console.log('再次授权结果', res);
  11 + self.setData({
  12 + lat: res.latitude,
  13 + lng: res.longitude
  14 + });
  15 + },
  16 + fail: function () {
  17 + wx.showToast({
  18 + title: '授权失败',
  19 + icon: 'none',
  20 + duration: 1000
  21 + })
  22 + }
  23 + })
  24 +}
  25 +
6 26
7 Page({ 27 Page({
8 data: { 28 data: {
9 - is_show_city:false, 29 + is_show_city: false,
10 items: [], 30 items: [],
11 slide: [], 31 slide: [],
12 cate: [], 32 cate: [],
@@ -22,7 +42,7 @@ Page({ @@ -22,7 +42,7 @@ Page({
22 current_swiper: 0, 42 current_swiper: 0,
23 page_number: 1, 43 page_number: 1,
24 cityList: [], 44 cityList: [],
25 - addr:'', //切换地址 45 + addr: '', //切换地址
26 img_heights: [], //图片宽度 46 img_heights: [], //图片宽度
27 img_width: 750, //默认 47 img_width: 750, //默认
28 48
@@ -37,10 +57,10 @@ Page({ @@ -37,10 +57,10 @@ Page({
37 }, 57 },
38 //关闭金火柴勋章弹框 58 //关闭金火柴勋章弹框
39 close() { 59 close() {
40 - this.setData({show_gold_modal:false,show_normal_modal:false}); 60 + this.setData({show_gold_modal: false, show_normal_modal: false});
41 try { 61 try {
42 wx.removeStorageSync('score'); 62 wx.removeStorageSync('score');
43 - console.log('删除后score',wx.getStorageSync('score')); 63 + console.log('删除后score', wx.getStorageSync('score'));
44 } catch (e) { 64 } catch (e) {
45 // Do something when catch error 65 // Do something when catch error
46 } 66 }
@@ -84,7 +104,7 @@ Page({ @@ -84,7 +104,7 @@ Page({
84 is_showAnswer: false, 104 is_showAnswer: false,
85 is_showUserInfo: false, 105 is_showUserInfo: false,
86 is_showRelease: false, 106 is_showRelease: false,
87 - is_show_city:false, 107 + is_show_city: false,
88 }) 108 })
89 }, 109 },
90 //进入分类详情 110 //进入分类详情
@@ -92,10 +112,10 @@ Page({ @@ -92,10 +112,10 @@ Page({
92 const id = +e.currentTarget.dataset.id; 112 const id = +e.currentTarget.dataset.id;
93 const name = e.currentTarget.dataset.name; 113 const name = e.currentTarget.dataset.name;
94 let city = null; 114 let city = null;
95 - if(this.data.is_city_change) { 115 + if (this.data.is_city_change) {
96 city = JSON.stringify({ 116 city = JSON.stringify({
97 // name:this.data.cityList[this.data.current_city].title, 117 // name:this.data.cityList[this.data.current_city].title,
98 - name:this.data.addr, 118 + name: this.data.addr,
99 lat: this.data.lat, 119 lat: this.data.lat,
100 lng: this.data.lng, 120 lng: this.data.lng,
101 }) 121 })
@@ -114,10 +134,10 @@ Page({ @@ -114,10 +134,10 @@ Page({
114 //查看更多 134 //查看更多
115 goMore() { 135 goMore() {
116 let city = null; 136 let city = null;
117 - if(this.data.is_city_change) { 137 + if (this.data.is_city_change) {
118 city = JSON.stringify({ 138 city = JSON.stringify({
119 // name:this.data.cityList[this.data.current_city].title, 139 // name:this.data.cityList[this.data.current_city].title,
120 - name:this.data.addr, 140 + name: this.data.addr,
121 lat: this.data.lat, 141 lat: this.data.lat,
122 lng: this.data.lng, 142 lng: this.data.lng,
123 }) 143 })
@@ -149,7 +169,7 @@ Page({ @@ -149,7 +169,7 @@ Page({
149 }) 169 })
150 }, 170 },
151 bannerChange(e) { 171 bannerChange(e) {
152 - console.log(e,'current', current); 172 + console.log(e, 'current', current);
153 const current = e.detail.current; 173 const current = e.detail.current;
154 this.setData({currentBannerIndex: current}) 174 this.setData({currentBannerIndex: current})
155 }, 175 },
@@ -217,21 +237,22 @@ Page({ @@ -217,21 +237,22 @@ Page({
217 }, 237 },
218 //发布按钮 238 //发布按钮
219 release(e) { 239 release(e) {
220 - console.log('发布',app.globalData.isLogin); 240 + console.log('发布', app.globalData.isLogin);
221 const self = this; 241 const self = this;
222 // self.setData({ 242 // self.setData({
223 // is_showAnswer: true, //弹出 去答题弹框 243 // is_showAnswer: true, //弹出 去答题弹框
224 // is_showRelease:false, 244 // is_showRelease:false,
225 // }); 245 // });
226 - if(!app.globalData.isLogin) {  
227 - wx.showToast({title: '还没登录,先去登录吧~', icon: 'none',  
228 - success:function () { 246 + if (!app.globalData.isLogin) {
  247 + wx.showToast({
  248 + title: '还没登录,先去登录吧~', icon: 'none',
  249 + success: function () {
229 setTimeout(function () { 250 setTimeout(function () {
230 - wx.redirectTo({url:'/pages/my/my'})  
231 - },500) 251 + wx.redirectTo({url: '/pages/my/my'})
  252 + }, 500)
232 } 253 }
233 }); 254 });
234 - }else { 255 + } else {
235 if (+self.data.is_write === 2 && wx.getStorageSync('is_canSend') === 2) { //没填写个人信息并且不可以发布 =>去填写 256 if (+self.data.is_write === 2 && wx.getStorageSync('is_canSend') === 2) { //没填写个人信息并且不可以发布 =>去填写
236 self.setData({ 257 self.setData({
237 is_showUserInfo: true //弹出 去填写个人信息弹框 258 is_showUserInfo: true //弹出 去填写个人信息弹框
@@ -240,7 +261,7 @@ Page({ @@ -240,7 +261,7 @@ Page({
240 } else if (+self.data.is_write === 1 && wx.getStorageSync('is_answer') === 2) { //已填写且不能发布=>去答题 261 } else if (+self.data.is_write === 1 && wx.getStorageSync('is_answer') === 2) { //已填写且不能发布=>去答题
241 self.setData({ 262 self.setData({
242 is_showAnswer: true, //弹出 去答题弹框 263 is_showAnswer: true, //弹出 去答题弹框
243 - is_showRelease:false, 264 + is_showRelease: false,
244 }); 265 });
245 } else if (+self.data.is_write === 1 && wx.getStorageSync('is_canSend') === 1 && wx.getStorageSync('is_answer') === 1) { //已填写且可以发布 =>去发布 266 } else if (+self.data.is_write === 1 && wx.getStorageSync('is_canSend') === 1 && wx.getStorageSync('is_answer') === 1) { //已填写且可以发布 =>去发布
246 self.setData({ 267 self.setData({
@@ -288,7 +309,7 @@ Page({ @@ -288,7 +309,7 @@ Page({
288 //打开城市列表页面 309 //打开城市列表页面
289 chooseCity() { 310 chooseCity() {
290 // this.setData({is_show_city:!this.data.is_show_city}) 311 // this.setData({is_show_city:!this.data.is_show_city})
291 - wx.navigateTo({url:'/pages/index/city-list/city-list'}) 312 + wx.navigateTo({url: '/pages/index/city-list/city-list'})
292 }, 313 },
293 //切换城市 314 //切换城市
294 // cityPickerChange(e) { 315 // cityPickerChange(e) {
@@ -302,35 +323,99 @@ Page({ @@ -302,35 +323,99 @@ Page({
302 // // console.log('lat', 'lng',lat,lng); 323 // // console.log('lat', 'lng',lat,lng);
303 // this.getIndex(); 324 // this.getIndex();
304 // }, 325 // },
  326 + getPosition() {
  327 + const self = this;
  328 + console.log('2获取当前经纬度');
  329 + wx.getLocation({ //获取当前经纬度
  330 + type: 'wgs84', //返回可以用于wx.openLocation的经纬度,官方提示bug: iOS 6.3.30 type 参数不生效,只会返回 wgs84 类型的坐标信息
  331 + success: function (res) {
  332 + console.log('获取当前经纬度', res);
  333 + self.setData({lat: res.latitude, lng: res.longitude});
  334 + // app.loginCallback();
  335 + // app.loginCallback = res => {
  336 + // console.log('show-callback-res', res);
  337 + app.getLogin().then(function (res) {
  338 + console.log('res', res);
  339 + console.log('1');
  340 + self.getIndex(res.token);//获取首页
  341 + self.getIsWrite(); //判断是否已填写个人信息
  342 + });
  343 + // };
  344 + },
  345 + fail: function (res) {
  346 + console.log('定位失败', res)
  347 + console.log('重新调起授权,获取当前城市');
  348 + wx.getSetting({
  349 + success: (res) => {
  350 + // console.log('成功');
  351 + if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {//非初始化进入该页面,且未授权
  352 + console.log('非初始化进入该页面,且未授权');
  353 + wx.showModal({
  354 + title: '是否授权当前位置',
  355 + content: '需要获取您的地理位置,请确认授权,否则无法加载内容',
  356 + success: function (res) {
  357 + if (res.cancel) {
  358 + self.setData({
  359 + isshowCIty: false
  360 + })
  361 + wx.showToast({
  362 + title: '授权失败',
  363 + icon: 'none',
  364 + duration: 1000
  365 + })
  366 + } else if (res.confirm) {
  367 + wx.openSetting({
  368 + success: function (dataAu) {
  369 + if (dataAu.authSetting["scope.userLocation"] == true) {
  370 + wx.showToast({
  371 + title: '授权成功',
  372 + icon: 'success',
  373 + duration: 1000
  374 + })
  375 + //再次授权,调用getLocationt的API
  376 + getLocation(self);
  377 + } else {
  378 + wx.showToast({
  379 + title: '授权失败',
  380 + icon: 'none',
  381 + duration: 1000
  382 + })
  383 + }
  384 + }
  385 + })
  386 + }
  387 + }
  388 + })
  389 + } else if (res.authSetting['scope.userLocation'] == undefined) {//初始化进入
  390 + console.log('初始化进入');
  391 + getLocation(self);
  392 + } else { //授权后默认加载
  393 + console.log('授权后默认加载');
  394 + getLocation(self);
  395 + }
  396 + }
  397 + })
  398 + }
  399 + })
  400 + },
305 //获取经纬度 401 //获取经纬度
306 getLat() { 402 getLat() {
307 const self = this; 403 const self = this;
308 - if(wx.getStorageSync('lat') === '' &&  
309 - wx.getStorageSync('lng') === '' &&  
310 - wx.getStorageSync('addr') === '') {  
311 - wx.getLocation({ //获取当前经纬度  
312 - type: 'wgs84', //返回可以用于wx.openLocation的经纬度,官方提示bug: iOS 6.3.30 type 参数不生效,只会返回 wgs84 类型的坐标信息  
313 - success: function (res) {  
314 - console.log('获取当前经纬度', res);  
315 - self.setData({lat: res.latitude, lng: res.longitude});  
316 - // app.loginCallback();  
317 - // app.loginCallback = res => {  
318 - // console.log('show-callback-res', res);  
319 - app.getLogin().then(function (res) {  
320 - console.log('res', res);  
321 - self.getIndex(res.token);//获取首页  
322 - self.getIsWrite(); //判断是否已填写个人信息  
323 - });  
324 - // };  
325 - }  
326 - }); 404 + console.log('没有经纬度', wx.getStorageSync('lat') === '');
  405 + if (wx.getStorageSync('lat') !== '' &&
  406 + wx.getStorageSync('lng') !== '' &&
  407 + wx.getStorageSync('addr') !== '') {
  408 + return
  409 + } else {
  410 + console.log('1获取当前经纬度');
  411 + self.getPosition()
327 } 412 }
328 // wx.setStorageSync('score',30); 413 // wx.setStorageSync('score',30);
329 - console.log('score',wx.getStorageSync('score'));  
330 - if(wx.getStorageSync('score') !== '' && wx.getStorageSync('score')>=80) {  
331 - self.setData({show_gold_modal:true,});  
332 - }else if(wx.getStorageSync('score') !== '' && wx.getStorageSync('score')<80) {  
333 - self.setData({show_normal_modal:true}) 414 + console.log('score', wx.getStorageSync('score'));
  415 + if (wx.getStorageSync('score') !== '' && wx.getStorageSync('score') >= 80) {
  416 + self.setData({show_gold_modal: true,});
  417 + } else if (wx.getStorageSync('score') !== '' && wx.getStorageSync('score') < 80) {
  418 + self.setData({show_normal_modal: true})
334 } 419 }
335 }, 420 },
336 //获取首页内容 421 //获取首页内容
@@ -393,21 +478,23 @@ Page({ @@ -393,21 +478,23 @@ Page({
393 onLoad: function () { 478 onLoad: function () {
394 // this.getCityList(); 479 // this.getCityList();
395 // this.getAbroadList(); 480 // this.getAbroadList();
396 - this._doRefreshMasonry(this.data.items) 481 + this._doRefreshMasonry(this.data.items);
  482 + // this.getPosition()
  483 + // this.getLat();//获取经纬度
397 }, 484 },
398 onShow() { 485 onShow() {
399 - if(wx.getStorageSync('isLogin') === true) { 486 + if (wx.getStorageSync('isLogin')) {
400 this.getLat();//获取经纬度 487 this.getLat();//获取经纬度
401 } 488 }
402 - if(wx.getStorageSync('lat') !== '' && 489 + if (wx.getStorageSync('lat') !== '' &&
403 wx.getStorageSync('lng') !== '' && 490 wx.getStorageSync('lng') !== '' &&
404 wx.getStorageSync('addr') !== '') { 491 wx.getStorageSync('addr') !== '') {
405 console.log('已选择城市'); 492 console.log('已选择城市');
406 this.setData({ 493 this.setData({
407 - is_city_change:true,  
408 - lat:wx.getStorageSync('lat'), 494 + is_city_change: true,
  495 + lat: wx.getStorageSync('lat'),
409 lng: wx.getStorageSync('lng'), 496 lng: wx.getStorageSync('lng'),
410 - addr:wx.getStorageSync('addr'), 497 + addr: wx.getStorageSync('addr'),
411 }); 498 });
412 this.getIndex() 499 this.getIndex()
413 } 500 }
@@ -423,7 +510,7 @@ Page({ @@ -423,7 +510,7 @@ Page({
423 // name: "", 510 // name: "",
424 // address: "" 511 // address: ""
425 // }) 512 // })
426 - console.log('index-show-isLogin',wx.getStorageSync('isLogin')); 513 + console.log('index-show-isLogin', wx.getStorageSync('isLogin'));
427 // console.log(app.globalData.userInfo); 514 // console.log(app.globalData.userInfo);
428 }, 515 },
429 onPageScroll(e) { 516 onPageScroll(e) {
@@ -439,7 +526,7 @@ Page({ @@ -439,7 +526,7 @@ Page({
439 */ 526 */
440 onShareAppMessage: function () { 527 onShareAppMessage: function () {
441 let self = this; 528 let self = this;
442 - self.setData({show_gold_modal:false}); 529 + self.setData({show_gold_modal: false});
443 // 设置菜单中的转发按钮触发转发事件时的转发内容 530 // 设置菜单中的转发按钮触发转发事件时的转发内容
444 var shareObj = { 531 var shareObj = {
445 title: '火柴西路', // 默认是小程序的名称(可以写slogan等) 532 title: '火柴西路', // 默认是小程序的名称(可以写slogan等)
@@ -462,4 +549,30 @@ Page({ @@ -462,4 +549,30 @@ Page({
462 } 549 }
463 } 550 }
464 }, 551 },
  552 + // onReady() {
  553 + // console.log('进入onReady');
  554 + // if (wx.getStorageSync('isLogin')) {
  555 + // this.getLat();//获取经纬度
  556 + // }
  557 + // wx.openSetting({
  558 + // success: function (dataAu) {
  559 + // console.log('进入onReady拉起授权',dataAu);
  560 + // if (dataAu.authSetting["scope.userLocation"]) {
  561 + // wx.showToast({
  562 + // title: '授权成功',
  563 + // icon: 'success',
  564 + // duration: 1000
  565 + // })
  566 + // //再次授权,调用getLocationt的API
  567 + // getLocation(self);
  568 + // } else {
  569 + // wx.showToast({
  570 + // title: '授权失败',
  571 + // icon: 'none',
  572 + // duration: 1000
  573 + // })
  574 + // }
  575 + // }
  576 + // })
  577 + // },
465 }); 578 });