ASProductListViewModel.m 9.5 KB

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