-
Notifications
You must be signed in to change notification settings - Fork 5
/
shopcart.html
494 lines (451 loc) · 16.4 KB
/
shopcart.html
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>购物车页面</title>
<link rel="shortcut icon" href="img/foodie.ico"/>
<link href="AmazeUI-2.4.2/assets/css/amazeui.css" rel="stylesheet" type="text/css" />
<link href="css/demo.css" rel="stylesheet" type="text/css" />
<link href="css/cartstyle.css" rel="stylesheet" type="text/css" />
<link href="css/optstyle.css" rel="stylesheet" type="text/css" />
<link href="css/lee.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery-1.7.2.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="shopcart">
<!--顶部导航条 -->
<div class="am-container header">
<ul class="message-l">
<div class="topMessage">
<div class="menu-hd" v-show="!userIsLogin">
<a href="login.html" target="_top" class="h">亲,请登录</a>
<a href="register.html" target="_top">免费注册</a>
</div>
<div class="menu-hd" v-show="userIsLogin">
<span style="color: #d2364c">欢迎,{{userInfo.username}}</span>
</div>
</div>
</ul>
<ul class="message-r">
<div class="topMessage home">
<div class="menu-hd"><a href="index.html" target="_top" class="h">商城首页</a></div>
</div>
<div class="topMessage my-shangcheng">
<div class="menu-hd MyShangcheng"><a href="javascript:void(0);" @click="goUserCenter">
<i class="am-icon-user am-icon-fw"></i>个人中心</a></div>
</div>
</ul>
</div>
<!--悬浮搜索框-->
<div class="nav white">
<div class="logoBig">
<li><a href="index.html"><img src="img/logobig.png" /></a></li>
</div>
<div class="search-bar pr">
<a name="index_none_header_sysc" href="#"></a>
<form>
<input id="searchInput" v-model="keywords" name="index_none_header_sysc" type="text"
placeholder="搜索" autocomplete="off">
<input id="ai-topsearch" @click="doSearch" class="submit am-btn" value="搜索" index="1">
</form>
</div>
</div>
<div class="clear"></div>
<!--购物车 -->
<div class="concent" >
<div class="cart-attention" v-show="!userIsLogin" style="margin-top: 10px">
<i style="margin-right: 10px;"><img src="img/omg.png" class="img-ico"></i>
您还没有登录!登录后购物车的商品将同步保存到您账号中
<button class="login-quick" @click="goLogin">立即登录</button>
</div>
<div id="cartTable" style="margin-top: 30px">
<div class="cart-table-th">
<div class="wp">
<div class="th th-chk">
<div id="J_SelectAll1" class="select-all J_SelectAll"></div>
</div>
<div class="th th-item">
<div class="td-inner">商品信息</div>
</div>
<div class="th th-price">
<div class="td-inner">单价</div>
</div>
<div class="th th-amount">
<div class="td-inner">数量</div>
</div>
<div class="th th-sum">
<div class="td-inner">金额</div>
</div>
<div class="th th-op">
<div class="td-inner">操作</div>
</div>
</div>
</div>
<div class="clear"></div>
<tr class="item-list">
<div class="bundle bundle-last">
<div class="clear"></div>
<div class="bundle-main">
<ul class="item-content clearfix" v-for="(cartItem, sidx) in shopcartList">
<li class="td td-chk">
<div class="cart-checkbox ">
<input class="check" v-model="specIds" :value="cartItem.specId" type="checkbox">
</div>
</li>
<li class="td td-item" style="width: 50%">
<div class="item-pic">
<a :href="'item.html?itemId=' + cartItem.itemId" target="_blank" class="J_MakePoint">
<img :src="cartItem.itemImgUrl" class="itempic J_ItemImg" style="width: 80px; height: 80px;"></a>
</div>
<div class="item-info">
<div class="item-basic-info">
<a :href="'item.html?itemId=' + cartItem.itemId" target="_blank" class="item-title J_MakePoint">{{cartItem.itemName}}</a>
<div class="item-props" style="margin-top: 5px;">
<span class="sku-line">规格:{{cartItem.specName}}</span>
</div>
</div>
</div>
</li>
<li class="td td-price">
<div class="item-price price-promo-promo">
<div class="price-content">
<div class="price-line">
<em class="price-original">{{cartItem.priceNormal / 100}}</em>
</div>
<div class="price-line">
<em class="J_Price price-now" tabindex="0">{{cartItem.priceDiscount / 100}}</em>
</div>
</div>
</div>
</li>
<li class="td td-amount">
<div class="amount-wrapper ">
<div class="item-amount ">
<div class="sl">
{{cartItem.buyCounts}}
</div>
</div>
</div>
</li>
<li class="td td-sum">
<div class="td-inner">
<em tabindex="0" class="J_ItemSum number">{{cartItem.priceDiscount / 100 * cartItem.buyCounts}}</em>
</div>
</li>
<li class="td td-op">
<div class="td-inner">
<a href="javascript:void(0);" @click="delFromCart(cartItem.specId)" data-point-url="#" class="delete">删除</a>
</div>
</li>
</ul>
</div>
</div>
</tr>
<!-- {{specIds}} -->
<div class="clear"></div>
</div>
<div class="clear"></div>
<div class="float-bar-wrapper">
<div id="J_SelectAll2" class="select-all J_SelectAll">
<div class="cart-checkbox">
<input class="check-all check" name="select-all" v-model="allChecked" type="checkbox" @click="checkedAll">
</div>
<span>全选</span>
</div>
<div class="float-bar-right">
<div class="amount-sum">
<span class="txt">已选商品</span>
<em id="J_SelectedItemsCount">{{allSelectedCounts}}</em><span class="txt">件</span>
<div class="arrow-box">
<span class="selected-items-arrow"></span>
<span class="arrow"></span>
</div>
</div>
<div class="price-sum">
<span class="txt">合计:</span>
<strong class="price">¥<em id="J_Total">{{totalAmount / 100}}</em>元</strong>
</div>
<div class="btn-area" @click="goPay">
<a href="javascript:void(0);" id="J_Go" class="submit-btn submit-btn-disabled">
<span>结 算</span>
</a>
</div>
</div>
</div>
<div class="footer ">
<div class="footer-hd ">
<p>
<a href="https://github.com/dsying" target="_blank">Github</a>
<b>|</b>
<a href="https://juejin.im/user/5b0e60f6f265da08f0339162" target="_blank">掘金社区</a>
</p>
</div>
<div class="footer-bd "><p><em></em></p></div>
</div>
</div>
<!--操作页面-->
<div class="theme-popover-mask"></div>
</div>
<script type="text/javascript " src="js/app.js"></script>
<script type="text/javascript">
var index = new Vue({
el: "#shopcart",
data: {
userIsLogin: false,
userInfo: {},
keywords: null,
shopcartList: [],
specIds: [], // 选中的那些商品规格
allChecked: false, // chkbox是否全部选中
allSelectedCounts: 0, // 购物车中选则的几项
totalAmount: 0, // 购物车中选则的几项商品总价
},
created() {
// var me = this;
// 通过cookie判断用户是否登录
app.judgeUserLoginStatus(this);
// 从购物车中拿到商品的数量
this.shopcartItemCounts = app.getShopcartItemCounts();
// 渲染购物车商品数据到页面
this.renderShopcart();
},
watch: { //深度 watcher
'specIds': {
handler: function(val, oldVal) {
if (val.length === this.shopcartList.length) {
this.allChecked = true;
} else {
this.allChecked = false;
}
this.reCalItemsCountsAndAmount();
},
deep: true
}
},
methods: {
// 跳转用户中心
goUserCenter() {
// 判断用户是否登录
var userIsLogin = this.userIsLogin;
if (!userIsLogin) {
// 用户未登录,则跳转
window.location.href = "login.html?returnUrl=" + app.centerServerUrl;
} else {
window.location.href = app.centerServerUrl;
}
},
// 前往结算页面
goPay() {
var shopcartList = app.getShopcartList();
if (shopcartList.length <= 0) {
alert("购物车中没有商品,无法结算");
return;
}
var selectedItemSpecIds = this.specIds;
if (selectedItemSpecIds.length <= 0) {
alert("请至少在购物车中选择一个商品后再结算");
return;
}
// 判断是否登录
var userIsLogin = this.userIsLogin;
if (userIsLogin) {
var specIdsStr = selectedItemSpecIds.toString();
window.location.href = "pay.html?selectedItemSpecIds=" + specIdsStr;
} else {
var bool = window.confirm("请登录/注册后再进行结算操作噢~!");
if (!bool) {
return;
} else {
this.goLogin();
}
}
},
// 从购物车中删除商品
delFromCart(specId) {
// console.log(specId);
var bool = window.confirm("确认从购物车中移除该商品吗?");
if (!bool) {
return;
}
// 删除cookie中的商品
var shopcartList = app.getShopcartList();
for (var i = 0 ; i < shopcartList.length ; i ++) {
var tmpItem = shopcartList[i];
if (tmpItem.specId == specId) {
shopcartList.splice(i, 1);
break;
}
}
// 重新放入cookie,更新一下
app.setCookie("shopcart", JSON.stringify(shopcartList));
this.shopcartList = shopcartList;
// 清除选中项
var specIds = this.specIds;
for (var i = 0 ; i < specIds.length ; i++) {
var tmpSpecId = specIds[i];
if (specId == tmpSpecId) {
specIds.splice(i, 1);
}
}
this.reCalItemsCountsAndAmount();
// 如果用户是已经登录状态,需要再把redis中的购物车商品删除
var userIsLogin = this.userIsLogin;
if (userIsLogin) {
var userInfo = this.userInfo;
var serverUrl = app.serverUrl;
axios.defaults.withCredentials = true;
axios.post(
serverUrl + '/shopcart/del?userId=' + userInfo.id + '&itemSpecId=' + specId,
{},
{
headers: {
'headerUserId': userInfo.id,
'headerUserToken': userInfo.userUniqueToken
}
})
.then(res => {
if (res.data.status == 200) {
} else if (res.data.status == 500) {
alert(res.data.msg);
}
});
}
},
// 重新计算购物车中选中的件数以及总价格
reCalItemsCountsAndAmount() {
var specIds = this.specIds;
if (specIds == null || specIds == '' || specIds == undefined || specIds.length == 0) {
this.allSelectedCounts = 0;
this.totalAmount = 0;
} else {
// 把specIds和cookie中的购物车进行对比
this.allSelectedCounts = specIds.length;
var shopcartList = app.getShopcartList();
var totalAmount = 0;
// debugger;
for (var i = 0 ; i < shopcartList.length ; i ++) {
var tmpItem = shopcartList[i];
for (var j = 0 ; j < specIds.length ; j ++) {
var selectSpecId = specIds[j];
if (tmpItem.specId == selectSpecId) {
totalAmount += tmpItem.priceDiscount * tmpItem.buyCounts;
}
}
}
this.totalAmount = totalAmount;
}
},
// 全选与反选
checkedAll() {
var _this = this;
// console.log(_this.specIds);
// console.log(_this.allChecked);
if (_this.allChecked) { //实现反选
_this.specIds = [];
} else { //实现全选
_this.specIds = [];
this.shopcartList.forEach(function(item, index) {
_this.specIds.push(item.specId);
});
}
this.reCalItemsCountsAndAmount();
},
// 渲染购物车
renderShopcart() {
// 获取购物车列表
var shopcartList = app.getShopcartList();
// console.log(shopcartList);
// this.shopcartList = shopcartList;
if (shopcartList.length <= 0) {
return;
}
// 刷新购物车中价格,以防长时间未登录网址,价格发生变动
// 拼接规格ids --> 1001,2002,3003,4004
var itemSpecIds = "";
for (var i = 0 ; i < shopcartList.length ; i ++) {
var tmpSpecId = shopcartList[i].specId;
itemSpecIds += tmpSpecId;
if (i < shopcartList.length - 1) {
itemSpecIds += ",";
}
}
// 传递购物车内所有产品的规格ID, 请求后端获得最新数据, 更新购物车原有数据的信息
var serverUrl = app.serverUrl;
axios.defaults.withCredentials = true;
axios.get(
serverUrl + '/shopcart/refresh?itemSpecIds=' + itemSpecIds,
{},)
.then(res => {
if (res.data.status == 200) {
var newItemList = res.data.data;
// 删除现有购物车cookie
app.deleteCookie("shopcart");
// console.log(newItemList);
// 拿到最新商品数据以后,重新组合成购物车数据
for (var i = 0 ; i < newItemList.length ; i ++) {
var tmpNewItem = newItemList[i];
var tmpNewItemSpecId = tmpNewItem.specId;
// 获取老购物车内该规格id 产品的数量
var buyCounts = this.getBuyCountsFromCookieShopcartList(shopcartList, tmpNewItemSpecId);
// console.log(buyCounts);
// 构建购物车商品对象
var shopcartItem = new app.ShopcartItem(tmpNewItem.itemId,
tmpNewItem.itemImgUrl,
tmpNewItem.itemName,
tmpNewItem.specId,
tmpNewItem.specName,
buyCounts,
tmpNewItem.priceDiscount,
tmpNewItem.priceNormal);
// 添加商品至购物车list
app.addItemToShopcart(shopcartItem);
}
// 重新获取cookie中的商品list渲染到页面
shopcartList = app.getShopcartList();
// console.log(shopcartList);
this.shopcartList = shopcartList;
} else if (res.data.status == 500) {
alert(res.data.msg);
}
});
},
// 根据规格id获取 购物车中该商品的数量
getBuyCountsFromCookieShopcartList(shopcartList, specId) {
for (var i = 0 ; i < shopcartList.length ; i ++) {
var tmpSpecId = shopcartList[i].specId;
if (tmpSpecId == specId) {
return shopcartList[i].buyCounts;
}
}
},
// 跳转购物车页面
goToShopcart() {
window.location.href = "shopcart.html";
},
// 点击搜索
doSearch() {
var keywords = this.keywords;
// console.log(keywords);
if (keywords == null || keywords == undefined || keywords == '') {
alert("搜索内容不能为空");
return;
}
// window.location.href = "search.html?keywords=" + keywords;
window.open("search.html?keywords=" + keywords);
},
// 跳转登录页面
goLogin() {
window.location.href = "login.html?returnUrl=shopcart.html";
},
// 打开商品详情页面
showItemDetail(itemId){
window.open("item.html?itemId=" + itemId);
},
}
});
</script>
</body>
</html>