cart.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. var shoppingCart = new Vue({
  2. el: "#shoppingCart",
  3. data: {
  4. userId: 0,
  5. userIdentity: '',
  6. userToken: '',
  7. listLoading: true,
  8. listData: [],
  9. invalidData: [],
  10. promotionsList: [],
  11. totalCouponList:[], // 优惠券算列表
  12. eligibleCoupons:[], // 最终需要用到优惠券
  13. kindCount: 0,
  14. totalCount: 0,
  15. totalPrice: 0,
  16. reducedPrice: 0,
  17. originalPrice: 0,
  18. allChecked: true,
  19. submitIds: [],//去结算商品Ids
  20. isShowPopup:false,
  21. currentTab:1,
  22. couponParam:{// 获取弹窗优惠券领取参数
  23. userId:0,
  24. shopId:0,
  25. status:1,
  26. source:1
  27. },
  28. isCouponEmpty:false,
  29. productCoupon:[], // 优惠券
  30. productCouponList:[], // 优惠券弹窗列表
  31. couponPrice:0, // 优惠金额
  32. totalDiscountAmount:0, // 总共减去金额
  33. isDiscount:false, // 控制显示优惠明细
  34. },
  35. watch:{
  36. listData: {
  37. handler: function() {
  38. console.log('obj.a changed');
  39. },
  40. immediate: true,
  41. deep: true
  42. }
  43. },
  44. filters: {
  45. NumFormat: function (text) {//处理金额
  46. return Number(text).toFixed(2);
  47. },
  48. TypeFormat:function(value) {
  49. switch (value) {
  50. case 0:
  51. return '活动券';
  52. break;
  53. case 1:
  54. return '品类券';
  55. break;
  56. case 2:
  57. return '用户专享券';
  58. break;
  59. case 3:
  60. return '店铺券';
  61. break;
  62. case 4:
  63. return '新用户券';
  64. break;
  65. }
  66. }
  67. },
  68. methods: {
  69. getCartLists: function () {
  70. var _self = this;
  71. if(this.userId ===0){return;}
  72. ShoppingApi.QueryShoppingCartList({userId: this.userId},function(response){
  73. if(response.code === 0){
  74. var data = response.data;
  75. _self.listData = data.list;
  76. _self.invalidData = data.invalid;
  77. _self.kindCount = data.kindCount;
  78. _self.totalCount = data.totalCount;
  79. _self.totalPrice = data.totalPrice;
  80. _self.promotionsList = data.promotions;
  81. _self.totalCouponList = data.couponList;
  82. // 默认全选
  83. _self.listData.forEach(function(supplier){
  84. supplier.checked = true;
  85. if(supplier.cartList.length>0){
  86. supplier.cartList.forEach(function(cartItem){
  87. _self.submitIds.push(cartItem.productId);
  88. })
  89. }
  90. });
  91. _self.allChecked = true;
  92. _self.computedPrice();
  93. _self.listLoading = false;
  94. }else{
  95. CAIMEI.Alert(response.msg,'确定',true, function(){});
  96. }
  97. })
  98. },
  99. toggleThisLadder: function(event){
  100. var el = event.currentTarget;
  101. if($(el).hasClass("on")){
  102. $(el).removeClass("on").siblings('.mFixed').hide();
  103. if(!isPC){looseBody();}
  104. }else{
  105. $(el).addClass("on").siblings('.mFixed').show();
  106. if(!isPC){fixedBody();}
  107. }
  108. },
  109. hideThisLadder: function(event){
  110. var el = event.currentTarget;
  111. $(el).parents('.priceTag').find('.tag').removeClass("on").siblings('.mFixed').hide();
  112. if(!isPC){looseBody();}
  113. },
  114. ckeckItem: function(event){
  115. var el = event.currentTarget;
  116. if(!$(el).prop("checked")){
  117. $(el).parents('.supplierItem').find('.supplierTit').find('input.check').prop("checked", false);
  118. }
  119. this.computedPrice();
  120. },
  121. ckeckSupplier: function(event, supplierChecked){
  122. var el = event.currentTarget;
  123. var _self = this;
  124. var cartList = $(el).parents('.supplierItem').find('input[name=cartList]');
  125. this.$nextTick(function(){
  126. if (supplierChecked) {
  127. $.each(cartList, function (index, item) {
  128. if (!_self.submitIds.includes(item.value*1)){
  129. _self.submitIds.push(item.value*1);
  130. }
  131. });
  132. }else{
  133. $.each(cartList, function (index, item) {
  134. var loc = _self.submitIds.indexOf(item.value*1);
  135. if(loc >=0 ){
  136. _self.submitIds.splice(loc, 1);
  137. _self.allChecked = false;
  138. }
  139. });
  140. }
  141. _self.computedPrice();
  142. });
  143. },
  144. ckeckAll: function(){
  145. var _self = this;
  146. this.$nextTick(function(){
  147. if (_self.allChecked) {
  148. _self.submitIds = [];
  149. _self.listData.forEach(function(supplier){
  150. supplier.checked = true;
  151. if(supplier.cartList.length>0){
  152. supplier.cartList.forEach(function(cartItem){
  153. _self.submitIds.push(cartItem.productId);
  154. })
  155. }
  156. });
  157. }else{
  158. _self.submitIds = [];
  159. _self.listData.forEach(function(supplier){
  160. supplier.checked = false;
  161. });
  162. }
  163. _self.computedPrice();
  164. });
  165. },
  166. setPromotions:function(){
  167. var _self = this;
  168. this.promotionsList.forEach(function(promotions){
  169. promotions.productList.forEach(function(product){
  170. _self.listData.map(function(supplier){
  171. supplier.cartList.map(function(cart){
  172. if(product.productId === cart.productId){
  173. product.number = cart.number
  174. }
  175. });
  176. });
  177. });
  178. });
  179. },
  180. computedPrice: function(){
  181. var _self = this;
  182. this.$nextTick(function(){
  183. var totalPrice = 0;
  184. var reducedPrice = 0;
  185. var originalPrice = 0;
  186. var kindCount = 0;
  187. var totalCount = 0;
  188. _self.listData.forEach(function(supplier){
  189. var supplierPrice = 0;
  190. var supplierReducedPrice = 0;
  191. if(supplier.cartList.length>0){
  192. supplier.cartList.forEach(function(cart){
  193. if (_self.submitIds.includes(cart.productId*1)){
  194. supplierPrice += cart.price*cart.number;
  195. kindCount += 1;
  196. totalCount += cart.number;
  197. // 单品满减
  198. if(cart.promotions && cart.promotions.type*1===1 && cart.promotions.mode*1===2){
  199. // 单品满减-重新计算供应商总价/满减金额
  200. if(cart.price*cart.number >= cart.promotions.touchPrice){
  201. supplierPrice -= cart.promotions.reducedPrice;
  202. supplierReducedPrice += cart.promotions.reducedPrice;
  203. }
  204. }
  205. }
  206. });
  207. // 店铺满减
  208. if(supplier.promotions && supplier.promotions.mode*1===2){
  209. // 店铺满减-计算供应商总价/满减金额
  210. if(supplierPrice >= supplier.promotions.touchPrice){
  211. supplierPrice -= supplier.promotions.reducedPrice;
  212. supplierReducedPrice += supplier.promotions.reducedPrice;
  213. }
  214. }
  215. }
  216. supplier.totalPrice = supplierPrice;
  217. supplier.reducedPrice = supplierReducedPrice;
  218. supplier.originalPrice = (supplierPrice+supplierReducedPrice);
  219. totalPrice += supplier.totalPrice;
  220. reducedPrice += supplier.reducedPrice;
  221. originalPrice += supplier.originalPrice;
  222. });
  223. // 总促销计算
  224. _self.promotionsList.forEach(function(promotions){
  225. // 凑单满减
  226. if(promotions.mode*1===2 && promotions.type*1===2){
  227. var total = 0;
  228. promotions.productList.forEach(function(product){
  229. if (_self.submitIds.includes(product.productId*1)){
  230. total += product.number * product.price;
  231. }
  232. });
  233. if(total>=promotions.touchPrice){
  234. totalPrice -= promotions.reducedPrice;
  235. reducedPrice += promotions.reducedPrice;
  236. }
  237. }
  238. });
  239. _self.totalPrice = totalPrice;
  240. _self.reducedPrice = reducedPrice;
  241. _self.originalPrice = originalPrice;
  242. _self.kindCount = kindCount;
  243. _self.totalCount = totalCount;
  244. // 计算优惠券
  245. if(_self.totalCouponList.length>0){
  246. _self.calculationCoupon();
  247. }
  248. // 最后满减金额 = 店铺减去金额 + 单品减去金额 + 凑单减去金额
  249. _self.totalDiscountAmount = _self.reducedPrice + _self.couponPrice;
  250. console.log('totalDiscountAmount',_self.totalDiscountAmount);
  251. // 控制显示优惠明细
  252. if(_self.totalDiscountAmount > 0 ){
  253. _self.isDiscount = true;
  254. }else{
  255. _self.isDiscount = false;
  256. }
  257. });
  258. },
  259. calculationCoupon:function(){// 优惠券计算
  260. var _self = this;
  261. var eligibleCoupons = [];
  262. _self.listData.forEach(function(shop,index){
  263. shop.cartList.forEach(function(pros){
  264. if(_self.submitIds.includes(pros.productId*1)){
  265. _self.totalCouponList.forEach(function (coupon,index){//循环优惠券
  266. switch(coupon.couponType){
  267. case 0:// 活动券
  268. if(coupon.productType == 1){// 活动券-全商城商品
  269. if(_self.totalPrice >= coupon.touchPrice){
  270. eligibleCoupons = _self.setCouponsArr(coupon)
  271. }
  272. }else if(coupon.productType == 2){// 活动券-指定商品
  273. if(coupon.productIds && coupon.productIds.includes(pros.productId)){
  274. if(_self.totalPrice >= coupon.touchPrice){
  275. eligibleCoupons = _self.setCouponsArr(coupon)
  276. }
  277. }
  278. }
  279. break;
  280. case 1:// 品类券适用于产品或者仪器 categoryType(commodityType) : 1 产品 2 仪器
  281. if( coupon.categoryType && coupon.categoryType == pros.commodityType){
  282. if(_self.totalPrice >= coupon.touchPrice){
  283. eligibleCoupons = _self.setCouponsArr(coupon)
  284. }
  285. }
  286. break;
  287. case 2:// 用户专享券 适用于商城全部商品
  288. if(_self.totalPrice >= coupon.touchPrice){
  289. eligibleCoupons = _self.setCouponsArr(coupon)
  290. }
  291. break;
  292. case 3:// 店铺券适用于某个供应商的全部商品
  293. if(coupon.shopId && pros.supplierId == coupon.shopId){
  294. if(_self.totalPrice >= coupon.touchPrice){
  295. eligibleCoupons = _self.setCouponsArr(coupon)
  296. }
  297. }
  298. break;
  299. case 4://新用户券 适用于商城全部商品
  300. if(_self.totalPrice >= coupon.touchPrice){
  301. eligibleCoupons = _self.setCouponsArr(coupon)
  302. }
  303. break;
  304. }
  305. })
  306. }else{
  307. return;
  308. }
  309. })
  310. })
  311. if(eligibleCoupons.length>0){
  312. _self.eligibleCoupons.splice(0,_self.eligibleCoupons.length);
  313. _self.eligibleCoupons.push(eligibleCoupons[0])
  314. _self.couponPrice = _self.eligibleCoupons.sort((a,b)=> b.couponAmount - a.couponAmount)[0].couponAmount
  315. }else{
  316. _self.couponPrice = 0
  317. }
  318. },
  319. setCouponsArr: function(coupon){//处理
  320. var list = [];
  321. list.push(coupon)
  322. list.sort((a,b)=> b.couponAmount - a.couponAmount )
  323. return list
  324. },
  325. cartNumberSub: function(cart){
  326. cart.number -= cart.step;
  327. if (cart.number<cart.min){
  328. cart.number = cart.min;
  329. }
  330. this.numberChange(cart);
  331. },
  332. cartNumberAdd: function(cart){
  333. cart.number += cart.step;
  334. if (cart.number>cart.stock){
  335. cart.number = cart.stock;
  336. }
  337. this.numberChange(cart);
  338. },
  339. cartNumberChange: function(cart){
  340. cart.number = Math.ceil(cart.number/cart.step)*cart.step;
  341. this.numberChange(cart);
  342. },
  343. numberChange: function(cart){
  344. if(cart.ladderFlag){
  345. cart.ladderPrices.forEach(function(ladder){
  346. if(cart.number>=ladder.buyNum){
  347. cart.price = ladder.buyPrice;
  348. cart.originalPrice = ladder.buyPrice;
  349. }
  350. });
  351. }
  352. // 设置优惠数量
  353. this.setPromotions();
  354. // 计算价格
  355. this.computedPrice();
  356. // 更新购物车
  357. var _self = this;
  358. this.$nextTick(function(){
  359. _self.updateCart(cart);
  360. });
  361. },
  362. updateCart: function(cart){
  363. var _self = this;
  364. ShoppingApi.ShoppingCartUpdate({userID:_self.userId,productID: cart.productId,productCount: cart.number},function(response){
  365. if(response.code === 0){
  366. console.log('更新购物车商品数量成功');
  367. }else{
  368. CAIMEI.Alert(response.msg,'确定',true, function(){});
  369. }
  370. })
  371. },
  372. deleteCart: function(productIds){
  373. var _self = this;
  374. ShoppingApi.DeleteCartProducts({userID:_self.userId,productIDs:productIds},function(response){
  375. if(response.code === 0){
  376. CAIMEI.dialog('删除成功',true,function () {
  377. _self.getCartLists();
  378. })
  379. }else{
  380. CAIMEI.Alert(response.msg,'确定',true, function(){});
  381. }
  382. })
  383. },
  384. deletefailureList:function(){
  385. var _self = this;
  386. var delGoodsList='';
  387. _self.invalidData.forEach(function(item){
  388. delGoodsList+= item.productId+','
  389. });
  390. CAIMEI.Modal('确定清空全部失效商品吗?','取消','确定',function(){
  391. _self.deleteCart(delGoodsList)
  392. });
  393. },
  394. deleteCartOne:function(productIds){
  395. var _self = this;
  396. CAIMEI.Modal('确定删除该商品吗?','取消','确定',function(){
  397. _self.deleteCart(productIds);
  398. });
  399. },
  400. deleteSelected: function(){
  401. var _self = this;
  402. var productIds = _self.submitIds.join(",");
  403. if(productIds == ''){
  404. CAIMEI.dialog('请选择要删除的商品~');
  405. return false;
  406. }
  407. CAIMEI.Modal('确定删除选中的商品吗?','取消','确定',function(){
  408. _self.deleteCart(productIds);
  409. });
  410. },
  411. submitCart: function(){
  412. if (this.submitIds.length>0){
  413. var productIds = this.submitIds.join(",");
  414. window.localStorage.setItem("shoppingProductIds", productIds);
  415. // type:(1购物车提交[对应表cm_cart],2直接购买提交, 3协销下单)
  416. window.location.href = '/shopping/confirm.html?type=1';
  417. }
  418. },
  419. queryPopupCoupons:function(){// 获取弹窗优惠券列表
  420. var _self = this;
  421. ShoppingApi.ShoppingCartGetCoupon(_self.couponParam, function (response) {
  422. if(response.code == 0){
  423. var data = response.data;
  424. if(data.couponList && data.couponList.length >0){
  425. _self.productCouponList = data.couponList;
  426. _self.isCouponEmpty = false;
  427. console.log('有优惠券')
  428. console.log(_self.productCouponList)
  429. }else{
  430. console.log('没有优惠券')
  431. _self.isCouponEmpty = true;
  432. }
  433. }else{
  434. console.log('获取优惠券列表失败')
  435. }
  436. })
  437. },
  438. receiveCoupon:function(coupon){// 点击领取优惠券
  439. var _self = this;
  440. ProductApi.ReceiveCoupon({userId:_self.couponParam.userId, couponId:coupon.couponId, source:1}, function (response) {
  441. if(response.code == 0){
  442. CAIMEI.dialog('领取成功',true,function () {
  443. _self.currentTab = 2;
  444. _self.couponParam.status = 2;
  445. _self.queryPopupCoupons();
  446. });
  447. }else{
  448. CAIMEI.Alert(response.msg, '确定', false);
  449. }
  450. })
  451. },
  452. queryCouponTabs:function (value){
  453. var _self = this;
  454. _self.currentTab = _self.couponParam.status = value;
  455. _self.queryPopupCoupons();
  456. },
  457. showPopup:function(shop){// 显示优惠券弹窗
  458. var _self = this;
  459. _self.productCouponList = [];
  460. _self.currentTab = 1;
  461. _self.couponParam.status = 1;
  462. _self.couponParam.shopId = shop.id
  463. _self.queryPopupCoupons();
  464. _self.isShowPopup = true;
  465. },
  466. hidePopup:function(){// 隐藏优惠券弹窗
  467. var _self = this;
  468. _self.isShowPopup = false;
  469. },
  470. },
  471. created: function () {
  472. if(globalUserData){
  473. this.userId = this.couponParam.userId = globalUserData.userId;
  474. this.userIdentity = globalUserData.identity;
  475. this.userToken = globalUserData.token;
  476. }
  477. // 获取列表数据
  478. this.getCartLists();
  479. },
  480. mounted: function () {
  481. var _self = this;
  482. var minAwayBtm = $("footer").height();
  483. //处理滚动条控制底部提交fixed
  484. $(window).scroll(function() {
  485. var awayBtm = $(document).height() - $(window).scrollTop() - $(window).height();
  486. if(minAwayBtm <= awayBtm){
  487. $(".summaryWrap").addClass('fixed');
  488. }else{
  489. $(".summaryWrap").removeClass("fixed");
  490. }
  491. });
  492. }
  493. });