car.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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.showLoading({
  85. title: '加载中...',
  86. })
  87. wx.request({
  88. url: host +'/queryShopCar',
  89. method:'post',
  90. header: {
  91. 'content-type': 'application/x-www-form-urlencoded'
  92. },
  93. data:{
  94. userId: app.globalData.user_id //app.globalData.user_id //'oovpNwjlsY6xx8ceCebFa1dOLd9E' //app.globalData.user_id,
  95. },
  96. success:function(res){
  97. console.log('购物车列表数据',res)
  98. let carList = [],imgArray=[];
  99. res.data.forEach((el,index)=>{
  100. imgArray =el.customGoodsImg.split(',');
  101. carList.push(el);
  102. carList[index]['imgArray'] = imgArray;
  103. carList[index]['checked'] = false;
  104. })
  105. that.setData({
  106. carList: carList
  107. })
  108. console.log(that.data.carList,"carListcarList")
  109. },
  110. complete(){
  111. wx.hideLoading()
  112. }
  113. })
  114. },
  115. carSubmit(){ // 提交订单
  116. let that = this, carList=[];
  117. if (that.data.checkStatic){
  118. that.data.carList.forEach(el=>{
  119. if(el.checked){
  120. carList.push(el)
  121. }
  122. })
  123. wx.setStorageSync('goodsList', carList);
  124. wx.setStorageSync('amount', that.data.amount);
  125. wx.navigateTo({
  126. url: '/customized/pages/accounts/accounts',
  127. })
  128. } else {
  129. wx.showToast({
  130. title: '您还没有选择商品',
  131. icon:'none'
  132. })
  133. }
  134. },
  135. totalPrice(that){
  136. let amount = 0, checkAll = true, checkStatic= false;
  137. that.data.carList.forEach(res=>{
  138. if(res.checked){
  139. amount += res.customgoodsPriceNow * res.customGoodsNum;
  140. checkStatic = true
  141. }
  142. if (!res.checked) {
  143. checkAll = false
  144. }
  145. })
  146. that.setData({
  147. amount:amount.toFixed(2),
  148. checkAll: checkAll,
  149. checkStatic: checkStatic
  150. })
  151. },
  152. previewImage(e){ // 点击图片放大
  153. let urls = [],that = this;
  154. console.log(e)
  155. e.target.dataset.srcarray.forEach(el=>{
  156. urls.push(that.data.host + el)
  157. })
  158. wx.previewImage({
  159. urls: urls,
  160. current:e.target.dataset.src,
  161. success(res){
  162. console.log(res,"放大成功")
  163. }
  164. })
  165. }
  166. })