更改如下:

新的IDE好像只能有三个文件夹,所以更改了json库的位置
完成cart的商品列表显示,可以修改数量,删除购物车物品
把 cartItems 的数据格式改的简单了些,先不考虑那么多了,本地方便就好
wechat_user_type
Ge Hao 8 years ago
parent e8bdd2c2c2
commit 8ae4eea501
  1. 2
      app.js
  2. 42
      pages/cart/cart.js
  3. 47
      pages/cart/cart.wxml
  4. 77
      pages/cart/cart.wxss
  5. 15
      pages/show_product/show_product.js
  6. 0
      utils/jsonapi-datastore/LICENSE.md
  7. 0
      utils/jsonapi-datastore/README.md
  8. 0
      utils/jsonapi-datastore/dist/jsonapi-datastore.js
  9. 0
      utils/jsonapi-datastore/package.json

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

@ -2,18 +2,25 @@ const district = require('../../utils/address_data.js')
Page({
data: {
address:{}
deleteModalHidden: true,
wantToDeleteItem: '',
address:{},
cartItems:[],
amount:0
},
onLoad: function (params) {
},
onShow: function (params) {
var cartItems = wx.getStorageSync("cartItems")
this.setData({cartItems: cartItems})
var address = {}
var detailAddress = wx.getStorageSync('detailAddress')
address.detail = detailAddress
var districtIndex = wx.getStorageSync('currentDistrict')
var districtIndex = wx.getStorageSync('currentDistrict') || [0,0,0]
address.province = district.provinces()[districtIndex[0]]
address.city = district.cities(address.province)[districtIndex[1]]
address.county = district.counties(address.province, address.city)[districtIndex[2]]
@ -21,6 +28,37 @@ Page({
this.setData({address: address})
},
bindChangeQuantity: function (e) {
},
// tap on item to delete cart item
bindTapOnItem: function (e) {
this.setData({
deleteModalHidden: false,
wantToDeleteItem: e.currentTarget.dataset.id
})
},
deleteModalChange: function (e) {
var that = this
if (e.type === "confirm") {
var cartItems = that.data.cartItems
var index = cartItems.findIndex(function(ele){
return ele.id === that.data.wantToDeleteItem
})
cartItems.splice(index, 1)
this.setData({ cartItems: cartItems })
wx.setStorage({
key: 'cartItems',
data: cartItems
})
}
this.setData({
deleteModalHidden: true
})
},
// bindBilling: function () {
// var cartItems = wx.getStorageSync('cartItems')
// },

@ -1,10 +1,47 @@
<view wx:if="{{ address }}" bindtap="bindTapAddress">
<text>{{address.province}} {{address.city}} {{address.county}} {{address.detail}}</text>
<view>
<text>修改地址</text>
<view class="address-container" wx:if="{{ address }}" bindtap="bindTapAddress">
<view class="receiver">
收货人:<text>{{address.name}}</text>
<text>{{address.mobile}}</text>
</view>
<view class="address">
<text>{{address.province}} {{address.city}} {{address.county}} {{address.detail}}</text>
</view>
<view class="button">
<button type='primary'>修改地址</button>
</view>
</view>
<view wx:else bindtap="bindTapAddress">
<text>填写收货地址</text>
<button class="button" type='primary'>填写收货地址</button>
</view>
<view wx:if="{{cartItems}}">
<block wx:for="{{cartItems}}" wx:for-item="item">
<view class="item" data-id="{{item.id}}">
<view class="image" bindtap="bindTapOnItem" data-id="{{item.id}}">
<image class="head-img" src="{{item.product['image-url']}}" mode="aspectFit"></image>
</view>
<view class="item-right">
<view class="title" bindtap="bindTapOnItem" data-id="{{item.id}}">
<text> {{item.product.name}} </text>
</view>
<view class="numbers">
<text class="price" bindtap="bindTapOnItem" data-id="{{item.id}}"> ¥{{item.product.price}} / 件 </text>
<view class="pull-right">
<text>购买数量:</text>
<input class="quantity" value="{{item.quantity}}" bindChange="bindChangeQuantity"></input>
</view>
</view>
</view>
</view>
</block>
</view>
<view class="billing-btn">
<button class="button" type='warn'> 去支付 </button>
</view>
<modal title="删除该商品" confirm-text="我不要它了" cancel-text="别删" hidden="{{deleteModalHidden}}" bindconfirm="deleteModalChange" bindcancel="deleteModalChange">
是否要删除购物车中的这件商品?
</modal>

@ -0,0 +1,77 @@
.button button {
border-radius: 0;
}
.address-container {
margin: 15rpx;
}
.item {
height: 180rpx;
padding: 15rpx;
background-color: #fff;
border-top: 1px solid #e4e4e4;
color: #787878;
display:flex;
align-items:center;
}
.item image {
display: block;
width: 300rpx;
}
.item-right {
display: block;
margin-left: 11px;
flex: 1;
}
.title {
padding-bottom: 15px;
}
.title text {
font-size: 16px;
color: #787878;
white-space: pre-wrap;
line-height: 17px;
}
text {
font-size: 15px;
color: #444;
}
.numbers {
display: flex;
}
.numbers text, input {
display: block;
}
.pull-right {
margin: auto;
font-size: 15px;
display: flex;
}
.numbers input {
border: 1rpx solid lightgrey;
width: 60rpx;
}
.numbers text {
padding: 10rpx;
}
.billing-btn {
position: fixed;
bottom: 0;
width: 100%;
}
.billing-btn .button {
border-radius: 0;
}

@ -28,19 +28,22 @@ Page({
},
bindAddToCart (e) {
var cartItems = wx.getStorageSync('cartItems') || {data:[]}
var cartItems = wx.getStorageSync('cartItems') || []
var exist = cartItems.data.find(function(ele){
var exist = cartItems.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('products', this.data.id)
product.setAttribute('quantity', this.data.quantity)
cartItems.data.push(product)
// var model = getApp().jsonModel
// var product = new model('products', this.data.id)
// product.setAttribute('quantity', this.data.quantity)
var product = {id: this.data.id,
quantity: this.data.quantity,
product: this.data.product}
cartItems.push(product)
}
this.setData({ toastAddProduct:false });

Loading…
Cancel
Save