car.js 4.1 KB

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