1
0

cart.js 19 KB

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