cart.js 13 KB

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