addressManager.js 9.48 KB
import AddressService from './addressService.js'
import Yas from '../../../utils/yas';


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

var app = getApp();
let PV_ID = new Date().getTime() + '';
let yas;

Page({
  data: {
    items: [],
    order_code: "",
    hiddenOpt: false,
    currentMode: modeSelect,
    modeEdit: modeEdit,
    modeSelect: modeSelect,
    modeChange: modeChange,
    from_page_name: '',
    from_page_param: '',
    current_page_name: 'addressManager',
    current_page_param: '',

    //data for edit page
    editeAddressPage: {
      currentEditMode: modeAdd,
      _consignee: "",
      _mobile: "",
      _detailAddress: "",
      _area_code: 0,
      _address: "",
      _id: "",
      _is_default: "N",
    },

    currentSelectedIndex: -1,
    addType: 'none' // normal or wechat
  },
  onLoad:function(options){
    yas = new Yas(this);
    yas.pageOpenReport();
    // 生命周期函数--监听页面加载
    this.data.currentMode = modeEdit;
    if (options.currentMode == modeEdit || options.currentMode == modeSelect || options.currentMode == modeChange){
      this.data.currentMode = options.currentMode
      wx.setNavigationBarTitle({
        title: "地址管理"
      })
      if (this.data.currentMode != modeEdit) {
        if (this.data.currentMode == modeChange) {
          this.data.order_code = options.order_code;
        }

        this.setData({
          hiddenOpt: options.fromPage == "statement"
        })
      }
    }

    let from_page_name = options.page_name ? options.page_name : '';
    let from_page_param = options.page_param ? options.page_param : '';

    this.setData({ from_page_param, from_page_name });

    var pages = getCurrentPages()
    var currentPage = pages[pages.length - 1]
    var url = currentPage.route

    // let params = {
    //   PAGE_NAME: this.data.current_page_name,
    //   PAGE_PARAM: this.data.current_page_param,
    //   FROM_PAGE_NAME: this.data.from_page_name,
    //   FROM_PAGE_PARAM: this.data.from_page_param,
    //   PV_ID: PV_ID,
    //   PAGE_PATH: url
    // };
    // logEvent(YB_PAGE_OPEN_L, params);

    let address_id = options.address_id == undefined || options.address_id == "" ? 0 : options.address_id;
    address_id = decodeURIComponent(address_id);
    let addType = options.addType ? options.addType : 'none';
    this.setData({
      currentMode: this.data.currentMode,
      address_id,
      addType,
    })

    this.getAddressList();

    if (addType == 'normal') {
      this.addButtonTapped();
    }
  },
  onReady:function(){
    // 生命周期函数--监听页面初次渲染完成
  },
  onShow:function(){
    // 生命周期函数--监听页面显示
  },
  onHide:function(){
    // 生命周期函数--监听页面隐藏
  },
  onUnload:function(){
    // 生命周期函数--监听页面卸载
    if (this.data.items.length == 0 && this.data.currentMode == modeSelect) {
      let prePage = getCurrentPages();
      if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
        prePage[prePage.length - 2].selectComplete(null);
      }
    }

  },
  onPullDownRefresh: function() {
    // 页面相关事件处理函数--监听用户下拉动作
    // let params = {
    //   PAGE_NAME: this.data.current_page_name,
    //   PAGE_PARAM: this.data.current_page_param,
    //   FROM_PAGE_NAME: this.data.from_page_name,
    //   FROM_PAGE_PARAM: this.data.from_page_param,
    //   PV_ID: PV_ID,
    // };
    // logEvent(YB_PAGE_OPEN_L, params);
  },
  onReachBottom: function() {
    // 页面上拉触底事件的处理函数
  },
  selectAddress: function(e) {
    let index = e.currentTarget.dataset.index;
    if (this.data.currentMode == modeSelect) {
      let prePage = getCurrentPages();
      if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
        let json = this.data.items[index];
        prePage[prePage.length - 2].selectComplete(json);
      }

      wx.navigateBack({
        delta: 1
      })

    }
    else if (this.data.currentMode == modeChange) {
      var that = this;
      var addressID = this.data.items[index].address_id;
      let api = new AddressService().yohoApi();

      wx.showModal({
        title: '提示',
        content: '关联订单的收货地址将一起修改且不可修改第二次,请确认是否修改',
        cancelText: '返回',
        confirmText: '确认修改',
        confirmColor: '#000000',
        success: function(res) {
          if (res.confirm) {
            let params = {
              order_code: that.data.order_code,
              address_id: addressID
            }
            api.delAddressRequest(params)
            .then(data => {
              if (data) {
                wx.navigateBack({
                  delta: 1
                })
              }
            })
            .catch(error => {
            })
          }
          else {
            wx.navigateBack({
              delta: 1
            });
          }
        }
      })
    }
  },

  editButtonTapped: function(e) {
    // console.log(e)
    let id = e.currentTarget.dataset.id;
    let consignee = e.currentTarget.dataset.consignee;
    let mobile = e.currentTarget.dataset.mobile;
    let area_code = e.currentTarget.dataset.area_code;
    let address = e.currentTarget.dataset.address;
    let detailAddress = e.currentTarget.dataset.detail_address;
    let is_default = e.currentTarget.dataset.is_default;
    let newEditeAddressPage = Object.assign(this.data.editeAddressPage, {
      _consignee: consignee,
      _mobile: mobile,
      _area_code: area_code,
      _address: address,
      _detailAddress: detailAddress,
      _page_name: '',
      _id: id,
      currentEditMode: modeEdit,
      _is_default: is_default,
    });

    var timestamp = Date.parse(new Date());
    try {
      wx.setStorageSync(timestamp + '', newEditeAddressPage);
    } catch (e) {
    }

    wx.navigateTo({
      url: './addressEditer/addressEditer?' + 'timestamp=' + timestamp ,
    })
  },

  addButtonTapped: function() {

    if (this.data.items.length >= 5) {
      wx.showToast({
        title: '地址不能超过5个',
        icon: 'none',
        duration: 1500,
      })
      return;
    }
    // if (app.globalData.userInfo.uid == 0 || app.globalData.userInfo.uid == undefined){
    //   //未登录状态
    //   wx.showModal({
    //     content: '请登录后再新增地址',
    //     confirmText:'确定',
    //     showCancel:false,
    //     success:function(res){
    //       if (res.confirm){
    //         let pages = getCurrentPages();
    //         if (pages && pages.length > 1 && pages[pages.length - 2]){
    //           let lastPage = pages[pages.length -2];
    //           if (lastPage.route ='pages/userCenter/userCenter'){
    //             wx.navigateBack({
    //               delta:1
    //             })
    //           }
    //         }else{
    //           wx.switchTab({
    //             url: '../userCenter/userCenter',
    //           })
    //         }
    //       }
    //     }
    //   })
    //   return;
    // }

    let newEditeAddressPage = Object.assign(this.data.editeAddressPage, {
      currentEditMode: modeAdd,
      _consignee: "",
      _mobile: "",
      _detailAddress: "",
      _area_code: 0,
      _address: "",
      _id: "",
      _page_name: '',
    });


    var timestamp = Date.parse(new Date());
    try {
      wx.setStorageSync(timestamp + '', newEditeAddressPage);
    } catch (e) {
    }

    wx.navigateTo({
      url: './addressEditer/addressEditer?' + 'timestamp=' + timestamp,
    })
  },

  /**
   * 获取地址列表
   */
  getAddressList: function() {
    let api = new AddressService().yohoApi();
    api.fetchAddressList()
      .then(data => {
        if (data) {
        this.data.items = data
        let currentSelectedIndex = -1;
        this.data.items.map((item, index) => {
          if (this.data.address_id == item.address_id) {
            currentSelectedIndex = index;
          }
        });

        //删除或更新地址后需要通知上一个页面刷新
        //修复删除或更新被选择的地址后,返回上一个页面,页面地址没有变化的bug 可能订单创建会出现失败的情况
        if (currentSelectedIndex == -1) {
          let prePage = getCurrentPages();
          if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
            prePage[prePage.length - 2].selectComplete(null);
          }
          //防止编辑地址页面回来后由于接口反响快导致页面层级关系不对,定位不到statement页面的bug
          if (prePage.length > 2 && prePage[prePage.length - 3].selectComplete != undefined) {
            prePage[prePage.length - 3].selectComplete(null);
          }
        }else {
          let prePage = getCurrentPages();
          if (prePage.length > 1 && prePage[prePage.length - 2].selectComplete != undefined) {
            prePage[prePage.length - 2].selectComplete(this.data.items[currentSelectedIndex]);
          }
          //防止编辑地址页面回来后由于接口反响快导致页面层级关系不对,定位不到statement页面的bug
          if (prePage.length > 2 && prePage[prePage.length - 3].selectComplete != undefined) {
            prePage[prePage.length - 3].selectComplete(this.data.items[currentSelectedIndex]);
          }
        }
        this.setData({
          items: this.data.items,
          currentSelectedIndex
        })
      }
      })
      .catch(error => {
        console.error(error);
      })
  },

})