cart.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. return false;
  367. }
  368. wx.navigateTo({
  369. url: '../shopping/checkout/checkout?checkCart=' + that.data.checkCart
  370. })
  371. }
  372. }
  373. });
  374. },
  375. deleteCart: function(event) {
  376. //获取已选择的商品
  377. let that = this;
  378. let cartId = event.target.dataset.cartId;
  379. let goodsName = event.target.dataset.goodsName;
  380. wx.showModal({
  381. title: '',
  382. content: '确定要删除' + goodsName + '?',
  383. success: function(res) {
  384. if (res.confirm) {
  385. util.request(api.CartDelete, {
  386. cartId: cartId,
  387. checkCart: that.data.checkCart
  388. }, 'POST').then(function(res) {
  389. if (res.errno === 0) {
  390. that.setCommonData(res);
  391. }
  392. that.setCheckedData();
  393. });
  394. console.log('用户点击确定')
  395. }
  396. }
  397. });
  398. },
  399. getFootprintList() {
  400. let that = this;
  401. util.request(api.GuessFootprintList, {
  402. storeId: wx.getStorageSync('storeId'),
  403. checkCart: that.data.checkCart
  404. }, ).then(function(res) {
  405. if (res.errno === 0) {
  406. that.setData({
  407. footprintList: res.data.list
  408. });
  409. }
  410. });
  411. },
  412. switchAttrPop: function() {
  413. this.setData({
  414. openAttr: !this.data.openAttr
  415. })
  416. },
  417. clickSkuValue: function(event) {
  418. let that = this;
  419. let specValueId = event.currentTarget.dataset.valueId;
  420. let index = event.currentTarget.dataset.index;
  421. let _specificationList = this.data.specificationList;
  422. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  423. if (_specificationList[index].valueList[j].id == specValueId) {
  424. //如果已经选中,则反选
  425. if (_specificationList[index].valueList[j].checked) {
  426. _specificationList[index].valueList[j].checked = false;
  427. } else {
  428. _specificationList[index].valueList[j].checked = true;
  429. }
  430. } else {
  431. _specificationList[index].valueList[j].checked = false;
  432. }
  433. }
  434. this.setData({
  435. 'specificationList': _specificationList
  436. });
  437. //重新计算spec改变后的信息
  438. goodsUtil.changeSpecInfo(that);
  439. },
  440. cutNumber2: function() {
  441. this.setData({
  442. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  443. });
  444. },
  445. addNumber2: function() {
  446. this.setData({
  447. number: this.data.number + 1
  448. });
  449. },
  450. //购物车增加
  451. addCart: function(e) {
  452. let that = this;
  453. that.setData({
  454. number: 1
  455. });
  456. var goodsId = e.currentTarget.dataset.goodsId;
  457. util.request(api.GoodsSku, {
  458. goodsId: goodsId
  459. }).then(function(res) {
  460. if (res.errno === 0 && null != res.data) {
  461. that.setData({
  462. goodsVo: res.data.goodsVo,
  463. specificationList: res.data.specificationList,
  464. productList: res.data.productList,
  465. openAttr: !that.data.openAttr,
  466. stockNum: res.data.productList[0].stock_num,
  467. cartNumber: res.data.cartNumber,
  468. checkedSpecText: res.data.specificationList[0].valueList[0].value
  469. });
  470. //
  471. let _specificationList = res.data.specificationList;
  472. for (let i = 0; i < _specificationList.length; i++) {
  473. if (_specificationList[i].valueList.length == 1) {
  474. //如果已经选中,则反选
  475. _specificationList[i].valueList[0].checked = true;
  476. }
  477. }
  478. that.setData({
  479. 'specificationList': _specificationList
  480. });
  481. }
  482. });
  483. },
  484. //购物车增加
  485. addToCart: function() {
  486. let that = this;
  487. var goodsId = that.data.goodsVo.id;
  488. //提示选择完整规格
  489. if (!that.data.productList || !that.data.productList.length) {
  490. util.showErrorToast('当前门店没有库存');
  491. return false;
  492. }
  493. //提示选择完整规格
  494. if (!goodsUtil.isCheckedAllSpec(that)) {
  495. return false;
  496. }
  497. //根据选中的规格,判断是否有对应的sku信息
  498. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  499. if (!checkedProduct || checkedProduct.length <= 0) {
  500. //找不到对应的product信息,提示没有库存
  501. return false;
  502. }
  503. //验证库存
  504. if (checkedProduct.stock_num < this.data.number) {
  505. //找不到对应的product信息,提示没有库存
  506. return false;
  507. }
  508. util.request(api.CartAdd, {
  509. goodsId: goodsId,
  510. productId: checkedProduct[0].id,
  511. number: this.data.number,
  512. checkCart: that.data.checkCart
  513. }, 'POST').then(function(res) {
  514. if (res.errno === 0 && null != res.data) {
  515. wx.showToast({
  516. title: '添加成功',
  517. icon: 'success',
  518. mask: true
  519. });
  520. that.setData({
  521. openAttr: !that.data.openAttr
  522. })
  523. // 页面显示
  524. that.getCartList();
  525. that.getFootprintList();
  526. } else {
  527. wx.showToast({
  528. title: res.errmsg,
  529. icon: 'none'
  530. })
  531. }
  532. });
  533. },
  534. deleteValidCart: function() { //获取购物车数据
  535. let that = this;
  536. wx.showModal({
  537. title: '',
  538. content: '确定要清空所有失效商品?',
  539. success: function(res) {
  540. if (res.confirm) {
  541. util.request(api.deleteValidCart, {
  542. checkCart: that.data.checkCart
  543. }).then(function(res) {
  544. if (res.errno === 0) {
  545. that.setCommonData(res);
  546. } else {
  547. util.showErrorToast(res.errmsg);
  548. }
  549. });
  550. }
  551. }
  552. });
  553. }
  554. })