picker.js 5.8 KB
function init(ctx) {
  let data = {};
  let view = ctx.data.pickerData.view;

  data.sourceType = ctx.data.pickerData.sourceType;
  data.view = view;
  ctx.setData({
    pickerData: data
  });
}

function pickerShow(ctx) {
  let data = ctx.data.pickerData;

  data.view.isShow = !data.view.isShow;
  ctx.setData({
    pickerData: data
  });
}

function pickerHide(ctx) {
  let data = ctx.data.pickerData;

  data.view.isShow = false;
  ctx.setData({
    pickerData: data
  });
}

const pickerAction = {
  chooseColor: function(e) {
    let pickData = this.data.pickerData;

    // 先遍历将所有颜色、尺码置为未选中状态
    pickData.view.goodsList && pickData.view.goodsList.map(item => {
      item.selected = false;
      item.size_list.map(subItem => {
        subItem.selected = false;
        subItem.enable = subItem.storage_number > 0;
      });
    });

    // 将当前颜色置为选中状态
    let tempItem = e.target.dataset.key;

    // 遍历颜色尺码,根据库存切换显示状态
    tempItem && tempItem.size_list && tempItem.size_list.map(item => {
      item.enable = item.storage_number > 0;
    });
    tempItem.selected = true;
    this.data.selectedProductId = tempItem.goods_id;

    // 设置标志位,表示已经选择颜色
    let colorSelected = true;

    // 重置库存、当前选中sku的值
    pickData.storageNumber = 0;
    pickData.selectedSKU = 0;
    this.data.selectedSKU = 0;

    pickData.view.goodsList[Number(e.target.id)] = tempItem;
    pickData.view.sizeList = tempItem.size_list;

    pickData.view.minusButtonEnable = false;

    // 将购买数量设为初始状态
    pickData.view.buyNumber = 1;

    this.setData({
      colorSelected,
      pickerData: pickData
    });
  },
  chooseSize: function(e) {
    let pickData = this.data.pickerData;

    // 现遍历将所有尺码置为未选中状态
    pickData.view.sizeList && pickData.view.sizeList.map(item => {
      item.selected = false;
    });

    // 将当前尺码置为选中状态
    let tempItem = e.currentTarget.dataset.key;

    tempItem.selected = true;
    pickData.view.sizeList[parseInt(e.currentTarget.id)] = tempItem;

    // 设置数量加减按钮启用状态
        pickData.view.plusButtonEnable = (!pickData.view.buy_limit_number || pickData.view.buy_limit_number > 1) && tempItem.storage_number > 1; // eslint-disable-line

    pickData.view.minusButtonEnable = false;
    pickData.view.buyButtonEnable = true;

    if (pickData.view.bundle_count === 0) {
      pickData.view.buyNumber = 1;
    }

    if (tempItem.storage_number <= 0) {
      wx.showToast({
        title: '该尺码已经售罄',
        icon: 'none',
        duration: 2000
      });

      pickData.view.buyButtonEnable = false;
    }

    this.setData({
      pickerData: pickData,
      selectedSKU: tempItem.storage_number > 0 ? tempItem.product_sku : 0,
      storageNumber: tempItem.storage_number
    });
  },
  bindMinus: function() {
    let pickerData = this.data.pickerData;
    let buyNumber = parseInt(pickerData.view.buyNumber);

    if (pickerData.view.bundle_count > 0) {
      if (buyNumber > pickerData.view.bundle_count) {
        pickerData.view.minusButtonEnable = true;
      } else {
        this.minReachedLimitAction(pickerData.view.bundle_count);
        pickerData.view.minusButtonEnable = false;
        return;
      }
    }

    if (buyNumber > 1) {
      buyNumber = buyNumber - 1;
    }
    pickerData.view.buyNumber = buyNumber;

    if (buyNumber === 1) {
      pickerData.view.minusButtonEnable = false;
    } else {
      pickerData.view.minusButtonEnable = true;
    }

    if (pickerData.view.buy_limit_number !== 0 && buyNumber < pickerData.view.buy_limit_number) {
      pickerData.view.plusButtonEnable = true;
    }

    if (buyNumber < this.data.storageNumber) {
      pickerData.view.plusButtonEnable = true;
    }

    this.setData({pickerData});
  },

  // 增加购买数量
  bindPlus: function() {
    if (!this.data.colorSelected) {
      return wx.showToast({
        title: '请选择颜色',
        icon: 'none',
        duration: 1000
      });
    }

    if (!this.data.selectedSKU) {
      return wx.showToast({
        title: '请选择尺码',
        icon: 'none',
        duration: 1000
      });
    }

    let pickerData = this.data.pickerData;
    let buyNumber = pickerData.view.buyNumber;

    if (pickerData.view.buy_limit_number !== 0 && buyNumber >= pickerData.view.buy_limit_number) {
      return this.plusReachedLimitAction(pickerData.view.buy_limit_number);
    }

    if (buyNumber >= this.data.storageNumber) {
      return this.plusReachedMaxAction();
    }

    if (pickerData.view.buy_limit_number !== 0 && buyNumber < pickerData.view.buy_limit_number) {
      pickerData.view.plusButtonEnable = true;
    }

    if (buyNumber < this.data.storageNumber) {
      pickerData.view.plusButtonEnable = true;
      buyNumber = parseInt(buyNumber) + 1;
    }


    if (buyNumber >= this.data.storageNumber) {
      pickerData.view.plusButtonEnable = false;
    }

    if (buyNumber > 1) {
      pickerData.view.minusButtonEnable = true;
    }

    pickerData.view.buyNumber = buyNumber;
    this.setData({pickerData});
  },

  plusReachedMaxAction: function() {
    return wx.showToast({
      title: '对不起,没有更多库存了',
      icon: 'none',
      duration: 1000
    });
  },

  plusReachedLimitAction: function(n) {
    return wx.showToast({
      title: '限购' + n + '件',
      icon: 'none',
      duration: 1000
    });

  },

  minReachedLimitAction: function(n) {
    return wx.showToast({
      title: n + '件起售',
      icon: 'none',
      duration: 1000
    });
  },
  clearPickerData() {
    this.setData({
      colorSelected: false,
      selectedSKU: '',
      storageNumber: 0
    });
  }
};

module.exports = {
  init: init,
  pickerShow: pickerShow,
  pickerHide: pickerHide,
  pickerAction
};