cart.js 7.3 KB

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