// // Cart_MyCartC.m // Asteria // // Created by 王猛 on 2024/1/26. // #import "Cart_MyCartC.h" #import "CartVM.h" #import "MyCarlTableV.h" #import "Cart_CheckoutC.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; @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); } - (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]; } #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.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)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.TableV reloadData]; } }else if ([mark isEqualToString:Cart_cartsMineItems_Post] || [mark isEqualToString:DEL_cartsMineItems] || [mark isEqualToString:PUT_cartsMineCoupons]){ if(sucessOrFail){ [self reqNet_Cart_cartsMineTotals]; }else{ [self.view makeToast:(NSString *)data duration:2 position:CSToastPositionCenter]; } } } - (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; } @end