Cart_CheckoutC.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // Cart_CheckoutC.m
  3. // Asteria
  4. //
  5. // Created by 王猛 on 2024/2/16.
  6. //
  7. #import "Cart_CheckoutC.h"
  8. #import "ASAddressListViewController.h"
  9. #import "ASCheckoutTopView.h"
  10. #import "CartCheckTableV.h"
  11. #import "CartVM.h"
  12. #import "ASUserModel.h"
  13. @interface Cart_CheckoutC ()<RY_baseVMprotocol>
  14. @property (nonatomic, strong) CartVM *VM;
  15. @property (nonatomic, strong) ASCheckoutTopView *topView;
  16. @property (nonatomic, strong) CartCheckTableV *TableV;
  17. @property (nonatomic, strong) MyCartCouponCellData *couponCellM;
  18. @property (nonatomic, strong) MyCartGrandTotalCellData *totalCellM;
  19. @property (nonatomic, strong) ASCheckoutPointData *pointCellM;
  20. @property (nonatomic, strong) ASCheckoutAddressData *addressCellM;
  21. //存放支付方式
  22. @property (nonatomic, strong) NSArray *paymentArray;
  23. @end
  24. @implementation Cart_CheckoutC
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.title = @"Checkout";
  28. //获取支付方式
  29. [self requestPaymentMode];
  30. //获取运输方式
  31. [self requestGetShipMethodByAddress];
  32. //获取购物车信息(地址信息)
  33. [self requestCartInfo];
  34. }
  35. - (void)initSubviews {
  36. [super initSubviews];
  37. //顶部
  38. [self.view addSubview:self.topView];
  39. IPhoneXHeigh
  40. [self setupTableV:[CartCheckTableV class] Frame:CGRectMake(0, CGRectGetMaxY(self.topView.frame) , KScreenWidth, KScreenHeight-securityBottom_H - 65 - CGRectGetMaxY(self.topView.frame))];
  41. self.TableV.backgroundColor = [UIColor colorWithHexString:@"#F8F8F8"];
  42. @weakify(self)
  43. self.TableV.tapClose = ^(NSInteger num, id data) {
  44. //num = 0 删除商品 ,1 修改数量 countV
  45. @strongify(self)
  46. if([data isKindOfClass:[CartTotalsItemsM class]]){
  47. switch (num) {
  48. case 0:
  49. [self reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)data];
  50. break;
  51. case 1:
  52. [self reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)data];
  53. break;
  54. default:
  55. break;
  56. }
  57. }else if ([data isKindOfClass:[MyCartCouponCellData class]]){
  58. MyCartCouponCellData *couponM = (MyCartCouponCellData *)data;
  59. if(num ==0){ //使用优惠券
  60. [self reqNet_PUT_cartsMineCoupons:couponM.coupon_code];
  61. }else if(num == 1){//删除优惠券
  62. [self reqNet_DEL_cartsMineCoupons];
  63. }
  64. }
  65. };
  66. // [self updateData];
  67. }
  68. - (void)ry_respnsData:(id)data
  69. parseAry:(NSMutableArray *)arry
  70. sucess:(BOOL)sucessOrFail
  71. mark:(NSString *)mark
  72. reqNetType:(ReqNetType)reqNetType{
  73. [MBProgressHUD hideHUDForView:self.view animated:YES];
  74. if([mark isEqualToString:Cart_cartsMineTotals]){
  75. if(sucessOrFail){
  76. CartTotalsM *model = [arry firstObject];
  77. self.totalsM = model;
  78. [self updateData];
  79. }
  80. } else if ([mark isEqualToString:Chectout_getPaymentMode]) {
  81. if(sucessOrFail){
  82. // 更新支付方式
  83. self.paymentArray = arry;
  84. //首次进入展示购物车数据UI
  85. [self updateData];
  86. }
  87. } else if ([mark isEqualToString:Cart_cartInfo]) {
  88. if(sucessOrFail){
  89. // 获取上次下单地址
  90. NSDictionary *dic = (NSDictionary *)data;
  91. NSArray *array = [[dic objectForKey:@"extension_attributes"] objectForKey:@"shipping_assignments"];
  92. if (array.count > 0) {
  93. NSDictionary *shipping_assignments = array[0];
  94. NSDictionary *shippingDic = [shipping_assignments objectForKey:@"shipping"];
  95. NSDictionary *addressDic = [shippingDic objectForKey:@"address"];
  96. // ASAddressModel *addressModel = [ASAddressModel defualtData];
  97. ASAddressModel *addressModel = [ASAddressModel mj_objectWithKeyValues:addressDic];
  98. if (NIL(addressModel.postcode)) {
  99. self.addressCellM.addressInfoStr = @"";
  100. self.addressCellM.shipmethodStr = @"";
  101. } else {
  102. self.addressCellM.addressInfoStr = [NSString stringWithFormat:@"%@ %@ %@ %@,%@ tel:%@", addressModel.firstname, addressModel.lastname, [addressModel.street componentsJoinedByString:@" "], addressModel.city, addressModel.postcode, addressModel.telephone];
  103. self.addressCellM.shipmethodStr = [shippingDic objectForKey:@"method"];
  104. self.addressCellM.addressId = [addressDic objectForKey:@"customer_address_id"];
  105. }
  106. }
  107. //首次进入展示购物车数据UI
  108. [self updateData];
  109. }
  110. } else if ([mark isEqualToString:Chectout_addShipAddress_id]) {
  111. if(sucessOrFail){
  112. NSArray *shipMethodArr = (NSArray *)data;
  113. if (shipMethodArr.count > 0) {
  114. NSDictionary *shipMethodDic = shipMethodArr[0];
  115. self.addressCellM.shipmethodStr = [NSString stringWithFormat:@"%@ %@", shipMethodDic[@"method_title"], shipMethodDic[@"carrier_title"]];
  116. self.addressCellM.shipmethodPrice = shipMethodDic[@"amount"];
  117. self.addressCellM.shipmethodSymbol = self.totalsM.currency_symbol;
  118. }
  119. }
  120. [self updateData];
  121. } else if ([mark isEqualToString:Chectout_getShipMethod]) {
  122. if(sucessOrFail){
  123. NSArray *shipMethodArr = (NSArray *)data;
  124. if (shipMethodArr.count > 0) {
  125. NSDictionary *shipMethodDic = shipMethodArr[0];
  126. self.addressCellM.shipmethodStr = [NSString stringWithFormat:@"%@ %@", shipMethodDic[@"method_title"], shipMethodDic[@"carrier_title"]];
  127. self.addressCellM.shipmethodPrice = shipMethodDic[@"amount"];
  128. self.addressCellM.shipmethodSymbol = self.totalsM.currency_symbol;
  129. }
  130. }
  131. [self updateData];
  132. } else if ([mark isEqualToString:Cart_cartsMineItems_Post]
  133. || [mark isEqualToString:DEL_cartsMineItems]
  134. || [mark isEqualToString:PUT_cartsMineCoupons]){
  135. if(sucessOrFail){
  136. [self reqNet_Cart_cartsMineTotals];
  137. }else{
  138. [self.view makeToast:(NSString *)data duration:2 position:CSToastPositionCenter];
  139. }
  140. }
  141. }
  142. - (void)updateData{
  143. [self.topView setTopViewData:self.totalsM.items_qty price:[NSString stringWithFormat:@"%@%@",self.totalsM.currency_symbol,self.totalsM.subtotal]];
  144. [self.TableV.infodata removeAllObjects];
  145. self.TableV.infodata = [NSMutableArray arrayWithArray:self.totalsM.items];
  146. self.totalCellM.total_segments = [NSMutableArray arrayWithArray:self.totalsM.total_segments];
  147. self.totalCellM.currency_symbol = self.totalsM.currency_symbol;
  148. [self.TableV.infodata addObject:self.totalCellM];
  149. self.couponCellM.couponAry =[NSMutableArray arrayWithArray: [ASNetTools shared].xxx_couponAry];
  150. self.couponCellM.coupon_code = self.totalsM.coupon_code;
  151. self.couponCellM.discount_amount = self.totalsM.discount_amount;
  152. self.couponCellM.currency_symbol = self.totalsM.currency_symbol;
  153. [self.TableV.infodata addObject:self.couponCellM];
  154. self.pointCellM.pointCountInput = @"20";
  155. self.pointCellM.pointBalance = @"10";
  156. [self.TableV.infodata addObject:self.pointCellM];
  157. //邮寄地址--邮寄方式
  158. [self.TableV.infodata addObject:self.addressCellM];
  159. // MyCartPayTypeCellData *data = [[MyCartPayTypeCellData alloc]init];
  160. // data.final_prices_f = [self.totalsM.subtotal floatValue];
  161. // data.currency_symbol = self.totalsM.currency_symbol;
  162. // [self.TableV.infodata addObject:data];
  163. //支付方式数据
  164. [self.TableV.infodata addObjectsFromArray:self.paymentArray];
  165. [self.TableV reloadData];
  166. }
  167. - (void)tapcellTriggereventIndex:(NSIndexPath *)index model:(id)model{
  168. if([model isKindOfClass:[CartTotalsItemsM class]]){
  169. CartTotalsItemsM *itemM = (CartTotalsItemsM *)model;
  170. if(itemM.isGift){
  171. return;
  172. }
  173. UIViewController *viewController = [[CTMediator sharedInstance] Goods_GoodsDetailsC:@{@"entity_id":itemM.product_id}];
  174. [self.navigationController pushViewController:viewController animated:YES];
  175. } else if ([model isKindOfClass:[ASCheckoutPaymentModel class]]) {
  176. ASCheckoutPaymentModel *paymentModel = (ASCheckoutPaymentModel *)model;
  177. if (paymentModel.isSelect) {
  178. return;
  179. }
  180. for (int i = 0; i < self.TableV.infodata.count; i++) {
  181. id itemModel = self.TableV.infodata[i];
  182. if ([itemModel isKindOfClass:[ASCheckoutPaymentModel class]]) {
  183. ASCheckoutPaymentModel *tempModel = (ASCheckoutPaymentModel *)itemModel;
  184. if (index.row == i) {
  185. tempModel.isSelect = YES;
  186. } else {
  187. tempModel.isSelect = NO;
  188. }
  189. }
  190. }
  191. [self.TableV reloadData];
  192. } else if ([model isKindOfClass:[ASCheckoutAddressData class]]) {
  193. ASAddressListViewController *vc = [ASAddressListViewController new];
  194. vc.isSelMode = true;
  195. vc.sel_Id = self.addressCellM.addressId;
  196. @weakify(self)
  197. vc.selectAddressBlock = ^(ASAddressModel * _Nonnull addressM) {
  198. @strongify(self)
  199. self.addressCellM.addressInfoStr = [NSString stringWithFormat:@"%@ %@ %@ %@,%@ tel:%@", addressM.firstname, addressM.lastname, [addressM.street componentsJoinedByString:@" "], addressM.city, addressM.postcode, addressM.telephone];
  200. self.addressCellM.addressId = addressM.Id;
  201. [self requestAddShipAddress];
  202. };
  203. [self.navigationController pushViewController:vc animated:true];
  204. }
  205. }
  206. #pragma mark - **************** reqNet ****************
  207. //获取支付方式
  208. - (void)requestPaymentMode {
  209. [self.VM ry_requestGetApi:Chectout_getPaymentMode param:@{}];
  210. }
  211. //添加邮寄地址
  212. - (void)requestAddShipAddress {
  213. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  214. [self.VM ry_requestPostApi:Chectout_addShipAddress_id param:@{@"addressId":self.addressCellM.addressId}];
  215. }
  216. //根据邮寄地址获取邮寄方式
  217. - (void)requestGetShipMethodByAddress {
  218. [self.VM ry_requestGetApi:Chectout_getShipMethod param:@{}];
  219. }
  220. //获取购物车信息(地址信息)
  221. - (void)requestCartInfo {
  222. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  223. [self.VM ry_requestGetApi:Cart_cartInfo param:@{}];
  224. }
  225. ///获取购物车信息(报价整合)
  226. -(void)reqNet_Cart_cartsMineTotals{
  227. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  228. [self.VM ry_requestGetApi:Cart_cartsMineTotals param:@{}];
  229. }
  230. ///修改商品的数量
  231. -(void)reqnet_Cart_cartsMineItems_Post:(CartTotalsItemsM *)itemM{
  232. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  233. NSDictionary *cartItemDic = @{
  234. @"item_id":itemM.item_id,
  235. @"qty":itemM.qty,
  236. @"quote_id":self.totalsM.cart_id
  237. };
  238. NSMutableDictionary *param = [NSMutableDictionary dictionaryWithDictionary:@{
  239. @"cartItem":cartItemDic
  240. }];
  241. [self.VM ry_requestPostApi:Cart_cartsMineItems_Post param:param];
  242. }
  243. ///删除商品
  244. -(void)reqNet_DEL_cartsMineItems:(CartTotalsItemsM *)itemM{
  245. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  246. [self.VM ry_requestDeleteApi:DEL_cartsMineItems paramStr:itemM.item_id];
  247. }
  248. -(void)reqNet_PUT_cartsMineCoupons:(NSString *)coupon{
  249. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  250. [self.VM ry_requestPutApi:PUT_cartsMineCoupons param:coupon];
  251. }
  252. -(void)reqNet_DEL_cartsMineCoupons{
  253. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  254. [self.VM ry_requestDeleteApi:DEL_cartsMineCoupons paramStr:@""];
  255. }
  256. #pragma mark --- 懒加载 ---
  257. - (void)ucm_bindvmmodel{
  258. self.VM = [[CartVM alloc] initDelegate:self];
  259. }
  260. - (MyCartCouponCellData *)couponCellM {
  261. if (!_couponCellM) {
  262. _couponCellM = [[MyCartCouponCellData alloc] init];
  263. }
  264. return _couponCellM;
  265. }
  266. - (MyCartGrandTotalCellData *)totalCellM {
  267. if (!_totalCellM) {
  268. _totalCellM = [[MyCartGrandTotalCellData alloc] init];
  269. }
  270. return _totalCellM;
  271. }
  272. - (ASCheckoutPointData *)pointCellM {
  273. if (!_pointCellM) {
  274. _pointCellM = [[ASCheckoutPointData alloc] init];
  275. }
  276. return _pointCellM;
  277. }
  278. - (ASCheckoutAddressData *)addressCellM {
  279. if (!_addressCellM) {
  280. _addressCellM = [[ASCheckoutAddressData alloc] init];
  281. _addressCellM.addressInfoStr = @"";
  282. }
  283. return _addressCellM;
  284. }
  285. - (ASCheckoutTopView *)topView {
  286. if (!_topView) {
  287. IPhoneXHeigh
  288. _topView = [[ASCheckoutTopView alloc] initWithFrame:CGRectMake(0, securitytop_Y, KScreenWidth, 60)];
  289. @weakify(self)
  290. _topView.isFlodBlock = ^(BOOL isFlod) {
  291. @strongify(self)
  292. self.TableV.isFlod = isFlod;
  293. [self.TableV reloadData];
  294. };
  295. //
  296. }
  297. return _topView;
  298. }
  299. #pragma mark - **************** reqNet ****************
  300. @end