123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- //
- // ASProductListViewModel.m
- // Asteria
- //
- // Created by iOS on 2023/6/12.
- //
- #import "ASProductListViewModel.h"
- @implementation ASProductListViewModel
- - (NSArray<ASProductBaseModel *> *)productList {
- if (!_productList) {
- NSMutableArray *arr = [NSMutableArray array];
- _productList = arr;
- }
- return _productList;
- }
- - (NSMutableDictionary<NSString *,NSMutableArray<KWProductListFilterSubModel *> *> *)selectDic {
- if (!_selectDic) {
- NSMutableDictionary *tempDic = [NSMutableDictionary dictionary];
- _selectDic = tempDic;
- }
- return _selectDic;
- }
- //-(NSMutableArray<KWProductListFilterModel *> *)showArr {
- // NSMutableArray *arr = [NSMutableArray array];
- //// [arr addObjectsFromArray:self.selectArr];
- // [arr addObjectsFromArray:self.sourceArr];
- // return arr;
- //}
- - (NSArray<KWProductListFilterModel *> *)sourceArr {
- if (!_sourceArr) {
- NSMutableArray *arr = [NSMutableArray array];
- // for (int i=0; i<(arc4random()%10+1);i++) {
- // [arr addObject:[KWProductListFilterModel demoData]];
- // }
- // KWProductListFilterModel *priceM = [KWProductListFilterModel new];
- // priceM.attribute_id = @"-100";
- // priceM.attribute_name = @"PRICE RANGE";
- // priceM.attribute_data = [NSMutableArray arrayWithObject:[KWProductListFilterSubModel demoData]];
- // [arr addObject:priceM];
- _sourceArr = arr;
- }
- return _sourceArr;
- }
- - (void)getProductListData:(NSString *)catId page:(NSInteger)page orderBy:(NSInteger)orderBy dir:(NSString *)dir com:(void(^)(BOOL hasNext, NSString *msg))com {
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- para[@"currency_code"] = ASCurrencyManager.shared.currentCur;
- para[@"ajax_app"] = @1;
- para[@"id"] = catId;
- para[@"p"] = @(page);
- if (orderBy != 0) {
- NSArray *arr = @[@"",@"price_l",@"price_h",@"popularity"];
- if (arr.count > orderBy && orderBy >= 0) {
- para[@"product_list_order"] = arr[orderBy];
- } else {
- para[@"product_list_order"] = @"";
- }
- } else {
- para[@"product_list_order"] = @"";
- }
- NSArray *filterKeys = self.selectDic.allKeys;
- for (NSString *key in filterKeys) {
- NSArray *subMs = self.selectDic[key];
- NSMutableString *valueStr = [NSMutableString string];
- for (KWProductListFilterSubModel *subM in subMs) {
- if ([key isEqualToString:@"price"]) {
- if (subM.inputMax.length > 0 && subM.inputMin.length > 0) {
-
- //搜索价格根据当前币种汇率转换成美元
- float rateCount = [[[ASCurrencyManager shared] getCurrentCurrencyRate] floatValue];
- NSString *minStr = [NSString stringWithFormat:@"%lf", [subM.inputMin floatValue] / rateCount];
- NSString *maxStr = [NSString stringWithFormat:@"%lf", [subM.inputMax floatValue] / rateCount];
- valueStr = [NSMutableString stringWithFormat:@"%@-%@", minStr, maxStr];
- para[key] = valueStr;
- }
- continue;
- }
- [valueStr appendString:[NSString stringWithFormat:@"%@,",subM.Id]];
- para[key] = valueStr;
- }
- }
-
- [ASNetTools.shared getWithPath:getProductListUrl param:para success:^(id _Nonnull json) {
-
- NSLog(@"------url:%@----param----%@---json:%@------", getProductListUrl, para, json);
-
- ASProductListCategoryModel *m = [ASProductListCategoryModel mj_objectWithKeyValues:json[@"category"]];
- self.cateModel = m;
- NSArray *arr = json[@"product"][@"productLists"];
- NSMutableArray *products = [ASProductBaseModel mj_objectArrayWithKeyValuesArray:arr];
-
- NSArray *filtArr = json[@"filter"];
- NSMutableArray *filters = [KWProductListFilterModel mj_objectArrayWithKeyValuesArray:filtArr];
- self.sourceArr = [NSMutableArray arrayWithArray:filters];
- self.selectDic = [NSMutableDictionary dictionary];
- for (KWProductListFilterModel *m in filters) {
- if (m.attribute_data.count > 0) {
- for (KWProductListFilterSubModel *sub in m.attribute_data) {
- if (sub.status != 0) {
- NSMutableArray *tempSubs = self.selectDic[m.attribute_id];
- if (tempSubs == nil) {
- tempSubs = [NSMutableArray array];
- }
- if ([m.attribute_id isEqualToString:@"price"]) {
- sub.inputMin = sub.minPrice;
- sub.inputMax = sub.maxPrice;
- } else {
- sub.inputMax = @"";
- sub.inputMin = @"";
- }
- [tempSubs addObject:sub.copySelf];
- self.selectDic[m.attribute_id] = tempSubs;
- }
- }
- }
- }
-
-
- if (products.count > 0) {
- if (page == 1) {
- self.productList = products;
- } else {
- self.productList = [self.productList arrayByAddingObjectsFromArray:products];
- }
- if (products.count < 6) {
- com(false, @"");
- } else {
- com(true, @"");
- }
- } else {
- self.productList = @[];
- com(false, @"");
- }
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- com(false, msg);
- }];
-
-
- }
- - (void)getSearchResult:(NSString *)keyWord page:(NSInteger)page orderBy:(NSInteger)orderBy dir:(NSString *)dir com:(void(^)(BOOL hasNext, NSString *msg))com {
- NSMutableDictionary *para = [NSMutableDictionary dictionary];
- para[@"currency_code"] = ASCurrencyManager.shared.currentCur;
- para[@"ajax_app"] = @1;
- para[@"q"] = keyWord;
- para[@"p"] = @(page);
- if (orderBy != 0) {
- NSArray *arr = @[@"",@"price_l",@"price_h",@"popularity"];
- if (arr.count > orderBy && orderBy >= 0) {
- para[@"product_list_order"] = arr[orderBy];
- } else {
- para[@"product_list_order"] = @"";
- }
- } else {
- para[@"product_list_order"] = @"";
- }
- NSArray *filterKeys = self.selectDic.allKeys;
- for (NSString *key in filterKeys) {
- NSArray *subMs = self.selectDic[key];
- NSMutableString *valueStr = [NSMutableString string];
- for (KWProductListFilterSubModel *subM in subMs) {
- if ([key isEqualToString:@"price"]) {
- // if (subM.inputMax.length > 0 && subM.inputMin.length > 0) {
- // valueStr = [NSMutableString stringWithFormat:@"%@-%@", subM.inputMin, subM.inputMax];
- // para[key] = valueStr;
- // }
- continue;
- }
- [valueStr appendString:[NSString stringWithFormat:@"%@,",subM.Id]];
- para[key] = valueStr;
- }
- }
-
- [ASNetTools.shared getWithPath:SearchProduct param:para success:^(id _Nonnull json) {
- ASProductListCategoryModel *m = [ASProductListCategoryModel mj_objectWithKeyValues:json[@"category"]];
- self.cateModel = m;
- NSArray *arr = json[@"product"][@"productLists"];
- NSMutableArray *products = [ASProductBaseModel mj_objectArrayWithKeyValuesArray:arr];
-
- NSArray *filtArr = json[@"filter"];
- NSMutableArray *filters = [KWProductListFilterModel mj_objectArrayWithKeyValuesArray:filtArr];
- self.sourceArr = [NSMutableArray arrayWithArray:filters];
- self.selectDic = [NSMutableDictionary dictionary];
- for (KWProductListFilterModel *m in filters) {
- if (m.attribute_data.count > 0) {
- for (KWProductListFilterSubModel *sub in m.attribute_data) {
- if (sub.status != 0) {
- NSMutableArray *tempSubs = self.selectDic[m.attribute_id];
- if (tempSubs == nil) {
- tempSubs = [NSMutableArray array];
- }
- if ([m.attribute_id isEqualToString:@"price"]) {
- // sub.inputMin = sub.minPrice;
- // sub.inputMax = sub.maxPrice;
- continue;
- } else {
- sub.inputMax = @"";
- sub.inputMin = @"";
- }
- [tempSubs addObject:sub.copySelf];
- self.selectDic[m.attribute_id] = tempSubs;
- }
- }
- }
- }
-
-
- if (products.count > 0) {
- if (page == 1) {
- self.productList = products;
- } else {
- self.productList = [self.productList arrayByAddingObjectsFromArray:products];
- }
- if (products.count < 6) {
- com(false, @"");
- } else {
- com(true, @"");
- }
- } else {
- com(false, @"");
- }
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- com(false, msg);
- }];
-
-
- }
- - (void)getPoint:(NSString *)pointType com:(void(^)(BOOL isSuc, NSString *msg))com {
- [ASNetTools.shared postWithPath:postTakeExtraPoints param:@{@"type": pointType} success:^(id _Nonnull json) {
- com(true, @"");
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- com(false,msg);
- }];
- }
- -(void)getTopLinkList:(btnClickBlock)success {
- __weak typeof(self) weakSelf = self;
- [ASNetTools.shared getWithPath:getHotList param:@{@"type":@"banner"} success:^(id _Nonnull json) {
- NSLog(@"------url:%@---json:%@------", getHotList, json);
- NSArray *arr = [KWSearchHotKeyModel mj_objectArrayWithKeyValuesArray:json];
- weakSelf.topLinks = arr;
- success();
- } faild:^(NSString * _Nonnull code, NSString * _Nonnull msg) {
- NSLog(@"------url:%@---code:%@---msg:%@---", getHotList, code, msg);
- weakSelf.topLinks = @[];
- success();
- }];
- }
- @end
|