ASProductListViewModel.m 10 KB

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