Cart_MyCartC.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // Cart_MyCartC.m
  3. // Asteria
  4. //
  5. // Created by 王猛 on 2024/1/26.
  6. //
  7. #import "Cart_MyCartC.h"
  8. #import "CartVM.h"
  9. #import "MyCarlTableV.h"
  10. #import "Cart_CheckoutC.h"
  11. #import "ASGoodsDetailsVM.h"
  12. #import "ASAddGiftGoodsView.h"
  13. #import "ASGoodsDetailsViewController.h"
  14. @interface Cart_MyCartC ()<RY_baseVMprotocol>
  15. @property (nonatomic, strong) CartVM *VM;
  16. @property (nonatomic, strong) UIView *topBgV;
  17. @property (nonatomic, strong) UILabel *top_totalLab;
  18. @property (nonatomic, strong) QMUILabel *top_priceLab;
  19. @property (nonatomic, strong) MyCarlTableV *TableV;
  20. @property (nonatomic, strong) CartTotalsM *totalsM;
  21. @property (nonatomic, strong) MyCartCouponCellData *couponCellM;
  22. @property (nonatomic, strong) MyCartGrandTotalCellData *totalCellM;
  23. @property (nonatomic, strong) UIButton *postBtn;
  24. //手动添加赠品至购物车
  25. @property (nonatomic, strong) ASGoodsDetailsVM *goodsDetailsVM;
  26. @property (nonatomic, strong) ASAddGiftGoodsView *addGiftView;
  27. @property (nonatomic, strong) NSMutableArray *addGiftParamArr;
  28. @property (nonatomic, assign) int index;
  29. @property (nonatomic, assign) int giftShowCount;
  30. @end
  31. @implementation Cart_MyCartC
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. self.title = @"My Cart";
  35. [self ucm_subVeiwsTapBlock];
  36. }
  37. -(void)viewWillAppear:(BOOL)animated{
  38. [super viewWillAppear:animated];
  39. [self reqNet_Cart_cartsMineTotals];
  40. }
  41. - (void)initSubviews {
  42. [super initSubviews];
  43. [self.view addSubview:self.topBgV];
  44. [self.topBgV addSubview:self.top_totalLab];
  45. [self.topBgV addSubview:self.top_priceLab];
  46. [self.top_totalLab mas_makeConstraints:^(MASConstraintMaker *make) {
  47. make.left.mas_offset(20);
  48. make.centerY.equalTo(self.topBgV);
  49. }];
  50. [self.top_priceLab mas_makeConstraints:^(MASConstraintMaker *make) {
  51. make.left.equalTo(self.top_totalLab.mas_right).offset(20);
  52. make.right.mas_equalTo(-20);
  53. make.centerY.equalTo(self.topBgV);
  54. }];
  55. IPhoneXHeigh
  56. [self setupTableV:[MyCarlTableV class] Frame:CGRectMake(0, CGRectGetMaxY(self.topBgV.frame), KScreenWidth, KScreenHeight-securityBottom_H - 65- CGRectGetMaxY(self.topBgV.frame)-self.postBtn.mj_h)];
  57. self.TableV.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
  58. [self.view addSubview:self.postBtn];
  59. self.postBtn.mj_y = CGRectGetMaxY(self.TableV.frame);
  60. self.addGiftParamArr = [[NSMutableArray alloc] initWithCapacity:1];
  61. self.index = 0;
  62. self.giftShowCount = 0;
  63. }
  64. - (void)ucm_subVeiwsTapBlock{
  65. @weakify(self)
  66. self.TableV.tapClose = ^(NSInteger num, id data) {
  67. //num = 0 删除商品 ,1 修改数量 countV
  68. @strongify(self)
  69. if([data isKindOfClass:[CartTotalsItemsM class]]){
  70. switch (num) {
  71. case 0:
  72. [self reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)data];
  73. break;
  74. case 1:
  75. [self reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)data];
  76. break;
  77. default:
  78. break;
  79. }
  80. } else if ([data isKindOfClass:[MyCartCouponCellData class]]){
  81. MyCartCouponCellData *couponM = (MyCartCouponCellData *)data;
  82. if(num ==0){ //使用优惠券
  83. [self reqNet_PUT_cartsMineCoupons:couponM.coupon_code];
  84. }else if(num == 1){//删除优惠券
  85. [self reqNet_DEL_cartsMineCoupons];
  86. }
  87. } else if ([data isKindOfClass:[MyCartGrandTotalCellData class]]){
  88. MyCartGrandTotalCellData *totalData = (MyCartGrandTotalCellData *)data;
  89. [self requestLostExtraFee:num];
  90. }
  91. };
  92. }
  93. - (void)ucm_bindvmmodel{
  94. self.VM = [[CartVM alloc] initDelegate:self];
  95. self.goodsDetailsVM = [[ASGoodsDetailsVM alloc] initDelegate:self];
  96. }
  97. #pragma mark - **************** reqNet ****************
  98. ///获取购物车
  99. -(void)reqNet_Cart_cartsMineTotals{
  100. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  101. [self.VM ry_requestGetApi:Cart_cartsMineTotals param:@{}];
  102. }
  103. ///修改商品的数量
  104. -(void)reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)itemM{
  105. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  106. NSDictionary *cartItemDic = @{
  107. @"item_id":itemM.item_id,
  108. @"qty":itemM.qty,
  109. @"quote_id":self.totalsM.cart_id
  110. };
  111. NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{
  112. @"cartItem":cartItemDic
  113. }];
  114. [self.VM ry_requestPostApi:Cart_cartsMineItems_Post param:param];
  115. }
  116. ///删除商品
  117. -(void)reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)itemM{
  118. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  119. [self.VM ry_requestDeleteApi:DEL_cartsMineItems paramStr:itemM.item_id];
  120. }
  121. -(void)reqNet_PUT_cartsMineCoupons:(NSString *)coupon{
  122. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  123. self.giftShowCount = 0;
  124. [self.VM ry_requestPutApi:PUT_cartsMineCoupons param:coupon];
  125. }
  126. -(void)reqNet_DEL_cartsMineCoupons{
  127. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  128. [self.VM ry_requestDeleteApi:DEL_cartsMineCoupons paramStr:@""];
  129. }
  130. //丢件险
  131. - (void)requestLostExtraFee:(BOOL)isOpen {
  132. NSDictionary *address = @{@"countryId":@"US",
  133. @"postcode":@"*"};
  134. NSDictionary *addressInformation = @{@"address":address,
  135. @"shipping_method_code":@"bestway",
  136. @"shipping_carrier_code":@"tablerate"};
  137. NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithCapacity:1];
  138. if(isOpen ==0){ //取消丢件险
  139. NSDictionary *information = @{@"fee_id":@(1), @"options_ids":@[]};
  140. [params addEntriesFromDictionary:@{@"information":information}];
  141. }else if(isOpen == 1){//打开丢件险
  142. NSDictionary *information = @{@"fee_id":@(1), @"options_ids":@[@"1"]};
  143. [params addEntriesFromDictionary:@{@"information":information}];
  144. }
  145. [params setValue:addressInformation forKey:@"addressInformation"];
  146. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  147. [self.VM ry_requestPostApi:Chectout_lost_extrafee param:params];
  148. }
  149. //请求赠品商品详情
  150. -(void)reqNet_GoodsDetails_productGetProduct:(NSString *)enterId{
  151. NSMutableDictionary * params = [[NSMutableDictionary alloc] init];
  152. // [params setObject:@"55475" forKey:@"productId"];
  153. [params setObject:enterId forKey:@"productId"];
  154. [params setObject:ASCurrencyManager.shared.currentCur forKey:@"currencyCode"];
  155. // [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  156. [self.goodsDetailsVM ry_requestGetApi:Goods_productGetProductsById param:params];
  157. }
  158. //赠品加车
  159. - (void)requestGiftAddCart {
  160. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  161. for (int i = 0; i < self.addGiftParamArr.count; i++) {
  162. NSDictionary *param = self.addGiftParamArr[i];
  163. self.index ++;
  164. [self.VM ry_formDataRequestPostApi:Chectout_giftAddCart param:param];
  165. }
  166. }
  167. - (void)ry_respnsData:(id)data
  168. parseAry:(NSMutableArray *)arry
  169. sucess:(BOOL)sucessOrFail
  170. mark:(NSString *)mark
  171. reqNetType:(ReqNetType)reqNetType{
  172. [MBProgressHUD hideHUDForView:self.view animated:YES];
  173. if([mark isEqualToString:Cart_cartsMineTotals]){
  174. if(sucessOrFail){
  175. CartTotalsM *model = [arry firstObject];
  176. self.totalsM = model;
  177. self.top_totalLab.text = [NSString stringWithFormat:@"%@ pcs | total",model.items_qty];
  178. self.top_priceLab.text =[NSString stringWithFormat:@"%@%@",model.currency_symbol,model.subtotal];
  179. [self.TableV.infodata removeAllObjects];
  180. self.TableV.infodata = [NSMutableArray arrayWithArray:model.items];
  181. self.couponCellM.couponAry =[NSMutableArray arrayWithArray: [ASNetTools shared].xxx_couponAry];
  182. self.couponCellM.coupon_code = model.coupon_code;
  183. self.couponCellM.discount_amount = model.discount_amount;
  184. self.couponCellM.currency_symbol = model.currency_symbol;
  185. [self.TableV.infodata addObject:self.couponCellM];
  186. self.totalCellM.total_segments = [NSMutableArray arrayWithArray:model.total_segments];
  187. self.totalCellM.currency_symbol = model.currency_symbol;
  188. [self.TableV.infodata addObject:self.totalCellM];
  189. MyCartPayTypeCellData *data = [[MyCartPayTypeCellData alloc]init];
  190. data.final_prices_f = [model.subtotal floatValue];
  191. data.currency_symbol = model.currency_symbol;
  192. [self.TableV.infodata addObject:data];
  193. //手动添加赠品功能
  194. [self.addGiftParamArr removeAllObjects];
  195. NSDictionary *freeData = (NSDictionary *)[[self.totalsM.extension_attributes objectForKey:@"free_data"] mj_JSONObject];
  196. BOOL is_show = [[freeData objectForKey:@"is_show"] boolValue];
  197. if (is_show && self.giftShowCount< 1) {//显示存在赠品
  198. NSArray *productArr = [freeData objectForKey:@"products"];
  199. if (productArr.count > 0) { //存在赠品
  200. for (int i = 0; i < productArr.count; i++) {
  201. NSDictionary *productDic = productArr[i];
  202. NSString *entity_id = productDic[@"entity_id"];
  203. [self reqNet_GoodsDetails_productGetProduct:entity_id];
  204. }
  205. }
  206. }
  207. [self.TableV reloadData];
  208. }
  209. }else if ([mark isEqualToString:Cart_cartsMineItems_Post]
  210. || [mark isEqualToString:DEL_cartsMineItems]
  211. || [mark isEqualToString:PUT_cartsMineCoupons]
  212. || [mark isEqualToString:Chectout_giftAddCart]
  213. || [mark isEqualToString:Chectout_lost_extrafee]){
  214. if(sucessOrFail){
  215. if ([mark isEqualToString:Chectout_giftAddCart]) {
  216. self.index --;
  217. if (self.index == 0) {
  218. [self reqNet_Cart_cartsMineTotals];
  219. }
  220. } else {
  221. [self reqNet_Cart_cartsMineTotals];
  222. }
  223. }else{
  224. [self.view makeToast:(NSString *)data duration:2 position:CSToastPositionCenter];
  225. }
  226. } else if ([mark isEqualToString:Goods_productGetProductsById]) {
  227. //请求赠品详情
  228. GoodsInformationM *model = (GoodsInformationM *)[arry firstObject];
  229. NSLog(@"optionsoptions====%@=====%@", model.Id, model.options);
  230. NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{@"product_id":model.Id, @"isPromoItems":@"yes"}];
  231. if (model.options.count > 0) {
  232. NSMutableDictionary *optionDic = [NSMutableDictionary dictionary];
  233. for (OptionsModel *optionM in model.options) {
  234. OptionsValuesM *valuesM = optionM.values[0];
  235. NSString *tempStr =[NSString stringWithFormat:@"options[%@]",optionM.option_id];
  236. optionDic[tempStr] =valuesM.option_type_id;
  237. }
  238. [param addEntriesFromDictionary:optionDic];
  239. }
  240. // param = /*@{@"product_id":model.Id, @"isPromoItems":@"1"}*/;
  241. [self.addGiftParamArr addObject:param];
  242. if (self.giftShowCount < 1 && self.addGiftParamArr.count == 1 && self.addGiftView.hidden) {
  243. self.addGiftView.hidden = NO;
  244. [self.addGiftView setGiftData:model.add_gooodsImgUrl];
  245. }
  246. }
  247. }
  248. - (void)tapcellTriggereventIndex:(NSIndexPath *)index model:(id)model{
  249. if([model isKindOfClass:[CartTotalsItemsM class]]){
  250. CartTotalsItemsM *itemM = (CartTotalsItemsM *)model;
  251. if(itemM.isGift){
  252. return;
  253. }
  254. // UIViewController *viewController = [[CTMediator sharedInstance] Goods_GoodsDetailsC:@{@"entity_id":itemM.product_id}];
  255. // [self.navigationController pushViewController:viewController animated:YES];
  256. ASGoodsDetailsViewController *goodsVC = [[ASGoodsDetailsViewController alloc] init];
  257. goodsVC.entity_id = itemM.product_id;
  258. [self.navigationController pushViewController:goodsVC animated:YES];
  259. }
  260. }
  261. -(void)handle_postEvent:(UIButton *)btn{
  262. Cart_CheckoutC *vc = [[Cart_CheckoutC alloc]init];
  263. vc.totalsM = self.totalsM;
  264. [[Current_normalTool topViewController].navigationController pushViewController:vc animated:YES];
  265. }
  266. #pragma mark - **************** lazy ****************
  267. -(UIView *)topBgV{
  268. if(!_topBgV){
  269. IPhoneXHeigh
  270. _topBgV = [[UIView alloc]initWithFrame:CGRectMake(0, securitytop_Y, KScreenWidth, 60)];
  271. _topBgV.backgroundColor = [UIColor colorWithHexString:@"#E0FFF5"];
  272. }
  273. return _topBgV;
  274. }
  275. - (UILabel *)top_totalLab {
  276. if (!_top_totalLab) {
  277. _top_totalLab = [[UILabel alloc] initWithFrame:CGRectZero];
  278. _top_totalLab.textAlignment = NSTextAlignmentLeft;
  279. _top_totalLab.font = [UIFont fontWithName:Rob_Regular size:14];
  280. _top_totalLab.textColor = [UIColor colorWithHexString:@"#000000"];
  281. }
  282. return _top_totalLab;
  283. }
  284. - (QMUILabel *)top_priceLab {
  285. if (!_top_priceLab) {
  286. _top_priceLab = [[QMUILabel alloc] initWithFrame:CGRectZero];
  287. _top_priceLab.textAlignment = NSTextAlignmentLeft;
  288. _top_priceLab.font = [UIFont fontWithName:Rob_Bold size:16];
  289. }
  290. return _top_priceLab;
  291. }
  292. - (MyCartCouponCellData *)couponCellM {
  293. if (!_couponCellM) {
  294. _couponCellM = [[MyCartCouponCellData alloc] init];
  295. }
  296. return _couponCellM;
  297. }
  298. - (MyCartGrandTotalCellData *)totalCellM {
  299. if (!_totalCellM) {
  300. _totalCellM = [[MyCartGrandTotalCellData alloc] init];
  301. }
  302. return _totalCellM;
  303. }
  304. - (UIButton *)postBtn {
  305. if (!_postBtn) {
  306. _postBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  307. _postBtn.layer.cornerRadius = 4;
  308. _postBtn.clipsToBounds = YES;
  309. IPhoneXHeigh
  310. _postBtn.frame = CGRectMake(10, 0, KScreenWidth-20, 45);
  311. _postBtn.backgroundColor = [UIColor colorWithHexString:@"#000000"];
  312. [_postBtn setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
  313. _postBtn.titleLabel.font = [UIFont fontWithName:Rob_Regular size:16];
  314. [_postBtn setTitle:@"POST COMMENT" forState:UIControlStateNormal];
  315. [_postBtn addTarget:self action:@selector(handle_postEvent:) forControlEvents:UIControlEventTouchUpInside];
  316. }
  317. return _postBtn;
  318. }
  319. - (ASAddGiftGoodsView *)addGiftView {
  320. if (!_addGiftView) {
  321. _addGiftView = [[ASAddGiftGoodsView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
  322. @weakify(self)
  323. _addGiftView.addBlock = ^(int type) {
  324. @strongify(self)
  325. self.addGiftView.hidden = YES;
  326. if (type == 1) {
  327. [self requestGiftAddCart];
  328. } else {
  329. self.giftShowCount ++;
  330. }
  331. };
  332. _addGiftView.hidden = YES;
  333. [[UIApplication sharedApplication].keyWindow addSubview:_addGiftView];
  334. }
  335. return _addGiftView;
  336. }
  337. @end