Cart_MyCartC.m 17 KB

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