1
0

cart.js 14 KB

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