改进地址选择页面的逻辑,但是代码有点不可控制

wechat_user_type
Ge Hao 8 years ago
parent de637846ad
commit 7b66c133b3
  1. 28
      pages/address/address.js
  2. 15
      pages/cart/cart.js
  3. 8
      pages/cart/cart.wxml
  4. 4
      utils/address_data.js

@ -13,19 +13,23 @@ Page({
}, },
bindChangeProvince: function(e) { bindChangeProvince: function(e) {
var p = this.data.arrayProvince[this.data.indexProvince] var p = this.data.arrayProvince[e.detail.value]
var arrayCity = district.cities(p)
var c = arrayCity[0]
var currentAddress = this.data.address var currentAddress = this.data.address
currentAddress.province = p currentAddress.province = p
this.setData({ this.setData({
indexProvince: e.detail.value, indexProvince: e.detail.value,
arrayCity: district.cities(p), arrayCity: arrayCity,
arrayCounty:district.counties(p,c),
address: currentAddress address: currentAddress
}) })
wx.setStorageSync('currentDistrict', [this.data.indexProvince, this.data.indexCity, this.data.indexCounty])
}, },
bindChangeCity: function(e) { bindChangeCity: function(e) {
var p = this.data.arrayProvince[this.data.indexProvince] var p = this.data.arrayProvince[this.data.indexProvince]
var c = this.data.arrayCity[this.data.indexCity] var c = this.data.arrayCity[e.detail.value]
var currentAddress = this.data.address var currentAddress = this.data.address
currentAddress.city = c currentAddress.city = c
this.setData({ this.setData({
@ -33,6 +37,7 @@ Page({
arrayCounty: district.counties(p,c), arrayCounty: district.counties(p,c),
address: currentAddress address: currentAddress
}) })
wx.setStorageSync('currentDistrict', [this.data.indexProvince, this.data.indexCity, this.data.indexCounty])
}, },
bindChangeCounty: function(e) { bindChangeCounty: function(e) {
@ -43,6 +48,7 @@ Page({
indexCounty: e.detail.value, indexCounty: e.detail.value,
address: currentAddress address: currentAddress
}) })
wx.setStorageSync('currentDistrict', [this.data.indexProvince, this.data.indexCity, this.data.indexCounty])
}, },
formSubmit: function(e) { formSubmit: function(e) {
@ -57,6 +63,20 @@ Page({
}, },
onLoad (params) { onLoad (params) {
this.setData({'arrayProvince': district.provinces()}) var address = wx.getStorageSync('address')
this.setData({'address': address})
var currentDistrict = wx.getStorageSync('currentDistrict') || [0, 0, 0]
var arrayProvince = district.provinces()
this.setData({
indexProvince: currentDistrict[0],
indexCity: currentDistrict[1],
indexCounty: currentDistrict[2],
arrayProvince: arrayProvince
})
var arrayCity = district.cities(arrayProvince[currentDistrict[0]])
this.setData({arrayCity: arrayCity})
var arrayCounty = district.counties(arrayProvince[currentDistrict[0]], arrayCity[currentDistrict[1]])
this.setData({arrayCounty: arrayCounty})
} }
}) })

@ -1,16 +1,15 @@
Page({ Page({
data: { data: {
address: 0 address: null
}, },
// onLoad: function (params) { onLoad: function (params) {
// debugger },
// },
// onShow: function (params) { onShow: function (params) {
// var address = wx.getStorageSync('address') var address = wx.getStorageSync('address')
// this.setData({'address': address}) this.setData({'address': address})
// }, },
// bindBilling: function () { // bindBilling: function () {
// var cartItems = wx.getStorageSync('cartItems') // var cartItems = wx.getStorageSync('cartItems')

@ -1,8 +1,10 @@
<view wx:if="{{ address > 0 }}" bindtap="bindTapAddress"> <view wx:if="{{ address }}" bindtap="bindTapAddress">
<text>{{address}}</text> <text>{{address.province}} {{address.city}} {{address.county}} {{address.detail}}</text>
<view>
<text>修改地址</text> <text>修改地址</text>
</view> </view>
</view>
<view wx:else bindtap="bindTapAddress"> <view wx:else bindtap="bindTapAddress">
<text>add new address</text> <text>填写收货地址</text>
</view> </view>

@ -12,9 +12,7 @@ function cities (province) {
function counties (province, city) { function counties (province, city) {
var counties = district[province].find(function(ele){ var counties = district[province].find(function(ele){
var pageData = getApp().getCurrentPage().data return ele.city === city
var city = pageData.arrayCity[pageData.indexCity]
return city !== undefined
}) })
return counties.county return counties.county
} }

Loading…
Cancel
Save