header_top.js 10.3 KB
$(function () {
    //渲染hello头
    jQuery.ajax({
        type: "GET",
        url: "https://passport.nong12.com/uc/ws/helloService",
        dataType: "jsonp",
        cache: false,
        success: function (data) {
            if (data) {
                if (data.info.indexOf("logout") > 0) {
                    $(".top .left").html("");
                    $(".top .left").append(data.info);
                    if (window.pageConfig) {
                        if (window.pageConfig.navId && window.pageConfig.navId == "home") {
                            $(".pack-mo-tr1").find(".btn-big-y").each(function () {
                                $(this).remove();
                            });
                            $(".pack-mo-tr1").prepend(
                                '<a class="btn-big-w" href="http://user.nong12.com/purchase/order_pay.html"><img width="14" height="14" src="http://static.nong12.com/static/common/images/i/obligation.png"> 待付款</a>'
                                + '<a class="btn-big-w" href="http://user.nong12.com/purchase/order_delivery.html"><img width="16" height="15" src="http://static.nong12.com/static/common/images/i/picking.png"> 待提货</a>').addClass("p-l1");
                        }
                    }

                    $(".top .left").append('<a class="h-user-msg" href="#" id="notify"></a>');
                    IM.notification($("#notify"));
                } else {
                    $(".top .left a").first().attr("href", DiliPath.loginPath + "?ReturnUrl=" + window.location.href);
                    $(".top .left a").last().attr("href", DiliPath.registPath);
                }
            }
        },
        error: function () {
            return false;
        }
    });

    //渲染footer
    jQuery.ajax({
        type: "GET",
        url: DiliPath.homePath + "common/footerJson.html",
        dataType: "jsonp",
        jsonp: "callFn",
        jsonpCallback: 'success_jsonpCallback1' + (new Date()).getTime(),
        success: function (data) {
            var html = [];
            for (var i = 0; i < data.length; i++) {
                html.push("<li><i class='m-icon icon-a" + (i + 1) + "'></i> <span>" + data[i].title + "</span><br>");
                var vc = eval(data[i].extendsOne);
                for (var j = 0; j < vc.length; j++) {
                    if (j == vc.length - 1) {
                        html.push("<a href='" + vc[j].link + "'>" + vc[j].text + "</a></li>");
                    } else {
                        html.push("<a href='" + vc[j].link + "'>" + vc[j].text + "</a><br>");
                    }
                }
            }
            $(".m-left ul").empty().append(html.join(""));
        }
    });

    //迷你购物车加载
    if (window.location.href.indexOf("cartList") < 0 && window.location.href.indexOf("purchase_list") < 0) {
        cartTools.showCart();
    }
    $(".mo-fn3 > a").hover(function () {
        cartTools.showCart();
    }, function () {
    });

    if ($("head").find("script[src*='jquery.lazyload']").size() > 0) {
        $("body").find("img").each(function () {
            $(this).lazyload({
                effect: "fadeIn",
                skip_invisible: false
            });
        });
    }

});
//公用系统域名
window.DiliPath = {
    homePath: "http://www.nong12.com/",
    userPath: "http://user.nong12.com/",
    staticPath: "http://static.nong12.com/",
    shopPath: "http://shop.nong12.com/",
    loginPath: "https://passport.nong12.com/uc/login.aspx",
    registPath: "https://passport.nong12.com/regedit/regedit.aspx",
    packageProductPath: function (productId) {
        return DiliPath.homePath + "product/" + productId + ".html";
    }
};

window.Money = {
    centToYuan: function (cent) {
        cent = parseInt(cent);
        if (cent <= 0) {
            return "0.00";
        }
        yuan = (cent / 100).toFixed(2);
        var result = yuan.toString();
        if (result.indexOf(".") <= 0) {
            result = result + ".00";
        }
        return result;
    }
};

