ASVipCouponsViewModel.m 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // ASVipCouponsViewModel.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/12/9.
  6. //
  7. #import "ASVipCouponsViewModel.h"
  8. @implementation ASVipCouponsViewModel
  9. - (void)postGetCoupon:(NSString *)ID com:(void(^)(BOOL isSuccess, NSString *msg))com {
  10. [ASNetTools.shared postWithPath:postAddVipCoupon param:@{@"id":ID} success:^(id _Nonnull json) {
  11. com(true, @"");
  12. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  13. com(false, msg);
  14. }];
  15. }
  16. - (void)getVipCouponList:(NSString *)v com:(void(^)(BOOL isSuccess, NSString *msg))com {
  17. [ASNetTools.shared getWithPath:getVipCouponUrl param:@{@"v":v} success:^(id _Nonnull json) {
  18. NSDictionary<NSString*, NSDictionary*> *coupsData = json;
  19. if (![coupsData.allKeys containsObject:@"coupons"]) {
  20. com(false, @"DATA ERROR!");
  21. return;
  22. }
  23. NSDictionary<NSString*, NSDictionary*> *coups = [coupsData valueForKey:@"coupons"];
  24. NSMutableArray<ASVipCouponModel *> *arr = [NSMutableArray array];
  25. if (coups != nil && coups.count != 0) {
  26. NSArray *keys = coups.allKeys;
  27. NSMutableArray *sortedArr = [NSMutableArray array];
  28. for (NSString *key in keys) {
  29. if (sortedArr.count == 0) {
  30. [sortedArr addObject:key];
  31. continue;
  32. }
  33. for (int i=0; i<sortedArr.count; i++) {
  34. NSString *this = sortedArr[i];
  35. if (sortedArr.count > i+1) {
  36. NSString *nx = sortedArr[i+1];
  37. if (key.intValue >= this.intValue && key.intValue < nx.intValue) {
  38. [sortedArr insertObject:key atIndex:i+1];
  39. break;
  40. }
  41. } else {
  42. if (sortedArr.count == 1 && key.intValue <= this.intValue) {
  43. [sortedArr insertObject:key atIndex:i];
  44. } else {
  45. [sortedArr addObject:key];
  46. }
  47. break;
  48. }
  49. }
  50. }
  51. for (int i=0; i<sortedArr.count; i++) {
  52. ASVipCouponModel *coupM = [ASVipCouponModel mj_objectWithKeyValues: coups[sortedArr[i]]];
  53. if (i+1<sortedArr.count) {
  54. ASVipCouponModel *tempNext = [ASVipCouponModel mj_objectWithKeyValues: coups[sortedArr[i+1]]];
  55. }
  56. [arr addObject:coupM];
  57. }
  58. }
  59. self.couponArr = arr;
  60. com(true, @"");
  61. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  62. com(false, msg);
  63. }];
  64. }
  65. @end