Cart_MyCartC.m 13 KB

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