shopCart.js 9.1 KB

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