Cart_MyCartC.m 17 KB

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