cart.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. that.setCommonData(res);
  273. }
  274. that.setCheckedData();
  275. });
  276. },
  277. cutNumber: function (event) {
  278. let itemIndex = event.target.dataset.itemIndex;
  279. let cartItem = this.data.cartGoods[itemIndex];
  280. let beforeNumber = cartItem.number;
  281. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  282. cartItem.number = number;
  283. this.setData({
  284. cartGoods: this.data.cartGoods
  285. });
  286. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  287. },
  288. addNumber: function (event) {
  289. let itemIndex = event.target.dataset.itemIndex;
  290. let cartItem = this.data.cartGoods[itemIndex];
  291. let beforeNumber = cartItem.number;
  292. let number = cartItem.number + 1;
  293. cartItem.number = number;
  294. this.setData({
  295. cartGoods: this.data.cartGoods
  296. });
  297. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  298. },
  299. checkoutOrder: function () {
  300. //获取已选择的商品
  301. let that = this;
  302. util.request(api.getCurUser, {
  303. userInfo: app.globalData.userInfo
  304. }, 'POST').then(function (res) {
  305. if (res.errno === 0) {
  306. // console.log('that.data.mobile:' + res.data.mobile);
  307. if (res.data.mobile == '' || res.data.mobile == null) {
  308. wx.showModal({
  309. title: '',
  310. confirmColor: '#b4282d',
  311. showCancel: false,
  312. content: '您的手机号码未绑定,请先绑定手机号再进行购买',
  313. success: function (res) {
  314. if (res.confirm) {
  315. wx.navigateTo({
  316. url: '../../pages/auth/newuser/newuser'
  317. });
  318. }
  319. }
  320. });
  321. } else {
  322. var checkedGoods = that.data.cartGoods.filter(function (element, index, array) {
  323. if (element.checked == true) {
  324. return true;
  325. } else {
  326. return false;
  327. }
  328. });
  329. if (checkedGoods.length <= 0) {
  330. return false;
  331. }
  332. wx.navigateTo({
  333. url: '../shopping/checkout/checkout'
  334. })
  335. }
  336. }
  337. });
  338. },
  339. deleteCart: function (event) {
  340. //获取已选择的商品
  341. let that = this;
  342. let cartId = event.target.dataset.cartId;
  343. let goodsName = event.target.dataset.goodsName;
  344. wx.showModal({
  345. title: '',
  346. content: '确定要删除' + goodsName + '?',
  347. success: function (res) {
  348. if (res.confirm) {
  349. util.request(api.CartDelete, {
  350. cartId: cartId
  351. }, 'POST').then(function (res) {
  352. if (res.errno === 0) {
  353. that.setCommonData(res);
  354. }
  355. that.setCheckedData();
  356. });
  357. console.log('用户点击确定')
  358. }
  359. }
  360. });
  361. },
  362. getFootprintList() {
  363. let that = this;
  364. util.request(api.GuessFootprintList, { storeId: wx.getStorageSync('storeId') },).then(function (res) {
  365. if (res.errno === 0) {
  366. that.setData({
  367. footprintList: res.data.list
  368. });
  369. }
  370. });
  371. },
  372. switchAttrPop: function () {
  373. this.setData({
  374. openAttr: !this.data.openAttr
  375. })
  376. },
  377. clickSkuValue: function (event) {
  378. let that = this;
  379. let specValueId = event.currentTarget.dataset.valueId;
  380. let index = event.currentTarget.dataset.index;
  381. let _specificationList = this.data.specificationList;
  382. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  383. if (_specificationList[index].valueList[j].id == specValueId) {
  384. //如果已经选中,则反选
  385. if (_specificationList[index].valueList[j].checked) {
  386. _specificationList[index].valueList[j].checked = false;
  387. } else {
  388. _specificationList[index].valueList[j].checked = true;
  389. }
  390. } else {
  391. _specificationList[index].valueList[j].checked = false;
  392. }
  393. }
  394. this.setData({
  395. 'specificationList': _specificationList
  396. });
  397. //重新计算spec改变后的信息
  398. goodsUtil.changeSpecInfo(that);
  399. },
  400. cutNumber2: function () {
  401. this.setData({
  402. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  403. });
  404. },
  405. addNumber2: function () {
  406. this.setData({
  407. number: this.data.number + 1
  408. });
  409. },
  410. //购物车增加
  411. addCart: function (e) {
  412. let that = this;
  413. that.setData({
  414. number:1
  415. });
  416. var goodsId = e.currentTarget.dataset.goodsId;
  417. util.request(api.GoodsSku, { goodsId: goodsId }).then(function (res) {
  418. if (res.errno === 0 && null != res.data) {
  419. that.setData({
  420. goodsVo: res.data.goodsVo,
  421. specificationList: res.data.specificationList,
  422. productList: res.data.productList,
  423. openAttr: !that.data.openAttr,
  424. stockNum: res.data.productList[0].stock_num,
  425. cartNumber: res.data.cartNumber,
  426. checkedSpecText: res.data.specificationList[0].valueList[0].value
  427. });
  428. //
  429. let _specificationList = res.data.specificationList;
  430. for (let i = 0; i < _specificationList.length; i++) {
  431. if (_specificationList[i].valueList.length == 1) {
  432. //如果已经选中,则反选
  433. _specificationList[i].valueList[0].checked = true;
  434. }
  435. }
  436. that.setData({
  437. 'specificationList': _specificationList
  438. });
  439. }
  440. });
  441. },
  442. //购物车增加
  443. addToCart: function () {
  444. let that = this;
  445. var goodsId = that.data.goodsVo.id;
  446. //提示选择完整规格
  447. if (!that.data.productList || !that.data.productList.length) {
  448. util.showErrorToast('当前门店没有库存');
  449. return false;
  450. }
  451. //提示选择完整规格
  452. if (!goodsUtil.isCheckedAllSpec(that)) {
  453. return false;
  454. }
  455. //根据选中的规格,判断是否有对应的sku信息
  456. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  457. if (!checkedProduct || checkedProduct.length <= 0) {
  458. //找不到对应的product信息,提示没有库存
  459. return false;
  460. }
  461. //验证库存
  462. if (checkedProduct.stock_num < this.data.number) {
  463. //找不到对应的product信息,提示没有库存
  464. return false;
  465. }
  466. util.request(api.CartAdd, {
  467. goodsId: goodsId,
  468. productId: checkedProduct[0].id,
  469. number: this.data.number
  470. }, 'POST').then(function (res) {
  471. if (res.errno === 0 && null != res.data) {
  472. wx.showToast({
  473. title: '添加成功',
  474. icon: 'success',
  475. mask: true
  476. });
  477. that.setData({
  478. openAttr: !that.data.openAttr
  479. })
  480. // 页面显示
  481. that.getCartList();
  482. that.getFootprintList();
  483. } else {
  484. wx.showToast({
  485. title: res.errmsg,
  486. icon: 'none'
  487. })
  488. }
  489. });
  490. },
  491. deleteValidCart: function () {//获取购物车数据
  492. let that = this;
  493. wx.showModal({
  494. title: '',
  495. content: '确定要清空所有失效商品?',
  496. success: function (res) {
  497. if (res.confirm) {
  498. util.request(api.deleteValidCart).then(function (res) {
  499. if (res.errno === 0) {
  500. that.setCommonData(res);
  501. } else {
  502. util.showErrorToast(res.errmsg);
  503. }
  504. });
  505. }
  506. }
  507. });
  508. }
  509. })