page-render.js 5.72 KB

var $ = require('yoho-jquery'),
    chosePanel = require('../../common/chose-panel'),
    tip = require('../../plugin/tip'),
    dbClass = 'data-bind';

module.exports = function(callback) {
    var productId = $('#productId').val();
    var goodsId = $('#goodsId').val();
    var productSkn = $('#productSkn').val();
    if (productId && (goodsId || productSkn)) {
        $.ajax({
            type: 'POST',
            url: '/product/detail/info',
            data: {
                id: productId,
                goodsId: goodsId,
                productSkn: productSkn
            },
            success: function(data) {

                // 如果当前是秒杀商品,且不在秒杀路径下,跳到该商品的秒杀详情页
                var reg = /\/product\/show_([\d]+)/;
                var regPro = /\/product\/pro_([\d]+)_([\d]+)/;
                var regSeckill = /\/product\/seckill/;

                // var regProSeckill = /\/product\/seckill\/pro_([\d]+)_([\d]+)/;
                var thisHref = window.location.href;
                var thisRefer = document.referrer;

                // 如果秒杀商品没有吊牌价,显示原销售价
                if (data.isSecKill === 'Y' && !data.cartInfo.price) {
                    $('.previous-price').text(data.cartInfo.salePrice);
                }

                if (!regSeckill.test(thisRefer)) {
                    if (data.isSecKill === 'Y' && (reg.test(thisHref) || regPro.test(thisHref))) {
                        window.location.replace('/product/seckill/show_' + $('#productSkn').val() + '.html');
                    }
                }

                render(data);
                callback(data);
            }
        });
    } else {
        callback();
    }
};
function render(data) {
    if (data.goodsPrice) {
        $('.goods-price>.current-price').text(data.goodsPrice.currentPrice);
        $('.goods-price>.previous-price').text(data.goodsPrice.previousPrice);
        $('.goods-price').removeClass(dbClass);
    }
    if (!data.noLimitGoodsBtn) {
        if (data.canGetLimitCode) {
            $('.data-can-get-limit-code').removeClass(dbClass);
        }
        if (data.codeEmpty) {
            $('.data-code-empty').removeClass(dbClass);
        }
        if (data.gotCode) {
            $('.data-got-code').removeClass(dbClass);
        }
    }
    if (data.studentPrice) {
        $('.price-date').removeClass(dbClass);
        $('.student-value').text('¥' + Math.round(studentPrice));
    } else if (data.vipLevel) {
        var li = $('.vip-level>li').first().remove();
        for (var i = 0; i < data.vipLevel.list.length; i++) {
            var item = data.vipLevel.list[i];
            var itemLi = li.clone();
            if (item.currentLevel) {
                itemLi.addClass('current-level');
            }
            itemLi.find('.vip-price').text(item.text);
            $('.vip-level').append(itemLi);
        }
        $('.vip-level').removeClass(dbClass);
    }
    if (data.goodsDiscount && data.goodsDiscount.list && data.goodsDiscount.list.length) {

        var shortText = $('#goodsDiscount>.short-text').remove();
        var liText = $('#goodsDiscount>.discount-folder>.folder-item').remove();
        for (var i = 0; i < data.goodsDiscount.list.length; i++) {
            var discount = data.goodsDiscount.list[i];
            var itemText = i === 0 ? shortText.clone() : liText.clone();
            if (discount.text) {
                if (i === 0) {
                    itemText.text(discount.text).append('<span class="icon-down iconfont dropdown">&#xe609;</span>').prependTo('#goodsDiscount');
                } else {
                    itemText.text(discount.text).appendTo('#goodsDiscount>.discount-folder');
                }
            }
        }
        $('#goodsDiscount').removeClass('data-bind');
    }
    if (data.cartInfo) {
        $('.num-incart').attr('href', data.cartInfo.cartUrl);
        if (data.cartInfo.addToCartUrl) {
            $('.add-to-cart-url').text(data.tickets ? '立即购买' : '加入购物车').removeClass(dbClass);
        }
        if (data.cartInfo.soldOut) {
            $('#soldOut').removeClass(dbClass);
        }
        if (data.cartInfo.notForSale) {
            $('#notForSale').removeClass(dbClass);
        }
        if (data.cartInfo.limitNotForSale) {
            $('#limitNotForSale').removeClass(dbClass);
        }
        if (data.cartInfo.canBuyLimit) {
            $('.can-buy-limit').removeClass(dbClass);
        }
        if (data.cartInfo.noLimitCode) {
            $('#noLimitCode').removeClass(dbClass);
        }
        if (data.cartInfo.canNotBuy) {
            $('#noLimitCode').removeClass(dbClass);
        }
        if (data.cartInfo.preSale) {
            $('#preSale').removeClass(dbClass);
        }
        $('.cart-bar').removeClass('data-bind');
        $('#limitCodeUrl').val(data.cartInfo.limitCodeUrl);
        $('#limitProductPay').val(data.cartInfo.limitProductPay);
        if (data.cartInfo.limitProductCode) {
            $('#limitProductCode').val(data.cartInfo.limitProductCode).removeClass(dbClass);
        }
    }
    if (data.isCollect === true) {
        $('#likeBtn').addClass('liked');
    }
    if (data.tickets) {
        $('#buyNowForm').attr('action', data.ticketsConfirm).removeClass(dbClass);
    }
    if (data.loginUrl) {
        $('#loginUrl').val(data.loginUrl).removeClass(dbClass);
    }

    // 定金预售商品
    if (data.isDepositAdvance === 'Y') {
        setTimeout(function() {
            $('#addtoCart').text('立即购买').off('touchstart').on('touchstart', function() {
                tip.show('定金预售商品只能在APP端购买');
                return false;
            });
        }, 200);
    } else {
        chosePanel(data);
    }

    $('.' + dbClass).remove();
}