CardDontUseViewController.m 11.5 KB
Newer Older
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
//
//  CardDontUseViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2016/10/19.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "CardDontUseViewController.h"
#import "CardCollectionViewCell.h"
#import "CardOrderInformationReusableView.h"
#import "DeviceDirectionManager.h"
#import "CardAmplificationViewController.h"

@interface CardDontUseViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UploadReceiptsDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>


@property (nonatomic,strong) RsJingDongECardRequest *requestModel;

/**
 *  数据源
 */
@property (nonatomic,strong) NSMutableArray *datasArray;

/**
 *  总页数
 */
@property (nonatomic,assign) NSInteger totalPage;

/**
 *  记录上传小票时的订单号
 */
@property (nonatomic,copy) NSString *orderNumber;

@end

@implementation CardDontUseViewController

#pragma mark - lazy
- (RsJingDongECardRequest *)requestModel
{
    if (!_requestModel) {
        _requestModel = [[RsJingDongECardRequest alloc]init];
        DataPage *page = [[DataPage alloc]init];
45
        page.page = ZERO;
46
        page.rows = KROWS;
47
        page.order = @"desc";
48
        _requestModel.guideIdEquals = [Shoppersmanager manager].shoppers.employee.fid;
曹云霄's avatar
曹云霄 committed
49
        _requestModel.stateEquals = DISPENSED;
50 51 52 53 54 55 56 57 58 59 60 61 62
        _requestModel.page = page;
    }
    return _requestModel;
}

- (NSMutableArray *)datasArray
{
    if (!_datasArray) {
        _datasArray = [NSMutableArray array];
    }
    return _datasArray;
}

曹云霄's avatar
曹云霄 committed
63 64
- (void)viewDidAppear:(BOOL)animated
{
65 66 67 68
    [super viewDidAppear:animated];
    if (!self.isRefreshValue) {
        [self.dontUseCardCollectionView.mj_header beginRefreshing];
    }
曹云霄's avatar
曹云霄 committed
69 70
}

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCollectionView];
    [self setupRefreshAction];
}

#pragma mark - UICollectionView
- (void)setupCollectionView
{
    self.dontUseFlowLayout.itemSize = CGSizeMake((ScreenWidth-48-20*4)/3, 200);
    self.dontUseFlowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.dontUseFlowLayout.minimumLineSpacing = 20;
    self.dontUseFlowLayout.minimumInteritemSpacing = 20;
    [self.dontUseCardCollectionView registerNib:[UINib nibWithNibName:@"CardOrderInformationReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView"];
}

#pragma mark - 设置刷新
- (void)setupRefreshAction
{
    WS(weakSelf);
    MjRefreshHeaderCustom *headerRefresh = [MjRefreshHeaderCustom headerWithRefreshingBlock:^{
93
        weakSelf.requestModel.page.page = ZERO;
94 95 96 97 98 99 100 101
        [weakSelf.dontUseCardCollectionView.mj_footer resetNoMoreData];
        [weakSelf getCardDatasAction:YES];
    }];
    headerRefresh.stateLabel.hidden = YES;
    headerRefresh.lastUpdatedTimeLabel.hidden = YES;
    self.dontUseCardCollectionView.mj_header = headerRefresh;
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
102
        if (++ weakSelf.requestModel.page.page > weakSelf.totalPage) {
103 104 105 106 107 108 109 110 111 112 113 114 115 116
            [weakSelf.dontUseCardCollectionView.mj_footer endRefreshingWithNoMoreData];
        }else
        {
            [weakSelf getCardDatasAction:NO];
        }
    }];
    footer.automaticallyHidden = YES;
    self.dontUseCardCollectionView.mj_footer = footer;
}

