ASProductListViewModel.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // ASProductListViewModel.m
  3. // Asteria
  4. //
  5. // Created by iOS on 2023/6/12.
  6. //
  7. #import "ASProductListViewModel.h"
  8. @implementation ASProductListViewModel
  9. - (NSArray<ASProductBaseModel *> *)productList {
  10. if (!_productList) {
  11. NSMutableArray *arr = [NSMutableArray array];
  12. _productList = arr;
  13. }
  14. return _productList;
  15. }
  16. - (NSMutableDictionary<NSString *,NSMutableArray<KWProductListFilterSubModel *> *> *)selectDic {
  17. if (!_selectDic) {
  18. NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
  19. _selectDic = tempDic;
  20. }
  21. return _selectDic;
  22. }
  23. //-(NSMutableArray<KWProductListFilterModel *> *)showArr {
  24. // NSMutableArray *arr = [NSMutableArray array];
  25. //// [arr addObjectsFromArray:self.selectArr];
  26. // [arr addObjectsFromArray:self.sourceArr];
  27. // return arr;
  28. //}
  29. - (NSArray<KWProductListFilterModel *> *)sourceArr {
  30. if (!_sourceArr) {
  31. NSMutableArray *arr = [NSMutableArray array];
  32. // for (int i=0; i<(arc4random()%10+1);i++) {
  33. // [arr addObject:[KWProductListFilterModel demoData]];
  34. // }
  35. // KWProductListFilterModel *priceM = [KWProductListFilterModel new];
  36. // priceM.attribute_id = @"-100";
  37. // priceM.attribute_name = @"PRICE RANGE";
  38. // priceM.attribute_data = [NSMutableArray arrayWithObject:[KWProductListFilterSubModel demoData]];
  39. // [arr addObject:priceM];
  40. _sourceArr = arr;
  41. }
  42. return _sourceArr;
  43. }
  44. - (void)getProductListData:(NSString *)catId page:(NSInteger)page orderBy:(NSInteger)orderBy dir:(NSString *)dir com:(void(^)(BOOL hasNext, NSString *msg))com {
  45. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  46. para[@"currency_code"] = ASCurrencyManager.shared.currentCur;
  47. para[@"ajax_app"] = @1;
  48. para[@"id"] = catId;
  49. para[@"p"] = @(page);
  50. if (orderBy != 0) {
  51. NSArray *arr = @[@"",@"price_l",@"price_h",@"popularity"];
  52. if (arr.count > orderBy && orderBy >= 0) {
  53. para[@"product_list_order"] = arr[orderBy];
  54. } else {
  55. para[@"product_list_order"] = @"";
  56. }
  57. } else {
  58. para[@"product_list_order"] = @"";
  59. }
  60. NSArray *filterKeys = self.selectDic.allKeys;
  61. for (NSString *key in filterKeys) {
  62. NSArray *subMs = self.selectDic[key];
  63. NSMutableString *valueStr = [NSMutableString string];
  64. for (KWProductListFilterSubModel *subM in subMs) {
  65. if ([key isEqualToString:@"price"]) {
  66. if (subM.inputMax.length > 0 && subM.inputMin.length > 0) {
  67. valueStr = [NSMutableString stringWithFormat:@"%@-%@", subM.inputMin, subM.inputMax];
  68. para[key] = valueStr;
  69. }
  70. continue;
  71. }
  72. [valueStr appendString:[NSString stringWithFormat:@"%@,",subM.Id]];
  73. para[key] = valueStr;
  74. }
  75. }
  76. [ASNetTools.shared getWithPath:getProductListUrl param:para success:^(id _Nonnull json) {
  77. ASProductListCategoryModel *m = [ASProductListCategoryModel mj_objectWithKeyValues:json[@"category"]];
  78. self.cateModel = m;
  79. NSArray *arr = json[@"product"][@"productLists"];
  80. NSMutableArray *products = [ASProductBaseModel mj_objectArrayWithKeyValuesArray:arr];
  81. NSArray *filtArr = json[@"filter"];
  82. NSMutableArray *filters = [KWProductListFilterModel mj_objectArrayWithKeyValuesArray:filtArr];
  83. self.sourceArr = [NSMutableArray arrayWithArray:filters];
  84. self.selectDic = [NSMutableDictionary dictionary];
  85. for (KWProductListFilterModel *m in filters) {
  86. if (m.attribute_data.count > 0) {
  87. for (KWProductListFilterSubModel *sub in m.attribute_data) {
  88. if (sub.status != 0) {
  89. NSMutableArray *tempSubs = self.selectDic[m.attribute_id];
  90. if (tempSubs == nil) {
  91. tempSubs = [NSMutableArray array];
  92. }
  93. if ([m.attribute_id isEqualToString:@"price"]) {
  94. sub.inputMin = sub.minPrice;
  95. sub.inputMax = sub.maxPrice;
  96. } else {
  97. sub.inputMax = @"";
  98. sub.inputMin = @"";
  99. }
  100. [tempSubs addObject:sub.copySelf];
  101. self.selectDic[m.attribute_id] = tempSubs;
  102. }
  103. }
  104. }
  105. }
  106. if (products.count > 0) {
  107. if (page == 1) {
  108. self.productList = products;
  109. } else {
  110. self.productList = [self.productList arrayByAddingObjectsFromArray:products];
  111. }
  112. if (products.count < 6) {
  113. com(false, @"");
  114. } else {
  115. com(true, @"");
  116. }
  117. } else {
  118. com(false, @"");
  119. }
  120. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  121. com(false, msg);
  122. }];
  123. }
  124. - (void)getPoint:(NSString *)pointType com:(void(^)(BOOL isSuc, NSString *msg))com {
  125. [ASNetTools.shared postWithPath:postTakeExtraPoints param:@{@"type": pointType} success:^(id _Nonnull json) {
  126. com(true, @"");
  127. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  128. com(false,msg);
  129. }];
  130. }
  131. -(void)getTopLinkList:(btnClickBlock)success {
  132. __weak typeof(self) weakSelf = self;
  133. [ASNetTools.shared getWithPath:getHotList param:@{@"type":@"banner"} success:^(id _Nonnull json) {
  134. NSLog(@"------url:%@---json:%@------", getHotList, json);
  135. NSArray *arr = [KWSearchHotKeyModel mj_objectArrayWithKeyValuesArray:json];
  136. weakSelf.topLinks = arr;
  137. success();
  138. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  139. NSLog(@"------url:%@---code:%@---msg:%@---", getHotList, code, msg);
  140. weakSelf.topLinks = @[];
  141. success();
  142. }];
  143. }
  144. @end