cart.js 15 KB

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