shopCart.1.js 7.0 KB

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