1. 程式人生 > >微信小程式微商城(十):使用者收貨地址管理

微信小程式微商城(十):使用者收貨地址管理

上一篇:微信小程式微商城(九):微信授權並實現個人中心頁面

看效果

 

開發計劃

1、佈局收貨地址列表和新增收貨地址頁面

2、實現省市縣三級聯動功能

3、使用快取管理資料

一、收貨地址列表管理

addressList.wxml

<scroll-view class="scroll" scroll-y="true">

  <view wx:for="{{addressList}}">

    <view class="product-name-wrap">

      <view class="ui-list-item-info">{{item.consignee}}

        <text decode="{{true}}" space="{{true}}">&nbsp;&nbsp;</text> {{item.mobile}}

      </view>

      <view class="ui-list-item-address">

        {{item.address}}

      </view>

      <view class="ui-list-item-time">

        <p>{{item.transportDay}}</p>

        <p class="ui-list-item-del" data-id="{{index}}" bindtap="delAddress">刪除</p>

      </view>

      <view class="separate"></view>

    </view>

  </view>

</scroll-view>

<view class="add-address" bindtap="addAddress">

  <image class="add-img"  src="../../images/add.png"></image>新增地址

</view>

addressList.wxss

page{  

  display: flex;  

  flex-direction: column;  

  height: 100%;  

}  

.product-name-wrap{

  margin: 0px 10px;

  font-size: 14px;

  color: #404040;

}

.ui-list-item-info{

  margin: 5px 0px;

}

.ui-list-item-address{

  color: #585c64;

}

.ui-list-item-time{

  margin: 5px 0px;

}

.ui-list-item-del{

  position: absolute;

  right: 10px;

  color: #585c64;

}

/* 分割線 */

.separate {

  margin: 5px 0px;

  height: 2rpx;

  background-color: #f2f2f2;

}

.add-address{

  margin: 0 auto;

  margin-top: 30px;

  width: 150px;

  height: 35px;

  border: 1px #000 solid;

  line-height: 35px;

  text-align: center;

  color: #000;

  border-radius: 5rpx;

      display: block;

}

.add-img{

  margin-right: 15rpx;

  width: 15px;

  height: 15px;

}

addressList.json

{

  "navigationBarTitleText": "管理地址"

}

addressList.js

Page({

 

  /**

   * 頁面的初始資料

   */

  data: {

    addressList:[]

  },

  /**

   * 生命週期函式--監聽頁面載入

   */

  onLoad: function (options) {

    var arr = wx.getStorageSync('addressList') || [];

    console.info("快取資料:" + arr);

    // 更新資料  

    this.setData({

      addressList: arr

    });

  },

 

  /**

   * 生命週期函式--監聽頁面顯示

   */

  onShow: function () {

    this.onLoad();

  },

  addAddress:function(){

    wx.navigateTo({ url: '../address/address' });

  },

  /* 刪除item */

  delAddress: function (e) {

    this.data.addressList.splice(e.target.id.substring(3), 1);

    // 更新data資料物件  

    if (this.data.addressList.length > 0) {

      this.setData({

        addressList: this.data.addressList

      })

      wx.setStorageSync('addressList', this.data.addressList);

    } else {

      this.setData({

        addressList: this.data.addressList

      })

      wx.setStorageSync('addressList', []);

    }

  }

})

二、新增收貨資訊

address.wxml

