cart.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. cartGoods00: [],//保税仓数据
  8. cartGoods02: [],//保税展示数据
  9. cartGoods11: [],//现场速递数据
  10. cartGoods: [],
  11. goodsBizType: '',//货品业务类型
  12. footprintList: [],
  13. cartTotal: {
  14. "goodsCount": 0,
  15. "goodsAmount": 0.00,
  16. "checkedGoodsCount": 0,
  17. "checkedGoodsAmount": 0.00
  18. },
  19. checkedAllStatus: true,
  20. checkedTypeStatus00: true,
  21. checkedTypeStatus02: true,
  22. checkedTypeStatus11: true,
  23. couponInfoList: [],
  24. openAttr: false,
  25. specificationList: {},
  26. checkedSpecText: '请选择规格数量',
  27. number: 1
  28. },
  29. onLoad: function (options) {
  30. // 页面初始化 options为页面跳转所带来的参数
  31. },
  32. onReady: function () {
  33. // 页面渲染完成
  34. },
  35. onShow: function () {
  36. // 页面显示
  37. this.getCartList();
  38. this.getFootprintList();
  39. },
  40. onHide: function () {
  41. // 页面隐藏
  42. },
  43. onUnload: function () {
  44. // 页面关闭
  45. },
  46. getCartList: function () {//获取购物车数据
  47. let that = this;
  48. util.request(api.CartList).then(function (res) {
  49. if (res.errno === 0) {
  50. that.setData({
  51. cartGoods: res.data.cartList,
  52. cartGoods00: res.data.cart00List,
  53. cartGoods02: res.data.cart02List,
  54. cartGoods11: res.data.cart11List,
  55. cartTotal: res.data.cartTotal,
  56. couponInfoList: res.data.couponInfoList
  57. });
  58. }
  59. //数据渲染选中
  60. that.setData({
  61. checkedAllStatus: that.isCheckedAll()
  62. });
  63. that.setData({
  64. checkedTypeStatus00: that.isCheckedTypeStatus00(),
  65. checkedTypeStatus02: that.isCheckedTypeStatus02(),
  66. checkedTypeStatus11: that.isCheckedTypeStatus11()
  67. });
  68. });
  69. },
  70. isCheckedAll: function () {
  71. //判断购物车所有商品是否已全选
  72. return this.data.cartGoods.every(function (element, index, array) {
  73. if (element.checked == true) {
  74. return true;
  75. } else {
  76. return false;
  77. }
  78. });
  79. },
  80. isCheckedTypeStatus00: function () {
  81. //判断该业务类型的购物车商品是否已全选
  82. return this.data.cartGoods00.every(function (element, index, array) {
  83. if (element.checked == true) {
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. });
  89. },
  90. isCheckedTypeStatus02: function () {
  91. //判断该业务类型的购物车商品是否已全选
  92. return this.data.cartGoods02.every(function (element, index, array) {
  93. if (element.checked == true) {
  94. return true;
  95. } else {
  96. return false;
  97. }
  98. });
  99. },
  100. isCheckedTypeStatus11: function () {
  101. //判断该业务类型的购物车商品是否已全选
  102. return this.data.cartGoods11.every(function (element, index, array) {
  103. if (element.checked == true) {
  104. return true;
  105. } else {
  106. return false;
  107. }
  108. });
  109. },
  110. toIndexPage: function () {
  111. wx.switchTab({
  112. url: "/pages/index/index"
  113. });
  114. },
  115. checkedItem: function (event) {
  116. let itemIndex = event.target.dataset.itemIndex;
  117. let that = this;
  118. util.request(api.CartChecked, {
  119. productIds: that.data.cartGoods[itemIndex].product_id,
  120. isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1
  121. }, 'POST').then(function (res) {
  122. if (res.errno === 0) {
  123. console.log(res.data);
  124. that.setData({
  125. cartGoods: res.data.cartList,
  126. cartGoods00: res.data.cart00List,
  127. cartGoods02: res.data.cart02List,
  128. cartGoods11: res.data.cart11List,
  129. cartTotal: res.data.cartTotal,
  130. couponInfoList: res.data.couponInfoList
  131. });
  132. }
  133. that.setData({
  134. checkedAllStatus: that.isCheckedAll(),
  135. checkedTypeStatus00: that.isCheckedTypeStatus00(),
  136. checkedTypeStatus02: that.isCheckedTypeStatus02(),
  137. checkedTypeStatus11: that.isCheckedTypeStatus11()
  138. });
  139. });
  140. },
  141. getCheckedGoodsCount: function () {
  142. let checkedGoodsCount = 0;
  143. this.data.cartGoods.forEach(function (v) {
  144. if (v.checked === true) {
  145. checkedGoodsCount += v.number;
  146. }
  147. });
  148. console.log(checkedGoodsCount);
  149. return checkedGoodsCount;
  150. },
  151. checkedAll: function () {
  152. let that = this;
  153. var productIds = this.data.cartGoods.map(function (v) {
  154. return v.product_id;
  155. });
  156. util.request(api.CartChecked, {
  157. productIds: productIds.join(','),
  158. isChecked: that.isCheckedAll() ? 0 : 1
  159. }, 'POST').then(function (res) {
  160. if (res.errno === 0) {
  161. console.log(res.data);
  162. that.setData({
  163. cartGoods: res.data.cartList,
  164. cartGoods00: res.data.cart00List,
  165. cartGoods02: res.data.cart02List,
  166. cartGoods11: res.data.cart11List,
  167. cartTotal: res.data.cartTotal,
  168. couponInfoList: res.data.couponInfoList
  169. });
  170. }
  171. that.setData({
  172. checkedAllStatus: that.isCheckedAll()
  173. });
  174. that.setData({
  175. checkedTypeStatus00: that.isCheckedAll(),
  176. checkedTypeStatus02: that.isCheckedAll(),
  177. checkedTypeStatus11: that.isCheckedAll()
  178. });
  179. });
  180. },
  181. checkedAllGoodType: function (e) {
  182. let that = this;
  183. let goodsBizType = e.target.dataset.goodsBizType;
  184. let isCheckedTypeStatu;
  185. if (goodsBizType == '00') {
  186. isCheckedTypeStatu = that.isCheckedTypeStatus00();
  187. }
  188. if (goodsBizType == '02') {
  189. isCheckedTypeStatu = that.isCheckedTypeStatus02();
  190. }
  191. if (goodsBizType == '11') {
  192. isCheckedTypeStatu = that.isCheckedTypeStatus11();
  193. }
  194. util.request(api.CartChecked, {
  195. isChecked: isCheckedTypeStatu ? 0 : 1,
  196. goodsBizType: goodsBizType
  197. }, 'POST').then(function (res) {
  198. if (res.errno === 0) {
  199. console.log(res.data);
  200. //渲染数据
  201. that.setData({
  202. cartGoods: res.data.cartList,
  203. cartGoods00: res.data.cart00List,
  204. cartGoods02: res.data.cart02List,
  205. cartGoods11: res.data.cart11List,
  206. cartTotal: res.data.cartTotal,
  207. couponInfoList: res.data.couponInfoList,
  208. goodsBizType: goodsBizType
  209. });
  210. }
  211. if (goodsBizType == '00') {
  212. that.setData({
  213. checkedTypeStatus00: that.isCheckedTypeStatus00()
  214. });
  215. }
  216. if (goodsBizType == '02') {
  217. that.setData({
  218. checkedTypeStatus02: that.isCheckedTypeStatus02()
  219. });
  220. }
  221. if (goodsBizType == '11') {
  222. that.setData({
  223. checkedTypeStatus11: that.isCheckedTypeStatus11()
  224. });
  225. }
  226. that.setData({
  227. checkedAllStatus: that.isCheckedAll()
  228. });
  229. });
  230. },
  231. updateCart: function (productId, goodsId, number, beforeNumber, id, itemIndex) {
  232. let that = this;
  233. util.request(api.CartUpdate, {
  234. productId: productId,
  235. goodsId: goodsId,
  236. number: number,
  237. id: id
  238. }, 'POST').then(function (res) {
  239. if (res.errno === 0) {
  240. console.log(res.data);
  241. if (res.errmsg) {
  242. wx.showModal({
  243. title: '修改失败',
  244. showCancel: false,
  245. content: res.errmsg
  246. })
  247. }
  248. that.setData({
  249. cartGoods: res.data.cartList,
  250. cartGoods00: res.data.cart00List,
  251. cartGoods02: res.data.cart02List,
  252. cartGoods11: res.data.cart11List,
  253. cartTotal: res.data.cartTotal,
  254. couponInfoList: res.data.couponInfoList
  255. });
  256. } else {
  257. // util.showErrorToast(res.errmsg);
  258. wx.showModal({
  259. title: '提示信息',
  260. content: res.errmsg,
  261. showCancel: false
  262. });
  263. let cartItem = that.data.cartGoods[itemIndex];
  264. cartItem.number = beforeNumber;
  265. that.setData({
  266. cartGoods: that.data.cartGoods
  267. });
  268. }
  269. that.setData({
  270. checkedAllStatus: that.isCheckedAll(),
  271. checkedTypeStatus00: that.isCheckedTypeStatus00(),
  272. checkedTypeStatus02: that.isCheckedTypeStatus02(),
  273. checkedTypeStatus11: that.isCheckedTypeStatus11()
  274. });
  275. });
  276. },
  277. cutNumber: function (event) {
  278. let itemIndex = event.target.dataset.itemIndex;
  279. let cartItem = this.data.cartGoods[itemIndex];
  280. let beforeNumber = cartItem.number;
  281. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  282. cartItem.number = number;
  283. this.setData({
  284. cartGoods: this.data.cartGoods
  285. });
  286. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  287. },
  288. addNumber: function (event) {
  289. let itemIndex = event.target.dataset.itemIndex;
  290. let cartItem = this.data.cartGoods[itemIndex];
  291. let beforeNumber = cartItem.number;
  292. let number = cartItem.number + 1;
  293. cartItem.number = number;
  294. this.setData({
  295. cartGoods: this.data.cartGoods
  296. });
  297. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  298. },
  299. checkoutOrder: function () {
  300. //获取已选择的商品
  301. let that = this;
  302. var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
  303. if (element.checked == true) {
  304. return true;
  305. } else {
  306. return false;
  307. }
  308. });
  309. if (checkedGoods.length <= 0) {
  310. return false;
  311. }
  312. wx.navigateTo({
  313. url: '../shopping/checkout/checkout'
  314. })
  315. },
  316. deleteCart: function (event) {
  317. //获取已选择的商品
  318. let that = this;
  319. let cartId = event.target.dataset.cartId;
  320. let goodsName = event.target.dataset.goodsName;
  321. wx.showModal({
  322. title: '',
  323. content: '确定要删除' + goodsName + '?',
  324. success: function (res) {
  325. if (res.confirm) {
  326. util.request(api.CartDelete, {
  327. cartId: cartId
  328. }, 'POST').then(function (res) {
  329. if (res.errno === 0) {
  330. that.setData({
  331. cartGoods: res.data.cartList,
  332. cartGoods00: res.data.cart00List,
  333. cartGoods02: res.data.cart02List,
  334. cartGoods11: res.data.cart11List,
  335. cartTotal: res.data.cartTotal,
  336. couponInfoList: res.data.couponInfoList
  337. });
  338. }
  339. that.setData({
  340. checkedAllStatus: that.isCheckedAll(),
  341. checkedTypeStatus00: that.isCheckedTypeStatus00(),
  342. checkedTypeStatus02: that.isCheckedTypeStatus02(),
  343. checkedTypeStatus11: that.isCheckedTypeStatus11()
  344. });
  345. });
  346. console.log('用户点击确定')
  347. }
  348. }
  349. })
  350. },
  351. getFootprintList() {
  352. let that = this;
  353. util.request(api.GuessFootprintList).then(function (res) {
  354. if (res.errno === 0) {
  355. that.setData({
  356. footprintList: res.data.list
  357. });
  358. }
  359. });
  360. },
  361. switchAttrPop: function () {
  362. this.setData({
  363. openAttr: !this.data.openAttr
  364. })
  365. },
  366. clickSkuValue: function (event) {
  367. let that = this;
  368. let specValueId = event.currentTarget.dataset.valueId;
  369. let index = event.currentTarget.dataset.index;
  370. let _specificationList = this.data.specificationList;
  371. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  372. if (_specificationList[index].valueList[j].id == specValueId) {
  373. //如果已经选中,则反选
  374. if (_specificationList[index].valueList[j].checked) {
  375. _specificationList[index].valueList[j].checked = false;
  376. } else {
  377. _specificationList[index].valueList[j].checked = true;
  378. }
  379. } else {
  380. _specificationList[index].valueList[j].checked = false;
  381. }
  382. }
  383. this.setData({
  384. 'specificationList': _specificationList
  385. });
  386. //重新计算spec改变后的信息
  387. goodsUtil.changeSpecInfo(that);
  388. },
  389. cutNumber2: function () {
  390. this.setData({
  391. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  392. });
  393. },
  394. addNumber2: function () {
  395. this.setData({
  396. number: this.data.number + 1
  397. });
  398. },
  399. //购物车增加
  400. addCart: function (e) {
  401. let that = this;
  402. var goodsId = e.currentTarget.dataset.goodsId;
  403. util.request(api.GoodsSku, { goodsId: goodsId }).then(function (res) {
  404. if (res.errno === 0 && null != res.data) {
  405. that.setData({
  406. goodsVo: res.data.goodsVo,
  407. specificationList: res.data.specificationList,
  408. productList: res.data.productList,
  409. openAttr: !that.data.openAttr
  410. });
  411. //
  412. let _specificationList = res.data.specificationList;
  413. for (let i = 0; i < _specificationList.length; i++) {
  414. if (_specificationList[i].valueList.length == 1) {
  415. //如果已经选中,则反选
  416. _specificationList[i].valueList[0].checked = true;
  417. }
  418. }
  419. that.setData({
  420. 'specificationList': _specificationList
  421. });
  422. }
  423. });
  424. },
  425. //购物车增加
  426. addToCart: function () {
  427. let that = this;
  428. var goodsId = that.data.goodsVo.id;
  429. //提示选择完整规格
  430. if (!that.data.productList || !that.data.productList.length) {
  431. util.showErrorToast('当前门店没有库存');
  432. return false;
  433. }
  434. //提示选择完整规格
  435. if (!goodsUtil.isCheckedAllSpec(that)) {
  436. return false;
  437. }
  438. //根据选中的规格,判断是否有对应的sku信息
  439. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  440. if (!checkedProduct || checkedProduct.length <= 0) {
  441. //找不到对应的product信息,提示没有库存
  442. return false;
  443. }
  444. //验证库存
  445. if (checkedProduct.stock_num < this.data.number) {
  446. //找不到对应的product信息,提示没有库存
  447. return false;
  448. }
  449. util.request(api.CartAdd, {
  450. goodsId: goodsId,
  451. productId: checkedProduct[0].id,
  452. number: this.data.number
  453. }, 'POST').then(function (res) {
  454. if (res.errno === 0 && null != res.data) {
  455. wx.showToast({
  456. title: '添加成功',
  457. icon: 'success',
  458. mask: true
  459. });
  460. that.setData({
  461. openAttr: !that.data.openAttr
  462. })
  463. // 页面显示
  464. that.getCartList();
  465. that.getFootprintList();
  466. } else {
  467. util.showErrorToast(res.errmsg)
  468. }
  469. });
  470. }
  471. })