ASProductListViewModel.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 *,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[@"currencyCode"] = ASCurrencyManager.shared.currentCur;
  47. para[@"page"] = @(page);
  48. para[@"size"] = @20;
  49. if (orderBy != 0) {
  50. NSArray *arr = @[@"position", @"price", @"sold_qty"];
  51. if (arr.count > orderBy && orderBy >= 0) {
  52. para[@"order"] = arr[orderBy];
  53. } else {
  54. para[@"order"] = @"sold_qty";
  55. }
  56. } else {
  57. para[@"order"] = @"sold_qty";
  58. }
  59. if (dir != nil && ![dir isEqualToString:@""]) {
  60. para[@"dir"] = dir;
  61. } else {
  62. para[@"dir"] = @"DESC";
  63. }
  64. NSString *url = [NSString stringWithFormat:getProductListUrl, catId];
  65. [ASNetTools.shared getWithPath:url param:para success:^(id _Nonnull json) {
  66. ASProductListCategoryModel *m = [ASProductListCategoryModel mj_objectWithKeyValues:json[@"category"]];
  67. self.cateModel = m;
  68. NSArray *arr = json[@"product"];
  69. NSMutableArray *products = [ASProductBaseModel mj_objectArrayWithKeyValuesArray:arr];
  70. if (products.count > 0) {
  71. if (page == 1) {
  72. self.productList = products;
  73. } else {
  74. self.productList = [self.productList arrayByAddingObjectsFromArray:products];
  75. }
  76. if (products.count < 20) {
  77. com(false, @"");
  78. } else {
  79. com(true, @"");
  80. }
  81. } else {
  82. com(false, @"");
  83. }
  84. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  85. com(false, msg);
  86. }];
  87. }
  88. - (void)getPoint:(NSString *)pointType com:(void(^)(BOOL isSuc, NSString *msg))com {
  89. [ASNetTools.shared postWithPath:postTakeExtraPoints param:@{@"type": pointType} success:^(id _Nonnull json) {
  90. com(true, @"");
  91. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  92. com(false,msg);
  93. }];
  94. }
  95. -(void)getTopLinkList:(btnClickBlock)success {
  96. __weak typeof(self) weakSelf = self;
  97. [ASNetTools.shared getWithPath:getHotList param:@{@"type":@"banner"} success:^(id _Nonnull json) {
  98. NSLog(@"------url:%@---json:%@------", getHotList, json);
  99. NSArray *arr = [KWSearchHotKeyModel mj_objectArrayWithKeyValuesArray:json];
  100. weakSelf.topLinks = arr;
  101. success();
  102. } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
  103. NSLog(@"------url:%@---code:%@---msg:%@---", getHotList, code, msg);
  104. weakSelf.topLinks = @[];
  105. success();
  106. }];
  107. }
  108. @end