cart.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. var util = require('../../utils/util.js');
  2. var api = require('../../config/api.js');
  3. var app = getApp();
  4. import { Router } from '../../utils/router.js'
  5. Router({
  6. data: {
  7. cartGoods: [],
  8. cartTotal: {
  9. "goodsCount": 0,
  10. "goodsAmount": 0.00,
  11. "checkedGoodsCount": 0,
  12. "checkedGoodsAmount": 0.00
  13. },
  14. isEditCart: false,
  15. checkedAllStatus: true,
  16. editCartList: []
  17. },
  18. onLoad: function (options) {
  19. // 页面初始化 options为页面跳转所带来的参数
  20. // this.getCartList();
  21. },
  22. onReady: function () {
  23. // 页面渲染完成
  24. },
  25. onShow: function () {
  26. // 页面显示
  27. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  28. this.getTabBar().setData({
  29. selected: 2
  30. })
  31. }
  32. let isLogin = wx.getStorageSync('isLogin')
  33. this.getCartList();
  34. },
  35. onPullDownRefresh(){
  36. this.getCartList();
  37. },
  38. onHide: function () {
  39. // 页面隐藏
  40. },
  41. onUnload: function () {
  42. // 页面关闭
  43. },
  44. getCartList: function () {
  45. let that = this;
  46. util.request(api.CartList).then(function (res) {
  47. if (res.errno === 0) {
  48. that.setData({
  49. cartGoods: res.data.cartList,
  50. cartTotal: res.data.cartTotal
  51. });
  52. }
  53. that.setData({
  54. checkedAllStatus: that.isCheckedAll()
  55. });
  56. });
  57. },
  58. isCheckedAll: function () {
  59. //判断购物车商品已全选
  60. return this.data.cartGoods.every(function (element, index, array) {
  61. if (element.checked == true) {
  62. return true;
  63. } else {
  64. return false;
  65. }
  66. });
  67. },
  68. checkedItem: function (event) {
  69. let itemIndex = event.currentTarget.dataset.itemIndex;
  70. let that = this;
  71. if (!this.data.isEditCart) {
  72. util.request(api.CartChecked, { productIds: that.data.cartGoods[itemIndex].product_id, isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1 }, "POST", "application/json").then(function (res) {
  73. if (res.errno === 0) {
  74. that.setData({
  75. cartGoods: res.data.cartList,
  76. cartTotal: res.data.cartTotal
  77. });
  78. }
  79. that.setData({
  80. checkedAllStatus: that.isCheckedAll()
  81. });
  82. });
  83. } else {
  84. //编辑状态
  85. let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
  86. if (index == itemIndex){
  87. element.checked = !element.checked;
  88. }
  89. return element;
  90. });
  91. that.setData({
  92. cartGoods: tmpCartData,
  93. checkedAllStatus: that.isCheckedAll(),
  94. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  95. });
  96. }
  97. },
  98. getCheckedGoodsCount: function(){
  99. let checkedGoodsCount = 0;
  100. this.data.cartGoods.forEach(function (v) {
  101. if (v.checked === true) {
  102. checkedGoodsCount += v.number;
  103. }
  104. });
  105. return checkedGoodsCount;
  106. },
  107. checkedAll: function () {
  108. let that = this;
  109. if (!this.data.isEditCart) {
  110. var productIds = this.data.cartGoods.map(function (v) {
  111. return v.product_id;
  112. });
  113. util.request(api.CartChecked, { productIds: productIds.join(','), isChecked: that.isCheckedAll() ? 0 : 1 }, "POST", "application/json").then(function (res) {
  114. if (res.errno === 0) {
  115. that.setData({
  116. cartGoods: res.data.cartList,
  117. cartTotal: res.data.cartTotal
  118. });
  119. }
  120. that.setData({
  121. checkedAllStatus: that.isCheckedAll()
  122. });
  123. });
  124. } else {
  125. //编辑状态
  126. let checkedAllStatus = that.isCheckedAll();
  127. let tmpCartData = this.data.cartGoods.map(function (v) {
  128. v.checked = !checkedAllStatus;
  129. return v;
  130. });
  131. that.setData({
  132. cartGoods: tmpCartData,
  133. checkedAllStatus: that.isCheckedAll(),
  134. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  135. });
  136. }
  137. },
  138. editCart: function () {
  139. var that = this;
  140. if (this.data.isEditCart) {
  141. this.getCartList();
  142. this.setData({
  143. isEditCart: !this.data.isEditCart
  144. });
  145. } else {
  146. //编辑状态
  147. let tmpCartList = this.data.cartGoods.map(function (v) {
  148. v.checked = false;
  149. return v;
  150. });
  151. this.setData({
  152. editCartList: this.data.cartGoods,
  153. cartGoods: tmpCartList,
  154. isEditCart: !this.data.isEditCart,
  155. checkedAllStatus: that.isCheckedAll(),
  156. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  157. });
  158. }
  159. },
  160. toIndexPage: function () {
  161. wx.switchTab({
  162. url: "/pages/index/index"
  163. });
  164. },
  165. updateCart: function (itemIndex, tag) {
  166. let that = this;
  167. let cartItem = this.data.cartGoods[itemIndex];
  168. let { product_id, goods_id, id, number } = cartItem
  169. if(tag ==='add'){
  170. number += 1
  171. } else {
  172. number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  173. }
  174. util.request(api.CartUpdate, {
  175. productId: product_id,
  176. goodsId: goods_id,
  177. number: number,
  178. id: id
  179. }).then(function (res) {
  180. if (res.errno === 0) {
  181. cartItem.number = number
  182. that.setData({
  183. cartGoods: that.data.cartGoods
  184. });
  185. } else if (res.errno === 400){
  186. util.showErrorToast('库存不足');
  187. that.setData({
  188. canAdd:false
  189. })
  190. }
  191. that.setData({
  192. checkedAllStatus: that.isCheckedAll()
  193. });
  194. });
  195. },
  196. cutNumber: function (event) {
  197. let itemIndex = event.target.dataset.itemIndex;
  198. // let cartItem = this.data.cartGoods[itemIndex];
  199. // let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  200. // cartItem.number = number;
  201. // this.setData({
  202. // cartGoods: this.data.cartGoods
  203. // });
  204. this.updateCart(itemIndex, 'cut');
  205. // this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  206. },
  207. addNumber: function (event) {
  208. let itemIndex = event.target.dataset.itemIndex;
  209. // let cartItem = this.data.cartGoods[itemIndex];
  210. // let number = cartItem.number + 1;
  211. this.updateCart(itemIndex,'add');
  212. },
  213. checkoutOrder: function () {
  214. //获取已选择的商品
  215. let that = this;
  216. var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
  217. if (element.checked == true) {
  218. return true;
  219. } else {
  220. return false;
  221. }
  222. });
  223. if (checkedGoods.length <= 0) {
  224. util.showErrorToast('请选择商品');
  225. return false;
  226. }
  227. wx.navigateTo({
  228. url: '../shopping/checkout/checkout'
  229. })
  230. },
  231. deleteCart: function () {
  232. //获取已选择的商品
  233. let that = this;
  234. let productIds = this.data.cartGoods.filter(function (element, index, array) {
  235. if (element.checked == true) {
  236. return true;
  237. } else {
  238. return false;
  239. }
  240. });
  241. if (productIds.length <= 0) {
  242. util.showErrorToast('请选择商品');
  243. return false;
  244. }
  245. productIds = productIds.map(function (element, index, array) {
  246. if (element.checked == true) {
  247. return element.product_id;
  248. }
  249. });
  250. util.request(api.CartDelete, {
  251. productIds: productIds.join(',')
  252. }, 'POST', 'application/json').then(function (res) {
  253. if (res.errno === 0) {
  254. let cartList = res.data.cartList.map(v => {
  255. v.checked = false;
  256. return v;
  257. });
  258. that.setData({
  259. cartGoods: cartList,
  260. cartTotal: res.data.cartTotal
  261. });
  262. }
  263. that.setData({
  264. checkedAllStatus: that.isCheckedAll()
  265. });
  266. });
  267. }
  268. })