#pragma mark - 获取未使用卡劵
- (void)getCardDatasAction:(BOOL)isRemove
{
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
117
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
118
    [HTTP networkRequestWithURL:SERVERREQUESTURL(CARDINFORMATION)  withRequestType:ZERO withParameter:self.requestModel withReturnValueBlock:^(id returnValue) {
119
        
曹云霄's avatar
曹云霄 committed
120
        [XBLoadingView hideHUDViewWithDefault];
121 122
        weakSelf.dontUseCardCollectionView.emptyDataSetSource = weakSelf;
        weakSelf.dontUseCardCollectionView.emptyDataSetDelegate = weakSelf;
123
        [weakSelf endRefreshingForTableView:weakSelf.dontUseCardCollectionView];
曹云霄's avatar
曹云霄 committed
124
        if (RESULT(returnValue)) {
曹云霄's avatar
曹云霄 committed
125
            RsJingDongECardResponse *cardInformation = [[RsJingDongECardResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
126
            if (isRemove) {
曹云霄's avatar
曹云霄 committed
127 128
                [weakSelf.datasArray removeAllObjects];
            }
129
            weakSelf.totalPage = cardInformation.totalpages;
130
            [weakSelf.datasArray addObjectsFromArray:[NSMutableArray arrayWithArray:cardInformation.list]];
131 132
            [weakSelf.dontUseCardCollectionView reloadData];
        }else{
133
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
134
        }
135
    }withFailureBlock:^(NSError *error) {
136
        [weakSelf endRefreshingForTableView:weakSelf.dontUseCardCollectionView];
曹云霄's avatar
曹云霄 committed
137
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
138 139 140 141 142
    }];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
143
    CardCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CardCollectionViewCell" forIndexPath:indexPath];
144 145
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    cell.Cardmodel = model.eCards[indexPath.row];
146 147 148 149 150 151
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    GroupByOrderNumber *model = self.datasArray[section];
152
    return model.eCards.count;
153 154 155 156
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
曹云霄's avatar
曹云霄 committed
157 158 159 160 161
    CardOrderInformationReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView" forIndexPath:indexPath];
    headerView.delegate = self;
    headerView.model = self.datasArray[indexPath.section];
    headerView.sectionIndex = indexPath.section;
    return headerView;
162 163 164 165 166 167 168 169 170 171 172 173 174 175
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(0, 65);
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.datasArray.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
176 177
    GroupByOrderNumber *model = self.datasArray[indexPath.section];
    if ([[self class] isBlankString:model.orderReceiptUrl]) {
曹云霄's avatar
曹云霄 committed
178
        [XBLoadingView showHUDViewWithText:@"不能查看未激活卡劵,请先上传小票激活卡劵"];
179
    }
180 181 182 183 184 185
}


#pragma mark - <UploadReceiptsDelegate>
- (void)startUploadReceipts:(NSInteger)sectionIndex
{
186
    WS(weakSelf);
187 188 189 190 191 192 193 194 195 196
    self.orderNumber = [self.datasArray[sectionIndex] orderNumber];
    UIAlertController *alertView = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIImagePickerController *PcCamera = [[UIImagePickerController alloc]init];
    PcCamera.delegate = self;
    [DeviceDirectionManager instance].isHorizontal=YES;
    [alertView addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //拍照
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            [PcCamera setSourceType:UIImagePickerControllerSourceTypeCamera];
            PcCamera.allowsEditing = YES;
197
            [weakSelf presentViewController:PcCamera animated:YES completion:nil];
198 199 200 201
        }
        else
        {
            [DeviceDirectionManager instance].isHorizontal=NO;
曹云霄's avatar
曹云霄 committed
202
            [XBLoadingView showHUDViewWithText:@"相机无法使用"];
203 204 205 206 207 208 209
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册中选择
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [PcCamera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            PcCamera.allowsEditing = YES;
210
            [weakSelf presentViewController:PcCamera animated:YES completion:nil];
211 212 213 214
        }
        else
        {
            [DeviceDirectionManager instance].isHorizontal=NO;
曹云霄's avatar
曹云霄 committed
215
            [XBLoadingView showHUDViewWithText:@"相册无法打开"];
216 217 218 219 220 221
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [DeviceDirectionManager instance].isHorizontal=NO;
        [alertView dismissViewControllerAnimated:YES completion:nil];
    }]];
222
    self.isRefreshValue = YES;
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
    [self presentViewController:alertView animated:YES completion:nil];
}

#pragma mark -拍照、从相册选择
#pragma -mark -UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [DeviceDirectionManager instance].isHorizontal=NO;
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *Headimage = [info objectForKey:UIImagePickerControllerOriginalImage];
    [self uploadReceipts:Headimage];
}

#pragma mark -上传小票
- (void)uploadReceipts:(UIImage *)image
{
    WS(weakSelf);
    NSData *data = UIImageJPEGRepresentation(image, 0.5);
曹云霄's avatar
曹云霄 committed
241
    XBLoadingView *progressView = [XBLoadingView showHUDViewProgressLabel:@"上传小票中"];
曹云霄's avatar
曹云霄 committed
242
    [HTTP uploadImageWithURL:SERVERREQUESTURL(UPLOADHEADER) withRequestType:ONE withImageDatas:data withParameter:nil withReturnValueBlock:^(id returnValue) {
243
        
曹云霄's avatar
曹云霄 committed
244
        if (RESULT(returnValue)) {
曹云霄's avatar
曹云霄 committed
245
            [weakSelf activationJDECard:RESPONSE(returnValue)];
246 247
        }else
        {
248
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
249
        }
250
    } withprogressBlock:^(double progress) {
曹云霄's avatar
曹云霄 committed
251
        if (progress >= 1) {
252 253 254 255 256
            dispatch_async(dispatch_get_main_queue(), ^{
                progressView.labelText = @"上传小票成功";
                [progressView hide:YES afterDelay:1];
            });

曹云霄's avatar
曹云霄 committed
257 258 259
        }else {
            progressView.progress = progress;
        }
260
    } withFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
261
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
262 263 264 265 266 267 268
    }];
}

#pragma mark - 激活京东E卡
- (void)activationJDECard:(NSString *)imagePath
{
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
269
    [XBLoadingView showHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
270
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(ACTIVIATIONJDECARD),self.orderNumber,imagePath]  withRequestType:ZERO withParameter:nil withReturnValueBlock:^(id returnValue) {
271
        
曹云霄's avatar
曹云霄 committed
272
        [XBLoadingView hideHUDViewWithDefault];
曹云霄's avatar
曹云霄 committed
273
        if (RESULT(returnValue)) {
274 275 276 277
            [XBLoadingView showHUDViewWithSuccessText:@"激活成功" completeBlock:^{
                weakSelf.isRefreshValue = NO;
                [weakSelf.dontUseCardCollectionView.mj_header beginRefreshing];
            }];
曹云霄's avatar
曹云霄 committed
278
            
279
        }else{
280
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
281
        }
282
    } withFailureBlock:^(NSError *error) {
曹云霄's avatar
曹云霄 committed
283
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    }];
}

#pragma mark -友好界面
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return kNoDataImage;
}

- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView
{
    return YES;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc]initWithString:@"暂无数据" attributes:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end