cart.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. var util = require('../../utils/util.js');
  2. var goodsUtil = require('../../utils/goods.js');
  3. var api = require('../../config/api.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. cartGoods: [],
  8. footprintList: [],
  9. cartTotal: {
  10. "goodsCount": 0,
  11. "goodsAmount": 0.00,
  12. "checkedGoodsCount": 0,
  13. "checkedGoodsAmount": 0.00
  14. },
  15. checkedAllStatus: true,
  16. couponInfoList: [],
  17. openAttr: false,
  18. specificationList: {},
  19. checkedSpecText: '请选择规格数量',
  20. number: 1
  21. },
  22. onLoad: function (options) {
  23. // 页面初始化 options为页面跳转所带来的参数
  24. },
  25. onReady: function () {
  26. // 页面渲染完成
  27. },
  28. onShow: function () {
  29. // 页面显示
  30. this.getCartList();
  31. this.getFootprintList();
  32. },
  33. onHide: function () {
  34. // 页面隐藏
  35. },
  36. onUnload: function () {
  37. // 页面关闭
  38. },
  39. getCartList: function () {
  40. let that = this;
  41. util.request(api.CartList).then(function (res) {
  42. if (res.errno === 0) {
  43. that.setData({
  44. cartGoods: res.data.cartList,
  45. cartTotal: res.data.cartTotal,
  46. couponInfoList: res.data.couponInfoList
  47. });
  48. }
  49. that.setData({
  50. checkedAllStatus: that.isCheckedAll()
  51. });
  52. });
  53. },
  54. isCheckedAll: function () {
  55. //判断购物车商品已全选
  56. return this.data.cartGoods.every(function (element, index, array) {
  57. if (element.checked == true) {
  58. return true;
  59. } else {
  60. return false;
  61. }
  62. });
  63. },
  64. toIndexPage: function () {
  65. wx.switchTab({
  66. url: "/pages/index/index"
  67. });
  68. },
  69. checkedItem: function (event) {
  70. let itemIndex = event.target.dataset.itemIndex;
  71. let that = this;
  72. util.request(api.CartChecked, {
  73. productIds: that.data.cartGoods[itemIndex].product_id,
  74. isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1
  75. }, 'POST').then(function (res) {
  76. if (res.errno === 0) {
  77. console.log(res.data);
  78. that.setData({
  79. cartGoods: res.data.cartList,
  80. cartTotal: res.data.cartTotal,
  81. couponInfoList: res.data.couponInfoList
  82. });
  83. }
  84. that.setData({
  85. checkedAllStatus: that.isCheckedAll()
  86. });
  87. });
  88. },
  89. getCheckedGoodsCount: function () {
  90. let checkedGoodsCount = 0;
  91. this.data.cartGoods.forEach(function (v) {
  92. if (v.checked === true) {
  93. checkedGoodsCount += v.number;
  94. }
  95. });
  96. console.log(checkedGoodsCount);
  97. return checkedGoodsCount;
  98. },
  99. checkedAll: function () {
  100. let that = this;
  101. var productIds = this.data.cartGoods.map(function (v) {
  102. return v.product_id;
  103. });
  104. util.request(api.CartChecked, {
  105. productIds: productIds.join(','),
  106. isChecked: that.isCheckedAll() ? 0 : 1
  107. }, 'POST').then(function (res) {
  108. if (res.errno === 0) {
  109. console.log(res.data);
  110. that.setData({
  111. cartGoods: res.data.cartList,
  112. cartTotal: res.data.cartTotal,
  113. couponInfoList: res.data.couponInfoList
  114. });
  115. }
  116. that.setData({
  117. checkedAllStatus: that.isCheckedAll()
  118. });
  119. });
  120. },
  121. updateCart: function (productId, goodsId, number, beforeNumber, id, itemIndex) {
  122. let that = this;
  123. util.request(api.CartUpdate, {
  124. productId: productId,
  125. goodsId: goodsId,
  126. number: number,
  127. id: id
  128. }, 'POST').then(function (res) {
  129. if (res.errno === 0) {
  130. console.log(res.data);
  131. if (res.errmsg){
  132. wx.showModal({
  133. title: '修改失败',
  134. showCancel: false,
  135. content: res.errmsg
  136. })
  137. }
  138. that.setData({
  139. cartGoods: res.data.cartList,
  140. cartTotal: res.data.cartTotal,
  141. couponInfoList: res.data.couponInfoList
  142. });
  143. } else {
  144. // util.showErrorToast(res.errmsg);
  145. wx.showModal({
  146. title: '提示信息',
  147. content: res.errmsg,
  148. showCancel: false
  149. });
  150. let cartItem = that.data.cartGoods[itemIndex];
  151. cartItem.number = beforeNumber;
  152. that.setData({
  153. cartGoods: that.data.cartGoods
  154. });
  155. }
  156. that.setData({
  157. checkedAllStatus: that.isCheckedAll()
  158. });
  159. });
  160. },
  161. cutNumber: function (event) {
  162. let itemIndex = event.target.dataset.itemIndex;
  163. let cartItem = this.data.cartGoods[itemIndex];
  164. let beforeNumber = cartItem.number;
  165. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  166. cartItem.number = number;
  167. this.setData({
  168. cartGoods: this.data.cartGoods
  169. });
  170. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  171. },
  172. addNumber: function (event) {
  173. let itemIndex = event.target.dataset.itemIndex;
  174. let cartItem = this.data.cartGoods[itemIndex];
  175. let beforeNumber = cartItem.number;
  176. let number = cartItem.number + 1;
  177. cartItem.number = number;
  178. this.setData({
  179. cartGoods: this.data.cartGoods
  180. });
  181. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  182. },
  183. checkoutOrder: function () {
  184. //获取已选择的商品
  185. let that = this;
  186. var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
  187. if (element.checked == true) {
  188. return true;
  189. } else {
  190. return false;
  191. }
  192. });
  193. if (checkedGoods.length <= 0) {
  194. return false;
  195. }
  196. wx.navigateTo({
  197. url: '../shopping/checkout/checkout'
  198. })
  199. },
  200. deleteCart: function (event) {
  201. //获取已选择的商品
  202. let that = this;
  203. let cartId = event.target.dataset.cartId;
  204. let goodsName = event.target.dataset.goodsName;
  205. wx.showModal({
  206. title: '',
  207. content: '确定要删除' + goodsName + '?',
  208. success: function (res) {
  209. if (res.confirm) {
  210. util.request(api.CartDelete, {
  211. cartId: cartId
  212. }, 'POST').then(function (res) {
  213. if (res.errno === 0) {
  214. that.setData({
  215. cartGoods: res.data.cartList,
  216. cartTotal: res.data.cartTotal,
  217. couponInfoList: res.data.couponInfoList
  218. });
  219. }
  220. that.setData({
  221. checkedAllStatus: that.isCheckedAll()
  222. });
  223. });
  224. console.log('用户点击确定')
  225. }
  226. }
  227. })
  228. },
  229. getFootprintList() {
  230. let that = this;
  231. util.request(api.GuessFootprintList).then(function (res) {
  232. if (res.errno === 0) {
  233. that.setData({
  234. footprintList: res.data.list
  235. });
  236. }
  237. });
  238. },
  239. switchAttrPop: function () {
  240. this.setData({
  241. openAttr: !this.data.openAttr
  242. })
  243. },
  244. clickSkuValue: function (event) {
  245. let that = this;
  246. let specValueId = event.currentTarget.dataset.valueId;
  247. let index = event.currentTarget.dataset.index;
  248. let _specificationList = this.data.specificationList;
  249. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  250. if (_specificationList[index].valueList[j].id == specValueId) {
  251. //如果已经选中,则反选
  252. if (_specificationList[index].valueList[j].checked) {
  253. _specificationList[index].valueList[j].checked = false;
  254. } else {
  255. _specificationList[index].valueList[j].checked = true;
  256. }
  257. } else {
  258. _specificationList[index].valueList[j].checked = false;
  259. }
  260. }
  261. this.setData({
  262. 'specificationList': _specificationList
  263. });
  264. //重新计算spec改变后的信息
  265. goodsUtil.changeSpecInfo(that);
  266. },
  267. cutNumber2: function () {
  268. this.setData({
  269. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  270. });
  271. },
  272. addNumber2: function () {
  273. this.setData({
  274. number: this.data.number + 1
  275. });
  276. },
  277. //购物车增加
  278. addCart: function (e) {
  279. let that = this;
  280. var goodsId = e.currentTarget.dataset.goodsId;
  281. util.request(api.GoodsSku, { goodsId: goodsId }).then(function (res) {
  282. if (res.errno === 0 && null != res.data) {
  283. that.setData({
  284. goodsVo: res.data.goodsVo,
  285. specificationList: res.data.specificationList,
  286. productList: res.data.productList,
  287. openAttr: !that.data.openAttr
  288. });
  289. //
  290. let _specificationList = res.data.specificationList;
  291. for (let i = 0; i < _specificationList.length; i++) {
  292. if (_specificationList[i].valueList.length == 1) {
  293. //如果已经选中,则反选
  294. _specificationList[i].valueList[0].checked = true;
  295. }
  296. }
  297. that.setData({
  298. 'specificationList': _specificationList
  299. });
  300. }
  301. });
  302. },
  303. //购物车增加
  304. addToCart: function () {
  305. let that = this;
  306. var goodsId = that.data.goodsVo.id;
  307. //提示选择完整规格
  308. if (!that.data.productList || !that.data.productList.length) {
  309. util.showErrorToast('当前门店没有库存');
  310. return false;
  311. }
  312. //提示选择完整规格
  313. if (!goodsUtil.isCheckedAllSpec(that)) {
  314. return false;
  315. }
  316. //根据选中的规格,判断是否有对应的sku信息
  317. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  318. if (!checkedProduct || checkedProduct.length <= 0) {
  319. //找不到对应的product信息,提示没有库存
  320. return false;
  321. }
  322. //验证库存
  323. if (checkedProduct.stock_num < this.data.number) {
  324. //找不到对应的product信息,提示没有库存
  325. return false;
  326. }
  327. util.request(api.CartAdd, {
  328. goodsId: goodsId,
  329. productId: checkedProduct[0].id,
  330. number: this.data.number
  331. }, 'POST').then(function (res) {
  332. if (res.errno === 0 && null != res.data) {
  333. wx.showToast({
  334. title: '添加成功',
  335. icon: 'success',
  336. mask: true
  337. });
  338. that.setData({
  339. openAttr: !that.data.openAttr
  340. })
  341. // 页面显示
  342. that.getCartList();
  343. that.getFootprintList();
  344. } else {
  345. util.showErrorToast(res.errmsg)
  346. }
  347. });
  348. }
  349. })