1
0

cart.js 18 KB

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