1
0

cart.js 13 KB

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