购物车的实现,不是很清楚 json model 的格式

准备加入购买功能
wechat_user_type
Ge Hao 8 years ago
parent 0581e3d5dd
commit bf4c3cb301
  1. 11
      app.js
  2. 6
      pages/cart/cart.js
  3. 21
      pages/show_product/show_product.js
  4. 22
      utils/product.js

@ -1,10 +1,7 @@
App({
onLaunch: function () {
this.store = new(this.jsonApi())
},
jsonApi: function() {
const jsonApi = require('node_modules/jsonapi-datastore/dist/jsonapi-datastore.js').JsonApiDataStore
return jsonApi
const jsonApi = require('node_modules/jsonapi-datastore/dist/jsonapi-datastore.js')
this.store = new(jsonApi.JsonApiDataStore)
this.jsonModel = jsonApi.JsonApiDataStoreModel
}
})
})

@ -3,5 +3,11 @@ Page({
onLoad: function() {
var that = this
},
bindBilling (e) {
var cartItems = wx.getStorageSync('cartItems') || []
}
})

@ -33,11 +33,24 @@ Page({
},
bindAddToCart (e) {
//var existItem = wx.getStorageSync('cartItems')
// TODO
var cartItems = wx.getStorageSync('cartItems') || {data:[]}
var exist = cartItems.data.find(function(ele){
return ele.id === app.getCurrentPage().data.id
})
if (exist) {
exist.quantity = this.data.quantity
} else {
var model = getApp().jsonModel
var product = new model('product', this.data.id)
product.setAttribute('quantity', this.data.quantity)
cartItems.data.push(product)
}
wx.setStorage({
key: `cart-${this.data.id}`,
data: {quantity: this.data.quantity}
key: 'cartItems',
data: cartItems
})
},

@ -35,9 +35,23 @@ function getSlides () {
})
}
function postBilling (data) {
return new Promise((resolve, reject) => {
wx.request({
method: 'POST',
url: `${API_URL}/cart/billing`,
data: data,
header: { 'Content-Type': 'application/json'},
success: resolve,
fail: reject
})
})
}
module.exports = {
getProducts (data) {
return getProducts(data)
getProducts () {
return getProducts()
},
getProduct (data) {
return getProducts(data)
@ -45,5 +59,9 @@ module.exports = {
getSlides () {
return getSlides()
},
postBilling (data) {
return postBilling(data)
}
}

Loading…
Cancel
Save