// // Cart_CheckoutC.m // Asteria // // Created by 王猛 on 2024/2/16. // #import "Cart_CheckoutC.h" #import "ASAddressListViewController.h" #import "ASCheckoutTopView.h" #import "ASCheckoutBottomView.h" #import "CartCheckTableV.h" #import "CartVM.h" #import "ASUserModel.h" @interface Cart_CheckoutC () @property (nonatomic, strong) CartVM *VM; @property (nonatomic, strong) ASCheckoutTopView *topView; @property (nonatomic, strong) ASCheckoutBottomView *bottomView; @property (nonatomic, strong) CartCheckTableV *TableV; @property (nonatomic, strong) MyCartCouponCellData *couponCellM; @property (nonatomic, strong) MyCartGrandTotalCellData *totalCellM; @property (nonatomic, strong) ASCheckoutPointData *pointCellM; @property (nonatomic, strong) ASCheckoutAddressData *addressCellM; @property (nonatomic, strong) ASCheckoutShipMethodData *shipmethodCellM; @property (nonatomic, strong) ASCheckoutCommentData *commentCellM; //存放支付方式 @property (nonatomic, strong) NSArray *paymentArray; //选中支付方式 @property (nonatomic, strong) ASCheckoutPaymentModel *paymentModel; @end @implementation Cart_CheckoutC - (void)viewDidLoad { [super viewDidLoad]; self.title = @"Checkout"; //获取支付方式 [self requestPaymentMode]; //获取运输方式 [self requestGetShipMethodByAddress]; //获取购物车信息(地址信息) [self requestCartInfo]; } - (void)initSubviews { [super initSubviews]; IPhoneXHeigh [self setupTableV:[CartCheckTableV class] Frame:CGRectMake(0, securitytop_Y , KScreenWidth, KScreenHeight - securitytop_Y)]; self.TableV.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"]; self.TableV.tableHeaderView = self.topView; self.TableV.tableFooterView = self.bottomView; @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]; } } else if ([data isKindOfClass:[ASCheckoutPointData class]]) { ASCheckoutPointData *pointData = (ASCheckoutPointData *)data; if(num ==0){ //使用积分 [self requestUseLimitPoint:pointData.usePoint]; }else if(num == 1){//取消使用积分 [self requestCancelUsePoint]; } } else if ([data isKindOfClass:[ASCheckoutCommentData class]]) { ASCheckoutCommentData *commentData = (ASCheckoutCommentData *)data; if(num ==0){ //使用积分 self.commentCellM.commentStr = commentData.commentStr; } } else if ([data isKindOfClass:[ASCheckoutShipMethodData class]]) { //选择运输方式、、、、并设置 ASCheckoutShipMethodData *shipMethodData = (ASCheckoutShipMethodData *)data; self.shipmethodCellM = shipMethodData; for (int i = 0; i < self.shipmethodCellM.shipMethodArray.count; i++) { ASCheckoutShipMethodModel *shipModel = [self.shipmethodCellM.shipMethodArray objectAtIndex:i]; if (i == num) { shipModel.isSelect = YES; self.shipmethodCellM.shipMethodModel = shipModel; } else { shipModel.isSelect = NO; } } [self requestSetShipMethodByAddress]; } }; } - (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 updateData]; } } else if ([mark isEqualToString:Chectout_getPaymentMode]) { if(sucessOrFail){ // 更新支付方式 self.paymentArray = arry; if (self.paymentArray.count > 0) { self.paymentModel = self.paymentArray[0]; [self.bottomView setBottomPayStyle:self.paymentModel.code]; } //首次进入展示购物车数据UI [self updateData]; } } else if ([mark isEqualToString:Cart_cartInfo]) { if(sucessOrFail){ // 获取上次下单地址 NSDictionary *dic = (NSDictionary *)data; NSArray *array = [[dic objectForKey:@"extension_attributes"] objectForKey:@"shipping_assignments"]; if (array.count > 0) { NSDictionary *shipping_assignments = array[0]; NSDictionary *shippingDic = [shipping_assignments objectForKey:@"shipping"]; NSDictionary *addressDic = [shippingDic objectForKey:@"address"]; // ASAddressModel *addressModel = [ASAddressModel defualtData]; ASAddressReginModel *reginModel = [ASAddressReginModel mj_objectWithKeyValues:addressDic]; ASAddressModel *addressModel = [ASAddressModel mj_objectWithKeyValues:addressDic]; addressModel.region = reginModel; if (!NIL(addressModel.postcode)) { self.addressCellM.addressModel = addressModel; if (self.shipmethodCellM.shipMethodModel) { //默认设置第一个运输方式 [self requestSetShipMethodByAddress]; } } } //首次进入展示购物车数据UI [self updateData]; } } else if ([mark isEqualToString:Chectout_addShipAddress_id]) { if(sucessOrFail){ NSArray *shipMethodArr = (NSArray *)data; if (shipMethodArr.count > 0) { self.shipmethodCellM.shipMethodArray = [ASCheckoutShipMethodModel mj_objectArrayWithKeyValuesArray:shipMethodArr]; ASCheckoutShipMethodModel *firstModel = self.shipmethodCellM.shipMethodArray[0]; firstModel.isSelect = YES; self.shipmethodCellM.shipMethodModel = firstModel; self.shipmethodCellM.shipmethodSymbol = self.totalsM.currency_symbol; //默认设置第一个运输方式 [self requestSetShipMethodByAddress]; } [self reqNet_Cart_cartsMineTotals]; } } else if ([mark isEqualToString:Chectout_getShipMethod]) { if(sucessOrFail){ NSArray *shipMethodArr = (NSArray *)data; if (shipMethodArr.count > 0) { self.shipmethodCellM.shipMethodArray = [ASCheckoutShipMethodModel mj_objectArrayWithKeyValuesArray:shipMethodArr]; ASCheckoutShipMethodModel *firstModel = self.shipmethodCellM.shipMethodArray[0]; firstModel.isSelect = YES; self.shipmethodCellM.shipMethodModel = firstModel; self.shipmethodCellM.shipmethodSymbol = self.totalsM.currency_symbol; if (self.addressCellM.addressModel) { //默认设置第一个运输方式 [self requestSetShipMethodByAddress]; } } } [self updateData]; } else if ([mark isEqualToString:Cart_cartsMineItems_Post] || [mark isEqualToString:DEL_cartsMineItems] || [mark isEqualToString:PUT_cartsMineCoupons] || [mark isEqualToString:Chectout_PUT_usePoints] || [mark isEqualToString:Chectout_DEL_cancelusePoints] || [mark isEqualToString:Chectout_setShipMethod]){ if(sucessOrFail){ [self reqNet_Cart_cartsMineTotals]; }else{ [self.view makeToast:(NSString *)data duration:2 position:CSToastPositionCenter]; } } } - (void)updateData{ [self.topView setTopViewData:self.totalsM.items_qty price:[NSString stringWithFormat:@"%@%@",self.totalsM.currency_symbol,self.totalsM.subtotal]]; [self.TableV.infodata removeAllObjects]; self.TableV.infodata = [NSMutableArray arrayWithArray:self.totalsM.items]; //邮寄地址--邮寄方式 if (self.addressCellM.addressModel) { [self.TableV.infodata addObject:self.addressCellM]; } //运输方式 [self.TableV.infodata addObject:self.shipmethodCellM]; //优惠券 self.couponCellM.couponAry =[NSMutableArray arrayWithArray: [ASNetTools shared].xxx_couponAry]; self.couponCellM.coupon_code = self.totalsM.coupon_code; self.couponCellM.discount_amount = self.totalsM.discount_amount; self.couponCellM.currency_symbol = self.totalsM.currency_symbol; [self.TableV.infodata addObject:self.couponCellM]; //积分 NSDictionary *pointScaleDic = [Current_normalTool dicFromjsonStr:[self.totalsM.extension_attributes objectForKey:@"mw_earn_points_data"]]; if (pointScaleDic.count > 0) { self.pointCellM.pointScale = [[pointScaleDic allValues] objectAtIndex:0]; self.pointCellM.priceScale = [[pointScaleDic allKeys] objectAtIndex:0]; } self.pointCellM.usePoint = [NSString stringWithFormat:@"%@", [self.totalsM.extension_attributes objectForKey:@"mw_rwrdpoints_amnt"]]; self.pointCellM.usePrice = [NSString stringWithFormat:@"%@", [self.totalsM.extension_attributes objectForKey:@"mw_rwrdpoints_cur_amnt"]]; self.pointCellM.pointBalance = [ASUserInfoManager shared].userPoints; self.pointCellM.priceSymbol = self.totalsM.currency_symbol; [self.TableV.infodata addObject:self.pointCellM]; //备注 [self.TableV.infodata addObject:self.commentCellM]; //价格明细 self.totalCellM.total_segments = [NSMutableArray arrayWithArray:self.totalsM.total_segments]; self.totalCellM.currency_symbol = self.totalsM.currency_symbol; [self.TableV.infodata addObject:self.totalCellM]; //支付方式数据 [self.TableV.infodata addObjectsFromArray:self.paymentArray]; [self.TableV reloadData]; } - (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]; } else if ([model isKindOfClass:[ASCheckoutPaymentModel class]]) { ASCheckoutPaymentModel *paymentModel = (ASCheckoutPaymentModel *)model; if (paymentModel.isSelect) { return; } for (int i = 0; i < self.TableV.infodata.count; i++) { id itemModel = self.TableV.infodata[i]; if ([itemModel isKindOfClass:[ASCheckoutPaymentModel class]]) { ASCheckoutPaymentModel *tempModel = (ASCheckoutPaymentModel *)itemModel; if (index.row == i) { tempModel.isSelect = YES; self.paymentModel = tempModel; [self.bottomView setBottomPayStyle:tempModel.code]; } else { tempModel.isSelect = NO; } } } [self.TableV reloadData]; } else if ([model isKindOfClass:[ASCheckoutAddressData class]]) { ASAddressListViewController *vc = [ASAddressListViewController new]; vc.isSelMode = true; vc.sel_Id = self.addressCellM.addressModel.Id; @weakify(self) vc.selectAddressBlock = ^(ASAddressModel * _Nonnull addressM) { @strongify(self) self.addressCellM.addressModel = addressM; [self requestAddShipAddress]; }; [self.navigationController pushViewController:vc animated:true]; } } #pragma mark - **************** reqNet **************** //获取支付方式 - (void)requestPaymentMode { [self.VM ry_requestGetApi:Chectout_getPaymentMode param:@{}]; } //添加邮寄地址 - (void)requestAddShipAddress { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestPostApi:Chectout_addShipAddress_id param:@{@"addressId":self.addressCellM.addressModel.Id}]; } //根据邮寄地址获取邮寄方式 - (void)requestGetShipMethodByAddress { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestGetApi:Chectout_getShipMethod param:@{}]; } //设置邮寄方式 - (void)requestSetShipMethodByAddress { NSDictionary *addressDic1 = (NSDictionary *)[self.addressCellM.addressModel.region mj_JSONObject]; NSDictionary *addressDic = (NSDictionary *)[self.addressCellM.addressModel mj_JSONObject]; NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:addressDic]; [param addEntriesFromDictionary:addressDic1]; [param removeObjectForKey:@"extension_attributes"];//移除格式不正确的无用参数 NSDictionary *params = @{@"shipping_address":param, @"shipping_method_code":self.shipmethodCellM.shipMethodModel.method_code, @"shipping_carrier_code":self.shipmethodCellM.shipMethodModel.carrier_code}; [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestPostApi:Chectout_setShipMethod param:@{@"addressInformation":params}]; } //获取购物车信息(地址信息) - (void)requestCartInfo { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestGetApi:Cart_cartInfo param:@{}]; } ///获取购物车信息(报价整合) -(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)requestUseLimitPoint:(NSString *)point { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestPutApi:Chectout_PUT_usePoints param:point]; } //取消使用积分 - (void)requestCancelUsePoint { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; [self.VM ry_requestDeleteApi:Chectout_DEL_cancelusePoints paramStr:@""]; } //下单 -(void)requestSureOrder{ [MBProgressHUD showHUDAddedTo:self.view animated:YES]; NSDictionary *param = @{@"method":self.paymentModel.code}; [ASNetTools.shared putWithPath:Chectout_PUT_sureOrder param:@{@"paymentMethod":param} success:^(id _Nonnull json) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSLog(@"=======%@", json); } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSLog(@"=======%@=====%@", code, msg); }]; } #pragma mark --- 懒加载 --- - (void)ucm_bindvmmodel{ self.VM = [[CartVM alloc] initDelegate:self]; } - (MyCartCouponCellData *)couponCellM { if (!_couponCellM) { _couponCellM = [[MyCartCouponCellData alloc] init]; } return _couponCellM; } - (MyCartGrandTotalCellData *)totalCellM { if (!_totalCellM) { _totalCellM = [[MyCartGrandTotalCellData alloc] init]; } return _totalCellM; } - (ASCheckoutPointData *)pointCellM { if (!_pointCellM) { _pointCellM = [[ASCheckoutPointData alloc] init]; } return _pointCellM; } - (ASCheckoutAddressData *)addressCellM { if (!_addressCellM) { _addressCellM = [[ASCheckoutAddressData alloc] init]; } return _addressCellM; } - (ASCheckoutShipMethodData *)shipmethodCellM { if (!_shipmethodCellM) { _shipmethodCellM = [[ASCheckoutShipMethodData alloc] init]; } return _shipmethodCellM; } - (ASCheckoutCommentData *)commentCellM { if (!_commentCellM) { _commentCellM = [[ASCheckoutCommentData alloc] init]; _commentCellM.commentStr = @""; } return _commentCellM; } - (ASCheckoutTopView *)topView { if (!_topView) { // IPhoneXHeigh _topView = [[ASCheckoutTopView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 60)]; @weakify(self) _topView.isFlodBlock = ^(BOOL isFlod) { @strongify(self) self.TableV.isFlod = isFlod; [self.TableV reloadData]; }; } return _topView; } - (ASCheckoutBottomView *)bottomView { if (!_bottomView) { _bottomView = [[ASCheckoutBottomView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)]; @weakify(self) _bottomView.bottomBlock = ^{ @strongify(self) // [self.view makeToast:@"调起支付"]; [self requestSureOrder]; }; } return _bottomView; } #pragma mark - **************** reqNet **************** @end