cart.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. requirejs(['vue','store','helper'],function(Vue,storeApi,$h){
  2. new Vue({
  3. el:"#store-cart",
  4. data:{
  5. validCartList:[],
  6. invalidCartList:[],
  7. totalPrice:0,
  8. checkedAll:true,
  9. changeStatus:false,
  10. loading:false
  11. },
  12. watch:{
  13. validCartList:{
  14. handler:function(){
  15. this.getTotalPrice();
  16. },
  17. deep:true
  18. }
  19. },
  20. methods:{
  21. cartNumTotal:function(){
  22. return this.validCartList.reduce(function(total,cart){
  23. return total+=cart.cart_num;
  24. },0);
  25. },
  26. getStoreUrl:function (cart) {
  27. return $h.U({
  28. c:'store',
  29. a:'detail',
  30. p:{id:cart.productInfo.id}
  31. });
  32. },
  33. cartCount:function(){
  34. return this.getCheckedCart().reduce(function(total,cart){
  35. return total+=cart.cart_num;
  36. },0);
  37. },
  38. checkedAllCart:function(){
  39. var that = this;
  40. var validCartList = this.validCartList.map(function(cart){
  41. if(cart.is_del !== true) cart.checked = that.checkedAll;
  42. });
  43. },
  44. checkedCart:function(cart){
  45. this.checkedAllStatus();
  46. },
  47. checkedAllStatus:function(){
  48. this.checkedAll = this.validCartList.length > 0 && this.getCheckedCart().length == this.validCartList.length;
  49. },
  50. getCheckedCart:function(){
  51. return this.validCartList.filter(function(cart){
  52. return cart.is_del != true && cart.checked == true;
  53. });
  54. },
  55. getTotalPrice:function(){
  56. this.totalPrice = this.getCheckedCart().reduce(function(total,cart){
  57. return $h.Add(total,$h.Mul(cart.cart_num,cart.truePrice));
  58. },0);
  59. },
  60. getCartList:function(){
  61. var that = this;
  62. storeApi.getCartList(function(cartGroup){
  63. cartGroup.valid.map(function(cart){
  64. cart.checked = true;
  65. cart.is_del = false;
  66. });
  67. that.checkedAll = cartGroup.valid.length > 0;
  68. that.validCartList = cartGroup.valid;
  69. that.invalidCartList = cartGroup.invalid;
  70. that.loading = true;
  71. });
  72. },
  73. getAttrValues:function (cart) {
  74. return cart.productInfo.attrInfo == undefined ? '' : '属性:'+cart.productInfo.attrInfo.suk;
  75. },
  76. changeCartNum:function(cart,index,changeNum){
  77. var num = +cart.cart_num + changeNum;
  78. if(num <= 0) num = 1;
  79. if(num > cart.trueStock){
  80. $h.pushMsgOnce('该商品库存不足'+num);
  81. num = cart.trueStock;
  82. }
  83. if(cart.cart_num != num){
  84. storeApi.changeCartNum(cart.id,num);
  85. cart.cart_num = num;
  86. this.$set(this.validCartList,index,cart);
  87. }
  88. },
  89. removeCart:function(){
  90. var ids = [],validCartList = [];
  91. this.validCartList.map(function(cart){
  92. if(cart.checked){
  93. cart.is_del = true;
  94. ids.push(cart.id);
  95. }else{
  96. validCartList.push(cart);
  97. }
  98. });
  99. if(ids.length) storeApi.removeCart(ids);
  100. this.$set(this,'validCartList',validCartList);
  101. this.$nextTick(function(){
  102. this.checkedAllStatus();
  103. this.changeStatus = false;
  104. });
  105. },
  106. submitCart:function(){
  107. var ids = this.getCheckedCart().map(function(cart){
  108. return cart.id;
  109. });
  110. if(!ids.length) return false;
  111. location.href = $h.U({
  112. c:'store',
  113. a:'confirm_order',
  114. p:{cartId:ids}
  115. });
  116. },
  117. removeInvalidCart:function(cart,index){
  118. storeApi.removeCart([cart.id]);
  119. this.invalidCartList.splice(index,1);
  120. }
  121. },
  122. mounted:function(){
  123. this.getCartList();
  124. }
  125. })
  126. });