|
|
|
|
// pages/gouwu/gouwu.js
|
|
|
|
|
Page({
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面的初始数据
|
|
|
|
|
*/
|
|
|
|
|
data: {
|
|
|
|
|
'iscart': false,
|
|
|
|
|
'checked': [],
|
|
|
|
|
'goodList': [{
|
|
|
|
|
'cover': '/pages/index/image/suanfa.jpg',
|
|
|
|
|
'isbn': '9787535482051',
|
|
|
|
|
'desc': '计算机算法设计',
|
|
|
|
|
'price': 20,
|
|
|
|
|
'count': 1,
|
|
|
|
|
'checked': false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'cover': '/pages/index/image/jichu.jpg',
|
|
|
|
|
'isbn': '9787540455958',
|
|
|
|
|
'desc': '计算机基础',
|
|
|
|
|
'price': 30,
|
|
|
|
|
'count': 1,
|
|
|
|
|
'checked': false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'cover': '/pages/index/image/rjgcdl.jpg',
|
|
|
|
|
'isbn': '9787539982830',
|
|
|
|
|
'desc': '软件工程导论',
|
|
|
|
|
'price': 25,
|
|
|
|
|
'count': 1,
|
|
|
|
|
'checked': false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'cover': '/pages/index/image/java.jpg',
|
|
|
|
|
'isbn': '9787550013247',
|
|
|
|
|
'desc': 'Java Web',
|
|
|
|
|
'price': 17,
|
|
|
|
|
'count': 1,
|
|
|
|
|
'checked': false
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
'cover': '/pages/index/image/byyl.jpg',
|
|
|
|
|
'isbn': '9787208061644',
|
|
|
|
|
'desc': '编译原理',
|
|
|
|
|
'price': 15,
|
|
|
|
|
'count': 1,
|
|
|
|
|
'checked': false
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
'bookList': [],
|
|
|
|
|
'checkAll': false,
|
|
|
|
|
'totalCount': 0,
|
|
|
|
|
'totalPrice': 0,
|
|
|
|
|
'consignee': "杨盼成",
|
|
|
|
|
'phone': '12345678912'
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除购物车当前商品
|
|
|
|
|
*/
|
|
|
|
|
deleteList(e) {
|
|
|
|
|
const index = e.currentTarget.dataset.index;
|
|
|
|
|
let bookList = this.data.bookList;
|
|
|
|
|
bookList.splice(index, 1);
|
|
|
|
|
this.setData({
|
|
|
|
|
bookList: bookList
|
|
|
|
|
});
|
|
|
|
|
if (!bookList.length) {
|
|
|
|
|
this.setData({
|
|
|
|
|
iscart: true
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.calculateTotal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 计算商品总数
|
|
|
|
|
*/
|
|
|
|
|
calculateTotal: function() {
|
|
|
|
|
var bookList = this.data.bookList;
|
|
|
|
|
var totalCount = 0;
|
|
|
|
|
var totalPrice = 0;
|
|
|
|
|
for (var i = 0; i < bookList.length; i++) {
|
|
|
|
|
var book = bookList[i];
|
|
|
|
|
if (book.checked) {
|
|
|
|
|
totalCount += book.count;
|
|
|
|
|
totalPrice += book.count * book.price;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
totalPrice = totalPrice.toFixed(2);
|
|
|
|
|
this.setData({
|
|
|
|
|
'totalCount': totalCount,
|
|
|
|
|
'totalPrice': totalPrice
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户点击商品减1
|
|
|
|
|
*/
|
|
|
|
|
subtracttap: function(e) {
|
|
|
|
|
var index = e.target.dataset.index;
|
|
|
|
|
var bookList = this.data.bookList;
|
|
|
|
|
var count = bookList[index].count;
|
|
|
|
|
if (count <= 1) {
|
|
|
|
|
return;
|
|
|
|
|
} else {
|
|
|
|
|
bookList[index].Count--;
|
|
|
|
|
this.setData({
|
|
|
|
|
'bookList': bookList
|
|
|
|
|
});
|
|
|
|
|
this.calculateTotal();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户点击商品加1
|
|
|
|
|
*/
|
|
|
|
|
addtap: function(e) {
|
|
|
|
|
var index = e.target.dataset.index;
|
|
|
|
|
var bookList = this.data.bookList;
|
|
|
|
|
var count = bookList[index].count;
|
|
|
|
|
bookList[index].Count++;
|
|
|
|
|
this.setData({
|
|
|
|
|
'bookList': bookList
|
|
|
|
|
});
|
|
|
|
|
this.calculateTotal();
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* 用户选择购物车商品
|
|
|
|
|
*/
|
|
|
|
|
checkboxChange: function(e) {
|
|
|
|
|
console.log('checkbox发生change事件,携带value值为:', e.detail.value);
|
|
|
|
|
var checkboxItems = this.data.bookList;
|
|
|
|
|
var values = e.detail.value;
|
|
|
|
|
for (var i = 0; i < checkboxItems.length; ++i) {
|
|
|
|
|
checkboxItems[i].checked = false;
|
|
|
|
|
for (var j = 0; j < values.length; ++j) {
|
|
|
|
|
if (checkboxItems[i].BookID == values[j]) {
|
|
|
|
|
checkboxItems[i].checked = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var checkAll = false;
|
|
|
|
|
if (checkboxItems.length == values.length) {
|
|
|
|
|
checkAll = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
'bookList': checkboxItems,
|
|
|
|
|
'checkAll': checkAll
|
|
|
|
|
});
|
|
|
|
|
this.calculateTotal();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户点击全选
|
|
|
|
|
*/
|
|
|
|
|
selectalltap: function(e) {
|
|
|
|
|
// console.log('用户点击全选,携带value值为:', e.detail.value);
|
|
|
|
|
var value = e.detail.value;
|
|
|
|
|
var checkAll = false;
|
|
|
|
|
if (value && value[0]) {
|
|
|
|
|
checkAll = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var bookList = this.data.bookList;
|
|
|
|
|
for (var i = 0; i < bookList.length; i++) {
|
|
|
|
|
var book = bookList[i];
|
|
|
|
|
book['checked'] = checkAll;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
'checkAll': checkAll,
|
|
|
|
|
'bookList': bookList
|
|
|
|
|
});
|
|
|
|
|
this.calculateTotal();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
todetail: function(e) {
|
|
|
|
|
var id = e.target.dataset.id
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: "/pages/detail/detail?id=" + id
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面加载
|
|
|
|
|
*/
|
|
|
|
|
onLoad: function(options) {
|
|
|
|
|
var that = this
|
|
|
|
|
wx.request({
|
|
|
|
|
url: "http://45.76.158.31:8080/web/GetFrontInfo.do",
|
|
|
|
|
header: {
|
|
|
|
|
"content-type": "json"
|
|
|
|
|
},
|
|
|
|
|
success: function(res) {
|
|
|
|
|
for (var i = 0; i < res.data.length; i++) {
|
|
|
|
|
res.data[i].checked = false;
|
|
|
|
|
}
|
|
|
|
|
console.log(res.data)
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
|
that.setData({
|
|
|
|
|
bookList: res.data,
|
|
|
|
|
})
|
|
|
|
|
//wx.hideNavigationBarLoading()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
|
|
|
*/
|
|
|
|
|
onReady: function() {
|
|
|
|
|
this.calculateTotal();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面显示
|
|
|
|
|
*/
|
|
|
|
|
onShow: function() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面隐藏
|
|
|
|
|
*/
|
|
|
|
|
onHide: function() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 生命周期函数--监听页面卸载
|
|
|
|
|
*/
|
|
|
|
|
onUnload: function() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
|
|
|
*/
|
|
|
|
|
onPullDownRefresh: function() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 页面上拉触底事件的处理函数
|
|
|
|
|
*/
|
|
|
|
|
onReachBottom: function() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用户点击右上角分享
|
|
|
|
|
*/
|
|
|
|
|
onShareAppMessage: function() {
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
newAddress: function() {
|
|
|
|
|
var checkList = [];
|
|
|
|
|
if (!this.data.iscart) {
|
|
|
|
|
var bookList = this.data.bookList
|
|
|
|
|
for (var i = 0; i < bookList.length; i++) {
|
|
|
|
|
if (bookList[i].checked) {
|
|
|
|
|
checkList.push(this.data.bookList[i])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
var checkLists = JSON.stringify(checkList)
|
|
|
|
|
console.log(checkLists)
|
|
|
|
|
//if (this.data.totalCount > 0) {
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: '/pages/order/order?typeId=0&checkLists=' + checkLists
|
|
|
|
|
})
|
|
|
|
|
// }
|
|
|
|
|
// else {
|
|
|
|
|
// wx.showToast({
|
|
|
|
|
// title: '没有选择商品',
|
|
|
|
|
// icon: 'success',
|
|
|
|
|
// duration: 2000
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|