cart.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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. cartGoods10: [], //现场速递数据
  10. cartGoods11: [], //普通商品
  11. cartGoods: [],
  12. validCartList: [],
  13. goodsBizTypeList: ['保税仓', '保税展示', '现场速递', '普通商品'],
  14. goodsBizType: '', //货品业务类型
  15. footprintList: [],
  16. cartTotal: {
  17. "goodsCount": 0,
  18. "goodsAmount": 0.00,
  19. "checkedGoodsCount": 0,
  20. "checkedGoodsAmount": 0.00
  21. },
  22. checkedAllStatus: true,
  23. checkedTypeStatus00: true,
  24. checkedTypeStatus02: true,
  25. checkedTypeStatus10: true,
  26. checkedTypeStatus11: true,
  27. couponInfoList: [],
  28. openAttr: false,
  29. specificationList: {},
  30. checkedSpecText: '请选择规格数量',
  31. number: 1,
  32. isAdd: true,
  33. mobile: '',
  34. retailPrice: '',
  35. stockNum: 0,
  36. cartNumber: 0,
  37. checkCart: '00',
  38. total00: 0,
  39. total11: 0
  40. },
  41. onLoad: function(options) {
  42. // 页面初始化 options为页面跳转所带来的参数
  43. },
  44. onReady: function() {
  45. // 页面渲染完成
  46. },
  47. onShow: function() {
  48. // 页面显示
  49. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  50. this.getCartList();
  51. this.getFootprintList();
  52. } else {
  53. wx.navigateTo({
  54. url: '/pages/auth/btnAuth/btnAuth',
  55. })
  56. }
  57. },
  58. openSortFilter: function(event) {
  59. let that = this;
  60. let currentId = event.currentTarget.id;
  61. switch (currentId) {
  62. case 'defaultActivity':
  63. that.setData({
  64. 'checkCart': '00'
  65. });
  66. this.getCartList();
  67. this.getFootprintList();
  68. break;
  69. case 'ordActivity':
  70. that.setData({
  71. 'checkCart': '11'
  72. });
  73. this.getCartList();
  74. this.getFootprintList();
  75. break;
  76. default:
  77. //综合排序
  78. that.setData({
  79. 'checkCart': '00'
  80. });
  81. this.getCartList();
  82. this.getFootprintList();
  83. }
  84. },
  85. onHide: function() {
  86. // 页面隐藏
  87. },
  88. onUnload: function() {
  89. // 页面关闭
  90. },
  91. setCommonData(res) {
  92. let that = this;
  93. that.setData({
  94. cartGoods: res.data.cartList,
  95. cartGoods00: res.data.cart00List,
  96. cartGoods02: res.data.cart02List,
  97. cartGoods10: res.data.cart10List,
  98. cartGoods11: res.data.cart11List,
  99. validCartList: res.data.validCartList,
  100. cartTotal: res.data.cartTotal,
  101. couponInfoList: res.data.couponInfoList,
  102. total00: res.data.total00,
  103. total11: res.data.total11
  104. });
  105. },
  106. setCheckedData() {
  107. let that = this;
  108. that.setData({
  109. checkedAllStatus: that.isCheckedAll(),
  110. checkedTypeStatus00: that.isCheckedTypeStatus00(),
  111. checkedTypeStatus02: that.isCheckedTypeStatus02(),
  112. checkedTypeStatus10: that.isCheckedTypeStatus10(),
  113. checkedTypeStatus11: that.isCheckedTypeStatus11()
  114. });
  115. },
  116. setCheckedAllData() {
  117. let that = this;
  118. that.setData({
  119. checkedAllStatus: that.isCheckedAll(),
  120. checkedTypeStatus00: that.isCheckedAll(),
  121. checkedTypeStatus02: that.isCheckedAll(),
  122. checkedTypeStatus10: that.isCheckedAll(),
  123. checkedTypeStatus11: that.isCheckedAll()
  124. });
  125. },
  126. getCartList: function() { //获取购物车数据
  127. let that = this;
  128. wx.showLoading({
  129. title: '加载中...',
  130. });
  131. util.request(api.CartList, {
  132. checkCart: that.data.checkCart
  133. }).then(function(res) {
  134. if (res.errno === 0) {
  135. that.setCommonData(res);
  136. }
  137. wx.hideLoading();
  138. //数据渲染选中
  139. that.setCheckedData();
  140. });
  141. },
  142. isCheckedAll: function() {
  143. //判断购物车所有商品是否已全选
  144. return this.data.cartGoods.every(function(element, index, array) {
  145. if (element.checked == true) {
  146. return true;
  147. } else {
  148. return false;
  149. }
  150. });
  151. },
  152. isCheckedTypeStatus00: function() {
  153. //判断该业务类型的购物车商品是否已全选
  154. return this.data.cartGoods00.every(function(element, index, array) {
  155. if (element.checked == true) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. });
  161. },
  162. isCheckedTypeStatus02: function() {
  163. //判断该业务类型的购物车商品是否已全选
  164. return this.data.cartGoods02.every(function(element, index, array) {
  165. if (element.checked == true) {
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. });
  171. },
  172. isCheckedTypeStatus10: function() {
  173. //判断该业务类型的购物车商品是否已全选
  174. return this.data.cartGoods10.every(function(element, index, array) {
  175. if (element.checked == true) {
  176. return true;
  177. } else {
  178. return false;
  179. }
  180. });
  181. },
  182. isCheckedTypeStatus11: function() {
  183. //判断该业务类型的购物车商品是否已全选
  184. return this.data.cartGoods11.every(function(element, index, array) {
  185. if (element.checked == true) {
  186. return true;
  187. } else {
  188. return false;
  189. }
  190. });
  191. },
  192. toIndexPage: function() {
  193. wx.switchTab({
  194. url: "/pages/index/index"
  195. });
  196. },
  197. checkedItem: function(event) {
  198. let itemIndex = event.target.dataset.itemIndex;
  199. let goodsBizType = event.target.dataset.goodsBizType;
  200. let goodsId = event.target.dataset.goodsId;
  201. let checked = event.target.dataset.checked;
  202. let that = this;
  203. // console.log(goodsBizType);
  204. // console.log(goodsId);
  205. // console.log(that.data.cartGoods[itemIndex].checked);
  206. util.request(api.CartChecked, {
  207. goodsIds: goodsId,
  208. isChecked: checked ? 0 : 1,
  209. checkCart: that.data.checkCart
  210. }, 'POST').then(function(res) {
  211. if (res.errno === 0) {
  212. that.setCommonData(res);
  213. }
  214. that.setCheckedData();
  215. });
  216. },
  217. getCheckedGoodsCount: function() {
  218. let checkedGoodsCount = 0;
  219. this.data.cartGoods.forEach(function(v) {
  220. if (v.checked === true) {
  221. checkedGoodsCount += v.number;
  222. }
  223. });
  224. return checkedGoodsCount;
  225. },
  226. checkedAll: function() {
  227. let that = this;
  228. var goodsIds = this.data.cartGoods.map(function(v) {
  229. return v.goodsIds;
  230. });
  231. util.request(api.CartChecked, {
  232. goodsIds: goodsIds.join(','),
  233. isChecked: that.isCheckedAll() ? 0 : 1
  234. }, 'POST').then(function(res) {
  235. if (res.errno === 0) {
  236. that.setCommonData(res);
  237. }
  238. that.setCheckedAllData();
  239. });
  240. },
  241. checkedAllGoodType: function(e) {
  242. let that = this;
  243. let goodsBizType = e.target.dataset.goodsBizType;
  244. let isCheckedTypeStatu;
  245. let checkCart;
  246. if (goodsBizType == '00') {
  247. isCheckedTypeStatu = that.isCheckedTypeStatus00();
  248. checkCart = '00';
  249. }
  250. if (goodsBizType == '02') {
  251. isCheckedTypeStatu = that.isCheckedTypeStatus02();
  252. checkCart = '00';
  253. }
  254. if (goodsBizType == '10') {
  255. isCheckedTypeStatu = that.isCheckedTypeStatus10();
  256. checkCart = '00';
  257. }
  258. if (goodsBizType == '11') {
  259. isCheckedTypeStatu = that.isCheckedTypeStatus11();
  260. checkCart = '11';
  261. }
  262. util.request(api.CartChecked, {
  263. isChecked: isCheckedTypeStatu ? 0 : 1,
  264. goodsBizType: goodsBizType,
  265. checkCart: checkCart
  266. }, 'POST').then(function(res) {
  267. if (res.errno === 0) {
  268. // that.setCommonData(res);
  269. that.setData({
  270. validCartList: res.data.validCartList,
  271. couponInfoList: res.data.couponInfoList,
  272. });
  273. if (goodsBizType == '11') {
  274. that.setData({
  275. cartGoods: res.data.cartList,
  276. cartTotal: res.data.cartTotal,
  277. cartGoods11: res.data.cart11List,
  278. total11: res.data.total11
  279. });
  280. } else {
  281. that.setData({
  282. cartGoods: res.data.cartList,
  283. cartTotal: res.data.cartTotal,
  284. cartGoods00: res.data.cart00List,
  285. cartGoods02: res.data.cart02List,
  286. cartGoods10: res.data.cart10List,
  287. total00: res.data.total00
  288. });
  289. }
  290. }
  291. if (goodsBizType == '00') {
  292. that.setData({
  293. checkedTypeStatus00: that.isCheckedTypeStatus00()
  294. });
  295. }
  296. if (goodsBizType == '02') {
  297. that.setData({
  298. checkedTypeStatus02: that.isCheckedTypeStatus02()
  299. });
  300. }
  301. if (goodsBizType == '10') {
  302. that.setData({
  303. checkedTypeStatus10: that.isCheckedTypeStatus10()
  304. });
  305. }
  306. if (goodsBizType == '11') {
  307. that.setData({
  308. checkedTypeStatus11: that.isCheckedTypeStatus11()
  309. });
  310. }
  311. that.setData({
  312. checkedAllStatus: that.isCheckedAll()
  313. });
  314. });
  315. },
  316. updateCart: function(productId, goodsId, number, beforeNumber, id, itemIndex) {
  317. let that = this;
  318. util.request(api.CartUpdate, {
  319. productId: productId,
  320. goodsId: goodsId,
  321. number: number,
  322. id: id,
  323. checkCart: that.data.checkCart
  324. }, 'POST').then(function(res) {
  325. if (res.errno === 0) {
  326. // console.log(res.data);
  327. that.setCommonData(res);
  328. } else {
  329. // util.showErrorToast(res.errmsg);
  330. wx.showModal({
  331. title: '提示信息',
  332. content: res.errmsg,
  333. showCancel: false
  334. });
  335. let cartItem = that.data.cartGoods[itemIndex];
  336. cartItem.number = beforeNumber;
  337. that.setData({
  338. cartGoods: that.data.cartGoods
  339. });
  340. that.setCommonData(res);
  341. }
  342. that.setCheckedData();
  343. });
  344. },
  345. cutNumber: function(event) {
  346. let itemIndex = event.target.dataset.itemIndex;
  347. let cartItem = this.data.cartGoods[itemIndex];
  348. let beforeNumber = cartItem.number;
  349. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  350. cartItem.number = number;
  351. this.setData({
  352. cartGoods: this.data.cartGoods
  353. });
  354. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  355. },
  356. addNumber: function(event) {
  357. let itemIndex = event.target.dataset.itemIndex;
  358. let cartItem = this.data.cartGoods[itemIndex];
  359. let beforeNumber = cartItem.number;
  360. let number = cartItem.number + 1;
  361. cartItem.number = number;
  362. this.setData({
  363. cartGoods: this.data.cartGoods
  364. });
  365. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  366. },
  367. checkoutOrder: function() {
  368. //获取已选择的商品
  369. let that = this;
  370. util.request(api.getCurUser, {
  371. userInfo: app.globalData.userInfo
  372. }, 'POST').then(function(res) {
  373. if (res.errno === 0) {
  374. // console.log('that.data.mobile:' + res.data.mobile);
  375. if (res.data.mobile == '' || res.data.mobile == null) {
  376. wx.showModal({
  377. title: '',
  378. confirmColor: '#b4282d',
  379. showCancel: false,
  380. content: '您的手机号码未绑定,请先绑定手机号再进行购买',
  381. success: function(res) {
  382. if (res.confirm) {
  383. wx.navigateTo({
  384. url: '../../pages/auth/newuser/newuser'
  385. });
  386. }
  387. }
  388. });
  389. } else {
  390. var checkedGoods = that.data.cartGoods.filter(function(element, index, array) {
  391. if (element.checked == true) {
  392. return true;
  393. } else {
  394. return false;
  395. }
  396. });
  397. if (checkedGoods.length <= 0) {
  398. wx.showToast({
  399. title: '请选择要购买的商品',
  400. icon: 'none'
  401. })
  402. return false;
  403. }
  404. wx.navigateTo({
  405. url: '../shopping/checkout/checkout?checkCart=' + that.data.checkCart
  406. })
  407. }
  408. }
  409. });
  410. },
  411. deleteCart: function(event) {
  412. //获取已选择的商品
  413. let that = this;
  414. let cartId = event.target.dataset.cartId;
  415. let goodsName = event.target.dataset.goodsName;
  416. wx.showModal({
  417. title: '',
  418. content: '确定要删除' + goodsName + '?',
  419. success: function(res) {
  420. if (res.confirm) {
  421. util.request(api.CartDelete, {
  422. cartId: cartId,
  423. checkCart: that.data.checkCart
  424. }, 'POST').then(function(res) {
  425. if (res.errno === 0) {
  426. that.setCommonData(res);
  427. }
  428. that.setCheckedData();
  429. });
  430. console.log('用户点击确定')
  431. }
  432. }
  433. });
  434. },
  435. getFootprintList() {
  436. let that = this;
  437. util.request(api.GuessFootprintList, {
  438. storeId: wx.getStorageSync('storeId'),
  439. checkCart: that.data.checkCart
  440. }, ).then(function(res) {
  441. if (res.errno === 0) {
  442. that.setData({
  443. footprintList: res.data.list
  444. });
  445. }
  446. });
  447. },
  448. switchAttrPop: function() {
  449. this.setData({
  450. openAttr: !this.data.openAttr
  451. })
  452. },
  453. hideSwitchAttrPop: function () {
  454. this.setData({
  455. openAttr: false
  456. })
  457. },
  458. clickSkuValue: function(event) {
  459. let that = this;
  460. let specValueId = event.currentTarget.dataset.valueId;
  461. let index = event.currentTarget.dataset.index;
  462. let _specificationList = this.data.specificationList;
  463. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  464. if (_specificationList[index].valueList[j].id == specValueId) {
  465. //如果已经选中,则反选
  466. if (_specificationList[index].valueList[j].checked) {
  467. _specificationList[index].valueList[j].checked = false;
  468. } else {
  469. _specificationList[index].valueList[j].checked = true;
  470. }
  471. } else {
  472. _specificationList[index].valueList[j].checked = false;
  473. }
  474. }
  475. this.setData({
  476. 'specificationList': _specificationList
  477. });
  478. //重新计算spec改变后的信息
  479. goodsUtil.changeSpecInfo(that);
  480. },
  481. cutNumber2: function() {
  482. this.setData({
  483. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  484. });
  485. },
  486. addNumber2: function() {
  487. this.setData({
  488. number: this.data.number + 1
  489. });
  490. },
  491. //购物车增加
  492. addCart: function(e) {
  493. let that = this;
  494. that.setData({
  495. number: 1
  496. });
  497. var goodsId = e.currentTarget.dataset.goodsId;
  498. util.request(api.GoodsSku, {
  499. goodsId: goodsId
  500. }).then(function(res) {
  501. if (res.errno === 0 && null != res.data) {
  502. that.setData({
  503. goodsVo: res.data.goodsVo,
  504. specificationList: res.data.specificationList,
  505. productList: res.data.productList,
  506. openAttr: !that.data.openAttr,
  507. stockNum: res.data.productList[0].stock_num,
  508. cartNumber: res.data.cartNumber,
  509. checkedSpecText: res.data.specificationList[0].valueList[0].value
  510. });
  511. //
  512. let _specificationList = res.data.specificationList;
  513. for (let i = 0; i < _specificationList.length; i++) {
  514. if (_specificationList[i].valueList.length == 1) {
  515. //如果已经选中,则反选
  516. _specificationList[i].valueList[0].checked = true;
  517. }
  518. }
  519. that.setData({
  520. 'specificationList': _specificationList
  521. });
  522. }
  523. });
  524. },
  525. //购物车增加
  526. addToCart: function() {
  527. let that = this;
  528. var goodsId = that.data.goodsVo.id;
  529. //提示选择完整规格
  530. if (!that.data.productList || !that.data.productList.length) {
  531. util.showErrorToast('当前门店没有库存');
  532. return false;
  533. }
  534. //提示选择完整规格
  535. if (!goodsUtil.isCheckedAllSpec(that)) {
  536. return false;
  537. }
  538. //根据选中的规格,判断是否有对应的sku信息
  539. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  540. if (!checkedProduct || checkedProduct.length <= 0) {
  541. //找不到对应的product信息,提示没有库存
  542. return false;
  543. }
  544. //验证库存
  545. if (checkedProduct.stock_num < this.data.number) {
  546. //找不到对应的product信息,提示没有库存
  547. return false;
  548. }
  549. util.request(api.CartAdd, {
  550. goodsId: goodsId,
  551. productId: checkedProduct[0].id,
  552. number: this.data.number,
  553. checkCart: that.data.checkCart
  554. }, 'POST').then(function(res) {
  555. if (res.errno === 0 && null != res.data) {
  556. wx.showToast({
  557. title: '添加成功',
  558. icon: 'success',
  559. mask: true
  560. });
  561. that.setData({
  562. openAttr: !that.data.openAttr
  563. })
  564. // 页面显示
  565. that.getCartList();
  566. that.getFootprintList();
  567. } else {
  568. wx.showToast({
  569. title: res.errmsg,
  570. icon: 'none'
  571. })
  572. }
  573. });
  574. },
  575. deleteValidCart: function() { //获取购物车数据
  576. let that = this;
  577. wx.showModal({
  578. title: '',
  579. content: '确定要清空所有失效商品?',
  580. success: function(res) {
  581. if (res.confirm) {
  582. util.request(api.deleteValidCart, {
  583. checkCart: that.data.checkCart
  584. }).then(function(res) {
  585. if (res.errno === 0) {
  586. that.setCommonData(res);
  587. } else {
  588. util.showErrorToast(res.errmsg);
  589. }
  590. });
  591. }
  592. }
  593. });
  594. }
  595. })