minicart.js
4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
$(function(){
//迷你购物车加载
if(window.location.href.indexOf("cartList") < 0 && window.location.href.indexOf("purchase_list") < 0) {
cartTools.showCart();
}
});
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) {
$(".cart-num").html(miniCart.length);
$(".show i").html(miniCart.length);
if($(".hover-micat").length > 0){
var miniCartHtml = [];
var totalPrice = 0;
if (miniCart.length == 0) {
$(".hover-micat .micat-on").remove();
$(".hover-micat").append('<div class="micat-on clearfix"><i class="u-con-no"></i>进货单中还没有商品,赶紧选购吧~</div>');
$(".hover-micat ul, .tab-xd, .til").hide();
} else {
$(".hover-micat 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] += '<a href="' + DiliPath.homePath + 'product/' + miniCart[i].pid + '.html"><img class="img" src="'+productImg+'" width="40" height="40" /></a>';
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] += '</div><p class="shop-title">店铺:'+miniCart[i].shopName+'</p>';
miniCartHtml[i] += '<p><span>'+Money.centToYuan(miniCart[i].price)+'元/'+miniCart[i].saleUnit+' × '+miniCart[i].amount+'</span>';
miniCartHtml[i] += '<span class="price">'+Money.centToYuan(miniCart[i].price* miniCart[i].amount)+'元</span></p>';
miniCartHtml[i] += '<p class="del" id="'+ miniCart[i].id + '" sku="'+miniCart[i].sku+'"><span></span></p>';
miniCartHtml[i] += '</div></li>';
totalPrice += miniCart[i].price* miniCart[i].amount;
}
$(".hover-micat .micat-on").remove();
$(".hover-micat ul").empty();
$(".hover-micat ul").append(miniCartHtml.join(""));
$(".hover-micat ul .del").each(function () {
$(this).click(function () {
cartTools.delMiniCart($(this).attr("id"),$(this).attr("sku"));
return false;
});
});
$(".tab-xd span").empty();
$(".tab-xd span").html("¥" + Money.centToYuan(totalPrice));
}
}
}
}
});
},
/**
* 删除迷你购物车中的sku
* @param cartId
*/
delMiniCart : function(cartId,sku) {
var self = this;
if(cartId=='undefined'){
cartId = '';
}
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 : {cartId:cartId,sku:sku},
async : false,
success : function(result) {
if (result) {
if (result.flag) {
self.showCart();
}
}
}
});
},
/**
* 计算购物车价格
*/
compute : function(){
var totalPrice = 0;
$(".hover-micat ul li").each(function() {
totalPrice += parseFloat($(this).attr("price")) * parseInt($(this).attr("amount"));
});
return Money.centToYuan(totalPrice);
}
};