cart.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. that.setData({
  38. cartGoods: res.data.cartList,
  39. cartTotal: res.data.cartTotal
  40. });
  41. }
  42. that.setData({
  43. checkedAllStatus: that.isCheckedAll()
  44. });
  45. });
  46. },
  47. isCheckedAll: function () {
  48. //判断购物车商品已全选
  49. return this.data.cartGoods.every(function (element, index, array) {
  50. if (element.checked == true) {
  51. return true;
  52. } else {
  53. return false;
  54. }
  55. });
  56. },
  57. checkedItem: function (event) {
  58. let itemIndex = event.target.dataset.itemIndex;
  59. let that = this;
  60. if (!this.data.isEditCart) {
  61. util.request(api.CartChecked, { productIds: that.data.cartGoods[itemIndex].product_id, isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1 }, "POST", "application/json").then(function (res) {
  62. if (res.errno === 0) {
  63. that.setData({
  64. cartGoods: res.data.cartList,
  65. cartTotal: res.data.cartTotal
  66. });
  67. }
  68. that.setData({
  69. checkedAllStatus: that.isCheckedAll()
  70. });
  71. });
  72. } else {
  73. //编辑状态
  74. let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
  75. if (index == itemIndex){
  76. element.checked = !element.checked;
  77. }
  78. return element;
  79. });
  80. that.setData({
  81. cartGoods: tmpCartData,
  82. checkedAllStatus: that.isCheckedAll(),
  83. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  84. });
  85. }
  86. },
  87. getCheckedGoodsCount: function(){
  88. let checkedGoodsCount = 0;
  89. this.data.cartGoods.forEach(function (v) {
  90. if (v.checked === true) {
  91. checkedGoodsCount += v.number;
  92. }
  93. });
  94. return checkedGoodsCount;
  95. },
  96. checkedAll: function () {
  97. let that = this;
  98. if (!this.data.isEditCart) {
  99. var productIds = this.data.cartGoods.map(function (v) {
  100. return v.product_id;
  101. });
  102. util.request(api.CartChecked, { productIds: productIds.join(','), isChecked: that.isCheckedAll() ? 0 : 1 }, "POST", "application/json").then(function (res) {
  103. if (res.errno === 0) {
  104. that.setData({
  105. cartGoods: res.data.cartList,
  106. cartTotal: res.data.cartTotal
  107. });
  108. }
  109. that.setData({
  110. checkedAllStatus: that.isCheckedAll()
  111. });
  112. });
  113. } else {
  114. //编辑状态
  115. let checkedAllStatus = that.isCheckedAll();
  116. let tmpCartData = this.data.cartGoods.map(function (v) {
  117. v.checked = !checkedAllStatus;
  118. return v;
  119. });
  120. that.setData({
  121. cartGoods: tmpCartData,
  122. checkedAllStatus: that.isCheckedAll(),
  123. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  124. });
  125. }
  126. },
  127. editCart: function () {
  128. var that = this;
  129. if (this.data.isEditCart) {
  130. this.getCartList();
  131. this.setData({
  132. isEditCart: !this.data.isEditCart
  133. });
  134. } else {
  135. //编辑状态
  136. let tmpCartList = this.data.cartGoods.map(function (v) {
  137. v.checked = false;
  138. return v;
  139. });
  140. this.setData({
  141. editCartList: this.data.cartGoods,
  142. cartGoods: tmpCartList,
  143. isEditCart: !this.data.isEditCart,
  144. checkedAllStatus: that.isCheckedAll(),
  145. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  146. });
  147. }
  148. },
  149. toIndexPage: function () {
  150. wx.switchTab({
  151. url: "/pages/index/index"
  152. });
  153. },
  154. updateCart: function (productId, goodsId, number, id) {
  155. let that = this;
  156. util.request(api.CartUpdate, {
  157. productId: productId,
  158. goodsId: goodsId,
  159. number: number,
  160. id: id
  161. }).then(function (res) {
  162. if (res.errno === 0) {
  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', 'application/json').then(function (res) {
  231. if (res.errno === 0) {
  232. let cartList = res.data.cartList.map(v => {
  233. v.checked = false;
  234. return v;
  235. });
  236. that.setData({
  237. cartGoods: cartList,
  238. cartTotal: res.data.cartTotal
  239. });
  240. }
  241. that.setData({
  242. checkedAllStatus: that.isCheckedAll()
  243. });
  244. });
  245. }
  246. })