addressEditer.js 10.6 KB
import api from '../../../../common/api.js';
import { getYHStorageSync } from '../../../../utils/util';
import Promise from '../../../../vendors/es6-promise';

var modeEdit = "modeEdit";
var modeSelect = "modeSelect";
var modeChange = "modeChange";
var modeAdd = "modeAdd"

var app = getApp();
const APP_ADDRESS_PROVINCES = "app.address.provinces";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    editeAddressPage: {
      currentEditMode: modeAdd,
      _consignee: "",
      _mobile: "",
      _detailAddress: "",
      _area_code: 0,
      _address: "",
      _id: "",
    },
    originAddressList: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    new app.WeToast;
    let timestamp = options.timestamp;
    let value = getYHStorageSync(timestamp, 'addressEditer');
    let address = value && value._address ? value._address : '';
    let originAddressList = address ? address.split(" ") : [];
    this.setData({
      editeAddressPage: value && value._id ? value : this.data.editeAddressPage,
      originAddressList,
    });
    try {
      tt.setStorageSync(timestamp, null);//清除内存
    } catch (e) {
    }
    tt.setNavigationBarTitle({
      title: value ? value._page_name : ''
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  editPageOnLoad: function (level,id) {
    let param = {
      method: APP_ADDRESS_PROVINCES,
      uid: app.getUid(),
      fromPage: 'addressEditer',
      id: id
    };
    let that = this;
    return new Promise(function (resolve, reject) {
      api.get({data:param})
        .then(json => {
          if (json && json.code && json.code == 200) {
            let value = json.data;
            let newEditeAddressPage = that.data.editeAddressPage;
            let key = 'address_' + level;
            newEditeAddressPage[key] = value;


            //比对_area_code 解决 “其他” 被选中后,后台自动过滤掉address文字的返回但是_area_code没过滤的情况,导致我们显示上就没有选中 “其他” 
            if (level == 3) {
              let list = value;
              for (var i = 0; i < list.length; i++) {
                let item = list[i];
                if (item.id == newEditeAddressPage._area_code) {
                  newEditeAddressPage['level3Select'] = item;
                  break;
                }
              }
            }

            that.setData({
              editeAddressPage: newEditeAddressPage,
            });
          }
          resolve(json);
        })
        .catch(error => {
          reject(error);
        });
    });
  },

  onInputChanged: function (e) {
    
    let id = e.target.dataset.id;
    let val = e.detail.value;
    if (id == "txtConsignee") {
      this.data.editeAddressPage._consignee = val;
    }
    else if (id == "txtMobile") {
      this.data.editeAddressPage._mobile = val;
    }
    else if (id == "txtAddress") {
      this.data.editeAddressPage._detailAddress = val;
    }
  },

  onDeleteButtonTapped: function () {
    let that = this;
    tt.showModal({
      title: '提示',
      content: '确认删除收货地址?',
      cancelText: '取消',
      confirmText: '确认',
      confirmColor: '#000000',
      success: function (res) {
        if (res.confirm) {
          let param = {
            method: "app.address.del",
            uid: app.getUid(),
            id: that.data.editeAddressPage._id
          };
          POST(API_HOST, param).then((json) => {
            let prePage = getCurrentPages();
            if (prePage.length > 1 && prePage[prePage.length - 2].getAddressList != undefined) {
              prePage[prePage.length - 2].getAddressList();
            }
            tt.navigateBack({
              delta: 1
            })
          }).catch((e) => {
            // console.log(e);
          })
        }
        else {

        }
      }
    })


  },
  /**
   * 保存按钮点击事件
   */
  onSubmitButtonTapped: function () {
    this.hiddenPicker();

    if (this.data.editeAddressPage.currentEditMode == modeAdd) {
      let param = {
        method: "app.address.add",
        consignee: this.data.editeAddressPage._consignee,
        mobile: this.data.editeAddressPage._mobile,
        area_code: this.data.editeAddressPage._area_code,
        address: this.data.editeAddressPage._detailAddress,
        uid: app.getUid()
      };

      POST(API_HOST, param).then((json) => {
        if (json.code == 200) {

          let prePage = getCurrentPages();
          if (prePage.length > 1 && prePage[prePage.length - 2].getAddressList != undefined) {
            prePage[prePage.length - 2].getAddressList();
          }
          tt.navigateBack({
            delta: 1
          })

        } else {
          tt.showToast({
            title: json.message,
            icon: 'none',
            duration: 1500
          });
        }
      }).catch((e) => {
        // console.log(e);
      })
    }
    else if (this.data.editeAddressPage.currentEditMode == modeEdit) {
      let param = {
        method: "app.address.update",
        consignee: this.data.editeAddressPage._consignee,
        mobile: this.data.editeAddressPage._mobile,
        area_code: this.data.editeAddressPage._area_code,
        address: this.data.editeAddressPage._detailAddress,
        uid: app.getUid(),
        id: this.data.editeAddressPage._id
      };

      POST(API_HOST, param).then((json) => {
        if (json.code == 200) {
          let prePage = getCurrentPages();
          if (prePage.length > 1 && prePage[prePage.length - 2].getAddressList != undefined) {
            prePage[prePage.length - 2].getAddressList();
          }
          tt.navigateBack({
            delta: 1
          })
        } else {
          tt.showToast({
            title: json.message,
            icon: 'none',
            duration: 1500
          });
        }
      }).catch((e) => {
        // console.log(e);
      })
    }

  },
  /**
   * 页面选址触发事件
   */
  choosearea: function (e) {
    let that = this;
    this.editPageOnLoad(0,0).then(json => {
        that.editData(0);
        let newEditeAddressPage0 = that.data.editeAddressPage;
        let item0 = newEditeAddressPage0.level0Select ? newEditeAddressPage0.level0Select : '';
        let item_id_0 = item0 ? item0.id : '';
        if (!item_id_0){
          newEditeAddressPage0.isShow = true;
          newEditeAddressPage0.currentLevel = 0;
          that.setData({
            editeAddressPage: newEditeAddressPage0,
          });
          return;
        }
        that.editPageOnLoad(1, item_id_0).then(json => {
          that.editData(1);
          let newEditeAddressPage1 = that.data.editeAddressPage;
          let item1 = newEditeAddressPage1.level1Select ? newEditeAddressPage1.level1Select : '';
          let item_id_1 = item1 ? item1.id : '';
          if (!item_id_1) {
            newEditeAddressPage1.isShow = true;
            newEditeAddressPage0.currentLevel = 1;
            that.setData({
              editeAddressPage: newEditeAddressPage1,
            });
            return;
          }
          that.editPageOnLoad(2, item_id_1).then(json => {
            that.editData(2);
            let newEditeAddressPage2 = that.data.editeAddressPage;
            let item2 = newEditeAddressPage2.level2Select ? newEditeAddressPage2.level2Select : '';
            let item_id_2 = item2 ? item2.id : '';
            if (!item_id_2) {
              newEditeAddressPage2.isShow = true;
              newEditeAddressPage0.currentLevel = 2;
              that.setData({
                editeAddressPage: newEditeAddressPage2,
              });
              return;
            }
            that.editPageOnLoad(3, item_id_2).then(json => {
              that.editData(3);
              let newEditeAddressPage3 = that.data.editeAddressPage;
              newEditeAddressPage3.isShow = true;
              newEditeAddressPage0.currentLevel = 3;
              that.setData({
                editeAddressPage: newEditeAddressPage3,
              });
            });
          });
        });
    });
  },  

  editData: function (level){
    let newEditeAddressPage = this.data.editeAddressPage;
    let key = 'address_' + level;
    if (this.data.originAddressList.length > 0 && this.data.originAddressList.length > parseInt(level)) {
      let adsressItem = this.data.originAddressList[parseInt(level)];
      let list = newEditeAddressPage[key];
      for (var i = 0; i < list.length; i++) {
        let item = list[i];
        if (item.caption == adsressItem) {
          newEditeAddressPage['level' + level + 'Select'] = item;
          break;
        }
      }
      this.setData({
        editeAddressPage: newEditeAddressPage,
      });
    }
  },

  updateAddress: function (event) {
    let fliterSelectIndex = parseInt(event.detail.fliterSelectIndex);
    let rowIndex = parseInt(event.detail.rowIndex);
    let editeAddressPage = this.data.editeAddressPage;
    for(var i = 0 ; i < 4 ; i++) {
      if (i > fliterSelectIndex){
        let key1 = 'address_' + i;
        delete editeAddressPage[key1];
        let key2 = 'level' + i + 'Select';
        delete editeAddressPage[key2];
      }
    }
    editeAddressPage.currentLevel = fliterSelectIndex;
    let that = this;
    let list = editeAddressPage['address_' + fliterSelectIndex];
    let item = list[rowIndex]
    let curIndex = fliterSelectIndex+1;
    this.editPageOnLoad(curIndex, item.id).then(json => {
      let key = 'level' + fliterSelectIndex + 'Select';
      editeAddressPage[key] = item;
      editeAddressPage.currentLevel = curIndex;
      that.setData({
        editeAddressPage: editeAddressPage,
      });
    });
  },

  selectAddressFinish: function (event) {
    let rowIndex = parseInt(event.detail);
    let editeAddressPage = this.data.editeAddressPage;
    let list = editeAddressPage['address_3'];
    let item = list[rowIndex]
    let key = 'level' + 3 + 'Select';
    editeAddressPage[key] = item;
    editeAddressPage.currentLevel = 3;
    let _address = editeAddressPage.level0Select.caption + ' ' + editeAddressPage.level1Select.caption + ' ' + editeAddressPage.level2Select.caption + ' ' + editeAddressPage.level3Select.caption;
    editeAddressPage._address = _address;
    editeAddressPage._area_code = item.id;

    let originAddressList = _address ? _address.split(" ") : [];

    this.setData({
      editeAddressPage: editeAddressPage,
      originAddressList,
    });
    this.hiddenPicker();
  },

  didTouchCancelButton: function () {
    this.hiddenPicker();
  },

  hiddenPicker: function () {
    let newEditeAddressPage = Object.assign(this.data.editeAddressPage, {
      isShow: false, // 显示区域选择框
    });
    this.setData({
      editeAddressPage: newEditeAddressPage,
    });
  },
 })