商店详情--规格

This commit is contained in:
Kiyan 2017-04-27 16:30:56 +08:00
parent 904083e258
commit 52704d6a76
5 changed files with 428 additions and 50 deletions

View File

@ -20,6 +20,8 @@ Page({
activeMenuIndex: 0,
showCart: false,
showSubGoods: false,
order: initOrder,
info: {
@ -274,7 +276,280 @@ Page({
}
]
},
]
],
"goods_map": {
"29": {
"goods_id": "29",
"goods_name": "鸡腿饭",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/1461034075146103407535640.jpg",
"pic_hd1": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd1_1461034075146103407535640.jpg",
"pic_hd2": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd2_1461034075146103407535640.jpg",
"detail": "鸡腿饭 xx",
"price": "22.00",
"packing_fee": "2.00",
"sales": "46",
"praise": "0",
"stock": "76",
"sub_goods": [
{
"sub_id": "50",
"sub_name": "大",
"price": "22.98",
"packing_fee": "2.00",
"stock": "74"
},
{
"sub_id": "51",
"sub_name": "小",
"price": "22.00",
"packing_fee": "2.00",
"stock": "76"
}
],
"sub_goods_map": {
"50": {
"sub_id": "50",
"sub_name": "大",
"price": "22.98",
"packing_fee": "2.00",
"stock": "74"
},
"51": {
"sub_id": "51",
"sub_name": "小",
"price": "22.00",
"packing_fee": "2.00",
"stock": "76"
}
}
},
"30": {
"goods_id": "30",
"goods_name": "排骨饭",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/1461034058146103405829482.jpg",
"pic_hd1": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd1_1461034058146103405829482.jpg",
"pic_hd2": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd2_1461034058146103405829482.jpg",
"detail": "排骨饭 xx",
"price": "20.00",
"packing_fee": "0.00",
"sales": "10",
"praise": "0",
"stock": "0",
"sub_goods": [],
"sub_goods_map": []
},
"31": {
"goods_id": "31",
"goods_name": "招牌饭",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/1461033463146103346359875.jpg",
"pic_hd1": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd1_1461033463146103346359875.jpg",
"pic_hd2": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd2_1461033463146103346359875.jpg",
"detail": "招牌饭 xx",
"price": "20.00",
"packing_fee": "0.00",
"sales": "15",
"praise": "0",
"stock": "85",
"sub_goods": [],
"sub_goods_map": []
},
"32": {
"goods_id": "32",
"goods_name": "鸡翅饭",
"seller_id": "2",
"pic_url": null,
"pic_hd1": null,
"pic_hd2": null,
"detail": "鸡翅饭 xx",
"price": "20.00",
"packing_fee": "0.00",
"sales": "43",
"praise": "0",
"stock": "57",
"sub_goods": [],
"sub_goods_map": []
},
"33": {
"goods_id": "33",
"goods_name": "香肠饭",
"seller_id": "2",
"pic_url": null,
"pic_hd1": null,
"pic_hd2": null,
"detail": "香肠饭 xx",
"price": "20.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"34": {
"goods_id": "34",
"goods_name": "咸蛋",
"seller_id": "2",
"pic_url": null,
"pic_hd1": null,
"pic_hd2": null,
"detail": "咸蛋 xx",
"price": "20.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"35": {
"goods_id": "35",
"goods_name": "可乐",
"seller_id": "2",
"pic_url": null,
"pic_hd1": null,
"pic_hd2": null,
"detail": "可乐 xx",
"price": "3.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"36": {
"goods_id": "36",
"goods_name": "雪碧",
"seller_id": "2",
"pic_url": null,
"pic_hd1": null,
"pic_hd2": null,
"detail": "雪碧 xx",
"price": "3.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"37": {
"goods_id": "37",
"goods_name": "香肠",
"seller_id": "2",
"pic_url": null,
"pic_hd1": null,
"pic_hd2": null,
"detail": "香肠 xx",
"price": "3.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"40": {
"goods_id": "40",
"goods_name": "香蕉",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/",
"pic_hd1": null,
"pic_hd2": null,
"detail": "",
"price": "6.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"41": {
"goods_id": "41",
"goods_name": "香蕉2",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/",
"pic_hd1": null,
"pic_hd2": null,
"detail": "",
"price": "6.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"42": {
"goods_id": "42",
"goods_name": "香蕉3",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/",
"pic_hd1": null,
"pic_hd2": null,
"detail": "",
"price": "6.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"43": {
"goods_id": "43",
"goods_name": "香蕉3",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/",
"pic_hd1": null,
"pic_hd2": null,
"detail": "",
"price": "6.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"44": {
"goods_id": "44",
"goods_name": "香蕉3",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/",
"pic_hd1": null,
"pic_hd2": null,
"detail": "",
"price": "6.00",
"packing_fee": "0.00",
"sales": "0",
"praise": "0",
"stock": "100",
"sub_goods": [],
"sub_goods_map": []
},
"46": {
"goods_id": "46",
"goods_name": "香肠2",
"seller_id": "2",
"pic_url": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/1466411009146641100940750.jpg",
"pic_hd1": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd1_1466411009146641100940750.jpg",
"pic_hd2": "http://mtest.ipaotui.com/Uploadfile/Img/seller_goods/hd2_1466411009146641100940750.jpg",
"detail": "",
"price": "2.00",
"packing_fee": "0.00",
"sales": "13",
"praise": "0",
"stock": "187",
"sub_goods": [],
"sub_goods_map": []
}
}
}
},
onLoad: function (options) {
@ -317,8 +592,7 @@ Page({
})
},
addGoodsNum(order, item) {
var {goods, goodsNums} = order
addGoods(goods, item) {
var {goods_id, sub_id, num} = item
var itemIndex;
if (sub_id) {
@ -335,15 +609,9 @@ Page({
} else {
goods.push(item)
}
if (goodsNums[goods_id]) {
goodsNums[goods_id] += num
} else {
goodsNums[goods_id] = num
}
return goods
},
removeGoodsNum(order, item) {
var {goods, goodsNums} = order
removeGoods(goods, item) {
var {goods_id, sub_id, num} = item
var itemIndex;
if (sub_id) {
@ -357,69 +625,106 @@ Page({
}
if (itemIndex >= 0) {
item = goods[itemIndex]
if (item.num > 1) {
if (item.num > num) {
item.num -= num
} else {
goods.splice(itemIndex, 1)
}
}
if (goodsNums[goods_id] > 1) {
goodsNums[goods_id] -= num
} else {
delete goodsNums[goods_id]
}
return goods
},
increase(e) {
var {order, info: {menus}} = this.data;
var {menuIndex, goodsIndex, subIndex} = e.currentTarget.dataset;
var goods = menus[menuIndex].goods2[goodsIndex];
var {order, info: {goods_map}} = this.data;
var {goodsId, subId} = e.currentTarget.dataset;
var goods = goods_map[goodsId];
var {goods_id, goods_name} = goods
if (subIndex >= 0) {
goods = goods.sub_goods[subIndex];
if (subId) {
goods = goods.sub_goods_map[subId];
var {sub_id, sub_name} = goods
}
order.totalNum += 1;
order.totalGoodsPrice += +goods.price;
order.totalPackingFee += +goods.packing_fee;
order.totalPrice = order.totalGoodsPrice + order.totalPackingFee;
this.addGoodsNum(order, {
order.totalPrice = +((order.totalGoodsPrice + order.totalPackingFee).toFixed(2));
order.goods = this.addGoods(order.goods, {
goods_id, goods_name,
sub_id, sub_name,
price: goods.price,
packing_fee: goods.packing_fee,
menuIndex, goodsIndex, subIndex,
num: 1
})
order.goodsNums = this.calcGoodsNums(order.goods)
this.setData({
order
})
if (subId) {
this.setData({
activeSubGoods: Object.assign(this.data.activeSubGoods, {
subNums: this.calcSubNums(order.goods, goodsId)
})
})
}
},
decrease(e) {
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;
if (subIndex >= 0) {
goods = goods.sub_goods[subIndex];
sub_id = goods['sub_id']
var {order, info: {goods_map}} = this.data;
var {goodsId, subId} = e.currentTarget.dataset;
var goods = goods_map[goodsId];
if (subId) {
goods = goods.sub_goods_map[subId];
}
order.totalNum -= 1;
order.totalGoodsPrice -= +goods.price;
order.totalPackingFee -= +goods.packing_fee;
order.totalPrice = order.totalGoodsPrice + order.totalPackingFee;
this.removeGoodsNum(order, {
goods_id, sub_id,
order.totalPrice = +((order.totalGoodsPrice + order.totalPackingFee).toFixed(2));
order.goods = this.removeGoods(order.goods, {
goods_id: goodsId,
sub_id: subId,
num: 1
})
order.goodsNums = this.calcGoodsNums(order.goods)
this.setData({
order
})
if (subId) {
this.setData({
activeSubGoods: Object.assign(this.data.activeSubGoods, {
subNums: this.calcSubNums(order.goods, goodsId)
})
})
}
if (order.totalNum == 0) {
this.hideCart()
}
},
calcGoodsNums(goods) {
var goodsNums = {}
for (let i = 0, len = goods.length; i < len; i++) {
let {goods_id, num} = goods[i]
if (goodsNums[goods_id]) {
goodsNums[goods_id] += num
} else {
goodsNums[goods_id] = num
}
}
return goodsNums
},
calcSubNums(goods, goodsId) {
var subNums = {}
for (let i = 0, len = goods.length; i < len; i++) {
let {goods_id, sub_id, num} = goods[i]
if (goods_id == goodsId) {
subNums[sub_id] = num
}
}
return subNums
},
clearCart(e) {
this.setData({
order: initOrder,
@ -440,5 +745,33 @@ Page({
this.setData({
showCart: !showCart
})
},
showSubGoods(e) {
var {info: {goods_map}, order} = this.data;
var {goodsId} = e.currentTarget.dataset;
var {goods_id, goods_name, sub_goods} = goods_map[goodsId];
this.setData({
showSubGoods: true,
activeSubGoods: {
goods_name, goods_id,
sub_goods,
activeIndex: 0,
subNums: this.calcSubNums(order.goods, goodsId)
}
})
},
hideSubGoods(e) {
this.setData({
showSubGoods: false
})
},
changeSubGoods(e) {
var {subIndex} = e.currentTarget.dataset;
var {activeSubGoods} = this.data;
activeSubGoods['activeIndex'] = subIndex
this.setData({
activeSubGoods
})
}
})

View File

@ -64,11 +64,11 @@
<view class="goods-item__desc text-small grey-color">月售{{item.sales}}份</view>
<view class="goods-item__price primary-color text-large">¥{{item.price}}</view>
<view class="goods-item__actions">
<view wx:if="{{item.sub_goods && item.sub_goods.length > 0}}" class="goods-item__sub-goods">
<view data-goods-id="{{item.goods_id}}" bindtap="showSubGoods" wx:if="{{item.sub_goods && item.sub_goods.length > 0}}" class="goods-item__sub-goods">
选规格
<view wx:if="{{order['goodsNum'][item.goods_id] > 0}}" class="weui-badge goods-item__sub-goods-badge">{{order['goodsNum'][item.goods_id]}}</view>
<view wx:if="{{order['goodsNums'][item.goods_id] > 0}}" class="weui-badge goods-item__sub-goods-badge">{{order['goodsNums'][item.goods_id]}}</view>
</view>
<template is="goods-actions" data="{{menuIndex: activeMenuIndex, goodsIndex: index, num: order['goodsNums'][item.goods_id]}}" wx:else />
<template is="goods-actions" data="{{goods_id: item.goods_id, num: order['goodsNums'][item.goods_id]}}" wx:else />
</view>
</view>
</view>
@ -100,4 +100,4 @@
</view>
</view>
<template wx:if="{{showCart}}" is="cart-box" data="{{...order}}" />
<template is="sub-goods" />
<template is="sub-goods" wx:if="{{showSubGoods}}" data="{{...activeSubGoods}}"/>

View File

@ -1,13 +1,13 @@
<template name="goods-actions">
<view class="goods-actions weui-flex">
<block wx:if="{{num > 0}}">
<view data-menu-index="{{menuIndex}}" data-goods-index="{{goodsIndex}}" data-sub-index="{{subIndex}}" bindtap="decrease" class="goods-action-btn goods-action-btn_decrease">
<view data-goods-id="{{goods_id}}" data-sub-id="{{sub_id}}" bindtap="decrease" class="goods-action-btn goods-action-btn_decrease">
<image src="/images/decrease@2x.png" class="goods-action-btn__img"></image>
</view>
<view class="goods-action-num">{{num}}</view>
</block>
<view data-menu-index="{{menuIndex}}" data-goods-index="{{goodsIndex}}" data-sub-index="{{subIndex}}" bindtap="increase" class="goods-action-btn goods-action-btn_increase">
<view data-goods-id="{{goods_id}}" data-sub-id="{{sub_id}}" bindtap="increase" class="goods-action-btn goods-action-btn_increase">
<image src="/images/increase@2x.png" class="goods-action-btn__img"></image>
</view>
</view>

View File

@ -2,22 +2,30 @@
<template name="sub-goods">
<view class="sub-goods">
<view class="sub-goods__overlay"></view>
<view class="sub-goods__overlay" bindtap="hideSubGoods"></view>
<view class="sub-goods__content">
<icon bindtap="hideSubGoods" class="sub-goods__close" type="cancel" size="25" color="#999"/>
<view class="sub-goods__hd">
test
{{goods_name}}
</view>
<view class="sub-goods__bd">
<view >规格: </view>
<view class="sub-goods__list">
<view class="sub-goods__item sub-goods__item_active">
750ml
</view>
<view class="sub-goods__item">
255ml
<view data-sub-index="{{index}}" bindtap="changeSubGoods" wx:for="{{sub_goods}}" wx:key="sub_id" class="sub-goods__item {{index == activeIndex? 'sub-goods__item_active': ''}} ">
{{item.sub_name}}
</view>
</view>
</view>
<view class="sub-goods__ft weui-flex">
<view class="weui-flex__item">
<text class="primary-color sub-goods__price">¥{{sub_goods[activeIndex]['price']}}</text>
<text class="text-small">({{sub_goods[activeIndex]['sub_name']}})</text>
</view>
<template is="goods-actions" wx:if="{{subNums[sub_goods[activeIndex]['sub_id']] > 0}}" data="{{goods_id, sub_id: sub_goods[activeIndex]['sub_id'], num: subNums[sub_goods[activeIndex]['sub_id']]}}"></template>
<view data-goods-id="{{goods_id}}" data-sub-id="{{sub_goods[activeIndex]['sub_id']}}" bindtap="increase" wx:else class="sub-goods__add-cart">
加入购物车
</view>
</view>
</view>
</view>
</template>

View File

@ -36,6 +36,43 @@
padding: 10px;
}
.sub-goods__list {
overflow: hidden;
}
.sub-goods__item {
float: left;
margin: 10px 15px 0 0;
padding: 3px 10px;
min-width: 20px;
text-align: center;
border: 1rpx solid #e8e8e8;
border-radius: 10px/50%;
}
.sub-goods__item_active {
border-color: #FFB000;
color: #FFB000;
background-color: #FFFBF1;
}
.sub-goods__ft {
padding: 10px;
align-items: center;
}
.sub-goods__price {
margin-right: 5px;
font-size: 1.5em;
}
.sub-goods__add-cart {
padding: 3px 10px;
color: #fff;
font-size: 14px;
background-color:#ff5801;
border-radius: 10px/50%;
}
.sub-goods__close {
position: absolute;
right: 10px;
top: 5px;
}