shopCart.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. const getReq = require('./../../config.js').getReq;
  2. Page({
  3. data: {
  4. free_info: {
  5. type: Array
  6. },
  7. summary: {
  8. type: Array
  9. },
  10. cart_list: {
  11. type: Array
  12. },
  13. all_checked: false,
  14. allPrice: 0,
  15. allGoodsPrice: 0
  16. },
  17. onLoad: function () {
  18. },
  19. onShow: function () {
  20. let self = this;
  21. getReq({
  22. act: 'cart',
  23. op: 'list'
  24. }, function (res) {
  25. if (res.code == 200) {
  26. let free_info = res.datas.free_info;
  27. let summary_datas = res.datas.summary;
  28. let cart_list = res.datas.cart_list;
  29. let summary = [];
  30. for (let item of cart_list) {
  31. item.checked = false;
  32. }
  33. for (let i = 0; i < cart_list.length; i++) {
  34. for (let j = 0; j < summary_datas.length; j++) {
  35. if (cart_list[i].goods_id == summary_datas[j].goods_id) {
  36. summary.push(summary_datas[j]);
  37. }
  38. }
  39. }
  40. self.setData({
  41. free_info,
  42. summary,
  43. cart_list
  44. });
  45. }
  46. })
  47. },
  48. goods_checked(e) {
  49. let goods_id = e.currentTarget.dataset.goodsid;
  50. let cart_list = this.data.cart_list;
  51. let all_checked_state = 0;
  52. for (let item of cart_list) {
  53. if (item.goods_id == goods_id) {
  54. item.checked = !item.checked;
  55. }
  56. }
  57. for (let item of cart_list) {
  58. if (item.checked) {
  59. all_checked_state = all_checked_state + 1;
  60. }
  61. }
  62. if (all_checked_state == cart_list.length) {
  63. all_checked_state = true;
  64. }
  65. else {
  66. all_checked_state = false;
  67. }
  68. this.setData({
  69. cart_list,
  70. all_checked: all_checked_state
  71. });
  72. this.total();
  73. },
  74. all_checked() {
  75. let cart_list = this.data.cart_list;
  76. let all_checked_state = this.data.all_checked;
  77. for (let item of cart_list) {
  78. item.checked = !all_checked_state;
  79. }
  80. this.setData({
  81. cart_list,
  82. all_checked: !all_checked_state
  83. });
  84. this.total();
  85. },
  86. req_goods_num(cart_id, quantity,callback){
  87. let self = this;
  88. getReq({
  89. act: 'cart',
  90. op: 'edit',
  91. cart_id,
  92. quantity
  93. }, function (res) {
  94. if (res.code == 200) {
  95. let goods_num = res.datas.goods_num;
  96. let cart_list = self.data.cart_list;
  97. for (let item of cart_list) {
  98. if (item.cart_id == cart_id) {
  99. item.goods_num = goods_num;
  100. }
  101. }
  102. self.setData({
  103. cart_list
  104. });
  105. callback;
  106. for (let i = 0; i < self.data.cart_list.length;i++) {
  107. if (self.data.cart_list[i].checked) {
  108. self.total();
  109. break;
  110. }
  111. }
  112. }
  113. else {
  114. wx.showToast({
  115. title:res.message,
  116. icon:"none"
  117. })
  118. }
  119. })
  120. },
  121. goods_num_handle(e) {
  122. let self = this;
  123. let cart_id = e.target.dataset.cartid;
  124. let num = e.target.dataset.num;
  125. let compute_function = e.target.dataset.function;
  126. let goods_id = e.target.dataset.goodsid;
  127. let quantity = 1;
  128. if (compute_function == 'add') {
  129. quantity = num + quantity;
  130. this.req_goods_num(cart_id, quantity);
  131. }
  132. else {
  133. quantity = num - quantity;
  134. if (quantity <= 0) {
  135. wx.showModal({
  136. title: "提示",
  137. content: "确定要删除吗?",
  138. success: function (res) {
  139. if (res.confirm) {
  140. self.req_goods_num(cart_id, quantity, self.del_cart(cart_id, goods_id));
  141. } else if (res.cancel) {
  142. return;
  143. }
  144. }
  145. })
  146. }
  147. else {
  148. self.req_goods_num(cart_id, quantity);
  149. }
  150. }
  151. },
  152. del_cart(cart_id, goods_id) {
  153. let summary = this.data.summary;
  154. let cart_list = this.data.cart_list;
  155. for (let i = 0; i < summary.length;i++) {
  156. if (summary[i].goods_id == goods_id) {
  157. summary.splice(i,1);
  158. }
  159. }
  160. for (let i = 0; i < cart_list.length; i++) {
  161. if (cart_list[i].goods_id == goods_id) {
  162. cart_list.splice(i,1);
  163. }
  164. }
  165. this.setData({
  166. summary,
  167. cart_list
  168. })
  169. },
  170. total(){
  171. let self = this;
  172. let cart_item = [];
  173. for (let item of this.data.cart_list){
  174. if(item.checked){
  175. cart_item.push(item.cart_id);
  176. }
  177. }
  178. if(cart_item.length <= 0) {
  179. self.setData({
  180. allPrice:0,
  181. allGoodsPrice:0
  182. })
  183. }
  184. let carts = cart_item.join(",");
  185. getReq({
  186. act: 'member_buy',
  187. op: 'calc_cash',
  188. ifcart: 1,
  189. cart_id: carts
  190. }, function (res) {
  191. if (res.code == 200) {
  192. let datas = res.datas.payinfo;
  193. let allPrice = parseFloat((datas.goods_amount + datas.freight - datas.available_pred - datas.full_discount - datas.opgoods_discount).toFixed(2));
  194. let allGoodsPrice = parseFloat((datas.goods_amount).toFixed(2));
  195. self.setData({
  196. allPrice,
  197. allGoodsPrice
  198. })
  199. }
  200. })
  201. }
  202. })