// // Cart_MyCartC.m // Asteria // // Created by 王猛 on 2024/1/26. // #import "Cart_MyCartC.h" #import "CartVM.h" #import "MyCarlTableV.h" #import "Cart_CheckoutC.h" #import "ASGoodsDetailsVM.h" #import "ASAddGiftGoodsView.h" @interface Cart_MyCartC () @property (nonatomic, strong) CartVM *VM; @property (nonatomic, strong) UIView *topBgV; @property (nonatomic, strong) UILabel *top_totalLab; @property (nonatomic, strong) QMUILabel *top_priceLab; @property (nonatomic, strong) MyCarlTableV *TableV; @property (nonatomic, strong) CartTotalsM *totalsM; @property (nonatomic, strong) MyCartCouponCellData *couponCellM; @property (nonatomic, strong) MyCartGrandTotalCellData *totalCellM; @property (nonatomic, strong) UIButton *postBtn; //手动添加赠品至购物车 @property (nonatomic, strong) ASGoodsDetailsVM *goodsDetailsVM; @property (nonatomic, strong) ASAddGiftGoodsView *addGiftView; @property (nonatomic, strong) NSMutableArray *addGiftParamArr; @property (nonatomic, assign) int index; @property (nonatomic, assign) int giftShowCount; @end @implementation Cart_MyCartC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"My Cart"; [self ucm_subVeiwsTapBlock]; } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [self reqNet_Cart_cartsMineTotals]; } - (void)initSubviews { [super initSubviews]; [self.view addSubview:self.topBgV]; [self.topBgV addSubview:self.top_totalLab]; [self.topBgV addSubview:self.top_priceLab]; [self.top_totalLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.mas_offset(20); make.centerY.equalTo(self.topBgV); }]; [self.top_priceLab mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.top_totalLab.mas_right).offset(20); make.right.mas_equalTo(-20); make.centerY.equalTo(self.topBgV); }]; IPhoneXHeigh [self setupTableV:[MyCarlTableV class] Frame:CGRectMake(0, CGRectGetMaxY(self.topBgV.frame), KScreenWidth, KScreenHeight-securityBottom_H - 65- CGRectGetMaxY(self.topBgV.frame)-self.postBtn.mj_h)]; self.TableV.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"]; [self.view addSubview:self.postBtn]; self.postBtn.mj_y = CGRectGetMaxY(self.TableV.frame); self.addGiftParamArr = [[NSMutableArray alloc] initWithCapacity:1]; self.index = 0; self.giftShowCount = 0; } - (void)ucm_subVeiwsTapBlock{ @weakify(self) self.TableV.tapClose = ^(NSInteger num, id data) { //num = 0 删除商品 ,1 修改数量 countV @strongify(self) if([data isKindOfClass:[CartTotalsItemsM class]]){ switch (num) { case 0: [self reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)data]; break; case 1: [self reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)data]; break; default: break; } }else if ([data isKindOfClass:[MyCartCouponCellData class]]){ MyCartCouponCellData *couponM = (MyCartCouponCellData *)data; if(num ==0){ //使用优惠券 [self reqNet_PUT_cartsMineCoupons:couponM.coupon_code]; }else if(num == 1){//删除优惠券 [self reqNet_DEL_cartsMineCoupons]; } } }; } - (void)ucm_bindvmmodel{ self.VM = [[CartVM alloc] initDelegate:self]; self.goodsDetailsVM = [[ASGoodsDetailsVM alloc] initDelegate:self]; } #pragma mark - **************** reqNet **************** ///获取购物车 -(void)reqNet_Cart_cartsMineTotals{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestGetApi:Cart_cartsMineTotals param:@{}]; } ///修改商品的数量 -(void)reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)itemM{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; NSDictionary *cartItemDic = @{ @"item_id":itemM.item_id, @"qty":itemM.qty, @"quote_id":self.totalsM.cart_id }; NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{ @"cartItem":cartItemDic }]; [self.VM ry_requestPostApi:Cart_cartsMineItems_Post param:param]; } ///删除商品 -(void)reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)itemM{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestDeleteApi:DEL_cartsMineItems paramStr:itemM.item_id]; } -(void)reqNet_PUT_cartsMineCoupons:(NSString *)coupon{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; self.giftShowCount = 0; [self.VM ry_requestPutApi:PUT_cartsMineCoupons param:coupon]; } -(void)reqNet_DEL_cartsMineCoupons{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestDeleteApi:DEL_cartsMineCoupons paramStr:@""]; } //请求赠品商品详情 -(void)reqNet_GoodsDetails_productGetProduct:(NSString *)enterId{ NSMutableDictionary * params = [[NSMutableDictionary alloc] init]; // [params setObject:@"55475" forKey:@"productId"]; [params setObject:enterId forKey:@"productId"]; [params setObject:ASCurrencyManager.shared.currentCur forKey:@"currencyCode"]; // [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.goodsDetailsVM ry_requestGetApi:Goods_productGetProductsById param:params]; } //赠品加车 - (void)requestGiftAddCart { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; for (int i = 0; i < self.addGiftParamArr.count; i++) { NSDictionary *param = self.addGiftParamArr[i]; self.index ++; [self.VM ry_formDataRequestPostApi:Chectout_giftAddCart param:param]; } } - (void)ry_respnsData:(id)data parseAry:(NSMutableArray *)arry sucess:(BOOL)sucessOrFail mark:(NSString *)mark reqNetType:(ReqNetType)reqNetType{ [MBProgressHUD hideHUDForView:self.view animated:YES]; if([mark isEqualToString:Cart_cartsMineTotals]){ if(sucessOrFail){ CartTotalsM *model = [arry firstObject]; self.totalsM = model; self.top_totalLab.text = [NSString stringWithFormat:@"%@ pcs | total",model.items_qty]; self.top_priceLab.text =[NSString stringWithFormat:@"%@%@",model.currency_symbol,model.subtotal]; [self.TableV.infodata removeAllObjects]; self.TableV.infodata = [NSMutableArray arrayWithArray:model.items]; self.couponCellM.couponAry =[NSMutableArray arrayWithArray: [ASNetTools shared].xxx_couponAry]; self.couponCellM.coupon_code = model.coupon_code; self.couponCellM.discount_amount = model.discount_amount; self.couponCellM.currency_symbol = model.currency_symbol; [self.TableV.infodata addObject:self.couponCellM]; self.totalCellM.total_segments = [NSMutableArray arrayWithArray:model.total_segments]; self.totalCellM.currency_symbol = model.currency_symbol; [self.TableV.infodata addObject:self.totalCellM]; MyCartPayTypeCellData *data = [[MyCartPayTypeCellData alloc]init]; data.final_prices_f = [model.subtotal floatValue]; data.currency_symbol = model.currency_symbol; [self.TableV.infodata addObject:data]; //手动添加赠品功能 [self.addGiftParamArr removeAllObjects]; NSDictionary *freeData = (NSDictionary *)[[self.totalsM.extension_attributes objectForKey:@"free_data"] mj_JSONObject]; BOOL is_show = [[freeData objectForKey:@"is_show"] boolValue]; if (is_show && self.giftShowCount< 1) {//显示存在赠品 NSArray *productArr = [freeData objectForKey:@"products"]; if (productArr.count > 0) { //存在赠品 for (int i = 0; i < productArr.count; i++) { NSDictionary *productDic = productArr[i]; NSString *entity_id = productDic[@"entity_id"]; [self reqNet_GoodsDetails_productGetProduct:entity_id]; } } } [self.TableV reloadData]; } }else if ([mark isEqualToString:Cart_cartsMineItems_Post] || [mark isEqualToString:DEL_cartsMineItems] || [mark isEqualToString:PUT_cartsMineCoupons] || [mark isEqualToString:Chectout_giftAddCart]){ if(sucessOrFail){ if ([mark isEqualToString:Chectout_giftAddCart]) { self.index --; if (self.index == 0) { [self reqNet_Cart_cartsMineTotals]; } } else { [self reqNet_Cart_cartsMineTotals]; } }else{ [self.view makeToast:(NSString *)data duration:2 position:CSToastPositionCenter]; } } else if ([mark isEqualToString:Goods_productGetProductsById]) { //请求赠品详情 GoodsInformationM *model = (GoodsInformationM *)[arry firstObject]; NSLog(@"optionsoptions====%@=====%@", model.Id, model.options); NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{@"product_id":model.Id, @"isPromoItems":@"yes"}]; if (model.options.count > 0) { NSMutableDictionary *optionDic = [NSMutableDictionary dictionary]; for (OptionsModel *optionM in model.options) { OptionsValuesM *valuesM = optionM.values[0]; NSString *tempStr =[NSString stringWithFormat:@"options[%@]",optionM.option_id]; optionDic[tempStr] =valuesM.option_type_id; } [param addEntriesFromDictionary:optionDic]; } // param = /*@{@"product_id":model.Id, @"isPromoItems":@"1"}*/; [self.addGiftParamArr addObject:param]; if (self.giftShowCount < 1 && self.addGiftParamArr.count == 1 && self.addGiftView.hidden) { self.addGiftView.hidden = NO; [self.addGiftView setGiftData:model.add_gooodsImgUrl]; } } } - (void)tapcellTriggereventIndex:(NSIndexPath *)index model:(id)model{ if([model isKindOfClass:[CartTotalsItemsM class]]){ CartTotalsItemsM *itemM = (CartTotalsItemsM *)model; if(itemM.isGift){ return; } UIViewController *viewController = [[CTMediator sharedInstance] Goods_GoodsDetailsC:@{@"entity_id":itemM.product_id}]; [self.navigationController pushViewController:viewController animated:YES]; } } -(void)handle_postEvent:(UIButton *)btn{ Cart_CheckoutC *vc = [[Cart_CheckoutC alloc]init]; vc.totalsM = self.totalsM; [[Current_normalTool topViewController].navigationController pushViewController:vc animated:YES]; } #pragma mark - **************** lazy **************** -(UIView *)topBgV{ if(!_topBgV){ IPhoneXHeigh _topBgV = [[UIView alloc]initWithFrame:CGRectMake(0, securitytop_Y, KScreenWidth, 60)]; _topBgV.backgroundColor = [UIColor colorWithHexString:@"#E0FFF5"]; } return _topBgV; } - (UILabel *)top_totalLab { if (!_top_totalLab) { _top_totalLab = [[UILabel alloc] initWithFrame:CGRectZero]; _top_totalLab.textAlignment = NSTextAlignmentLeft; _top_totalLab.font = [UIFont fontWithName:Rob_Regular size:14]; _top_totalLab.textColor = [UIColor colorWithHexString:@"#000000"]; } return _top_totalLab; } - (QMUILabel *)top_priceLab { if (!_top_priceLab) { _top_priceLab = [[QMUILabel alloc] initWithFrame:CGRectZero]; _top_priceLab.textAlignment = NSTextAlignmentLeft; _top_priceLab.font = [UIFont fontWithName:Rob_Bold size:16]; } return _top_priceLab; } - (MyCartCouponCellData *)couponCellM { if (!_couponCellM) { _couponCellM = [[MyCartCouponCellData alloc] init]; } return _couponCellM; } - (MyCartGrandTotalCellData *)totalCellM { if (!_totalCellM) { _totalCellM = [[MyCartGrandTotalCellData alloc] init]; } return _totalCellM; } - (UIButton *)postBtn { if (!_postBtn) { _postBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _postBtn.layer.cornerRadius = 4; _postBtn.clipsToBounds = YES; IPhoneXHeigh _postBtn.frame = CGRectMake(10, 0, KScreenWidth-20, 45); _postBtn.backgroundColor = [UIColor colorWithHexString:@"#000000"]; [_postBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal]; _postBtn.titleLabel.font = [UIFont fontWithName:Rob_Regular size:16]; [_postBtn setTitle:@"POST COMMENT" forState:UIControlStateNormal]; [_postBtn addTarget:self action:@selector(handle_postEvent:) forControlEvents:UIControlEventTouchUpInside]; } return _postBtn; } - (ASAddGiftGoodsView *)addGiftView { if (!_addGiftView) { _addGiftView = [[ASAddGiftGoodsView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)]; @weakify(self) _addGiftView.addBlock = ^(int type) { @strongify(self) self.addGiftView.hidden = YES; if (type == 1) { [self requestGiftAddCart]; } else { self.giftShowCount ++; } }; _addGiftView.hidden = YES; [[UIApplication sharedApplication].keyWindow addSubview:_addGiftView]; } return _addGiftView; } @end