// 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) { var that = this const index = e.currentTarget.dataset.index; let bookList = this.data.bookList; wx.request({ url: "http://45.76.158.31:8080/web/AddCart.do?action=Delete&BookID=" + that.data.bookList[index].BookID + "&UserID=1", header: { "content-type": "json" }, success: function(res) { if (res.statusCode == 200) { console.log(res.data) that.setData({ book: res.data, }) //wx.hideNavigationBarLoading() } } }) bookList.splice(index, 1); console.log(index) 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 * 1; totalPrice += book.Count * book.Price; } } totalPrice = totalPrice.toFixed(2); this.setData({ 'totalCount': totalCount, 'totalPrice': totalPrice }) }, /** * 用户点击商品减1 */ subtracttap: function(e) { var that = this 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(); wx.request({ url: "http://45.76.158.31:8080/web/AddCart.do?action=Modify&BookID=" + bookList[index].BookID + "&UserID=1&Count=" + bookList[index].Count, header: { "content-type": "json" }, success: function(res) { if (res.statusCode == 200) { console.log(res.data) that.setData({ book: res.data, }) //wx.hideNavigationBarLoading() } } }) } }, /** * 用户点击商品加1 */ addtap: function(e) { var that = this 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(); wx.request({ url: "http://45.76.158.31:8080/web/AddCart.do?action=Modify&BookID=" + bookList[index].BookID + "&UserID=1&Count=" + bookList[index].Count, header: { "content-type": "json" }, success: function(res) { if (res.statusCode == 200) { console.log(res.data) that.setData({ book: res.data, }) //wx.hideNavigationBarLoading() } } }) }, /** * 用户选择购物车商品 */ 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 console.log(id) wx.navigateTo({ url: "/pages/detail/detail?id=" + id }) }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { this.calculateTotal(); }, /** * 生命周期函数--监听页面显示 */ onShow: function(options) { var that = this wx.request({ url: "http://45.76.158.31:8080/web/AddCart.do?action=getinfo&UserID=1", 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() } } }) }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { }, /** * 点击结算触发 */ newAddress: function() { var that = this 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) { //向服务器传用户选中购买的东西生成订单 for (var i = 0; i < checkList.length; i++) { wx.request({ url: "http://45.76.158.31:8080/web/Buy.do?UserID=1&BookID="+checkList[i].BookID+"&Count="+checkList[i].Count, header: { "content-type": "json" }, success: function(res) { if (res.statusCode == 200) { console.log(res.data) that.setData({ book: res.data, }) //wx.hideNavigationBarLoading() wx.showToast({ title: '订单生成成功!', icon: 'success', duration: 2000 }) } } }) } setTimeout(function() { //要延时执行的代码 wx.navigateTo({ url: '/pages/order/order?typeId=0' }) }, 3000) } else { wx.showToast({ title: '没有选择商品', icon: 'success', duration: 2000 }) } } })