微信小程序 购物车代码
2018-01-15 23:10
399 查看
// pages/goods/goods.js
Page({
data: {
goods: [
{
"name": "热销榜",
"type": -1,
"foods": [
{
"name": "皮蛋瘦肉粥",
"price": 10,
"oldPrice": "",
"description": "咸粥",
"sellCount": 229,
"Count": 0,
"rating": 100,
"info": "一碗皮蛋瘦肉粥,总是我到粥店心满意足",
"ratings": [
{
"username": "3******c",
"rateTime": 1469281964000,
"rateType": 0,
"text": "很喜欢的粥",
"avatar": "http://static.galileo.xiaojukeji.com/static/tms/default_header.png"
}
],
"icon": "http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/114/h/114",
"image": "http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/750/h/750"
}
],
"icon": "http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/114/h/114",
"image": "http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/750/h/750"
}
]
}
],
toView: '0',
scrollTop: 100,
foodCounts: 0,
totalPrice: 0,// 总价格
totalCount: 0, // 总商品数
carArray: [],
minPrice: 20,//起送價格
payDesc: '',
deliveryPrice: 4,//配送費
fold: true,
selectFoods: [{ price: 20, count: 2 }],
cartShow: 'none'
},
// selectFoods: function (event) {
// var food = event.currentTarget.dataset.food;
// console.log(food);
// console.log(this.data.goods)
// let foods = [];
// this.data.goods.forEach((good) => {
// good.foods.forEach((food) => {
// // console.log(food)
// if (food.sellCount) {
// foods.push(food);
// console.log(foods);
// }
// })
// })
// },
selectMenu: function (e) {
var index = e.currentTarget.dataset.itemIndex;
this.setData({
toView: 'order' + index.toString()
})
console.log(this.data.toView);
},
//移除商品
decreaseCart: function (e) {
var index = e.currentTarget.dataset.itemIndex;
var parentIndex = e.currentTarget.dataset.parentindex;
this.data.goods.forEach((good) => {
good.foods.forEach((food) => {
var num = this.data.goods[parentIndex].foods[index].Count;
var mark = 'a' + index + 'b' + parentIndex
if (food.Count > 0) {
this.data.goods[parentIndex].foods[index].Count--
var price = this.data.goods[parentIndex].foods[index].price;
var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
var carArray1 = this.data.carArray.filter(item => item.mark != mark);
carArray1.push(obj);
console.log(carArray1);
this.setData({
carArray: carArray1,
goods: this.data.goods
})
this.calTotalPrice()
this.setData({
payDesc: this.payDesc()
})
}
if (num > 0) {
var carArray1 = this.data.carArray.filter(item => item.num > 0)
console.log(carArray1)
this.setData({
carArray: carArray1,
})
}
})
})
},
decreaseShopCart: function (e) {
this.decreaseCart(e);
},
//添加到购物车
addCart(e) {
var index = e.currentTarget.dataset.itemIndex;
var parentIndex = e.currentTarget.dataset.parentindex;
this.data.goods[parentIndex].foods[index].Count++;
var mark = 'a' + index + 'b' + parentIndex
var price = this.data.goods[parentIndex].foods[index].price;
var num = this.data.goods[parentIndex].foods[index].Count;
var name = this.data.goods[parentIndex].foods[index].name;
var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
var carArray1 = this.data.carArray.filter(item => item.mark != mark)
carArray1.push(obj)
console.log(carArray1);
this.setData({
carArray: carArray1,
goods: this.data.goods
})
this.calTotalPrice();
this.setData({
payDesc: this.payDesc()
})
},
addShopCart: function (e) {
this.addCart(e);
},
//计算总价
calTotalPrice: function () {
var carArray = this.data.carArray;
var totalPrice = 0;
var totalCount = 0;
for (var i = 0; i < carArray.length; i++) {
totalPrice += carArray[i].price * carArray[i].num;
totalCount += carArray[i].num
}
this.setData({
totalPrice: totalPrice,
totalCount: totalCount,
//payDesc: this.payDesc()
});
},
//差几元起送
payDesc() {
if (this.data.totalPrice === 0) {
return `¥${this.data.minPrice}元起送`;
} else if (this.data.totalPrice < this.data.minPrice) {
let diff = this.data.minPrice - this.data.totalPrice;
return '还差' + diff + '元起送';
} else {
return '去结算';
}
},
//結算
pay() {
if (this.data.totalPrice < this.data.minPrice) {
return;
}
// window.alert('支付' + this.totalPrice + '元');
//确认支付逻辑
var resultType = "success";
wx.redirectTo({
url: '../goods/pay/pay?resultType=' + resultType
})
},
//彈起購物車
toggleList: function () {
if (!this.data.totalCount) {
return;
}
this.setData({
fold: !this.data.fold,
})
var fold = this.data.fold
//console.log(this.data.fold);
this.cartShow(fold)
},
cartShow: function (fold) {
console.log(fold);
if (fold == false) {
this.setData({
cartShow: 'block',
})
} else {
this.setData({
cartShow: 'none',
})
}
console.log(this.data.cartShow);
},
listShow() {
if (!this.data.totalCount) {
this.data.fold = true;
return false;
}
let show = !this.fold;
// if (show) {
// this.$nextTick(() => {
// if (!this.scroll) {
// this.scroll = new BScroll(this.$refs.listContent, {
// click: true
// });
// } else {
// this.scroll.refresh();
// }
// });
// }
return show;
},
onLoad: function (options) {
// 页面初始化 options为页面跳转所带来的参数
this.setData({
payDesc: this.payDesc()
});
},
onReady: function () {
// 页面渲染完成
},
onShow: function () {
// 页面显示
},
onHide: function () {
// 页面隐藏
},
onUnload: function () {
// 页面关闭
}
})
Page({
data: {
goods: [
{
"name": "热销榜",
"type": -1,
"foods": [
{
"name": "皮蛋瘦肉粥",
"price": 10,
"oldPrice": "",
"description": "咸粥",
"sellCount": 229,
"Count": 0,
"rating": 100,
"info": "一碗皮蛋瘦肉粥,总是我到粥店心满意足",
"ratings": [
{
"username": "3******c",
"rateTime": 1469281964000,
"rateType": 0,
"text": "很喜欢的粥",
"avatar": "http://static.galileo.xiaojukeji.com/static/tms/default_header.png"
}
],
"icon": "http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/114/h/114",
"image": "http://fuss10.elemecdn.com/c/cd/c12745ed8a5171e13b427dbc39401jpeg.jpeg?imageView2/1/w/750/h/750"
}
],
"icon": "http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/114/h/114",
"image": "http://fuss10.elemecdn.com/f/28/a51e7b18751bcdf871648a23fd3b4jpeg.jpeg?imageView2/1/w/750/h/750"
}
]
}
],
toView: '0',
scrollTop: 100,
foodCounts: 0,
totalPrice: 0,// 总价格
totalCount: 0, // 总商品数
carArray: [],
minPrice: 20,//起送價格
payDesc: '',
deliveryPrice: 4,//配送費
fold: true,
selectFoods: [{ price: 20, count: 2 }],
cartShow: 'none'
},
// selectFoods: function (event) {
// var food = event.currentTarget.dataset.food;
// console.log(food);
// console.log(this.data.goods)
// let foods = [];
// this.data.goods.forEach((good) => {
// good.foods.forEach((food) => {
// // console.log(food)
// if (food.sellCount) {
// foods.push(food);
// console.log(foods);
// }
// })
// })
// },
selectMenu: function (e) {
var index = e.currentTarget.dataset.itemIndex;
this.setData({
toView: 'order' + index.toString()
})
console.log(this.data.toView);
},
//移除商品
decreaseCart: function (e) {
var index = e.currentTarget.dataset.itemIndex;
var parentIndex = e.currentTarget.dataset.parentindex;
this.data.goods.forEach((good) => {
good.foods.forEach((food) => {
var num = this.data.goods[parentIndex].foods[index].Count;
var mark = 'a' + index + 'b' + parentIndex
if (food.Count > 0) {
this.data.goods[parentIndex].foods[index].Count--
var price = this.data.goods[parentIndex].foods[index].price;
var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
var carArray1 = this.data.carArray.filter(item => item.mark != mark);
carArray1.push(obj);
console.log(carArray1);
this.setData({
carArray: carArray1,
goods: this.data.goods
})
this.calTotalPrice()
this.setData({
payDesc: this.payDesc()
})
}
if (num > 0) {
var carArray1 = this.data.carArray.filter(item => item.num > 0)
console.log(carArray1)
this.setData({
carArray: carArray1,
})
}
})
})
},
decreaseShopCart: function (e) {
this.decreaseCart(e);
},
//添加到购物车
addCart(e) {
var index = e.currentTarget.dataset.itemIndex;
var parentIndex = e.currentTarget.dataset.parentindex;
this.data.goods[parentIndex].foods[index].Count++;
var mark = 'a' + index + 'b' + parentIndex
var price = this.data.goods[parentIndex].foods[index].price;
var num = this.data.goods[parentIndex].foods[index].Count;
var name = this.data.goods[parentIndex].foods[index].name;
var obj = { price: price, num: num, mark: mark, name: name, index: index, parentIndex: parentIndex };
var carArray1 = this.data.carArray.filter(item => item.mark != mark)
carArray1.push(obj)
console.log(carArray1);
this.setData({
carArray: carArray1,
goods: this.data.goods
})
this.calTotalPrice();
this.setData({
payDesc: this.payDesc()
})
},
addShopCart: function (e) {
this.addCart(e);
},
//计算总价
calTotalPrice: function () {
var carArray = this.data.carArray;
var totalPrice = 0;
var totalCount = 0;
for (var i = 0; i < carArray.length; i++) {
totalPrice += carArray[i].price * carArray[i].num;
totalCount += carArray[i].num
}
this.setData({
totalPrice: totalPrice,
totalCount: totalCount,
//payDesc: this.payDesc()
});
},
//差几元起送
payDesc() {
if (this.data.totalPrice === 0) {
return `¥${this.data.minPrice}元起送`;
} else if (this.data.totalPrice < this.data.minPrice) {
let diff = this.data.minPrice - this.data.totalPrice;
return '还差' + diff + '元起送';
} else {
return '去结算';
}
},
//結算
pay() {
if (this.data.totalPrice < this.data.minPrice) {
return;
}
// window.alert('支付' + this.totalPrice + '元');
//确认支付逻辑
var resultType = "success";
wx.redirectTo({
url: '../goods/pay/pay?resultType=' + resultType
})
},
//彈起購物車
toggleList: function () {
if (!this.data.totalCount) {
return;
}
this.setData({
fold: !this.data.fold,
})
var fold = this.data.fold
//console.log(this.data.fold);
this.cartShow(fold)
},
cartShow: function (fold) {
console.log(fold);
if (fold == false) {
this.setData({
cartShow: 'block',
})
} else {
this.setData({
cartShow: 'none',
})
}
console.log(this.data.cartShow);
},
listShow() {
if (!this.data.totalCount) {
this.data.fold = true;
return false;
}
let show = !this.fold;
// if (show) {
// this.$nextTick(() => {
// if (!this.scroll) {
// this.scroll = new BScroll(this.$refs.listContent, {
// click: true
// });
// } else {
// this.scroll.refresh();
// }
// });
// }
return show;
},
onLoad: function (options) {
// 页面初始化 options为页面跳转所带来的参数
this.setData({
payDesc: this.payDesc()
});
},
onReady: function () {
// 页面渲染完成
},
onShow: function () {
// 页面显示
},
onHide: function () {
// 页面隐藏
},
onUnload: function () {
// 页面关闭
}
})
相关文章推荐
- 微信小程序实战篇之购物车的实现代码示例
- 微信小程序「官方示例代码」剖析【下】:运行机制
- 微信小程序 实现tabs选项卡效果实例代码
- 微信小程序 获取微信OpenId详解及实例代码
- 微信小程序-开心大转盘(圆盘指针)代码分析
- 微信小程序开发--购物车动画与加减
- 移动端+微信小程序实现,手机端滑动分页代码思路
- 微信小程序 倒计时组件实现代码
- 微信小程序 swiper组件详解及实例代码
- [转]微信小程序之购物车 —— 微信小程序实战商城系列(5)
- 转载:移动端+微信小程序实现,手机端滑动分页代码思路(ajax)
- 微信小程序 MD5的方法详解及实例代码
- 微信小程序 POST请求(网络请求)详解及实例代码
- PHP--解决微信小程序上传图片(wx.uploadFile)接口代码
- 微信小程序 倒计时组件实现代码
- 微信小程序,求绘制图片阴影的代码怎么写
- 微信小程序 wx.request(object) API详解及实例代码
- 微信小程序左右滑动切换页面详解及实例代码
- 微信小程序实现之手势锁功能实例代码
- 微信小程序动态生成二维码的实现代码