<form bindsubmit="saveAddress">

  <view class="weui-cells weui-cells_after-title">

    <view class="weui-cell weui-cell_input">

      <view class="weui-cell__hd">

        <view class="weui-label">收貨人</view>

      </view>

      <view class="weui-cell__bd">

        <input class="weui-input" name="consignee" placeholder="請輸入收貨人真實姓名" />

      </view>

    </view>

    <view class="weui-cell weui-cell_input">

      <view class="weui-cell__hd">

        <view class="weui-label">手機號</view>

      </view>

      <view class="weui-cell__bd">

        <input class="weui-input" name="mobile" placeholder="請輸入收貨人手機號" />

      </view>

    </view>

    <view class="weui-cell weui-cell_select">

      <view class="weui-cell__hd weui-cell__hd_in-select-after">

        <view class="weui-label">收貨時間</view>

      </view>

      <view class="weui-cell__bd">

        <picker bindchange="bindTransportDayChange" value="{{transportIndex}}" range="{{transportValues}}">

          <view class="weui-select weui-select_in-select-after">{{transportValues[transportIndex]}}</view>

          <input name="transportDay" hidden="true" value="{{transportValues[transportIndex]}}" />

        </picker>

      </view>

    </view>

  </view>

  <view class="weui-cells__title"></view>

  <view class="weui-cells weui-cells_after-title">

    <view class="weui-cell weui-cell_select">

      <view class="weui-cell__hd weui-cell__hd_in-select-after">

        <view class="weui-label">省份</view>

      </view>

      <view class="weui-cell__bd">

        <picker bindchange="bindProvinceNameChange" value="{{provinceIndex}}" range="{{provinceNames}}">

          <view class="weui-select weui-select_in-select-after">{{provinceNames[provinceIndex]}}</view>

          <input name="provinceName" hidden="true" value="{{provinceNames[provinceIndex]}}" />

        </picker>

      </view>

    </view>

    <view class="weui-cell weui-cell_select">

      <view class="weui-cell__hd weui-cell__hd_in-select-after">

        <view class="weui-label">城市</view>

      </view>

      <view class="weui-cell__bd">

        <picker bindchange="bindCityNameChange" value="{{cityIndex}}"range="{{cityNames}}">

          <view class="weui-select weui-select_in-select-after"name="city_name">{{cityNames[cityIndex]}}</view>

 

          <input name="cityName" hidden="true" value="{{cityNames[cityIndex]}}" />

        </picker>

      </view>

    </view>

    <view class="weui-cell weui-cell_select">

      <view class="weui-cell__hd weui-cell__hd_in-select-after">

        <view class="weui-label">區縣</view>

      </view>

      <view class="weui-cell__bd">

        <picker bindchange="bindCountyNameChange" value="{{countyIndex}}"range="{{countyNames}}">

          <view class="weui-select weui-select_in-select-after">{{countyNames[countyIndex]}}</view>

          <input name="countyName" hidden="true" value="{{countyNames[countyIndex]}}" />

        </picker>

      </view>

    </view>

    <view class="weui-cell weui-cell_input">

      <view class="weui-cell__hd">

        <view class="weui-label">詳細地址</view>

      </view>

      <view class="weui-cell__bd">

        <input class="weui-input" name="address" placeholder="請輸入收貨人詳細地址" />

      </view>

    </view>

  </view>

  <button class="weui-btn" type="primary" form-type="submit">儲存</button>

</form>

address.wxss

@import '../../utils/weui.wxss';

 

.weui-cells:before{

  top:0;

  border-top:1rpx solid white;

  }

.weui-cell{

  line-height: 3.5rem;

}

.weui-cells:after{

  bottom:0;border-bottom:1rpx solid white

}

 

.weui-btn{

  width: 80%;

}

address.json

{

  "navigationBarTitleText": "新增收貨地址"

}

address.js

var area = require('../../utils/area.js');

var areaInfo = []; //所有省市區縣資料

var provinces = []; //省

var provinceNames = []; //省名稱

var citys = []; //城市

var cityNames = []; //城市名稱

var countys = []; //區縣

var countyNames = []; //區縣名稱

var value = [0, 0, 0]; //資料位置下標

var addressList = null;

