car.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // customized/pages/made/car/car.js
  2. let app = getApp();
  3. let host = app.globalData.servsers; // 请求的url
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. carList:[],// 购物车列表数组
  10. host: app.globalData.servsers,
  11. amount:0, //总价
  12. checkAll:false, // 全选
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad: function (options) {
  18. },
  19. /**
  20. * 生命周期函数--监听页面初次渲染完成
  21. */
  22. onReady: function () {
  23. },
  24. /**
  25. * 生命周期函数--监听页面显示
  26. */
  27. onShow: function () {
  28. let that = this;
  29. that.getCarlist(that)
  30. },
  31. /**
  32. * 生命周期函数--监听页面隐藏
  33. */
  34. onHide: function () {
  35. },
  36. /**
  37. * 生命周期函数--监听页面卸载
  38. */
  39. onUnload: function () {
  40. },
  41. /**
  42. * 页面相关事件处理函数--监听用户下拉动作
  43. */
  44. onPullDownRefresh: function () {
  45. },
  46. /**
  47. * 页面上拉触底事件的处理函数
  48. */
  49. onReachBottom: function () {
  50. },
  51. /**
  52. * 用户点击右上角分享
  53. */
  54. onShareAppMessage: function () {
  55. },
  56. subtraction(e){ // 减少
  57. let that = this;
  58. console.log(e)
  59. let index = e.target.dataset.index,carList = that.data.carList;
  60. if (carList[index].customGoodsNum > 1){
  61. // 表示最小数量,不可减少
  62. carList[index].customGoodsNum--;
  63. that.setData({
  64. carList: carList
  65. })
  66. }
  67. that.totalPrice(that)
  68. },
  69. add(e) { // 减少
  70. let that = this;
  71. console.log(e)
  72. let index = e.target.dataset.index, carList = that.data.carList;
  73. carList[index].customGoodsNum++;
  74. that.setData({
  75. carList: carList
  76. })
  77. that.totalPrice(that)
  78. },
  79. checkboxChange(e){
  80. let index = e.target.dataset.index, that = this, carList = that.data.carList;
  81. if (e.detail.value[0]){ // 表示价格增加
  82. that.data.carList[index].checked = true; // 表示选中
  83. } else {
  84. that.data.carList[index].checked = false; // 表示选中
  85. }
  86. that.setData({
  87. carList: carList
  88. })
  89. that.totalPrice(that)
  90. },
  91. getCarlist(that){
  92. wx.request({
  93. url: host +'/queryShopCar',
  94. method:'post',
  95. header: {
  96. 'content-type': 'application/x-www-form-urlencoded'
  97. },
  98. data:{
  99. userId: 'oovpNwjlsY6xx8ceCebFa1dOLd9E' //app.globalData.user_id,
  100. },
  101. success:function(res){
  102. console.log('购物车列表数据',res)
  103. let carList = [],imgArray=[];
  104. res.data.forEach((el,index)=>{
  105. imgArray =el.customGoodsImg.split(',');
  106. carList.push(el);
  107. carList[index]['imgArray'] = imgArray;
  108. carList[index]['checked'] = false;
  109. })
  110. that.setData({
  111. carList: carList
  112. })
  113. console.log(that.data.carList,"carListcarList")
  114. }
  115. })
  116. },
  117. totalPrice(that){
  118. let amount = 0,checkAll = true;
  119. that.data.carList.forEach(res=>{
  120. if(res.checked){
  121. amount += res.customgoodsPriceNow * res.customGoodsNum
  122. }
  123. if (!res.checked) {
  124. checkAll = false
  125. }
  126. })
  127. that.setData({
  128. amount:amount.toFixed(2),
  129. checkAll: checkAll
  130. })
  131. }
  132. })