diff --git a/app/app.wxss b/app/app.wxss index 74fd1f1..2fda36c 100644 --- a/app/app.wxss +++ b/app/app.wxss @@ -65,6 +65,9 @@ button[type="primary"][plain] { color: #ff5801; border-color: #ff5801; } +button[type="primary"][disabled] { + background-color: #ffa97c; +} .button-hover[type=primary], .button-hover.weui-btn_primary { color: rgba(255, 255, 255, 0.8); diff --git a/app/images/cart-empty@2x.png b/app/images/cart-empty@2x.png new file mode 100644 index 0000000..89c5edf Binary files /dev/null and b/app/images/cart-empty@2x.png differ diff --git a/app/images/cart-full@2x.png b/app/images/cart-full@2x.png new file mode 100644 index 0000000..8f2b2a3 Binary files /dev/null and b/app/images/cart-full@2x.png differ diff --git a/app/images/decrease@2x.png b/app/images/decrease@2x.png new file mode 100644 index 0000000..1a0f526 Binary files /dev/null and b/app/images/decrease@2x.png differ diff --git a/app/images/delete@2x.png b/app/images/delete@2x.png new file mode 100644 index 0000000..8bc6bcb Binary files /dev/null and b/app/images/delete@2x.png differ diff --git a/app/images/increase@2x.png b/app/images/increase@2x.png new file mode 100644 index 0000000..98ebe18 Binary files /dev/null and b/app/images/increase@2x.png differ diff --git a/app/pages/shop/show.js b/app/pages/shop/show.js index afff6eb..c108594 100644 --- a/app/pages/shop/show.js +++ b/app/pages/shop/show.js @@ -1,6 +1,14 @@ // pages/shop/show.js var sliderWidth = 96; +var initOrder = { + totalNum: 0, + totalPrice: 0, + totalGoodsPrice: 0, + totalPackingFee: 0, + goodsNums: {}, + goods: [] +} Page({ data: { @@ -10,14 +18,9 @@ Page({ sliderLeft: 0, activeMenuIndex: 0, + showCart: false, - order: { - totalPrice: 0, - totalNum: 0, - totalPackingFee: 0, - goodsNums: {}, - goods: [] - }, + order: initOrder, info: { "seller_id": "2", @@ -103,7 +106,7 @@ Page({ "pic_hd2": null, "detail": "鸡翅饭 xx", "price": "20.00", - "packing_fee": "0.00", + "packing_fee": "2.00", "sales": "41", "praise": "0", "stock": "59", @@ -314,58 +317,56 @@ Page({ }) }, - addGoodsNum(order, goodsId, subId, num = 1) { + addGoodsNum(order, item) { var {goods, goodsNums} = order - var item; - if (subId) { - item = goods.find(item => { - return item['goods_id'] == goodsId && item['subId'] == subId + var {goods_id, sub_id, num} = item + var itemIndex; + if (sub_id) { + itemIndex = goods.findIndex(item => { + return item['goods_id'] == goods_id && item['sub_id'] == sub_id }) - if (item) { - item['num'] += num - } else { - goods.push({ - 'goods_id': goodsId, - 'sub_id': subId, - 'num': num - }) - } } else { - item = goods.find(item => { - return item['goods_id'] == goodsId + itemIndex = goods.findIndex(item => { + return item['goods_id'] == goods_id }) - if (item) { - item['num'] += num - } else { - goods.push({ - 'goods_id': goodsId, - 'num': num - }) - } } - if (goodsNums[goodsId]) { - goodsNums[goodsId] += num + if (itemIndex >= 0) { + goods[itemIndex]['num'] += num } else { - goodsNums[goodsId] = num + goods.push(item) + } + if (goodsNums[goods_id]) { + goodsNums[goods_id] += num + } else { + goodsNums[goods_id] = num } }, - removeGoodsNum(order, goodsId, subId, num = 1) { + removeGoodsNum(order, item) { var {goods, goodsNums} = order - if (subId) { - for(let i=0, len=goods.length; i { + return item['goods_id'] == goods_id && item['sub_id'] == sub_id + }) } else { - + itemIndex = goods.findIndex(item => { + return item['goods_id'] == goods_id + }) } - if (goodsNums[goodsId] > 1) { - goodsNums[goodsId] -= num + if (itemIndex >= 0) { + item = goods[itemIndex] + if (item.num > 1) { + item.num -= num + } else { + goods.splice(itemIndex, 1) + } + } + if (goodsNums[goods_id] > 1) { + goodsNums[goods_id] -= num } else { - delete goodsNums[goodsId] + delete goodsNums[goods_id] } }, @@ -373,16 +374,23 @@ Page({ var {order, info: {menus}} = this.data; var {menuIndex, goodsIndex, subIndex} = e.currentTarget.dataset; var goods = menus[menuIndex].goods2[goodsIndex]; - var goods_id = goods['goods_id'] - var sub_id; + var {goods_id, goods_name} = goods if (subIndex >= 0) { goods = goods.sub_goods[subIndex]; - sub_id = goods['sub_id'] + var {sub_id, sub_name} = goods } order.totalNum += 1; - order.totalPrice += +goods.price; + order.totalGoodsPrice += +goods.price; order.totalPackingFee += +goods.packing_fee; - this.addGoodsNum(order, goods_id, sub_id) + order.totalPrice = order.totalGoodsPrice + order.totalPackingFee; + this.addGoodsNum(order, { + goods_id, goods_name, + sub_id, sub_name, + price: goods.price, + packing_fee: goods.packing_fee, + menuIndex, goodsIndex, subIndex, + num: 1 + }) this.setData({ order }) @@ -398,11 +406,39 @@ Page({ sub_id = goods['sub_id'] } order.totalNum -= 1; - order.totalPrice -= +goods.price; + order.totalGoodsPrice -= +goods.price; order.totalPackingFee -= +goods.packing_fee; - this.removeGoodsNum(order, goods_id, sub_id) + order.totalPrice = order.totalGoodsPrice + order.totalPackingFee; + this.removeGoodsNum(order, { + goods_id, sub_id, + num: 1 + }) this.setData({ order }) + if (order.totalNum == 0) { + this.hideCart() + } + }, + clearCart(e) { + this.setData({ + order: initOrder, + showCart: false + }) + }, + hideCart(e) { + this.setData({ + showCart: false + }) + }, + toggleCart(e) { + var {showCart, order: {totalNum}} = this.data + + if (totalNum <= 0) { + return; + } + this.setData({ + showCart: !showCart + }) } }) \ No newline at end of file diff --git a/app/pages/shop/show.wxml b/app/pages/shop/show.wxml index 636ab86..2739b4a 100644 --- a/app/pages/shop/show.wxml +++ b/app/pages/shop/show.wxml @@ -1,4 +1,7 @@ - + + + + @@ -33,7 +36,7 @@ - + @@ -43,36 +46,58 @@ -