car.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. checkStatic:false, //默认一个都没有选择
  14. },
  15. /**
  16. * 生命周期函数--监听页面加载
  17. */
  18. onLoad: function (options) {
  19. },
  20. /**
  21. * 生命周期函数--监听页面初次渲染完成
  22. */
  23. onReady: function () {
  24. let that = this;
  25. that.getCarlist(that)
  26. },
  27. /**
  28. * 生命周期函数--监听页面显示
  29. */
  30. // onShow: function () {
  31. // },
  32. subtraction(e){ // 减少
  33. let that = this;
  34. console.log(e)
  35. let index = e.target.dataset.index,carList = that.data.carList;
  36. if (carList[index].customGoodsNum > 1){
  37. // 表示最小数量,不可减少
  38. carList[index].customGoodsNum--;
  39. that.setData({
  40. carList: carList
  41. })
  42. }
  43. that.totalPrice(that)
  44. },
  45. add(e) { // 减少
  46. let that = this;
  47. console.log(e)
  48. let index = e.target.dataset.index, carList = that.data.carList;
  49. carList[index].customGoodsNum++;
  50. that.setData({
  51. carList: carList
  52. })
  53. that.totalPrice(that)
  54. },
  55. checkboxChange(e){
  56. let index = e.target.dataset.index, that = this, carList = that.data.carList;
  57. if (e.detail.value[0]){ // 表示价格增加
  58. that.data.carList[index].checked = true; // 表示选中
  59. } else {
  60. that.data.carList[index].checked = false; // 表示选中
  61. }
  62. that.setData({
  63. carList: carList
  64. })
  65. that.totalPrice(that)
  66. },
  67. checkboxChangeAll(e){ // 全选
  68. console.log(e)
  69. let that = this, checkAll= that.data.checkAll , carList=that.data.carList;
  70. if (e.detail.value[0]) {
  71. checkAll = true; // 表示选中
  72. } else {
  73. checkAll = false;
  74. }
  75. carList.forEach(res => {
  76. res.checked = checkAll;
  77. })
  78. that.setData({
  79. carList:carList
  80. })
  81. that.totalPrice(that)
  82. },
  83. getCarlist(that){
  84. wx.request({
  85. url: host +'/queryShopCar',
  86. method:'post',
  87. header: {
  88. 'content-type': 'application/x-www-form-urlencoded'
  89. },
  90. data:{
  91. userId: app.globalData.user_id //app.globalData.user_id //'oovpNwjlsY6xx8ceCebFa1dOLd9E' //app.globalData.user_id,
  92. },
  93. success:function(res){
  94. console.log('购物车列表数据',res)
  95. let carList = [],imgArray=[];
  96. res.data.forEach((el,index)=>{
  97. imgArray =el.customGoodsImg.split(',');
  98. carList.push(el);
  99. carList[index]['imgArray'] = imgArray;
  100. carList[index]['checked'] = false;
  101. })
  102. that.setData({
  103. carList: carList
  104. })
  105. console.log(that.data.carList,"carListcarList")
  106. }
  107. })
  108. },
  109. carSubmit(){ // 提交订单
  110. let that = this, carList=[];
  111. if (that.data.checkStatic){
  112. that.data.carList.forEach(el=>{
  113. if(el.checked){
  114. carList.push(el)
  115. }
  116. })
  117. wx.setStorageSync('goodsList', carList);
  118. wx.setStorageSync('amount', that.data.amount);
  119. wx.navigateTo({
  120. url: '/customized/pages/order/order',
  121. })
  122. } else {
  123. wx.showToast({
  124. title: '您还没有选择商品',
  125. icon:'none'
  126. })
  127. }
  128. },
  129. totalPrice(that){
  130. let amount = 0, checkAll = true, checkStatic= false;
  131. that.data.carList.forEach(res=>{
  132. if(res.checked){
  133. amount += res.customgoodsPriceNow * res.customGoodsNum;
  134. checkStatic = true
  135. }
  136. if (!res.checked) {
  137. checkAll = false
  138. }
  139. })
  140. that.setData({
  141. amount:amount.toFixed(2),
  142. checkAll: checkAll,
  143. checkStatic: checkStatic
  144. })
  145. },
  146. previewImage(e){ // 点击图片放大
  147. let urls = [],that = this;
  148. console.log(e)
  149. e.target.dataset.srcarray.forEach(el=>{
  150. urls.push(that.data.host + el)
  151. })
  152. wx.previewImage({
  153. urls: urls,
  154. current:'e.target.dataset.src',
  155. success(res){
  156. console.log(res,"放大成功")
  157. }
  158. })
  159. }
  160. })