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.
hunjianghu/江可飞/小程序开发/pages/gouwu/gouwu.js

290 lines
6.0 KiB

6 years ago
// pages/gouwu/gouwu.js
Page({
/**
* 页面的初始数据
*/
data: {
6 years ago
'iscart': false,
'checked': [],
'goodList': [{
6 years ago
'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
}
],
6 years ago
'bookList': [],
6 years ago
'checkAll': false,
'totalCount': 0,
'totalPrice': 0,
6 years ago
'consignee': "杨盼成",
'phone': '12345678912'
6 years ago
},
/**
* 删除购物车当前商品
*/
deleteList(e) {
const index = e.currentTarget.dataset.index;
6 years ago
let bookList = this.data.bookList;
bookList.splice(index, 1);
6 years ago
this.setData({
6 years ago
bookList: bookList
6 years ago
});
6 years ago
if (!bookList.length) {
6 years ago
this.setData({
iscart: true
});
} else {
this.calculateTotal();
}
},
/**
* 计算商品总数
*/
6 years ago
calculateTotal: function() {
var bookList = this.data.bookList;
6 years ago
var totalCount = 0;
var totalPrice = 0;
6 years ago
for (var i = 0; i < bookList.length; i++) {
var book = bookList[i];
if (book.checked) {
totalCount += book.count;
totalPrice += book.count * book.price;
6 years ago
}
}
totalPrice = totalPrice.toFixed(2);
this.setData({
'totalCount': totalCount,
'totalPrice': totalPrice
})
},
/**
* 用户点击商品减1
*/
6 years ago
subtracttap: function(e) {
6 years ago
var index = e.target.dataset.index;
6 years ago
var bookList = this.data.bookList;
var count = bookList[index].count;
6 years ago
if (count <= 1) {
return;
} else {
6 years ago
bookList[index].Count--;
6 years ago
this.setData({
6 years ago
'bookList': bookList
6 years ago
});
this.calculateTotal();
}
},
/**
* 用户点击商品加1
*/
6 years ago
addtap: function(e) {
6 years ago
var index = e.target.dataset.index;
6 years ago
var bookList = this.data.bookList;
var count = bookList[index].count;
bookList[index].Count++;
6 years ago
this.setData({
6 years ago
'bookList': bookList
6 years ago
});
this.calculateTotal();
},
/**
* 用户选择购物车商品
*/
6 years ago
checkboxChange: function(e) {
6 years ago
console.log('checkbox发生change事件携带value值为', e.detail.value);
6 years ago
var checkboxItems = this.data.bookList;
6 years ago
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) {
6 years ago
if (checkboxItems[i].BookID == values[j]) {
6 years ago
checkboxItems[i].checked = true;
break;
}
}
}
var checkAll = false;
if (checkboxItems.length == values.length) {
checkAll = true;
}
this.setData({
6 years ago
'bookList': checkboxItems,
6 years ago
'checkAll': checkAll
});
this.calculateTotal();
},
/**
* 用户点击全选
*/
6 years ago
selectalltap: function(e) {
6 years ago
// console.log('用户点击全选携带value值为', e.detail.value);
var value = e.detail.value;
var checkAll = false;
if (value && value[0]) {
checkAll = true;
}
6 years ago
var bookList = this.data.bookList;
for (var i = 0; i < bookList.length; i++) {
var book = bookList[i];
book['checked'] = checkAll;
6 years ago
}
this.setData({
'checkAll': checkAll,
6 years ago
'bookList': bookList
6 years ago
});
this.calculateTotal();
},
6 years ago
todetail: function(e) {
var id = e.target.dataset.id
wx.navigateTo({
url: "/pages/detail/detail?id=" + id
})
},
6 years ago
6 years ago
/**
* 生命周期函数--监听页面加载
*/
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()
}
}
})
6 years ago
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
6 years ago
onReady: function() {
6 years ago
this.calculateTotal();
},
/**
* 生命周期函数--监听页面显示
*/
6 years ago
onShow: function() {
6 years ago
},
/**
* 生命周期函数--监听页面隐藏
*/
6 years ago
onHide: function() {
6 years ago
},
/**
* 生命周期函数--监听页面卸载
*/
6 years ago
onUnload: function() {
6 years ago
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
6 years ago
onPullDownRefresh: function() {
6 years ago
},
/**
* 页面上拉触底事件的处理函数
*/
6 years ago
onReachBottom: function() {
6 years ago
},
/**
* 用户点击右上角分享
*/
6 years ago
onShareAppMessage: function() {
6 years ago
},
6 years ago
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
// })
// }
}
6 years ago
6 years ago
})