Item.js 15.6 KB

var productdetails = {
    productapiDomain:'http://itemapi.yohobuy.com',
    dataTypes:'jsonp',
    data:'',
    mine:'',
    product:'',
    init:function (product_id) {
        mine = this;
        product = product_id;
        $.ajax({
            dataType:mine.dataTypes,
            cache:true,
            jsonpCallback:'itemsp',
            data:{product_id:product_id, goods_id:goods_id},
            url:mine.productapiDomain+'/item/item/goods',
            success:function (result){
                if (typeof result.code != 'undefined' && result.code == 200) {
                    data = result.data;
                    mine.data = data;
                    if (data.is_mobile) {
                    	window.location.href='http://m.yohobuy.com' + window.location.pathname;
                    	return true;
                    }
                    mine.goodsimage(data.goods_id);
                    mine.salebtn(data.sku_storage);
                    mine.price(data.price, data.is_advance, data.expect_arrival_time);
                    mine.outlet(data.is_outlets, data.outlets_discount);
                    mine.promotion(data.promotions, data.banner);
                    mine.vipprice(data.price, data.uid, data.vip_level);
                    mine.size(parseInt(data.goods_id));
                    mine.goods(parseInt(data.goods_id));
                    mine.tags();
                    mine.favitembtn();
                    mine.favbrand();
                }
            }

        });
        $('.amount-add').click(mine.plus);
        $('.amount-subtract').click(mine.subtract);
    },
    outlet:function(is_outlet, outlet_discount) {
        if (is_outlet != "Y") {
            return false;
        }
        var tpl = $('#outlet').html();
        var outlet = juicer(tpl, {discount:outlet_discount*10});
        $('.info-activity').html(outlet);
    },
    promotion:function (promotions, banner) {
        if (banner == '' && promotions == '') {
            return false;
        }
        var tpl = $('#promotion').html();
        var pro = juicer(tpl, {banner:banner, promotion:promotions});
        $('.info-activity').html(pro);
    },
    toCart: function (product_id, goods_id, size_id, num, promotion_id, callback_func) {
        $.ajax({
            dataType:mine.dataTypes,
            type:'GET',
            data:{'product_id':product_id,goods_id:goods_id,size_id:size_id,num:num, promotion_id:promotion_id},
            url:'http://shopping.yohobuy.com/shopping/add/tocart',
            success:function (result) {
                callback_func(result);
            }
        });
    },
    salebtn:function (sku_storage) {
        var tpl = $('#sale-btn').html();
        var salebtn = juicer(tpl, {data:data, num:sku_storage});
        $('.info-submit').children('div:first').html(salebtn);
        $('.button-cart').click(function () {
            var goods_id = $('#goods_id').val();
            var size_id = $('#size_id').val();
            var sku = $('#sku_id').val();
            var size_name = $('#size_name').val();
            var storage = parseInt($('#storage').val());
            var num = parseInt($('#num').val());
            if (num > data.limit_num && data.limit > 0) {
                alert('限购商品!您只能购买'+data.limit_num+'件!');
                return true;
            }
            if (size_id == '') {
                $('#sizeList .warning-tips').show();
                return true;
            }
            if (num < 1) {
                num = 1;
                $('#num').val(1);
            }
            if (mine.data.attribute == 3) {
                window.open('http://shopping.yohobuy.com/ticket?product_sku='+sku+'&buy_number=1');
                return true;
            }
            if (storage < num) {
                alert('库存不足!');
                return true;
            }
            mine.toCart(product_id, goods_id, size_id, num, 0, mine.addToCart);
        });
        $('.button-supply').click(function () {
            var sku = $('#sku_id').val();
            if (sku == '') {
                $('#sizeList .warning-tips').show();
                return true;
            }

            var tpl = $('#mobile-message-tmpl').html();
            $('.info-other').hide();
            $('.info-buy').children('div').eq(0).siblings();
            $('.info-buy').children('div').eq(1).html(tpl).show().slideDown('fast').siblings().slideUp('fast');
            $('#notifyBtn').click(function(){
                var mobile = $('#mobile').val();
                if(mobile == ''){
                    return false;
                }
                if(!mobile.match(/^1[3|4|5|8][0-9]\d{4,8}$/)){
                    return false;
                }
                var goods_id = $('#goods_id').val();
                $.ajax({
                    dataType:mine.dataTypes,
                    url:mine.productapiDomain+'/item/item/notify',
                    data:{'mobile' : mobile, 'product_id':product_id, 'goods_id' : goods_id, 'product_sku' : $('#sku_id').val()},
                    success:function (result) {
                        var _data = result.data;
                        if(_data.result == 1 || _data.result == 2){
                            $('#supply').removeClass('button-supply').addClass('button-wait').html('<span class="ifont font16">T</span>请等待补货');
                            $('#notifyBtn').click(function(){
                                return false;
                            });
                            $('.info-other').show();
                            $('.info-buy').children('div').eq(1).siblings();
                            $('.info-buy').children('div').eq(0).slideDown('slow').siblings().slideUp('slow');
                            if(_data.result == 2){
                                alert('您已经提交了补货通知,请不要重复提交.');
                            }
                            return false;
                        }
                        alert('补货通知提交失败,请稍后再试');
                        return false;
                    }
                });
            });
        });
    },
    addToCart:function (result) {
        if (typeof result.code != 'undefined' && result.code == 200) {
            //1普通,2赠品,3虚拟,4特价
            $('.info-other').hide();
            $('.info-buy').children('div').eq(0).siblings();
            var tpl = $('#cart-result-tmpl').html();
            var cart_result = juicer(tpl, {result:result.data});
            $('.info-buy').children('div').eq(1).html(cart_result).slideDown('fast').siblings().slideUp('fast');

            $('#shopping').click(function(){
                $('.info-other').show();
                $('.info-buy').children('div').eq(1).siblings();
                $('.info-buy').children('div').eq(0).slideDown('fast').siblings().slideUp('fast');
                if ($('.info-buy').children('div').last().html() != '') {
                    $('.info-buy').append('<div id="resultBox" class="move-panel" style="display:block;"></div>').show();
                }
                $('.info-buy').children('div').last().show();
            });

        } else if (typeof result.code != 'undefined') {
            alert(result.message);
        }
    },
    favitembtn:function () {
        var tpl = $('#favitemtpl').html();
        var favitem_html = juicer(tpl, {data:data});
        $('#fav_item').html(favitem_html);
        if (data.is_favorite == 1) {
            $('.button-favor').on({
                mouseenter: function (){
                    $(this).html('<span class="ifont favored">f</span>取消收藏');
                },
                mouseout: function () {
                    $(this).html('<span class="ifont favored">f</span>已收藏');
                }
            });
        }
    },
    price:function (price, is_advance, expect_arrival_time) {
        var tpl = $('#priceTpml').html();
        var price_html = juicer(tpl, {price:price, is_advance:is_advance, expect_arrival_time:expect_arrival_time,data:data});
        $('.info-price').html(price_html);
    },
    vipprice:function(price, uid, vip_level) {
        var tpl = $('#vip-price').html();
        var vipprice_html = juicer(tpl, {price:price, uid:uid, vip_level:vip_level});
        $('.info-price').append(vipprice_html);
    },
    size:function (good_id) {
        if (data.goods_list.length == 0) {
            return true;
        }
        var tpl = $('#size-tpml').html();
        var size_html = juicer(tpl, {goods_id:good_id, goods:data.goods_list[good_id], status:data.status, erp_sku_id:data.sku, data:data});
        $('#sizeList').html(size_html);
        $('#sizeList a').not(':last').each(function(){
            $(this).click(function(){
                $('#sizeList a').removeClass('cur');
                $(this).addClass('cur');
                $('#sizeList').children('span').hide();
                var num = $(this).attr('num');
                mine.salebtn(num);
                var size = $(this).attr('size');
                var sizename = $(this).attr('sizename');
                var sku = $(this).attr('sku');
                $('#size_id').val(size);
                $('#sku_id').val(sku);
                $('#size_name').val(sizename);
                $('#storage').val(num);
                $('#num').val(1);
            });
        });
        if (data.attribute == 3) {
            $('#select_size_title').html('选日期:');
        }
    },
    goods:function(good_id) {
        if (data.goods_list.length == 0 ) {
            return true;
        }
        $('#goods_id').val(good_id);
        var tpl = $('#goods-tpml').html();
        var goods_html = juicer(tpl, {goods_id:good_id, goods_list:data.goods_list, status:data.status});
        $('#goodsList').html(goods_html);
        $('#colorBox a').each(function(){
            $(this).click(function(){
                var this_goods_id = $(this).attr('goods_id');
                $('#goods_id').val(this_goods_id);
                mine.goodsimage(this_goods_id);
                mine.size(this_goods_id);
                $('#colorBox a').removeClass('cur');
                $(this).addClass('cur');
                var num = $(this).attr('storage');
                mine.salebtn(num);
                var color_name = $(this).attr('color_name');
                $('.color dd:last-child').hide();
                $('#goodsColor').html('<span>颜色:</span>' + color_name);
                $('#num').val(1);
                $('#size_id').val('');
                $('#sku_id').val('');
                $('#size_name').val('');
                $('#storage').val('');
            });
        });
        $('#goodsColor').html('<span>颜色:</span>' + data.goods_list[good_id]['color_name']);
    },
    tags:function () {
        var tpl = $('#imageTags-tmpl').html();
        var tags_html = juicer(tpl, {data:data});
        $('.goods-tag-small').html(tags_html);
        if (data.is_yohood == 'Y') {
            $('#goods_tag_small').before('<div class="absolute yohood-tag"></div>');
        }
        if (data.can_buy == 'N') {
            $('.info-other').before('<div class="yohoodNotice"><h3><b>YO‘HOOD会场限定商品</b></h3><p>该商品将优先于9月20-21日上海活动会场售卖,您可在YO’HOOD展会现场通过APP扫描商品二维码进行购买。9月22日,可在线购买。</p></div>');
        }
    },
    goodsimage:function (goods_id) {
        if (data.image_list.length == 0) {
            return true;
        }
        try {
            var tpl = $('#image-tmpl').html();
            var img_html = juicer(tpl, {image_list:data.image_list[goods_id]['image']});
            $('#bigImage').attr('src', data.image_list[goods_id]['image'][0]['big']);
            $('.goods-img-small').html(img_html);
            $('.goods-img-small a').on({
                click:function () {
                    var big = $(this).attr('data');
                    $('#bigImage').attr('src', big);
                },
                mouseenter:function () {
                    var big = $(this).attr('data');
                    $('#bigImage').attr('src', big);
                }
            });
        } catch (err){
            
        }
    },
    favitem:function() {
        $.ajax({
            dataType:mine.dataTypes,
            url:mine.productapiDomain+'/item/item/favitem?product_id='+product,
            success:function (result) {
                if (result.code == 403) {
                    window.location.href = 'http://www.yohobuy.com/signin.html?refer='+window.location.href;
                } else if (result.code == 200) {
                    $('.button-favor').html('<span class="ifont favored">f</span>已收藏');
                    $('.button-favor').on({
                        mouseenter: function (){
                            $(this).html('<span class="ifont favored">f</span>取消收藏');
                        },
                        mouseout: function () {
                            $(this).html('<span class="ifont favored">f</span>已收藏');
                        }
                    });
                } else if (result.code == 300) {
                    $('.button-favor').html('<span class="ifont">f</span>添加收藏');
                    $('.button-favor').off('mouseenter mouseout');
                }
            }
        });
    },
    favbrand:function () {
        if (data.brand_fav == 1) {
            $('#brandFavor').addClass('favored');
        }
        $('#brandFavor').click(function () {
            if(window.event && window.event.stopPropagation ) {
                window.event.stopPropagation();
            } else {
                window.event.cancelBubble = true;
            }
            $.ajax({
                    dataType: mine.dataTypes,
                    url:mine.productapiDomain+'/item/item/favbrand?brand_id='+data.brand_id,
                    success:function(result) {
                        if (result.code == 200) {
                            $('#brandFavor').addClass('favored');
                        } else if (result.code == 300){
                            $('#brandFavor').removeClass('favored');
                        } else if (result.code == 403) {
                            window.location.href = 'http://www.yohobuy.com/signin.html?refer='+window.location.href;
                        }
                    }
            });
        });
    },
    plus:function() {
        var now = parseInt($('#num').val());
        var sku_storage = parseInt($('#storage').val());
        if (sku_storage <= now) {
            alert('库存不足!');
            return true;
        }
        if (data.attribute == 3 && now == 1) {
            alert('限购一件!');
            return true;
        }
        $('#num').val(parseInt(now) + 1);
    },
    subtract:function () {
        var now = $('#num').val();
        if (now <= 1) {
            $('#num').val(1);
            alert('已经是最后一件了!不要再减了~ T_T');
            return true;
        }
        $('#num').val(parseInt(now) - 1);
    },
    like:function() {
        $.ajax({
            dataType:mine.dataTypes,
            cache:true,
            jsonpCallback:'recommends',
            url:mine.productapiDomain+'/item/item/recommends',
            data:{'product_skn':product_skn},
            success:function (result) {
                var tpl = $('#like-tmpl').html();
                if (typeof result.code == 'undefined' || result.data == null || result.data.length == 0) {
                    return false;
                }
                var like_html = juicer(tpl, {goods_list:result.data, len:result.data.length-1});
                $('#like').html(like_html);
            }
        });
    }
};