单军华
2018-07-19 83b9d5c682b21d88133f24da0f94dd56bd79e687
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//
//  DynamicService.m
//  screendisplay
//
//  Created by 单军华 on 2018/7/9.
//  Copyright © 2018年 单军华. All rights reserved.
//
 
#import "DynamicService.h"
#import "PPInterfacedConst.h"
 
@implementation DynamicService
 
/** 获取详情列表*/
- (NSURLSessionTask *)getDynamicListWithParameters:(id)parameters dataCount:(int)getCount completion:(void(^)(NSString *desc,int code))completion{
    // 将请求前缀与请求路径拼接成一个完整的URL
    NSString *url = [NSString stringWithFormat:@"%@%@",kApiPrefix,kHistory];
    
    return [self requestGETWithURL:url parameters:parameters success:^(id responseObject)
            {
                BaseResModel * resModel = [Global toBaseModel:responseObject];
                if(resModel.code == 1)
                {
                    if(!TWIsEmpty(resModel.content))
                    {
                        // 成功处理
                        if (getCount > 1)
                        {
                           self.dynamicViewModels = [DynamicModel mj_objectArrayWithKeyValuesArray:resModel.content];
                            for (int i = 0; i < self.dynamicViewModels.count; i++) {
                                DynamicModel * dynamicViewModel = [self.dynamicViewModels objectAtIndex:i];
                                if([StringUtil isPureFloat:dynamicViewModel.value] && ![StringUtil isPureInt:dynamicViewModel.value])
                                {
                                    dynamicViewModel.value = [NSString stringWithFormat:@"%.2f",[dynamicViewModel.value floatValue]];
                                }
                            }
                        }
                        else if(getCount == 1)
                        {
                            NSMutableArray<DynamicModel *> *dynamicTempViewModels = [DynamicModel mj_objectArrayWithKeyValuesArray:resModel.content];
                            if(self.dynamicViewModels.count > 0)
                            {
//                                for (int i = 0; i < self.dynamicViewModels.count-1; i++)
//                                {
//                                    if(self.dynamicViewModels.count-1 < i + 1) continue;
//                                    [self.dynamicViewModels exchangeObjectAtIndex:i withObjectAtIndex:i+1];
//                                }
                                [self.dynamicViewModels removeObjectAtIndex:0];
                                if(dynamicTempViewModels.count==1)
                                {
                                    DynamicModel * dynamicViewModel = [dynamicTempViewModels objectAtIndex:0];
                                    if(!TWIsEmpty(dynamicViewModel))
                                    {
                                        if([StringUtil isPureFloat:dynamicViewModel.value]&& ![StringUtil isPureInt:dynamicViewModel.value])
                                        {
                                            dynamicViewModel.value = [NSString stringWithFormat:@"%.2f",[dynamicViewModel.value floatValue]];
                                        }
                                        
                                        [self.dynamicViewModels addObject:dynamicViewModel];
                                    }
                                        //[self.dynamicViewModels replaceObjectAtIndex:(self.dynamicViewModels.count-1) withObject:dynamicViewModel];
                                }
 
                            }
                        }
                    }
                    else
                    {
                        // 出错处理
                        resModel.code = -5;
                        completion(@"返回数据错误,数据为空!",resModel.code);
                    }
                }
                
                // 在这里你可以根据项目自定义其他一些重复操作,比如加载页面时候的等待效果, 提醒弹窗....
                completion(resModel.desc,resModel.code);
                
            } failure:^(NSError *error) {
                // 同上
                completion(error.localizedDescription,-1);
            }];
}
 
- (NSMutableArray<DynamicModel *> *)dynamicViewModels
{
    if(_dynamicViewModels == nil)
    {
        _dynamicViewModels = [NSMutableArray array];
    }
    return _dynamicViewModels;
}
 
@end