You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.1 KiB

8 years ago
const product = require('../../utils/product.js')
8 years ago
Page({
data: {
title: '',
8 years ago
id: 0,
quantity: 1,
8 years ago
product: {}
},
8 years ago
onLoad (params) {
var allProducts = wx.getStorageSync('products')
8 years ago
var id = params.id
var product = allProducts.filter(function(i){
8 years ago
return i.id === id
})[0]
8 years ago
this.setData({
id: id,
product: product,
title: product.name
8 years ago
})
8 years ago
},
onReady() {
8 years ago
wx.setNavigationBarTitle({ title: this.data.title })
},
bindAddToCart (e) {
8 years ago
var that = this
var cartItems = wx.getStorageSync('cartItems') || []
var exist = cartItems.filter(function(ele){
8 years ago
return ele.id === that.data.id
})[0]
if (exist) {
exist.quantity = parseInt(exist.quantity) + 1
} else {
8 years ago
cartItems.push({
id: this.data.id,
quantity: this.data.quantity,
product: this.data.product
})
}
wx.showToast({
title: '成功加入购物车',
icon: 'success',
duration: 1200
})
wx.setStorage({
key: 'cartItems',
data: cartItems
})
},
bindQuantityInput (e) {
this.setData({'quantity': e.detail.value})
},
8 years ago
})