cart.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. var app = getApp();
  4. Page({
  5. data: {
  6. cartGoods: [],
  7. cartTotal: {
  8. "goodsCount": 0,
  9. "goodsAmount": 0.00,
  10. "checkedGoodsCount": 0,
  11. "checkedGoodsAmount": 0.00
  12. },
  13. isEditCart: false,
  14. checkedAllStatus: true,
  15. editCartList: []
  16. },
  17. onLoad: function (options) {
  18. // 页面初始化 options为页面跳转所带来的参数
  19. },
  20. onReady: function () {
  21. // 页面渲染完成
  22. },
  23. onShow: function () {
  24. // 页面显示
  25. this.getCartList();
  26. },
  27. onHide: function () {
  28. // 页面隐藏
  29. },
  30. onUnload: function () {
  31. // 页面关闭
  32. },
  33. getCartList: function () {
  34. let that = this;
  35. util.request(api.CartList).then(function (res) {
  36. if (res.errno === 0) {
  37. console.log(res.data);
  38. that.setData({
  39. cartGoods: res.data.cartList,
  40. cartTotal: res.data.cartTotal
  41. });
  42. }
  43. that.setData({
  44. checkedAllStatus: that.isCheckedAll()
  45. });
  46. });
  47. },
  48. isCheckedAll: function () {
  49. //判断购物车商品已全选
  50. return this.data.cartGoods.every(function (element, index, array) {
  51. if (element.checked == true) {
  52. return true;
  53. } else {
  54. return false;
  55. }
  56. });
  57. },
  58. checkedItem: function (event) {
  59. let itemIndex = event.target.dataset.itemIndex;
  60. let that = this;
  61. if (!this.data.isEditCart) {
  62. util.request(api.CartChecked, { productIds: that.data.cartGoods[itemIndex].product_id, isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1 }, 'POST').then(function (res) {
  63. if (res.errno === 0) {
  64. console.log(res.data);
  65. that.setData({
  66. cartGoods: res.data.cartList,
  67. cartTotal: res.data.cartTotal
  68. });
  69. }
  70. that.setData({
  71. checkedAllStatus: that.isCheckedAll()
  72. });
  73. });
  74. } else {
  75. //编辑状态
  76. let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
  77. if (index == itemIndex){
  78. element.checked = !element.checked;
  79. }
  80. return element;
  81. });
  82. that.setData({
  83. cartGoods: tmpCartData,
  84. checkedAllStatus: that.isCheckedAll(),
  85. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  86. });
  87. }
  88. },
  89. getCheckedGoodsCount: function(){
  90. let checkedGoodsCount = 0;
  91. this.data.cartGoods.forEach(function (v) {
  92. if (v.checked === true) {
  93. checkedGoodsCount += v.number;
  94. }
  95. });
  96. console.log(checkedGoodsCount);
  97. return checkedGoodsCount;
  98. },
  99. checkedAll: function () {
  100. let that = this;
  101. if (!this.data.isEditCart) {
  102. var productIds = this.data.cartGoods.map(function (v) {
  103. return v.product_id;
  104. });
  105. util.request(api.CartChecked, { productIds: productIds.join(','), isChecked: that.isCheckedAll() ? 0 : 1 }, 'POST').then(function (res) {
  106. if (res.errno === 0) {
  107. console.log(res.data);
  108. that.setData({
  109. cartGoods: res.data.cartList,
  110. cartTotal: res.data.cartTotal
  111. });
  112. }
  113. that.setData({
  114. checkedAllStatus: that.isCheckedAll()
  115. });
  116. });
  117. } else {
  118. //编辑状态
  119. let checkedAllStatus = that.isCheckedAll();
  120. let tmpCartData = this.data.cartGoods.map(function (v) {
  121. v.checked = !checkedAllStatus;
  122. return v;
  123. });
  124. that.setData({
  125. cartGoods: tmpCartData,
  126. checkedAllStatus: that.isCheckedAll(),
  127. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  128. });
  129. }
  130. },
  131. editCart: function () {
  132. var that = this;
  133. if (this.data.isEditCart) {
  134. this.getCartList();
  135. this.setData({
  136. isEditCart: !this.data.isEditCart
  137. });
  138. } else {
  139. //编辑状态
  140. let tmpCartList = this.data.cartGoods.map(function (v) {
  141. v.checked = false;
  142. return v;
  143. });
  144. this.setData({
  145. editCartList: this.data.cartGoods,
  146. cartGoods: tmpCartList,
  147. isEditCart: !this.data.isEditCart,
  148. checkedAllStatus: that.isCheckedAll(),
  149. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  150. });
  151. }
  152. },
  153. updateCart: function (productId, goodsId, number, id) {
  154. let that = this;
  155. util.request(api.CartUpdate, {
  156. productId: productId,
  157. goodsId: goodsId,
  158. number: number,
  159. id: id
  160. }, 'POST').then(function (res) {
  161. if (res.errno === 0) {
  162. console.log(res.data);
  163. that.setData({
  164. //cartGoods: res.data.cartList,
  165. //cartTotal: res.data.cartTotal
  166. });
  167. }
  168. that.setData({
  169. checkedAllStatus: that.isCheckedAll()
  170. });
  171. });
  172. },
  173. cutNumber: function (event) {
  174. let itemIndex = event.target.dataset.itemIndex;
  175. let cartItem = this.data.cartGoods[itemIndex];
  176. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  177. cartItem.number = number;
  178. this.setData({
  179. cartGoods: this.data.cartGoods
  180. });
  181. this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  182. },
  183. addNumber: function (event) {
  184. let itemIndex = event.target.dataset.itemIndex;
  185. let cartItem = this.data.cartGoods[itemIndex];
  186. let number = cartItem.number + 1;
  187. cartItem.number = number;
  188. this.setData({
  189. cartGoods: this.data.cartGoods
  190. });
  191. this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  192. },
  193. checkoutOrder: function () {
  194. //获取已选择的商品
  195. let that = this;
  196. var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
  197. if (element.checked == true) {
  198. return true;
  199. } else {
  200. return false;
  201. }
  202. });
  203. if (checkedGoods.length <= 0) {
  204. return false;
  205. }
  206. wx.navigateTo({
  207. url: '../shopping/checkout/checkout'
  208. })
  209. },
  210. deleteCart: function () {
  211. //获取已选择的商品
  212. let that = this;
  213. let productIds = this.data.cartGoods.filter(function (element, index, array) {
  214. if (element.checked == true) {
  215. return true;
  216. } else {
  217. return false;
  218. }
  219. });
  220. if (productIds.length <= 0) {
  221. return false;
  222. }
  223. productIds = productIds.map(function (element, index, array) {
  224. if (element.checked == true) {
  225. return element.product_id;
  226. }
  227. });
  228. util.request(api.CartDelete, {
  229. productIds: productIds.join(',')
  230. }, 'POST').then(function (res) {
  231. if (res.errno === 0) {
  232. console.log(res.data);
  233. let cartList = res.data.cartList.map(v => {
  234. console.log(v);
  235. v.checked = false;
  236. return v;
  237. });
  238. that.setData({
  239. cartGoods: cartList,
  240. cartTotal: res.data.cartTotal
  241. });
  242. }
  243. that.setData({
  244. checkedAllStatus: that.isCheckedAll()
  245. });
  246. });
  247. }
  248. })