1
0

cart.js 20 KB

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