Cart_CheckoutC.m 16 KB

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