123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630 |
- var shoppingCart = new Vue({
- el: "#shoppingCart",
- mixins: [cartMixins],
- data: {
- userId: 0,
- userIdentity: '',
- userToken: '',
- listLoading: true,
- listData: [],
- invalidData: [],
- promotionsList: [],
- totalCouponList:[], // 优惠券算列表
- eligibleCoupons:[], // 最终需要用到优惠券
- kindCount: 0,
- totalCount: 0,
- totalPrice: 0,
- reducedPrice: 0,
- originalPrice: 0,
- allChecked: true,
- submitIds: [],//去结算商品SkuIds
- checkenProsList:[],// 勾选中的商品
- isShowPopup:false,
- currentTab:1,
- couponParam:{// 获取弹窗优惠券领取参数
- userId:0,
- shopId:0,
- status:1,
- source:1
- },
- isCouponEmpty:false,
- productCoupon:[], // 优惠券
- productCouponList:[], // 优惠券弹窗列表
- couponPrice:0, // 优惠金额
- totalDiscountAmount:0, // 总共减去金额
- isDiscount:false, // 控制显示优惠明细
- isCheckedProductStatus:false
- },
- watch:{
- listData: {
- handler: function() {
- console.log('obj.a changed');
- },
- immediate: true,
- deep: true
- }
- },
- filters: {
- NumFormat: function (text) {//处理金额
- return Number(text).toFixed(2);
- },
- TypeFormat:function(value) {
- switch (value) {
- case 0:
- return '活动券';
- break;
- case 1:
- return '品类券';
- break;
- case 2:
- return '用户专享券';
- break;
- case 3:
- return '店铺券';
- break;
- case 4:
- return '新用户券';
- break;
- }
- }
- },
- methods: {
- getCartLists: function () {
- var _self = this;
- if(_self.userId ===0){return;}
- ShoppingApi.QueryShoppingCartList({userId: _self.userId,source:1},function(response){
- if(response.code === 0){
- var data = response.data;
- // _self.listData = data.list;
- _self.invalidData = data.invalid;
- _self.kindCount = data.kindCount;
- _self.totalCount = data.totalCount;
- _self.totalPrice = data.totalPrice;
- _self.promotionsList = data.promotions;
- _self.totalCouponList = data.couponList;
- _self.reducedPrice = data.reducedPrice;
- // 默认全选
- _self.listData = data.list.map(function(supplier){
- if(supplier.cartList.length>0){
- supplier.cartList.forEach(function(cart){
- if(cart.stock === 0 || cart.number > cart.stock ){
- cart.isChecked = false;
- supplier.isDisable = true
- cart.isDisable = true
- }else{
- cart.isDisable = false
- cart.isChecked = true;
- _self.submitIds.push(cart.skuId);
- _self.checkenProsList.push(cart);
- }
- cart.isSkuPopup = false;
- _self.isCheckedProductStatus = true;
- })
- }
- _self.updateProductCheckedAllBtn(supplier);
- return supplier;
- })
- _self.updateCheckAllBtn();
- _self.computedPrice();
- _self.listLoading = false;
- console.log('listData',_self.listData)
- }else{
- CAIMEI.Alert(response.msg,'确定',true, function(){});
- }
- })
- },
- toggleThisLadder: function(event){
- var el = event.currentTarget;
- if($(el).hasClass("on")){
- $(el).removeClass("on").siblings('.mFixed').hide();
- if(!isPC){looseBody();}
- }else{
- $(el).addClass("on").siblings('.mFixed').show();
- if(!isPC){fixedBody();}
- }
- },
- hideThisLadder: function(event){
- var el = event.currentTarget;
- $(el).parents('.priceTag').find('.tag').removeClass("on").siblings('.mFixed').hide();
- if(!isPC){looseBody();}
- },
- ckeckItem: function(supplier,pro){//为未选中的时候改变为true,反之为true
- var _self = this;
- if(pro.isDisable){ return }
- pro.isChecked = !pro.isChecked;
- if (pro.isChecked) {
- if (!_self.submitIds.includes(pro.skuId * 1)) {
- _self.submitIds.push(pro.skuId);
- _self.checkenProsList.push(pro);
- }
- _self.isCheckedProductStatus = true;
- } else {
- var lent = _self.submitIds.indexOf(pro.skuId * 1)
- if (lent >= 0) {
- _self.submitIds.splice(lent, 1);
- _self.checkenProsList.splice(lent, 1);
- }
- }
- _self.updateProductCheckedAllBtn(supplier)
- _self.updateCheckAllBtn()
- _self.computedPrice();
- },
- ckeckSupplier: function(supplier){
- var _self = this;
- if(supplier.isDisable){ return }
- supplier.isChecked = !supplier.isChecked;
- _self.setProductChecked(supplier);
- _self.updateCheckAllBtn();
- _self.computedPrice();
- },
- updateProductCheckedAllBtn: function(supplier) { // 单独每个供应商的勾选判断
- var productsList = supplier.cartList,
- productsCheckedLength = 0;
- productsList.forEach(pros => {
- if (pros.isChecked) {
- productsCheckedLength++;
- }
- })
- supplier.isChecked = productsCheckedLength === productsList.length
- },
- updateCheckAllBtn: function() { // 全选勾选判断
- var _self = this;
- var goodsCheckedLength = 0,
- goodsList = _self.listData;
- goodsList.forEach(function(supplier){
- if (supplier.isChecked) {
- goodsCheckedLength++;
- }
- })
- this.allChecked = goodsCheckedLength === goodsList.length;
- },
- updateBothCheckBtn: function(){// 更新商铺勾选状态
- var _self = this;
- this.listData = this.listData.map(supplier => {
- supplier.isChecked = _self.allChecked;
- _self.setProductChecked(supplier);
- return supplier
- })
- },
- setProductChecked: function(supplier) {//
- var _self = this;
- supplier.cartList.forEach(cart => {
- if (supplier.isChecked && (cart.stock !== 0 || cart.number < cart.stock)) {
- cart.isChecked = true;
- if (!_self.submitIds.includes(cart.skuId * 1)) {
- _self.submitIds.push(cart.skuId);
- _self.checkenProsList.push(cart);
- }
- _self.isCheckedProductStatus = true;
- } else {
- cart.isChecked = false;
- var lent = this.submitIds.indexOf(cart.skuId * 1);
- if (lent >= 0) {
- _self.submitIds.splice(lent, 1);
- _self.checkenProsList.splice(lent, 1);
- }
- }
- })
- this.computedPrice();
- },
- ckeckAll: function(){
- this.allChecked = !this.allChecked;
- this.updateBothCheckBtn()
- },
- setPromotions:function(){
- var _self = this;
- this.promotionsList.forEach(function(promotions){
- promotions.productList.forEach(function(product){
- _self.listData.map(function(supplier){
- supplier.cartList.map(function(cart){
- if(product.productId === cart.productId){
- product.number = cart.number
- }
- });
- });
- });
- });
- },
- computedPrice: function() {
- var _self = this;
- console.log('submitIds',this.submitIds)
- this.$nextTick(function () {
- var totalPrice = 0;
- var reducedPrice = 0;
- var originalPrice = 0;
- var kindCount = 0;
- var totalCount = 0;
- _self.listData.forEach(function (supplier) {
- var supplierPrice = 0;
- var supplierReducedPrice = 0;
- var svipSupplierPrice = 0;
- if (supplier.cartList.length > 0) {
- supplier.cartList.forEach(function (cart) {
- if (_self.submitIds.includes(cart.skuId * 1)) {
- supplierPrice += cart.price * cart.number;
- kindCount += 1;
- totalCount += cart.number;
- // 单品满减
- if (cart.promotions && cart.promotions.type * 1 === 1 && cart.promotions.mode * 1 === 2) {
- var price = cart.price * cart.number;
- if(cart.promotions.discount == 1){
- var discountNum = parseInt( price/cart.promotions.touchPrice);
- console.log('单品满减促销叠加通道叠加次数',discountNum)
- // 单品满减-重新计算供应商总价/满减金额
- if ( price >= cart.promotions.touchPrice) {
- supplierPrice -= cart.promotions.reducedPrice*discountNum
- supplierReducedPrice += cart.promotions.reducedPrice*discountNum
- }
- }else{
- console.log(' 单品满减非促销叠加通道')
- // 单品满减-重新计算供应商总价/满减金额
- if ( price >= cart.promotions.touchPrice) {
- supplierPrice -= cart.promotions.reducedPrice;
- supplierReducedPrice += cart.promotions.reducedPrice;
- }
- }
- }
- //sivp优惠价格
- if(cart.svipProductFlag && GLOBAL_VIP_FLAG === 1){
- svipSupplierPrice += ((cart.originalPrice - cart.price) * cart.number);
- }
- }
- });
- // 店铺满减
- if (supplier.promotions && supplier.promotions.mode * 1 === 2) {
- if(supplier.promotions.discount == 1){// 支持促销叠加条件的店铺满减 执行
- var prosDiscountNum = parseInt(supplierPrice/supplier.promotions.touchPrice) // 店铺满减促销叠加次数 = 店铺合计价格/满减促销价格
- console.log('店铺满减促销叠加通道叠加次数',prosDiscountNum)
- if (supplierPrice >= supplier.promotions.touchPrice) {
- supplierPrice -= supplier.promotions.reducedPrice*prosDiscountNum
- supplierReducedPrice += supplier.promotions.reducedPrice*prosDiscountNum
- }
- }else{
- console.log('店铺满减非促销叠加通道')
- // 店铺满减-计算供应商总价/满减金额
- if (supplierPrice >= supplier.promotions.touchPrice) {
- supplierPrice -= supplier.promotions.reducedPrice;
- supplierReducedPrice += supplier.promotions.reducedPrice;
- }
- }
- }
- }
- supplier.totalPrice = supplierPrice;
- supplier.reducedPrice = supplierReducedPrice + svipSupplierPrice; // 其他优惠价 + svip优惠价
- supplier.originalPrice = (supplierPrice + supplierReducedPrice);
- totalPrice += supplier.totalPrice;
- reducedPrice += supplier.reducedPrice;
- originalPrice += supplier.originalPrice;
- });
- if(_self.isCheckedProductStatus){
- // 总促销计算
- _self.promotionsList.forEach(function (promotions) {
- // 凑单满减
- if (promotions.mode * 1 === 2 && promotions.type * 1 === 2) {
- var collecTotal = 0;
- var collecDiscountNum = 0;
- promotions.productList.forEach(function (product) {
- collecTotal += _self.collecTotalPrice(product);
- });
- if(promotions.discount == 1){// 支持凑单满减促销叠加条件的执行
- collecDiscountNum += parseInt(collecTotal/promotions.touchPrice) // 凑单满减促销叠加次数 = 凑单商品合计价格/凑单满减促销价格
- console.log('凑单促销满减叠加通道叠加次数',`${collecDiscountNum}次`)
- if (collecTotal >= promotions.touchPrice) {
- totalPrice -= promotions.reducedPrice*collecDiscountNum
- reducedPrice += promotions.reducedPrice*collecDiscountNum
- }
- }else{
- console.log('凑单促销满减非叠加通道')
- if (collecTotal >= promotions.touchPrice) {
- totalPrice -= promotions.reducedPrice
- reducedPrice += promotions.reducedPrice
- }
- }
- }
- });
- _self.totalPrice = totalPrice;
- _self.reducedPrice = reducedPrice;
- _self.originalPrice = originalPrice;
- _self.kindCount = kindCount;
- _self.totalCount = totalCount;
- // 计算优惠券
- if (_self.totalCouponList.length > 0) {
- let eligibleCoupons = _self.calculationCoupon();
- if (eligibleCoupons.length > 0) {
- _self.eligibleCoupons.splice(0, _self.eligibleCoupons.length);
- _self.eligibleCoupons = eligibleCoupons.sort((a, b) => b.couponAmount - a.couponAmount)
- _self.couponPrice = this.eligibleCoupons[0].couponAmount
- } else {
- _self.couponPrice = 0
- }
- }
- // 最后满减金额 = 店铺减去金额 + 单品减去金额 + 凑单减去金额
- _self.totalDiscountAmount = _self.reducedPrice + _self.couponPrice;
- console.log('最终优惠金额', _self.totalDiscountAmount);
- // 控制显示优惠明细
- if (_self.totalDiscountAmount > 0) {
- _self.isDiscount = true;
- } else {
- _self.isDiscount = false;
- }
- }
- })
- },
- collecTotalPrice:function(pros){// 凑单满减计算勾选的凑单商品总价
- var _self = this;
- var price;
- _self.checkenProsList.forEach(el => {
- if(pros.productId == el.productId ){
- price = el.number * el.price
- }
- })
- return price
- },
- calculationCoupon:function(){// 优惠券计算
- var _self = this;
- var eligibleCoupons = [];
- _self.listData.forEach(function(shop,index){
- shop.cartList.forEach(function(pros){
- if(_self.submitIds.includes(pros.skuId*1)){
- _self.totalCouponList.forEach(function (coupon,index){//循环优惠券
- switch(coupon.couponType){
- case 0:// 活动券
- if(coupon.productType == 1){// 活动券-全商城商品
- if(_self.totalPrice >= coupon.touchPrice){
- eligibleCoupons.push(coupon)
- }
- }else if(coupon.productType == 2){// 活动券-指定商品
- if(coupon.productIds && coupon.productIds.includes(pros.productId)){
- if(_self.totalPrice >= coupon.touchPrice){
- eligibleCoupons.push(coupon)
- }
- }
- }
- break;
- case 1:// 品类券适用于产品或者仪器 categoryType(commodityType) : 1 产品 2 仪器
- if( coupon.categoryType && coupon.categoryType == pros.commodityType){
- if(_self.totalPrice >= coupon.touchPrice){
- eligibleCoupons.push(coupon)
- }
- }
- break;
- case 2:// 用户专享券 适用于商城全部商品
- if(_self.totalPrice >= coupon.touchPrice){
- eligibleCoupons.push(coupon)
- }
- break;
- case 3:// 店铺券适用于某个供应商的全部商品
- if(coupon.shopId && pros.shopId == coupon.shopId){
- if(_self.totalPrice >= coupon.touchPrice){
- eligibleCoupons.push(coupon)
- }
- }
- break;
- case 4://新用户券 适用于商城全部商品
- if(_self.totalPrice >= coupon.touchPrice){
- eligibleCoupons.push(coupon)
- }
- break;
- }
- })
- }else{
- return;
- }
- })
- });
- return eligibleCoupons;
- },
- cartNumberSub: function(cart){
- cart.number -= cart.step;
- if (cart.number<cart.min){
- cart.number = cart.min;
- }
- this.numberChange(cart);
- },
- cartNumberAdd: function(cart){
- cart.number += cart.step;
- if (cart.number>cart.stock){
- cart.number = cart.stock;
- }
- this.numberChange(cart);
- },
- cartNumberChange: function(cart){
- cart.number = Math.ceil(cart.number/cart.step)*cart.step;
- this.numberChange(cart);
- },
- numberChange: function(cart){
- if(cart.ladderFlag){
- cart.ladderPrices.forEach(function(ladder){
- if(cart.number>=ladder.buyNum){
- cart.price = ladder.buyPrice;
- cart.originalPrice = ladder.buyPrice;
- }
- });
- }
- // 设置优惠数量
- this.setPromotions();
- // 计算价格
- this.computedPrice();
- // 更新购物车
- var _self = this;
- this.$nextTick(function(){
- _self.updateCart(cart);
- });
- },
- updateCart: function(cart){
- var _self = this;
- ShoppingApi.ShoppingCartUpdate(
- {
- userId:_self.userId,
- skuId: cart.skuId,
- productCount: cart.number,
- source:1,
- },
- function(response){
- if(response.code === 0){
- console.log('更新购物车商品数量成功');
- }else{
- CAIMEI.Alert(response.msg,'确定',true, function(){});
- }
- }
- )
- },
- deleteCart: function(skuIds){
- var _self = this;
- ShoppingApi.DeleteCartProducts({userId:_self.userId,skuIds:skuIds},function(response){
- if(response.code === 0){
- CAIMEI.dialog('删除成功',true,function () {
- _self.getCartLists();
- })
- }else{
- CAIMEI.Alert(response.msg,'确定',true, function(){});
- }
- })
- },
- deletefailureList:function(){
- var _self = this;
- var delGoodsList='';
- _self.invalidData.forEach(function(item){
- delGoodsList+= item.skuId+','
- });
- CAIMEI.Modal('确定清空全部失效商品吗?','取消','确定',function(){
- _self.deleteCart(delGoodsList)
- });
- },
- deleteCartOne:function(skuId){
- var _self = this;
- CAIMEI.Modal('确定删除该商品吗?','取消','确定',function(){
- _self.deleteCart(skuId);
- });
- },
- deleteSelected: function(){
- var _self = this;
- var skuIds = _self.submitIds.join(",");
- if(skuIds === ''){
- CAIMEI.dialog('请选择要删除的商品~');
- return false;
- }
- CAIMEI.Modal('确定删除选中的商品吗?','取消','确定',function(){
- _self.deleteCart(skuIds);
- });
- },
- queryPopupCoupons:function(){// 获取弹窗优惠券列表
- var _self = this;
- ShoppingApi.ShoppingCartGetCoupon(_self.couponParam, function (response) {
- if(response.code === 0){
- var data = response.data;
- if(data.couponList && data.couponList.length >0){
- _self.productCouponList = data.couponList;
- _self.isCouponEmpty = false;
- }else{
- _self.isCouponEmpty = true;
- }
- }else{
- console.log('获取优惠券列表失败')
- }
- })
- },
- toDeductCoupon:function (coupon) {// 点击购买按钮事件处理
- var _self = this;
- console.log(coupon)
- if(_self.couponParam.userId == 0){
- window.location.href='/login.html';
- }else{
- ProductApi.CreatePayCouponBeans({userId:_self.couponParam.userId, couponId:coupon.couponId, source:2}, function (response) {
- if(response.code == 0){
- CAIMEI.dialog('抵扣成功',true,function () {
- setTimeout(function(){
- _self.currentTab = 2;
- _self.couponParam.status = 2;
- _self.queryPopupCoupons();
- },200)
- });
- }else{
- CAIMEI.Alert(response.msg, '确定', false);
- }
- })
- }
- },
- receiveCoupon:function(coupon){// 点击领取优惠券
- var _self = this;
- ProductApi.ReceiveCoupon({userId:_self.couponParam.userId, couponId:coupon.couponId, source:2}, function (response) {
- if(response.code == 0){
- CAIMEI.dialog('领取成功',true,function () {
- _self.currentTab = 2;
- _self.couponParam.status = 2;
- _self.queryPopupCoupons();
- });
- }else{
- CAIMEI.Alert(response.msg, '确定', false);
- }
- })
- },
- toBuyCoupon:function (coupon) {// 点击购买按钮事件处理
- var _self = this;
- ProductApi.createCouponRecord({userId:_self.userId, couponId:coupon.couponId}, function (response) {
- if(response.code == 0){
- let couponRecordId = response.data.couponRecordId
- window.location.href = '/pay/caimei-hlbpay.html?pageType=3&couponId='+coupon.couponId+'&couponRecordId='+couponRecordId;
- }else{
- if(response.code == -1){//个人机构不能购买
- CAIMEI.Alert('该优惠券仅限医美机构购买,请升级为医美机构后再次购买。', '去升级', true, function(){
- window.location.href = "/user/setting/upgrade.html";
- });
- }else if(response.code == -2){//会员机构不是医美机构不能购买
- CAIMEI.dialog('该优惠券仅限医美机构购买',false,function () {});
- }else{
- CAIMEI.Alert(response.msg, '确定', false);
- }
- }
- })
- },
- queryCouponTabs:function (value){
- var _self = this;
- _self.currentTab = _self.couponParam.status = value;
- _self.queryPopupCoupons();
- },
- showPopup:function(shop){// 显示优惠券弹窗
- var _self = this;
- _self.productCouponList = [];
- _self.currentTab = 1;
- _self.couponParam.status = 1;
- _self.couponParam.shopId = shop.shopId
- _self.queryPopupCoupons();
- _self.isShowPopup = true;
- },
- hidePopup:function(){// 隐藏优惠券弹窗
- var _self = this;
- _self.isShowPopup = false;
- },
- hanldHrefLink:function (shop) {
- if(shop.shopType == 2){
- return;
- }
- window.open('/supplier-'+shop.shopId+'.html');
- }
- },
- created: function () {
- if(globalUserData){
- this.userId = this.couponParam.userId = this.addParams.userId = globalUserData.userId;
- this.userIdentity = globalUserData.identity;
- this.userToken = globalUserData.token;
- }
- // 获取列表数据
- this.getCartLists();
- },
- mounted: function () {
- var _self = this;
- var minAwayBtm = $("footer").height();
- //处理滚动条控制底部提交fixed
- $(window).scroll(function() {
- var awayBtm = $(document).height() - $(window).scrollTop() - $(window).height();
- if(minAwayBtm <= awayBtm){
- $(".summaryWrap").addClass('fixed');
- }else{
- $(".summaryWrap").removeClass("fixed");
- }
- });
- }
- });
|