basket.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // pages/basket/basket.js
  2. var http = require("../../utils/http.js");
  3. // var config = require("../../utils/config.js");
  4. const Big = require("../../utils/big.min.js");
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. // picDomain: config.picDomain,
  11. shopCartItemDiscounts: [],
  12. finalMoney: 0,
  13. totalMoney: 0,
  14. subtractMoney: 0,
  15. allChecked: false
  16. },
  17. /**
  18. * 生命周期函数--监听页面加载
  19. */
  20. onLoad: function (options) {
  21. },
  22. /**
  23. * 生命周期函数--监听页面初次渲染完成
  24. */
  25. onReady: function () {
  26. },
  27. /**
  28. * 生命周期函数--监听页面显示
  29. */
  30. onShow: function () {
  31. this.loadBasketData();
  32. http.getCartCount();//重新计算购物车总数量
  33. },
  34. loadBasketData(){
  35. wx.showLoading(); //加载购物车
  36. var params = {
  37. url: "/p/shopCart/info",
  38. method: "POST",
  39. data: {},
  40. callBack: res => {
  41. if (res.length > 0) {
  42. // 默认不选中
  43. var shopCartItemDiscounts = res[0].shopCartItemDiscounts;
  44. shopCartItemDiscounts.forEach(shopCartItemDiscount => {
  45. shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
  46. shopCartItem.checked = false;
  47. });
  48. });
  49. this.setData({
  50. shopCartItemDiscounts: shopCartItemDiscounts,
  51. allChecked: false
  52. });
  53. } else {
  54. this.setData({
  55. shopCartItemDiscounts: []
  56. });
  57. }
  58. this.calTotalPrice(); //计算总价
  59. wx.hideLoading();
  60. }
  61. };
  62. http.request(params);
  63. },
  64. /**
  65. * 去结算
  66. */
  67. toFirmOrder: function () {
  68. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
  69. var basketIds = [];
  70. shopCartItemDiscounts.forEach(shopCartItemDiscount => {
  71. shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
  72. if (shopCartItem.checked) {
  73. basketIds.push(shopCartItem.basketId)
  74. }
  75. })
  76. })
  77. if (!basketIds.length) {
  78. wx.showToast({
  79. title: '请选择商品',
  80. icon: "none"
  81. })
  82. return
  83. }
  84. wx.setStorageSync("basketIds", JSON.stringify(basketIds));
  85. wx.navigateTo({
  86. url: '/pages/submit-order/submit-order?orderEntry=0',
  87. })
  88. },
  89. /**
  90. * 全选
  91. */
  92. onSelAll: function () {
  93. var allChecked = this.data.allChecked;
  94. allChecked = !allChecked; //改变状态
  95. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
  96. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  97. var cItems = shopCartItemDiscounts[i].shopCartItems;
  98. for (var j = 0; j < cItems.length; j++) {
  99. cItems[j].checked = allChecked;
  100. }
  101. }
  102. this.setData({
  103. allChecked: allChecked,
  104. shopCartItemDiscounts: shopCartItemDiscounts
  105. });
  106. this.calTotalPrice();//计算总价
  107. },
  108. /**
  109. * 每一项的选择事件
  110. */
  111. onSelectedItem: function (e) {
  112. var index = e.currentTarget.dataset.index;// 获取data- 传进来的index
  113. var scindex = e.currentTarget.dataset.scindex;
  114. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;// 获取购物车列表
  115. var checked = shopCartItemDiscounts[scindex].shopCartItems[index].checked; // 获取当前商品的选中状态
  116. shopCartItemDiscounts[scindex].shopCartItems[index].checked = !checked; // 改变状态
  117. this.setData({
  118. shopCartItemDiscounts: shopCartItemDiscounts
  119. });
  120. this.checkAllSelected();//检查全选状态
  121. this.calTotalPrice();//计算总价
  122. },
  123. /**
  124. * 检查全选状态
  125. */
  126. checkAllSelected: function () {
  127. var allChecked = true;
  128. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
  129. var flag = false;
  130. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  131. var cItems = shopCartItemDiscounts[i].shopCartItems;
  132. for (var j = 0; j < cItems.length; j++) {
  133. if (!cItems[j].checked) {
  134. allChecked = !allChecked;
  135. flag = true;
  136. break;
  137. }
  138. }
  139. if(flag){
  140. break;
  141. }
  142. }
  143. this.setData({
  144. allChecked: allChecked
  145. });
  146. },
  147. /**
  148. * 计算购物车总额
  149. */
  150. calTotalPrice: function () {
  151. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
  152. var shopCartIds = [];
  153. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  154. var cItems = shopCartItemDiscounts[i].shopCartItems;
  155. for (var j = 0; j < cItems.length; j++) {
  156. if (cItems[j].checked) {
  157. shopCartIds.push(cItems[j].basketId);
  158. }
  159. }
  160. }
  161. var ths = this;
  162. wx.showLoading();
  163. var params = {
  164. url: "/p/shopCart/totalPay",
  165. method: "POST",
  166. data: shopCartIds,
  167. callBack: function (res) {
  168. ths.setData({
  169. finalMoney: res.finalMoney,
  170. totalMoney: res.totalMoney,
  171. subtractMoney: res.subtractMoney
  172. });
  173. wx.hideLoading();
  174. }
  175. };
  176. http.request(params);
  177. },
  178. /**
  179. * 减少数量
  180. */
  181. onCountMinus: function (e) {
  182. var index = e.currentTarget.dataset.index;
  183. var scindex = e.currentTarget.dataset.scindex;
  184. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
  185. var prodCount = shopCartItemDiscounts[scindex].shopCartItems[index].prodCount;
  186. if (prodCount > 1) {
  187. this.updateCount(shopCartItemDiscounts, scindex, index, -1);
  188. }
  189. },
  190. /**
  191. * 增加数量
  192. */
  193. onCountPlus: function (e) {
  194. var index = e.currentTarget.dataset.index;
  195. var scindex = e.currentTarget.dataset.scindex;
  196. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
  197. this.updateCount(shopCartItemDiscounts, scindex, index, 1);
  198. },
  199. /**
  200. * 改变购物车数量接口
  201. */
  202. updateCount: function (shopCartItemDiscounts, scindex, index, prodCount) {
  203. var ths = this;
  204. wx.showLoading({
  205. mask: true
  206. });
  207. var params = {
  208. url: "/p/shopCart/changeItem",
  209. method: "POST",
  210. data: {
  211. count: prodCount,
  212. prodId: shopCartItemDiscounts[scindex].shopCartItems[index].prodId,
  213. skuId: shopCartItemDiscounts[scindex].shopCartItems[index].skuId,
  214. shopId: 1
  215. },
  216. callBack: function (res) {
  217. shopCartItemDiscounts[scindex].shopCartItems[index].prodCount += prodCount;
  218. ths.setData({
  219. shopCartItemDiscounts: shopCartItemDiscounts
  220. });
  221. ths.calTotalPrice();//计算总价
  222. wx.hideLoading();
  223. http.getCartCount();//重新计算购物车总数量
  224. }
  225. };
  226. http.request(params);
  227. },
  228. /**
  229. * 删除购物车商品
  230. */
  231. onDelBasket: function () {
  232. var ths = this;
  233. var shopCartItemDiscounts = this.data.shopCartItemDiscounts;
  234. var basketIds = [];
  235. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  236. var cItems = shopCartItemDiscounts[i].shopCartItems;
  237. for (var j = 0; j < cItems.length; j++) {
  238. if (cItems[j].checked) {
  239. basketIds.push(cItems[j].basketId);
  240. }
  241. }
  242. }
  243. if (basketIds.length == 0) {
  244. wx.showToast({
  245. title: '请选择商品',
  246. icon: "none"
  247. })
  248. } else {
  249. wx.showModal({
  250. title: '',
  251. content: '确认要删除选中的商品吗?',
  252. confirmColor: "#eb2444",
  253. success(res) {
  254. if (res.confirm) {
  255. wx.showLoading({
  256. mask: true
  257. });
  258. var params = {
  259. url: "/p/shopCart/deleteItem",
  260. method: "DELETE",
  261. data: basketIds,
  262. callBack: function (res) {
  263. wx.hideLoading();
  264. ths.loadBasketData();
  265. }
  266. };
  267. http.request(params);
  268. }
  269. }
  270. })
  271. }
  272. }
  273. })