cart.js 19 KB

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