Page({

 

  /**

   * 頁面的初始資料

   */

  data: {

    transportValues: ["收貨時間不限", "週六日/節假日收貨", "週一至週五收貨"],

    transportIndex: 0,

    provinceIndex: 0, //省份

    cityIndex: 0, //城市

    countyIndex: 0, //區縣

  },

 

 

  /**

   * 生命週期函式--監聽頁面載入

   */

  onLoad: function(options) {

 

  },

 

  /**

   * 生命週期函式--監聽頁面顯示

   */

  onShow: function() {

    var that = this;

    area.getAreaInfo(function(arr) {

      areaInfo = arr;

      //獲取省份資料

      that.getProvinceData();

    });

  },

  // 獲取省份資料

  getProvinceData: function() {

    var that = this;

    var s;

    provinces = [];

    provinceNames = [];

    var num = 0;

    for (var i = 0; i < areaInfo.length; i++) {

      s = areaInfo[i];

      if (s.di == "00" && s.xian == "00") {

        provinces[num] = s;

        provinceNames[num] = s.name;

        num++;

      }

    }

    that.setData({

      provinceNames: provinceNames

    })

 

    that.getCityArr();

    that.getCountyInfo();

  },

 

  // 獲取城市資料

  getCityArr: function(count = 0) {

    var c;

    citys = [];

    cityNames = [];

    var num = 0;

    for (var i = 0; i < areaInfo.length; i++) {

      c = areaInfo[i];

      if (c.xian == "00" && c.sheng == provinces[count].sheng && c.di != "00") {

        citys[num] = c;

        cityNames[num] = c.name;

        num++;

      }

    }

    if (citys.length == 0) {

      citys[0] = {

        name: ''

      };

      cityNames[0] = {

        name: ''

      };

    }

    var that = this;

    that.setData({

      citys: citys,

      cityNames: cityNames

    })

    console.log('cityNames:' + cityNames);

    that.getCountyInfo(count, 0);

  },

 

  // 獲取區縣資料

  getCountyInfo: function(column0 = 0, column1 = 0) {

    var c;

    countys = [];

    countyNames = [];

    var num = 0;

    for (var i = 0; i < areaInfo.length; i++) {

      c = areaInfo[i];

      if (c.xian != "00" && c.sheng == provinces[column0].sheng && c.di == citys[column1].di) {

        countys[num] = c;

        countyNames[num] = c.name;

        num++;

      }

    }

    if (countys.length == 0) {

      countys[0] = {

        name: ''

      };

      countyNames[0] = {

        name: ''

      };

    }

    console.log('countyNames:' + countyNames);

    var that = this;

    // value = [column0, column1, 0];

 

    that.setData({

      countys: countys,

      countyNames: countyNames,

      // value: value,

    })

  },

 

  bindTransportDayChange: function(e) {

    console.log('picker country 發生選擇改變,攜帶值為', e.detail.value);

    this.setData({

      transportIndex: e.detail.value

    })

  },

 

  bindProvinceNameChange: function(e) {

    var that = this;

    console.log('picker province 發生選擇改變,攜帶值為', e.detail.value);

    var val = e.detail.value

    that.getCityArr(val); //獲取地級市資料

    that.getCountyInfo(val, 0); //獲取區縣資料

 

    value = [val, 0, 0];

    this.setData({

      provinceIndex: e.detail.value,

      cityIndex: 0,

      countyIndex: 0,

      value: value

    })

 

  },

 

  bindCityNameChange: function(e) {

    var that = this;

    console.log('picker city 發生選擇改變,攜帶值為', e.detail.value);

 

    var val = e.detail.value

    that.getCountyInfo(value[0], val); //獲取區縣資料

    value = [value[0], val, 0];

    this.setData({

      cityIndex: e.detail.value,

      countyIndex: 0,

      value: value

    })

  },

 

  bindCountyNameChange: function(e) {

    var that = this;

    console.log('picker county 發生選擇改變,攜帶值為', e.detail.value);

    this.setData({

      countyIndex: e.detail.value

    })

  },

 

  saveAddress: function(e) {

    var consignee = e.detail.value.consignee;

    var mobile = e.detail.value.mobile;

    var transportDay = e.detail.value.transportDay;

    var provinceName = e.detail.value.provinceName;

    var cityName = e.detail.value.cityName;

    var countyName = e.detail.value.countyName;

    var address = e.detail.value.address;

 

    console.log(transportDay + "," + provinceName + "," + cityName + "," + countyName + "," + address); //輸出該文字 

 

    var arr = wx.getStorageSync('addressList') || [];

    console.log("arr,{}", arr);

    addressList = {

      consignee: consignee,

      mobile: mobile,

      address: provinceName + cityName + countyName+address,

      transportDay: transportDay

    }

      arr.push(addressList);

    wx.setStorageSync('addressList', arr);

    wx.navigateBack({

      

    })

  }

})

area.js和weui.wxss 可以看下方原始碼獲取方式,這裡就不多做解釋。

備註

微信小程式微商城系列 都是通過https 動態獲取資料並展示的,建議從第一篇開始閱讀。大家多多支援本系列文章會繼續更新下去,謝謝各位!大家在使用過程中有哪些建議可以提出來,我們一起學習哈~~~

微信小程式微商城系列

微信小程式微商城:開發者key獲取
微信小程式微商城(一):https框架搭建並實現導航功能
微信小程式微商城(二):電商首頁輪播、分類導航和新品特賣實現
微信小程式微商城(三):電商首頁福利專場無限下拉重新整理動態API資料實現
微信小程式微商城(四):動態API實現商品詳情頁(上)
微信小程式微商城(五):動態API實現商品詳情頁(下)
微信小程式微商城(六):動態API實現新品特賣商品流式佈局
微信小程式微商城(七):動態API實現商品分類
微信小程式微商城(八):快取實現商品購物車功能

微信小程式微商城(九):微信授權並實現個人中心頁面頁面

關注我們

如果需要原始碼和素材可以關注“IT實戰聯盟”公*眾*號並留言(微商城原始碼,5個字會收到原始碼下載地址,一定要看原始碼裡面的操作手冊會少走很多彎路),也可以加入交流群和作者互撩哦~~~