shopCart.js 8.2 KB

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