1
0

cart.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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 that = this;
  200. util.request(api.CartChecked, {
  201. productIds: that.data.cartGoods[itemIndex].product_id,
  202. isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1,
  203. checkCart: that.data.checkCart
  204. }, 'POST').then(function(res) {
  205. if (res.errno === 0) {
  206. that.setCommonData(res);
  207. }
  208. that.setCheckedData();
  209. });
  210. },
  211. getCheckedGoodsCount: function() {
  212. let checkedGoodsCount = 0;
  213. this.data.cartGoods.forEach(function(v) {
  214. if (v.checked === true) {
  215. checkedGoodsCount += v.number;
  216. }
  217. });
  218. console.log(checkedGoodsCount);
  219. return checkedGoodsCount;
  220. },
  221. checkedAll: function() {
  222. let that = this;
  223. var productIds = this.data.cartGoods.map(function(v) {
  224. return v.product_id;
  225. });
  226. util.request(api.CartChecked, {
  227. productIds: productIds.join(','),
  228. isChecked: that.isCheckedAll() ? 0 : 1
  229. }, 'POST').then(function(res) {
  230. if (res.errno === 0) {
  231. that.setCommonData(res);
  232. }
  233. that.setCheckedAllData();
  234. });
  235. },
  236. checkedAllGoodType: function(e) {
  237. let that = this;
  238. let goodsBizType = e.target.dataset.goodsBizType;
  239. let isCheckedTypeStatu;
  240. if (goodsBizType == '00') {
  241. isCheckedTypeStatu = that.isCheckedTypeStatus00();
  242. }
  243. if (goodsBizType == '02') {
  244. isCheckedTypeStatu = that.isCheckedTypeStatus02();
  245. }
  246. if (goodsBizType == '10') {
  247. isCheckedTypeStatu = that.isCheckedTypeStatus10();
  248. }
  249. if (goodsBizType == '11') {
  250. isCheckedTypeStatu = that.isCheckedTypeStatus11();
  251. }
  252. util.request(api.CartChecked, {
  253. isChecked: isCheckedTypeStatu ? 0 : 1,
  254. goodsBizType: goodsBizType
  255. }, 'POST').then(function(res) {
  256. if (res.errno === 0) {
  257. that.setCommonData(res);
  258. }
  259. if (goodsBizType == '00') {
  260. that.setData({
  261. checkedTypeStatus00: that.isCheckedTypeStatus00()
  262. });
  263. }
  264. if (goodsBizType == '02') {
  265. that.setData({
  266. checkedTypeStatus02: that.isCheckedTypeStatus02()
  267. });
  268. }
  269. if (goodsBizType == '10') {
  270. that.setData({
  271. checkedTypeStatus10: that.isCheckedTypeStatus10()
  272. });
  273. }
  274. if (goodsBizType == '11') {
  275. that.setData({
  276. checkedTypeStatus11: that.isCheckedTypeStatus11()
  277. });
  278. }
  279. that.setData({
  280. checkedAllStatus: that.isCheckedAll()
  281. });
  282. });
  283. },
  284. updateCart: function(productId, goodsId, number, beforeNumber, id, itemIndex) {
  285. let that = this;
  286. util.request(api.CartUpdate, {
  287. productId: productId,
  288. goodsId: goodsId,
  289. number: number,
  290. id: id,
  291. checkCart: that.data.checkCart
  292. }, 'POST').then(function(res) {
  293. if (res.errno === 0) {
  294. // console.log(res.data);
  295. that.setCommonData(res);
  296. } else {
  297. // util.showErrorToast(res.errmsg);
  298. wx.showModal({
  299. title: '提示信息',
  300. content: res.errmsg,
  301. showCancel: false
  302. });
  303. let cartItem = that.data.cartGoods[itemIndex];
  304. cartItem.number = beforeNumber;
  305. that.setData({
  306. cartGoods: that.data.cartGoods
  307. });
  308. that.setCommonData(res);
  309. }
  310. that.setCheckedData();
  311. });
  312. },
  313. cutNumber: function(event) {
  314. let itemIndex = event.target.dataset.itemIndex;
  315. let cartItem = this.data.cartGoods[itemIndex];
  316. let beforeNumber = cartItem.number;
  317. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  318. cartItem.number = number;
  319. this.setData({
  320. cartGoods: this.data.cartGoods
  321. });
  322. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  323. },
  324. addNumber: function(event) {
  325. let itemIndex = event.target.dataset.itemIndex;
  326. let cartItem = this.data.cartGoods[itemIndex];
  327. let beforeNumber = cartItem.number;
  328. let number = cartItem.number + 1;
  329. cartItem.number = number;
  330. this.setData({
  331. cartGoods: this.data.cartGoods
  332. });
  333. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  334. },
  335. checkoutOrder: function() {
  336. //获取已选择的商品
  337. let that = this;
  338. util.request(api.getCurUser, {
  339. userInfo: app.globalData.userInfo
  340. }, 'POST').then(function(res) {
  341. if (res.errno === 0) {
  342. // console.log('that.data.mobile:' + res.data.mobile);
  343. if (res.data.mobile == '' || res.data.mobile == null) {
  344. wx.showModal({
  345. title: '',
  346. confirmColor: '#b4282d',
  347. showCancel: false,
  348. content: '您的手机号码未绑定,请先绑定手机号再进行购买',
  349. success: function(res) {
  350. if (res.confirm) {
  351. wx.navigateTo({
  352. url: '../../pages/auth/newuser/newuser'
  353. });
  354. }
  355. }
  356. });
  357. } else {
  358. var checkedGoods = that.data.cartGoods.filter(function(element, index, array) {
  359. if (element.checked == true) {
  360. return true;
  361. } else {
  362. return false;
  363. }
  364. });
  365. if (checkedGoods.length <= 0) {
  366. wx.showToast({
  367. title: '请选择要购买的商品',
  368. icon: 'none'
  369. })
  370. return false;
  371. }
  372. wx.navigateTo({
  373. url: '../shopping/checkout/checkout?checkCart=' + that.data.checkCart
  374. })
  375. }
  376. }
  377. });
  378. },
  379. deleteCart: function(event) {
  380. //获取已选择的商品
  381. let that = this;
  382. let cartId = event.target.dataset.cartId;
  383. let goodsName = event.target.dataset.goodsName;
  384. wx.showModal({
  385. title: '',
  386. content: '确定要删除' + goodsName + '?',
  387. success: function(res) {
  388. if (res.confirm) {
  389. util.request(api.CartDelete, {
  390. cartId: cartId,
  391. checkCart: that.data.checkCart
  392. }, 'POST').then(function(res) {
  393. if (res.errno === 0) {
  394. that.setCommonData(res);
  395. }
  396. that.setCheckedData();
  397. });
  398. console.log('用户点击确定')
  399. }
  400. }
  401. });
  402. },
  403. getFootprintList() {
  404. let that = this;
  405. util.request(api.GuessFootprintList, {
  406. storeId: wx.getStorageSync('storeId'),
  407. checkCart: that.data.checkCart
  408. }, ).then(function(res) {
  409. if (res.errno === 0) {
  410. that.setData({
  411. footprintList: res.data.list
  412. });
  413. }
  414. });
  415. },
  416. switchAttrPop: function() {
  417. this.setData({
  418. openAttr: !this.data.openAttr
  419. })
  420. },
  421. hideSwitchAttrPop: function () {
  422. this.setData({
  423. openAttr: false
  424. })
  425. },
  426. clickSkuValue: function(event) {
  427. let that = this;
  428. let specValueId = event.currentTarget.dataset.valueId;
  429. let index = event.currentTarget.dataset.index;
  430. let _specificationList = this.data.specificationList;
  431. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  432. if (_specificationList[index].valueList[j].id == specValueId) {
  433. //如果已经选中,则反选
  434. if (_specificationList[index].valueList[j].checked) {
  435. _specificationList[index].valueList[j].checked = false;
  436. } else {
  437. _specificationList[index].valueList[j].checked = true;
  438. }
  439. } else {
  440. _specificationList[index].valueList[j].checked = false;
  441. }
  442. }
  443. this.setData({
  444. 'specificationList': _specificationList
  445. });
  446. //重新计算spec改变后的信息
  447. goodsUtil.changeSpecInfo(that);
  448. },
  449. cutNumber2: function() {
  450. this.setData({
  451. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  452. });
  453. },
  454. addNumber2: function() {
  455. this.setData({
  456. number: this.data.number + 1
  457. });
  458. },
  459. //购物车增加
  460. addCart: function(e) {
  461. let that = this;
  462. that.setData({
  463. number: 1
  464. });
  465. var goodsId = e.currentTarget.dataset.goodsId;
  466. util.request(api.GoodsSku, {
  467. goodsId: goodsId
  468. }).then(function(res) {
  469. if (res.errno === 0 && null != res.data) {
  470. that.setData({
  471. goodsVo: res.data.goodsVo,
  472. specificationList: res.data.specificationList,
  473. productList: res.data.productList,
  474. openAttr: !that.data.openAttr,
  475. stockNum: res.data.productList[0].stock_num,
  476. cartNumber: res.data.cartNumber,
  477. checkedSpecText: res.data.specificationList[0].valueList[0].value
  478. });
  479. //
  480. let _specificationList = res.data.specificationList;
  481. for (let i = 0; i < _specificationList.length; i++) {
  482. if (_specificationList[i].valueList.length == 1) {
  483. //如果已经选中,则反选
  484. _specificationList[i].valueList[0].checked = true;
  485. }
  486. }
  487. that.setData({
  488. 'specificationList': _specificationList
  489. });
  490. }
  491. });
  492. },
  493. //购物车增加
  494. addToCart: function() {
  495. let that = this;
  496. var goodsId = that.data.goodsVo.id;
  497. //提示选择完整规格
  498. if (!that.data.productList || !that.data.productList.length) {
  499. util.showErrorToast('当前门店没有库存');
  500. return false;
  501. }
  502. //提示选择完整规格
  503. if (!goodsUtil.isCheckedAllSpec(that)) {
  504. return false;
  505. }
  506. //根据选中的规格,判断是否有对应的sku信息
  507. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  508. if (!checkedProduct || checkedProduct.length <= 0) {
  509. //找不到对应的product信息,提示没有库存
  510. return false;
  511. }
  512. //验证库存
  513. if (checkedProduct.stock_num < this.data.number) {
  514. //找不到对应的product信息,提示没有库存
  515. return false;
  516. }
  517. util.request(api.CartAdd, {
  518. goodsId: goodsId,
  519. productId: checkedProduct[0].id,
  520. number: this.data.number,
  521. checkCart: that.data.checkCart
  522. }, 'POST').then(function(res) {
  523. if (res.errno === 0 && null != res.data) {
  524. wx.showToast({
  525. title: '添加成功',
  526. icon: 'success',
  527. mask: true
  528. });
  529. that.setData({
  530. openAttr: !that.data.openAttr
  531. })
  532. // 页面显示
  533. that.getCartList();
  534. that.getFootprintList();
  535. } else {
  536. wx.showToast({
  537. title: res.errmsg,
  538. icon: 'none'
  539. })
  540. }
  541. });
  542. },
  543. deleteValidCart: function() { //获取购物车数据
  544. let that = this;
  545. wx.showModal({
  546. title: '',
  547. content: '确定要清空所有失效商品?',
  548. success: function(res) {
  549. if (res.confirm) {
  550. util.request(api.deleteValidCart, {
  551. checkCart: that.data.checkCart
  552. }).then(function(res) {
  553. if (res.errno === 0) {
  554. that.setCommonData(res);
  555. } else {
  556. util.showErrorToast(res.errmsg);
  557. }
  558. });
  559. }
  560. }
  561. });
  562. }
  563. })