Cart_MyCartC.m 17 KB

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