detail.js 2.28 KB
const app = $('#app');
const tip = require('common/tip');

require('vue-swipe/dist/vue-swipe.css');

module.exports = {
    data() {
        return {
            intro: {},
            entity: {
                brand: {
                    brandName: '',
                    brandIco: ''
                },
                productPriceBo: {
                    formatMarketPrice: ''
                }
            },
            showFeatureSelector: false,
            cartCount: 0,

            /**
             * 加入购物车回调
             *
             * @param result
             */
            onAddToCart: (result)=> {
                // TODO: 库存不足
                // TODO: 商品已下架
                if (result.code === 200) {
                    this.cartCount = result.data.goods_count;
                    this.showFeatureSelector = false;
                } else {
                    this.showFeatureSelector = false;
                    tip('系统异常,请稍后重试');
                }
            }
        };
    },
    computed: {
        isSoldOut: function() {
            return this.entity.storage === 0;
        }
    },
    components: {
        imageCarousel: require('../image-carousel.vue'),
        featureSelector: require('component/product/feature-selector.vue'),
        showBox: require('../show-box.vue')
    },
    methods: {
        showAddToCart: function() {
            this.showFeatureSelector = true;
        }
    },
    created() {
        const self = this;

        // 显示商品特征选择组件
        this.$on('featureselector.close', function() {
            self.showFeatureSelector = false;
        });

        // 读取基础数据
        $.get(`/product/product_${app.data('pid')}_${app.data('goodsId')}.json`).then(result=> {
            this.entity = result;
            return result;
        }).then((result)=> {
            // 读取商品详情
            return $.get('/product/product/intro.json', {skn: result.productPriceBo.productSkn});
        }).then(result => {
            this.intro = result;
        });

        // 读取购物车数量
        $.get('/product/cart-count.json', {}).then(result=> {
            if (result.code === 200) {
                this.cartCount = result.data.cart_goods_count;
            }
        });
    }
};