Skip to content

Commit

Permalink
约定登录后,触发上一个Page的initData函数
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobinwu committed Jun 2, 2017
1 parent af74f3d commit b345f56
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 30 deletions.
21 changes: 18 additions & 3 deletions pages/address-switch/address-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@ Page({
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.initData();
},
initData: function(){
var finalAddress = JSON.parse(util.getStorage('final_address')),
regionShowName = '请选择';
if (finalAddress && finalAddress.region_name){
regionShowName = '请选择';
if (finalAddress && finalAddress.region_name) {
regionShowName = finalAddress.region_name;
}
appInstance.globalData.geoAddress.city_name = regionShowName;
this.setGeoAddress();
//定位当前位置
this.getCurrentPos();
//我的收货地址
this.getAddressList();
},
setGeoAddress: function(){
this.setData({
geoAddress: appInstance.globalData.geoAddress
});
},
// 定位到当前位置
getCurrentPos: function(type){
var _self = this;
Expand Down Expand Up @@ -127,7 +136,7 @@ Page({
});
}

// 设置当前地址
// 设置当前地址(存进本地储存)
location.setCurrentAddress(add);

// 跳转到首页
Expand All @@ -136,6 +145,12 @@ Page({
});
},

selectAddressEvent: function(e){
var curresntPos = e.currentTarget.dataset.currentPos,
sendSta = e.currentTarget.dataset.sendSta,
type = e.currentTarget.dataset.type;
this.selectAddress(curresntPos, sendSta, type);
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
Expand Down
12 changes: 6 additions & 6 deletions pages/address-switch/address-switch.wxml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

<view class="warning-bar">因就近匹配门店配送服务,请选择准确收货地址</view>
<view class="location-local gray-title">当前位置</view>
<view class="local-address" bindtap="selectAddress" data-current-pos="{{currentPos}}" data-send-sta="{{sendSta}}" data-type="currentPos" class="{{!showSendSta ? 'spb' : ''}}">
<text class="address-line">{{addressLine}}</text>
<block wx:if="{{showSendSta}}">
<text class="send-status" wx:if="{{sendSta == 1}}">支持配送</text>
<text class="send-status no" wx:if="{{sendSta == 0}}">不支持配送</text>
<view bindtap="selectAddressEvent" data-current-pos="{{currentPos}}" data-send-sta="{{currentPos.hasLocationStore}}" data-type="currentPos" class="local-address {{!currentPos.addressline ? 'spb' : ''}}">
<text class="address-line">{{currentPos.addressline}}</text>
<block wx:if="{{currentPos.addressline}}">
<text class="send-status" wx:if="{{currentPos.hasLocationStore == 1}}">支持配送</text>
<text class="send-status no" wx:if="{{currentPos.hasLocationStore == 0}}">不支持配送</text>
</block>
<block wx:else>
<text class="get-pos-again" catchtap="getCurrentPos" data-active="active">
Expand All @@ -25,7 +25,7 @@
<view class="gray-title">我的收货地址</view>
<view class="my-address-list">
<view class="address-list">
<view class="address-item {{addressList.length - 1 == index ? 'last-address-item' : ''}}" wx:for="{{addressList}}" data-item="{{item}}" data-delivery-sta="{{item.delivery_sta}}" bindtap="selectAddress">
<view class="address-item {{addressList.length - 1 == index ? 'last-address-item' : ''}}" wx:for="{{addressList}}" data-current-pos="{{item}}" data-send-sta="{{item.delivery_sta}}" bindtap="selectAddressEvent">
<view class="basic-info">
<text class="user-name">{{item.username}}</text>
<text class="user-phone">{{item.tel}}</text>
Expand Down
9 changes: 5 additions & 4 deletions pages/address-switch/address-switch.wxss
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
.area-search-bar .area-search-ipt{
display: block;
flex-grow:1;
padding: 20rpx;
border-radius: 50rpx;
padding: 10rpx;
border-radius: 5rpx;
background: #e8e8e8;
border: none;
font-size: 28rpx;
Expand All @@ -73,7 +73,7 @@
.local-address{
display: flex;
align-items:center;
padding: 30rpx 20rpx;
padding: 10rpx 20rpx;
height: 88rpx;
border-top: solid #e8e8e8 1px;
background-color: #fff;
Expand Down Expand Up @@ -115,4 +115,5 @@
}
.my-address-list .address-info{
color:#999;
}
}
@import "../../css/font.wxss";
3 changes: 3 additions & 0 deletions pages/center/center.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Page({
// 页面初始化 options为页面跳转所带来的参数
this.getInfo();
},
initData: function(){
this.getInfo();
},
onReady:function(){
// 页面渲染完成
},
Expand Down
2 changes: 1 addition & 1 deletion pages/list/list.wxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!--pages/list/list.wxml-->
<import src="../../template/slider/slider.wxml"/>
<template is="slider" data="{{slider}}"/>
<template is="slider" data="{{slider}}" wx:if="{{storeData.length > 0}}"/>
<view class="container">
<view class="warn-msg-box">
<span class="icon icon-warning"></span>
Expand Down
21 changes: 17 additions & 4 deletions pages/login/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Page({
if(!this.checkPhone(this.data.phone)){
return wx.showToast({
title: '手机号格式不正确或为空',
image: '../../image/wrong.png',
duration: 1000
});
}
Expand All @@ -60,12 +61,14 @@ Page({
}else{
return wx.showToast({
title: res.msg,
image: '../../image/wrong.png',
duration: 1000
});
}
}).catch((err) => {
return wx.showToast({
title: err,
image: '../../image/wrong.png',
duration: 1000
});
});
Expand All @@ -75,6 +78,7 @@ Page({
if(!this.checkCode(this.data.code)){
return wx.showToast({
title: '验证码错误',
image: '../../image/wrong.png',
duration: 1000
});
}
Expand All @@ -89,18 +93,27 @@ Page({
}).then(res => {
if(res.status === 0) {
return util.updateToken().then((res) => {
wx.navigateBack({
delta: 1
});
//约定触发上一Page的initData函数
var pages = getCurrentPages();
var prevPage = pages[pages.length - 2]; //上一个页面对象
prevPage.initData && prevPage.initData();
wx.navigateBack({
delta: 1
});
})
}else {
return wx.showToast({
title: res.msg,
image: '../../image/wrong.png',
duration: 1000
});
}
}).catch(err => {
console.log(err)
return wx.showToast({
title: err,
image: '../../image/wrong.png',
duration: 1000
});
})
},

Expand Down
22 changes: 11 additions & 11 deletions utils/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ function getCoords(){
type: 'wgs84'
}).then(function(res){
//列表 => 22.5373800000 114.0129300000
//主页 => 22.5099650000 113.9256690000
//主页 => 22.514278 113.923374
//更多 => 29.6441500000 91.1145000000
return Promise.resolve({
lat: 22.5099650000 ,
lng: 113.9256690000
lat: 22.514278 ,
lng: 113.923374
});
}).catch(function(err){
wx.showToast({
Expand Down Expand Up @@ -54,7 +54,7 @@ function getGPSInfo(){
region_name:''
};
getCoords().then((point)=>{
console.log(point)
// console.log(point)
// 请求用户授权定位
//逆地址解析
var ReverseGeocoder = util.wxPromisify(qqmapsdk.reverseGeocoder, qqmapsdk); //需改变作用域
Expand All @@ -70,8 +70,8 @@ function getGPSInfo(){
data.lng = res.result.ad_info.location.lng;
// 纬度
data.lat=res.result.ad_info.location.lat;
//详细地址
data.location_addr = res.result.address;
//详细地址(获取就近建筑)
data.location_addr = res.result.address_reference.landmark_l1.title || res.result.address_reference.landmark_l2.title || res.result.address_reference.street_number.title || res.result.address_reference.street_number.title || res.result.address;
return region.getRegionId(res.result.address_component.city);
}).then((region)=>{// 通过城市名拿到城市区域id后
data.region_id = region.value;
Expand Down Expand Up @@ -106,6 +106,11 @@ function getLocation(options){
ajaxCfg.header={'X-Auth-Token':token};
}
return new Promise((resolve,reject)=>{
if (ajaxCfg.header) {
ajaxCfg.header.push({ 'content-type': 'application/x-www-form-urlencoded' });
} else {
ajaxCfg.header = { 'content-type': 'application/x-www-form-urlencoded' };
}
// 如果有已存在的定位信息(一般来自本地存储)
if(typeof options.gpsInfo != 'undefined'){
ajaxCfg.data = options.gpsInfo;
Expand All @@ -120,11 +125,6 @@ function getLocation(options){
if(result.err){
options.gpsError && options.gpsError(result.err);
}
if(ajaxCfg.header){
ajaxCfg.header.push({'content-type': 'application/x-www-form-urlencoded'});
}else{
ajaxCfg.header={'content-type': 'application/x-www-form-urlencoded'};
}
ajaxCfg.data= result.data;
return util.wxRequest(ajaxCfg, true);
}).then(result=>{
Expand Down
1 change: 0 additions & 1 deletion utils/bearer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ address.prototype = {
gpsInfo: addressData,
token: addressData ? false : true
}).then((result)=>{
// console.log(result)
_self.userLocationData = result.data;
// 将最终得到地址地址存入本地
util.setStorage("final_address", JSON.stringify(_self.userLocationData.final_address));
Expand Down

0 comments on commit b345f56

Please sign in to comment.