Cart_CheckoutC.m 19 KB

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