page-render.js 5.8 KB

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

module.exports = function(callback) {
    let productId = $('#productId').val();
    let goodsId = $('#goodsId').val();
    let productSkn = $('#productSkn').val();

    if (productId && (goodsId || productSkn)) {
        $.ajax({
            type: 'GET',
            url: '/product/detail/newinfo',
            data: {
                id: productId,
                goodsId: goodsId,
                productSkn: productSkn
            },
            success: function(data) {

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

                // let regProSeckill = /\/product\/seckill\/pro_([\d]+)_([\d]+)/;
                let thisHref = window.location.href;
                let 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); // eslint-disable-line
                callback(data);
            }
        });
    } else {
        callback(); // eslint-disable-line
    }
};
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(data.studentPrice);
    } else if (data.vipLevel) {
        let li = $('.vip-level>li').first().remove();

        for (let i = 0; i < data.vipLevel.list.length; i++) {
            let item = data.vipLevel.list[i];
            let 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) {

        let shortText = $('#goodsDiscount>.short-text').remove();
        let liText = $('#goodsDiscount>.discount-folder>.folder-item').remove();

        for (let i = 0; i < data.goodsDiscount.list.length; i++) {
            let discount = data.goodsDiscount.list[i];
            let 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();
}