picker.js 6.69 KB
const app = getApp();
Component({

    properties: {
        pickerData: {
            type: Object,
            value: null,
            observer: function (value, oldValue) {
                if (value.sourceType !== 'goodsDetail')
                    this.setData({pickerData: value})
            }
        },
        isloadingForPayment: {
            type: Boolean,
            value: false
        },
        pickerShow: {
            type: Boolean,
            value: false,
            observer: function (value) {
                this.setData({_show: value})
            }
        }
    },
    data: {_show: false, colorSelected: false, storageNumber: 0, selectedSKU: 0},
    methods: {
        colorTap(event) {
            var that = this;
            var tempData = that.data.pickerData;
            //先遍历将所有颜色、尺码置为未选中状态
            tempData.view.goodsList && tempData.view.goodsList.map((item, index) => {
                item.selected = false
                item.size_list.map((subItem, index) => {
                    subItem.selected = false
                    subItem.enable = subItem.storage_number > 0 ? true : false
                });
            });

            //将当前颜色置为选中状态
            var tempItem = event.target.dataset.key
            console.log(tempItem)
            //遍历颜色尺码,根据库存切换显示状态
            tempItem && tempItem.size_list && tempItem.size_list.map((item, index) => {
                item.enable = item.storage_number > 0 ? true : false
            })

            tempItem.selected = true

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

            //重置库存、当前选中sku的值
            tempData.storageNumber = 0
            tempData.selectedSKU = 0

            tempData.view.goodsList[Number(event.target.id)] = tempItem
            tempData.view.sizeList = tempItem.size_list

            tempData.view.minusButtonEnable = false
            //将购买数量设为初始状态
            tempData.view.buyNumber = 1;
            this.setData({pickerData: tempData, colorSelected, selectedSKU: 0});
        },

        sizeTap(event) {
            var that = this;
            var tempData = that.data.pickerData

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

            //将当前尺码置为选中状态
            var tempItem = event.currentTarget.dataset.key
            tempItem.selected = true
            tempData.view.sizeList[parseInt(event.currentTarget.id)] = tempItem

            //设置数量加减按钮启用状态
            tempData.view.plusButtonEnable = tempItem.storage_number > 1
            tempData.view.minusButtonEnable = false
            tempData.view.buyButtonEnable = true
            //将购买数量设为初始状态
            tempData.view.buyNumber = 1
            if (tempItem.storage_number <= 0) {
                this.triggerEvent('errorToast', {
                    title: '该尺码已经售罄',
                    titleClassName: 'wetoast-title',
                    duration: 1000
                })
                tempData.view.buyButtonEnable = false
            }
            console.log(111111);
            console.log(tempItem.product_sku)
            this.setData({
                pickerData: tempData,
                storageNumber: tempItem.storage_number,
                selectedSKU: tempItem.storage_number > 0 ? tempItem.product_sku : 0
            });
        },
        show() {
            this.setData({_show: true});
            this.triggerEvent('showChange');
        },
        hide(event) {
            if (event.target.id === "picker-bg") {
                this.setData({_show: true});
                this.triggerEvent('showChange');
            }
        },
        bindMinus() {
            let pickerData = this.data.pickerData;
            let buyNumber = parseInt(pickerData.view.buyNumber);
            if (buyNumber > 1) {
                buyNumber = buyNumber - 1;
            }
            pickerData.view.buyNumber = buyNumber;

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

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

        bindPlus() {
            if (!this.data.colorSelected) {
                if (this.data.pickerData.view.goodsList.length) {
                    this.data.pickerData.view.goodsList.map((value, index, array) => {
                        if (value.selected) {
                            this.setData({colorSelected: true})
                        }
                    })
                } else {
                    this.triggerEvent('errorToast', {
                        title: '请选择颜色',
                        titleClassName: 'wetoast-title',
                        duration: 1000
                    })
                    return;
                }
            }

            if (!this.data.selectedSKU) {
                this.triggerEvent('errorToast', {
                    title: '请选择尺码',
                    titleClassName: 'wetoast-title',
                    duration: 1000
                })
                return;
            }

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

            if (buyNumber >= this.data.storageNumber) {
                this.triggerEvent('errorToast', {
                    title: '对不起,没有更多库存了',
                    titleClassName: 'wetoast-title',
                    duration: 1000
                })
                return;
            }

            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});
        },
        goPayment() {
            this.triggerEvent('goPayment', this.data)
        },
        addShopCart() {
            this.triggerEvent('addShopCart', this.data)
        },
        chooseSizeColorCompleted() {
          this.triggerEvent('chooseSizeColorCompleted', this.data)
        }
    }
})