cart.js 7.9 KB

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