page-render.js 4.26 KB

var $ = require('yoho-jquery'),
    chosePanel = require('../../common/chose-panel'),
    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) {
                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.limitNotForSale) {
            $('#limitNotForSale').removeClass(dbClass);
        }
        if (data.cartInfo.canBuyLimit) {
            $('.can-buy-limit').removeClass(dbClass);
        }
        if (data.cartInfo.noLimitCode) {
            $('#noLimitCode').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) {
        $('#likeBtn').addClass('liked');
    }
    if (data.tickets) {
        $('#buyNowForm').attr('action', data.ticketsConfirm).removeClass(dbClass);
    }
    if (data.loginUrl) {
        $('#loginUrl').val(data.loginUrl).removeClass(dbClass);
    }
    chosePanel(data);
    $('.' + dbClass).remove();
}