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

    properties: {
        pickerData: {
            type: Object,
            value: null,
            observer: function (value, oldValue) {
                if (!value.sourceType)
                    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: ''},
    methods: {
        colorTap(event) {
            var that = this;
            var tempData = that.data.pickerData;
            //先遍历将所有颜色、尺码置为未选中状态
            // console.log('color')
            // console.log(tempData.view.goodsList)
            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});
        },

        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.wetoast.toast({
                    title: '该尺码已经售罄',
                    titleClassName: 'wetoast-title',
                    duration: 1000
                });

                tempData.view.buyButtonEnable = false
            }
            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) {
                this.wetoast.toast({
                    title: '请选择颜色',
                    titleClassName: 'wetoast-title',
                    duration: 1000
                });

                return;
            }

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

                return;
            }

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

            if (buyNumber >= this.data.storageNumber) {
                this.plusReachedMaxAction();
                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)
        }
    }
})