cart.js 14 KB

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