shopCart.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. hasmore: false,
  17. curpage:0
  18. },
  19. onLoad: function () {
  20. },
  21. onShow: function () {
  22. let self = this;
  23. const curpage = this.data.curpage;
  24. getReq({
  25. act: 'cart',
  26. op: 'list',
  27. minest_cartid: curpage
  28. }, function (res) {
  29. if (res.code == 200) {
  30. let hasmore = res.datas.mobile_page.hasmore;
  31. let free_info = res.datas.free_info;
  32. let summary_datas = res.datas.summary;
  33. let cart_list = res.datas.cart_list;
  34. let lashCartListId = cart_list[cart_list.length - 1].cart_id;
  35. let summary = [];
  36. for (let item of cart_list) {
  37. item.checked = false;
  38. }
  39. for (let i = 0; i < cart_list.length; i++) {
  40. for (let j = 0; j < summary_datas.length; j++) {
  41. if (cart_list[i].goods_id == summary_datas[j].goods_id) {
  42. summary.push(summary_datas[j]);
  43. }
  44. }
  45. }
  46. if (hasmore) {
  47. self.setData({
  48. hasmore: true,
  49. curpage: lashCartListId
  50. })
  51. }
  52. self.setData({
  53. free_info,
  54. summary,
  55. cart_list
  56. });
  57. }
  58. })
  59. },
  60. goods_checked(e) {
  61. let goods_id = e.currentTarget.dataset.goodsid;
  62. let cart_list = this.data.cart_list;
  63. let all_checked_state = 0;
  64. for (let item of cart_list) {
  65. if (item.goods_id == goods_id) {
  66. item.checked = !item.checked;
  67. }
  68. }
  69. for (let item of cart_list) {
  70. if (item.checked) {
  71. all_checked_state = all_checked_state + 1;
  72. }
  73. }
  74. if (all_checked_state == cart_list.length) {
  75. all_checked_state = true;
  76. }
  77. else {
  78. all_checked_state = false;
  79. }
  80. this.setData({
  81. cart_list,
  82. all_checked: all_checked_state
  83. });
  84. this.total();
  85. },
  86. all_checked() {
  87. let cart_list = this.data.cart_list;
  88. let all_checked_state = this.data.all_checked;
  89. for (let item of cart_list) {
  90. item.checked = !all_checked_state;
  91. }
  92. this.setData({
  93. cart_list,
  94. all_checked: !all_checked_state
  95. });
  96. this.total();
  97. },
  98. req_goods_num(cart_id, quantity,callback){
  99. let self = this;
  100. getReq({
  101. act: 'cart',
  102. op: 'edit',
  103. cart_id,
  104. quantity
  105. }, function (res) {
  106. if (res.code == 200) {
  107. let goods_num = res.datas.goods_num;
  108. let cart_list = self.data.cart_list;
  109. for (let item of cart_list) {
  110. if (item.cart_id == cart_id) {
  111. item.goods_num = goods_num;
  112. }
  113. }
  114. self.setData({
  115. cart_list
  116. });
  117. callback;
  118. for (let i = 0; i < self.data.cart_list.length;i++) {
  119. if (self.data.cart_list[i].checked) {
  120. self.total();
  121. break;
  122. }
  123. }
  124. }
  125. else {
  126. wx.showToast({
  127. title:res.message,
  128. icon:"none"
  129. })
  130. }
  131. })
  132. },
  133. goods_num_handle(e) {
  134. let self = this;
  135. let cart_id = e.target.dataset.cartid;
  136. let num = e.target.dataset.num;
  137. let compute_function = e.target.dataset.function;
  138. let goods_id = e.target.dataset.goodsid;
  139. let quantity = 1;
  140. if (compute_function == 'add') {
  141. quantity = num + quantity;
  142. this.req_goods_num(cart_id, quantity);
  143. }
  144. else {
  145. quantity = num - quantity;
  146. if (quantity <= 0) {
  147. wx.showModal({
  148. title: "提示",
  149. content: "确定要删除吗?",
  150. success: function (res) {
  151. if (res.confirm) {
  152. self.req_goods_num(cart_id, quantity, self.del_cart(cart_id, goods_id));
  153. } else if (res.cancel) {
  154. return;
  155. }
  156. }
  157. })
  158. }
  159. else {
  160. self.req_goods_num(cart_id, quantity);
  161. }
  162. }
  163. },
  164. del_cart(cart_id, goods_id) {
  165. let summary = this.data.summary;
  166. let cart_list = this.data.cart_list;
  167. for (let i = 0; i < summary.length;i++) {
  168. if (summary[i].goods_id == goods_id) {
  169. summary.splice(i,1);
  170. }
  171. }
  172. for (let i = 0; i < cart_list.length; i++) {
  173. if (cart_list[i].goods_id == goods_id) {
  174. cart_list.splice(i,1);
  175. }
  176. }
  177. this.setData({
  178. summary,
  179. cart_list
  180. })
  181. },
  182. total(){
  183. let self = this;
  184. let cart_item = [];
  185. for (let item of this.data.cart_list){
  186. if(item.checked){
  187. cart_item.push(item.cart_id);
  188. }
  189. }
  190. if(cart_item.length <= 0) {
  191. self.setData({
  192. allPrice:0,
  193. allGoodsPrice:0
  194. })
  195. }
  196. let carts = cart_item.join(",");
  197. getReq({
  198. act: 'member_buy',
  199. op: 'calc_cash',
  200. ifcart: 1,
  201. cart_id: carts
  202. }, function (res) {
  203. if (res.code == 200) {
  204. let datas = res.datas.payinfo;
  205. let allPrice = parseFloat((datas.goods_amount + datas.freight - datas.available_pred - datas.full_discount - datas.opgoods_discount).toFixed(2));
  206. let allGoodsPrice = parseFloat((datas.goods_amount).toFixed(2));
  207. self.setData({
  208. allPrice,
  209. allGoodsPrice
  210. })
  211. }
  212. })
  213. },
  214. onReachBottom(){
  215. let self = this;
  216. let hasmore = this.data.hasmore;
  217. if(hasmore) {
  218. let req_curpage = this.data.curpage;
  219. getReq({
  220. act: 'cart',
  221. op: 'list',
  222. minest_cartid: req_curpage
  223. }, function (res) {
  224. self.setData({
  225. curpage: req_curpage
  226. })
  227. console.log(res);
  228. })
  229. }
  230. },
  231. settlement(){
  232. const cart_id_list = [];
  233. for (let cart_item of this.data.cart_list) {
  234. if (cart_item.checked) {
  235. cart_id_list.push(cart_item.cart_id);
  236. }
  237. }
  238. let cart_list = cart_id_list.join(',');
  239. wx.navigateTo({
  240. url: `/pages/confirmOrder/confirmOrder?iscart=1&cart_id=${cart_list}`
  241. })
  242. }
  243. })