Cart_MyCartC.m 17 KB

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