shopCart.js 6.5 KB

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