shopCart.js 9.4 KB

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