Skip to content

Commit 49f6c3b

Browse files
authored
Merge pull request bailicangdu#53 from clarkdo/master
[加入,移除购物车]代码逻辑简化
2 parents 8d6db06 + f402888 commit 49f6c3b

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/store/mutations.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,13 @@ export default {
6262
stock
6363
}) {
6464
let cart = state.cartList;
65-
if (cart[shopid] && cart[shopid][category_id] && cart[shopid][category_id][item_id] && cart[shopid][category_id][item_id][food_id]) {
66-
cart[shopid][category_id][item_id][food_id]['num']++;
65+
let shop = cart[shopid] = (cart[shopid] || {});
66+
let category = shop[category_id] = (shop[category_id] || {});
67+
let item = category[item_id] = (category[item_id] || {});
68+
if (item[food_id]) {
69+
item[food_id]['num']++;
6770
} else {
68-
cart[shopid] = (cart[shopid] || {});
69-
cart[shopid][category_id] = (cart[shopid][category_id] || {});
70-
cart[shopid][category_id][item_id] = (cart[shopid][category_id][item_id] || {});
71-
cart[shopid][category_id][item_id][food_id] = {
71+
item[food_id] = {
7272
"num" : 1,
7373
"id" : food_id,
7474
"name" : name,
@@ -94,15 +94,18 @@ export default {
9494
specs,
9595
}) {
9696
let cart = state.cartList;
97-
if (cart[shopid] && cart[shopid][category_id] && cart[shopid][category_id][item_id] && cart[shopid][category_id][item_id][food_id]) {
98-
if (cart[shopid][category_id][item_id][food_id]['num'] > 0) {
99-
cart[shopid][category_id][item_id][food_id]['num']--;
97+
let shop = cart[shopid] = (cart[shopid] || {});
98+
let category = shop[category_id] = (shop[category_id] || {});
99+
let item = category[item_id] = (category[item_id] || {});
100+
if (item) {
101+
if (item[food_id]['num'] > 0) {
102+
item[food_id]['num']--;
100103
state.cartList = {...cart};
101104
//存入localStorage
102105
setStore('buyCart', state.cartList);
103106
} else {
104107
//商品数量为0,则清空当前商品的信息
105-
cart[shopid][category_id][item_id][food_id] = null;
108+
item[food_id] = null;
106109
}
107110
}
108111
},

0 commit comments

Comments
 (0)