ReadPacketMainViewController.m 9.86 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
//
//  ReadPacketMainViewController.m
//  Lighting
//
//  Created by 曹云霄 on 2017/3/28.
//  Copyright © 2017年 上海勾芒科技有限公司. All rights reserved.
//

#import "ReadPacketMainViewController.h"
#import "ReadPacketCollectionViewCell.h"
#import "CardOrderInformationReusableView.h"
#import "DeviceDirectionManager.h"

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

/**
 查询红包
 */
@property (nonatomic,strong) RsRedPackageConditions *queryReadPacket;

/**
 红包数据
 */
@property (nonatomic,strong) RedPackageResponse *readPacketResult;

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

/**
 选中订单号
 */
@property (nonatomic,strong) NSString *orderNumber;
@end

@implementation ReadPacketMainViewController


- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
曹云霄's avatar
曹云霄 committed
43
    [self getReadPacketAction:YES];
44 45 46 47 48 49 50 51 52
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupCollectionView];
}

#pragma mark - 获取红包信息
曹云霄's avatar
曹云霄 committed
53
- (void)getReadPacketAction:(BOOL)isRefresh
54 55
{
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
56
    [XBLoadingView showHUDViewWithDefaultWithView:self.view];
57 58
    [HTTP networkRequestWithURL:SERVERREQUESTURL(QUERY_REDPACKAGE)  withRequestType:ZERO withParameter:self.queryReadPacket withReturnValueBlock:^(id returnValue) {
        
曹云霄's avatar
曹云霄 committed
59
        [XBLoadingView hideHUDViewWithDefaultWithView:weakSelf.view];
60 61 62 63
        weakSelf.readPacketMainCollectionView.emptyDataSetSource = weakSelf;
        weakSelf.readPacketMainCollectionView.emptyDataSetDelegate = weakSelf;
        [weakSelf endRefreshingForTableView:weakSelf.readPacketMainCollectionView];
        if (RESULT(returnValue)) {
曹云霄's avatar
曹云霄 committed
64 65 66
            if (isRefresh) {
                [weakSelf.readPacketArray removeAllObjects];
            }
67 68 69 70 71 72 73 74
            weakSelf.readPacketResult = [[RedPackageResponse alloc]initWithDictionary:RESPONSE(returnValue) error:nil];
            [weakSelf.readPacketArray addObjectsFromArray:[NSMutableArray arrayWithArray:weakSelf.readPacketResult.list]];
            [weakSelf.readPacketMainCollectionView reloadData];
        }else{
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    }withFailureBlock:^(NSError *error) {
        [weakSelf endRefreshingForTableView:weakSelf.readPacketMainCollectionView];
曹云霄's avatar
曹云霄 committed
75
        [XBLoadingView hideHUDViewWithDefaultWithView:weakSelf.view];
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    }];
}

#pragma mark - UICollectionView
- (void)setupCollectionView
{
    CGFloat width = (ScreenWidth-6*20)/5.0;
    self.readPacketMainFlowLayout.itemSize = CGSizeMake(width, width*0.6+width);
    self.readPacketMainFlowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    self.readPacketMainFlowLayout.minimumLineSpacing = 20;
    self.readPacketMainFlowLayout.minimumInteritemSpacing = 20;
    [self.readPacketMainCollectionView registerNib:[UINib nibWithNibName:@"CardOrderInformationReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView"];
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ReadPacketCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ReadPacketCollectionViewCell" forIndexPath:indexPath];
    RedPackageGroupbyOrderNumber *order = self.readPacketArray[indexPath.section];
    cell.readPacket = order.draws[indexPath.item];
    return cell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    RedPackageGroupbyOrderNumber *order = self.readPacketArray[section];
    return order.draws.count;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    CardOrderInformationReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CardOrderInformationReusableView" forIndexPath:indexPath];
    [headerView refreshHeaderView:self withEntity:self.readPacketArray[indexPath.section] withSectionIndex:indexPath.section];
    return headerView;
}

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

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


#pragma mark - <UploadReceiptsDelegate>
- (void)startUploadReceipts:(NSInteger)sectionIndex
{
    WS(weakSelf);
    self.orderNumber = [self.readPacketArray[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;
            [weakSelf presentViewController:PcCamera animated:YES completion:nil];
        }
        else
        {
            [DeviceDirectionManager instance].isHorizontal=NO;
            [XBLoadingView showHUDViewWithText:@"相机无法使用"];
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册中选择
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [PcCamera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            PcCamera.allowsEditing = YES;
            [weakSelf presentViewController:PcCamera animated:YES completion:nil];
        }
        else
        {
            [DeviceDirectionManager instance].isHorizontal=NO;
            [XBLoadingView showHUDViewWithText:@"相册无法打开"];
        }
    }]];
    [alertView addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [DeviceDirectionManager instance].isHorizontal=NO;
        [alertView dismissViewControllerAnimated:YES completion:nil];
    }]];
    [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);
    XBLoadingView *progressView = [XBLoadingView showHUDViewProgressLabel:@"上传小票中"];
    [HTTP uploadImageWithURL:SERVERREQUESTURL(UPLOADHEADER) withRequestType:ONE withImageDatas:data withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        if (RESULT(returnValue)) {
            [weakSelf activationReadPacket:RESPONSE(returnValue)];
        }else
        {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    } withprogressBlock:^(double progress) {
        if (progress >= 1) {
            dispatch_async(dispatch_get_main_queue(), ^{
                progressView.labelText = @"上传小票成功";
                [progressView hide:YES];
            });
            
        }else {
            progressView.progress = progress;
        }
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark - 激活红包
- (void)activationReadPacket:(NSString *)imagePath
{
    WS(weakSelf);
    [XBLoadingView showHUDViewWithDefault];
    [HTTP networkWithDictionaryRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(READPACKETPATH),self.orderNumber,imagePath]  withRequestType:POST withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        [XBLoadingView hideHUDViewWithDefault];
        if (RESULT(returnValue)) {
            [XBLoadingView showHUDViewWithSuccessText:@"激活成功" completeBlock:^{
                [weakSelf.readPacketMainCollectionView.mj_header beginRefreshing];
            }];
            
        }else{
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}


#pragma mark -友好界面
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    return [[NSAttributedString alloc]initWithString:@"暂无红包记录~" attributes:nil];
}


#pragma mark -lazy
- (RsRedPackageConditions *)queryReadPacket
{
    if (!_queryReadPacket) {
        _queryReadPacket = [[RsRedPackageConditions alloc] init];
        _queryReadPacket.guideIdEquals = [Shoppersmanager manager].shoppers.employee.fid;
        DataPage *page = [[DataPage alloc] init];
        page.page = ZERO;
        page.rows = KROWS;
        _queryReadPacket.page = page;
    }
    return _queryReadPacket;
}


- (NSMutableArray *)readPacketArray
{
    if (!_readPacketArray) {
        _readPacketArray = [NSMutableArray array];
    }
    return _readPacketArray;
}




@end