var cartTools = {
    /**
     * 加入购物车addrId:交割地 amount:数量 price:单价 sku:skuId
     * @param addrId
     * @param amount
     * @param price
     * @param sku
     */
    addCart: function (data, fn) {
        var data = JSON.stringify(data);
        jQuery.ajax({
            url: DiliPath.homePath + "cart/addCart.html",
            type: "POST",
            data: {cartJson: data},
            dataType: "jsonp",
            jsonp: 'callFn',
            jsonpCallback: 'success_jsonpCallback' + (new Date()).getTime(),
            cache: false,
            success: function (result) {
                if (fn && typeof(fn) == 'function') {
                    fn(result);
                }
                cartTools.showCart();
            }
        });
    },
    /**
     *  渲染迷你购物车
     */
    showCart: function () {
        jQuery.ajax({
            url: DiliPath.homePath + "cart/getMiniCart.html",
            type: "POST",
            dataType: "jsonp",//数据类型为jsonp
            jsonp: 'callFn',
            jsonpCallback: 'success_jsonpCallback' + (new Date()).getTime(),
            cache: true,
            success: function (miniCart) {
                if (miniCart) {
                    $(".receipt-icon").html(miniCart.length);
                    if ($(".hover-bill").length > 0) {
                        $(".a-tit").siblings("b").html("(" + miniCart.length + ")").attr(
                            "count", miniCart.length);
                        var miniCartHtml = [];
                        var totalPrice = 0;
                        if (miniCart.length == 0) {
                            $(".hover-bill .u-con-no").remove();
                            $(".hover-bill").append('<div class="u-con-no">购物车中还没有商品,赶紧选购吧!</div>');
                            $(".hover-bill ul, .tab-xd, .til").hide();
                        } else {
                            $(".hover-bill ul, .tab-xd, .til").show();
                            for (var i = 0; i < miniCart.length; i++) {
                                var productImg = miniCart[i].productImage ? miniCart[i].productImage.replace(/i[0-5]/g, 'i' + 4) : '';
                                miniCartHtml[i] = '<li price="' + miniCart[i].price + '" amount="' + miniCart[i].amount + '">';
                                miniCartHtml[i] += '<img class="img" src="' + productImg + '" width="40" height="40" />';
                                miniCartHtml[i] += '<div class="cont"><div class="cont-head"><p><a href="' + DiliPath.homePath + 'product/' + miniCart[i].pid + '.html">' + miniCart[i].productName + '</a></p>';
                                miniCartHtml[i] += '<p>提货点:' + miniCart[i].addrName + '</p></div>';
                                miniCartHtml[i] += '<p class="del" id="' + miniCart[i].sku + '" addrId="' + miniCart[i].addrId + '"><span></span></p><p>';
                                var diliServiceResps = miniCart[i].diliServiceResps || [];
                                for (var j = 0; j < diliServiceResps.length; j++) {
                                    miniCartHtml[i] += '<img src="' + diliServiceResps[j].icon + '">';
                                }
                                miniCartHtml[i] += '<span class="price">' + miniCart[i].amount + ' × ' + Money.centToYuan(miniCart[i].price) + '</span>';
                                miniCartHtml[i] += '<span>元/' + miniCart[i].saleUnit + '</span></p>';
                                miniCartHtml[i] += '</div></li>';
                                totalPrice += miniCart[i].price * miniCart[i].amount;
                            }
                            $(".hover-bill .u-con-no").remove();
                            $(".hover-bill ul").empty();
                            $(".hover-bill ul").append(miniCartHtml.join(""));
                            $(".hover-bill ul .del").each(function () {
                                $(this).click(function () {
                                    cartTools.delMiniCart($(this).attr("id"), $(this).attr("addrId"));
                                    return false;
                                });
                            });
                            $(".tab-xd span").empty();
                            $(".tab-xd span").html("¥" + Money.centToYuan(totalPrice));
//	                             overflow_y($('.hover-bill ul'), miniCart.length * 71, 355);
                        }
                    }
                }
            }
        });
    },
    /**
     * 删除迷你购物车中的sku
     * @param skuId
     * @param addrId
     */
    delMiniCart: function (skuId, addrId) {
        var self = this;
        jQuery.ajax({
            url: window.DiliPath.homePath + "cart/delSkuFromCart.html",
            type: "POST",
            dataType: "jsonp",//数据类型为jsonp
            jsonp: "callFn",//服务端用于接收callback调用的function名的参数
            jsonpCallback: 'success_jsonpCallback' + (new Date()).getTime(),
            data: {sku: skuId, addrId: addrId},
            async: false,
            success: function (result) {
                if (result) {
                    if (result.flag) {
                        self.showCart();
                    }
                }
            }
        });
    },
    /**
     * 计算购物车价格
     */
    compute: function () {
        var totalPrice = 0;
        $(".hover-bill ul li").each(function () {
            totalPrice += parseFloat($(this).attr("price")) * parseInt($(this).attr("amount"));
        });
        return Money.centToYuan(totalPrice);
    }
};

var otherTools = {
    /**
     * im
     * @param sellerType
     * @param sellerName
     */
    imConnection: function (sellerType, sellerName) {
        var type = 2;//2默认客服
        var name = '地利客服';
        //第3方
        if (sellerType == 30) {
            type = 0;
            name = sellerName;
        }

        var timestamp = (new Date()).valueOf();
        var ht = (screen.height - 500) / 2;
        var wt = (screen.width - 800) / 2;
        var url = 'http://chat.nong12.com/launch/launch.action?uid=' + name + '&type=' + type + '&_t=' + timestamp;
        var param = 'height=555,width=800,resizable=yes,top=' + ht + ',left=' + wt;
        window.open(url, name, param);
    }
};