ASProductListViewModel.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. NSLog(@"------url:%@----param----%@---json:%@------", getProductListUrl, para, json);
  78. ASProductListCategoryModel *m = [ASProductListCategoryModel mj_objectWithKeyValues:json[@"category"]];
  79. self.cateModel = m;
  80. NSArray *arr = json[@"product"][@"productLists"];
  81. NSMutableArray *products = [ASProductBaseModel mj_objectArrayWithKeyValuesArray:arr];
  82. NSArray *filtArr = json[@"filter"];
  83. NSMutableArray *filters = [KWProductListFilterModel mj_objectArrayWithKeyValuesArray:filtArr];
  84. self.sourceArr = [NSMutableArray arrayWithArray:filters];
  85. self.selectDic = [NSMutableDictionary dictionary];
  86. for (KWProductListFilterModel *m in filters) {
  87. if (m.attribute_data.count > 0) {
  88. for (KWProductListFilterSubModel *sub in m.attribute_data) {
  89. if (sub.status != 0) {
  90. NSMutableArray *tempSubs = self.selectDic[m.attribute_id];
  91. if (tempSubs == nil) {
  92. tempSubs = [NSMutableArray array];
  93. }
  94. if ([m.attribute_id isEqualToString:@"price"]) {
  95. sub.inputMin = sub.minPrice;
  96. sub.inputMax = sub.maxPrice;
  97. } else {
  98. sub.inputMax = @"";
  99. sub.inputMin = @"";
  100. }
  101. [tempSubs addObject:sub.copySelf];
  102. self.selectDic[m.attribute_id] = tempSubs;
  103. }
  104. }
  105. }
  106. }
  107. if (products.count > 0) {
  108. if (page == 1) {
  109. self.productList = products;
  110. } else {
  111. self.productList = [self.productList arrayByAddingObjectsFromArray:products];
  112. }
  113. if (products.count < 6) {
  114. com(false, @"");
  115. } else {
  116. com(true, @"");
  117. }
  118. } else {
  119. com(false, @"");
  120. }
  121. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  122. com(false, msg);
  123. }];
  124. }
  125. - (void)getSearchResult:(NSString *)keyWord page:(NSInteger)page orderBy:(NSInteger)orderBy dir:(NSString *)dir com:(void(^)(BOOL hasNext, NSString *msg))com {
  126. NSMutableDictionary *para = [NSMutableDictionary dictionary];
  127. para[@"currency_code"] = ASCurrencyManager.shared.currentCur;
  128. para[@"ajax_app"] = @1;
  129. para[@"q"] = keyWord;
  130. para[@"p"] = @(page);
  131. if (orderBy != 0) {
  132. NSArray *arr = @[@"",@"price_l",@"price_h",@"popularity"];
  133. if (arr.count > orderBy && orderBy >= 0) {
  134. para[@"product_list_order"] = arr[orderBy];
  135. } else {
  136. para[@"product_list_order"] = @"";
  137. }
  138. } else {
  139. para[@"product_list_order"] = @"";
  140. }
  141. NSArray *filterKeys = self.selectDic.allKeys;
  142. for (NSString *key in filterKeys) {
  143. NSArray *subMs = self.selectDic[key];
  144. NSMutableString *valueStr = [NSMutableString string];
  145. for (KWProductListFilterSubModel *subM in subMs) {
  146. if ([key isEqualToString:@"price"]) {
  147. // if (subM.inputMax.length > 0 && subM.inputMin.length > 0) {
  148. // valueStr = [NSMutableString stringWithFormat:@"%@-%@", subM.inputMin, subM.inputMax];
  149. // para[key] = valueStr;
  150. // }
  151. continue;
  152. }
  153. [valueStr appendString:[NSString stringWithFormat:@"%@,",subM.Id]];
  154. para[key] = valueStr;
  155. }
  156. }
  157. [ASNetTools.shared getWithPath:SearchProduct param:para success:^(id _Nonnull json) {
  158. ASProductListCategoryModel *m = [ASProductListCategoryModel mj_objectWithKeyValues:json[@"category"]];
  159. self.cateModel = m;
  160. NSArray *arr = json[@"product"][@"productLists"];
  161. NSMutableArray *products = [ASProductBaseModel mj_objectArrayWithKeyValuesArray:arr];
  162. NSArray *filtArr = json[@"filter"];
  163. NSMutableArray *filters = [KWProductListFilterModel mj_objectArrayWithKeyValuesArray:filtArr];
  164. self.sourceArr = [NSMutableArray arrayWithArray:filters];
  165. self.selectDic = [NSMutableDictionary dictionary];
  166. for (KWProductListFilterModel *m in filters) {
  167. if (m.attribute_data.count > 0) {
  168. for (KWProductListFilterSubModel *sub in m.attribute_data) {
  169. if (sub.status != 0) {
  170. NSMutableArray *tempSubs = self.selectDic[m.attribute_id];
  171. if (tempSubs == nil) {
  172. tempSubs = [NSMutableArray array];
  173. }
  174. if ([m.attribute_id isEqualToString:@"price"]) {
  175. // sub.inputMin = sub.minPrice;
  176. // sub.inputMax = sub.maxPrice;
  177. continue;
  178. } else {
  179. sub.inputMax = @"";
  180. sub.inputMin = @"";
  181. }
  182. [tempSubs addObject:sub.copySelf];
  183. self.selectDic[m.attribute_id] = tempSubs;
  184. }
  185. }
  186. }
  187. }
  188. if (products.count > 0) {
  189. if (page == 1) {
  190. self.productList = products;
  191. } else {
  192. self.productList = [self.productList arrayByAddingObjectsFromArray:products];
  193. }
  194. if (products.count < 6) {
  195. com(false, @"");
  196. } else {
  197. com(true, @"");
  198. }
  199. } else {
  200. com(false, @"");
  201. }
  202. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  203. com(false, msg);
  204. }];
  205. }
  206. - (void)getPoint:(NSString *)pointType com:(void(^)(BOOL isSuc, NSString *msg))com {
  207. [ASNetTools.shared postWithPath:postTakeExtraPoints param:@{@"type": pointType} success:^(id _Nonnull json) {
  208. com(true, @"");
  209. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  210. com(false,msg);
  211. }];
  212. }
  213. -(void)getTopLinkList:(btnClickBlock)success {
  214. __weak typeof(self) weakSelf = self;
  215. [ASNetTools.shared getWithPath:getHotList param:@{@"type":@"banner"} success:^(id _Nonnull json) {
  216. NSLog(@"------url:%@---json:%@------", getHotList, json);
  217. NSArray *arr = [KWSearchHotKeyModel mj_objectArrayWithKeyValuesArray:json];
  218. weakSelf.topLinks = arr;
  219. success();
  220. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  221. NSLog(@"------url:%@---code:%@---msg:%@---", getHotList, code, msg);
  222. weakSelf.topLinks = @[];
  223. success();
  224. }];
  225. }
  226. @end