ProductDetailsViewController.m 15.1 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9
//
//  ProductDetailsViewController.m
//  Lighting
//
//  Created by 曹云霄 on 16/5/4.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "ProductDetailsViewController.h"
曹云霄's avatar
曹云霄 committed
10 11
#import "ProductDetailsTableViewCell.h"
#import "ProductDetailsHeaderView.h"
曹云霄's avatar
曹云霄 committed
12
#import "goodsDetailsSectionview.h"
勾芒's avatar
勾芒 committed
13 14
#import "goodsDetailsTableViewCell.h"
#import "FullScreenViewController.h"
曹云霄's avatar
曹云霄 committed
15

16 17


曹云霄's avatar
曹云霄 committed
18
@interface ProductDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,CAAnimationDelegate>
曹云霄's avatar
曹云霄 committed
19 20

@property (nonatomic,strong) ProductDetailsHeaderView *headerView;
曹云霄's avatar
曹云霄 committed
21

曹云霄's avatar
曹云霄 committed
22 23 24 25 26
/**
 *  商品详情数据
 */
@property (nonatomic,strong )TOGoodsEntity *entity;

勾芒's avatar
勾芒 committed
27 28 29 30 31 32 33 34 35 36
/**
 *  图文详情数据源
 */
@property (nonatomic,strong) NSMutableArray *goodsDetailsArray;

/**
 *  商品小图数据源
 */
@property (nonatomic,strong) NSMutableArray *imagesArray;

勾芒's avatar
勾芒 committed
37 38 39 40 41
/**
 *  点击小图的下标 默认0
 */
@property (nonatomic,assign) NSInteger imageIndex;

曹云霄's avatar
曹云霄 committed
42 43 44 45
@end

@implementation ProductDetailsViewController

勾芒's avatar
勾芒 committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

/**
 *  初始化图文详情
 */
- (NSMutableArray *)goodsDetailsArray
{
    if (_goodsDetailsArray == nil) {
        _goodsDetailsArray = [NSMutableArray array];
    }
    return _goodsDetailsArray;
}

/**
 *  商品小图数据源
 */
- (NSMutableArray *)imagesArray
{
    if (_imagesArray == nil) {
        _imagesArray = [NSMutableArray array];
    }
    return _imagesArray;
}


曹云霄's avatar
曹云霄 committed
70 71
- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
72 73
    
    [self uiConfigAction];
曹云霄's avatar
曹云霄 committed
74
    [self getGoodsListDetails];
曹云霄's avatar
曹云霄 committed
75 76 77 78 79
}

#pragma mark -UI
- (void)uiConfigAction
{
勾芒's avatar
勾芒 committed
80
    self.imageIndex = 0;
曹云霄's avatar
曹云霄 committed
81 82
    self.productDetilsTableview.dataSource = self;
    self.productDetilsTableview.delegate = self;
勾芒's avatar
勾芒 committed
83
    [self.productDetilsTableview registerNib:[UINib nibWithNibName:@"goodsDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:@"goodsdetailscell"];
曹云霄's avatar
曹云霄 committed
84 85 86
    [self CreateHeaderView];
}

曹云霄's avatar
曹云霄 committed
87

曹云霄's avatar
曹云霄 committed
88 89 90
#pragma mark -获取商品详情
- (void)getGoodsListDetails
{
91
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
92
    [self CreateMBProgressHUDLoding];
93
    NSString *url = [NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(GOODSDETAILS),_goodsID];
94
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:url WithCallClass:weakSelf WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
95
        
96
        [weakSelf RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
97
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
98 99 100
           weakSelf.entity = [[TOGoodsEntity alloc]initWithDictionary:returnValue[@"data"] error:nil];
            [weakSelf.productDetilsTableview reloadData];
            [weakSelf HeaderViewAssignment];
曹云霄's avatar
曹云霄 committed
101 102 103
        }
        else
        {
104
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
曹云霄's avatar
曹云霄 committed
105 106
        }
    } WithErrorCodeBlock:^(id errorCodeValue) {
107
        [weakSelf RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
108
        [weakSelf ErrorMBProgressView:NETWORK];
曹云霄's avatar
曹云霄 committed
109
         
110
    } WithFailureBlock:^(NSError *error) {
111 112
        [weakSelf RemoveMBProgressHUDLoding];
        [weakSelf ErrorMBProgressView:error.localizedDescription];
曹云霄's avatar
曹云霄 committed
113 114 115 116
    }];
}


曹云霄's avatar
曹云霄 committed
117 118 119 120
#pragma mark -头部视图
- (void)CreateHeaderView
{
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"ProductDetailsHeaderView" owner:self options:nil] lastObject];
曹云霄's avatar
曹云霄 committed
121 122 123 124
    self.headerView.goodsNumber.delegate = self;
    //增加,减少商品
    [self.headerView.reduceButton addTarget:self action:@selector(reduceGoodsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.addButton addTarget:self action:@selector(reduceGoodsButtonClick:) forControlEvents:UIControlEventTouchUpInside];
勾芒's avatar
勾芒 committed
125 126
    //放大
    [self.headerView.amplificationButton addTarget:self action:@selector(amplificationButtonClick) forControlEvents:UIControlEventTouchUpInside];
曹云霄's avatar
曹云霄 committed
127
    self.productDetilsTableview.tableHeaderView = self.headerView;
曹云霄's avatar
曹云霄 committed
128
    //添加至购物车
勾芒's avatar
勾芒 committed
129
    [self.headerView.addGoodsShoppingbagsButton addTarget:self action:@selector(addGoodsShoppingbags:) forControlEvents:UIControlEventTouchUpInside];
曹云霄's avatar
曹云霄 committed
130 131 132 133 134
}

#pragma mark -header赋值
- (void)HeaderViewAssignment
{
勾芒's avatar
勾芒 committed
135
    self.headerView.serialNumber.text = self.entity.code;
曹云霄's avatar
曹云霄 committed
136
    self.headerView.nameLabe.text = self.entity.name;
勾芒's avatar
勾芒 committed
137
    self.headerView.brandName.text = self.entity.company;
曹云霄's avatar
曹云霄 committed
138
    self.headerView.inventoryNumber.text = [NSString stringWithFormat:@"%ld",self.entity.inv];
勾芒's avatar
勾芒 committed
139
    self.headerView.dorpPriceLabe.text = [NSString stringWithFormat:@"¥%.2f",[self.entity.tagPrice floatValue]];
勾芒's avatar
勾芒 committed
140 141 142 143 144
    self.imagesArray = [NSMutableArray arrayWithArray:[self.entity.pictures componentsSeparatedByString:@","]];
    [self.headerView.goodsImageview sd_setImageWithURL:[self.imagesArray firstObject] placeholderImage:REPLACEIMAGE];
    //商品小图
    self.imagesArray = [NSMutableArray arrayWithArray:[self.entity.pictures componentsSeparatedByString:@","]];
    for (int i=0; i<self.imagesArray.count; i++) {
勾芒's avatar
勾芒 committed
145
        UIImageView *subImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, i*100, 80, 60)];
勾芒's avatar
勾芒 committed
146
        subImageview.tag = 100+i;
勾芒's avatar
勾芒 committed
147
        subImageview.userInteractionEnabled = YES;
勾芒's avatar
勾芒 committed
148 149 150 151 152 153 154 155 156 157
        [subImageview sd_setImageWithURL:[NSURL URLWithString:[self.imagesArray objectAtIndex_opple:i]] placeholderImage:REPLACEIMAGE];
        [subImageview addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(SubimageViewClickAction:)]];
        [self.headerView.goodsBrotherScrollview addSubview:subImageview];
    }
    self.headerView.goodsBrotherScrollview.contentSize = CGSizeMake(0, self.imagesArray.count*100);
    self.headerView.goodsBrotherScrollview.showsVerticalScrollIndicator = NO;
    self.headerView.goodsBrotherScrollview.pagingEnabled = YES;
    self.headerView.goodsBrotherScrollview.userInteractionEnabled = YES;
    
    //图文大图
158 159 160
    self.goodsDetailsArray = [NSMutableArray array];
    WS(weakSelf);
    NSArray *imagesArray = [self.entity.detailedIntro componentsSeparatedByString:@","];
161 162 163 164 165 166 167
    NSMutableArray *array = [NSMutableArray array];
    for (NSString *imageString in imagesArray) {
        NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@","];
        NSString *string = [imageString stringByTrimmingCharactersInSet:set];
        [array addObject:string];
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:string] options:SDWebImageProgressiveDownload progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            NSDictionary *dict = @{@"image":string,@"height":@(image.size.height)};
168 169 170 171 172
            [weakSelf.goodsDetailsArray addObject:dict];
            if (weakSelf.goodsDetailsArray.count == imagesArray.count) {
                [weakSelf.productDetilsTableview reloadData];
            }
        }];
173
    }
曹云霄's avatar
曹云霄 committed
174 175
}

勾芒's avatar
勾芒 committed
176
#pragma mark -小图点击手势、切换图片显示
勾芒's avatar
勾芒 committed
177 178
- (void)SubimageViewClickAction:(UITapGestureRecognizer *)tap
{
勾芒's avatar
勾芒 committed
179
    self.imageIndex = tap.view.tag-100;
勾芒's avatar
勾芒 committed
180
    [self.headerView.goodsImageview sd_setImageWithURL:[self.imagesArray objectAtIndex_opple:tap.view.tag-100] placeholderImage:REPLACEIMAGE];
勾芒's avatar
勾芒 committed
181
}
曹云霄's avatar
曹云霄 committed
182

勾芒's avatar
勾芒 committed
183 184 185 186
#pragma mark -图片放大点击
- (void)amplificationButtonClick
{
    FullScreenViewController *fullScreenVC = [[FullScreenViewController alloc]init];
勾芒's avatar
勾芒 committed
187
    fullScreenVC.datasArray = self.imagesArray;
勾芒's avatar
勾芒 committed
188
    fullScreenVC.isProductLibrary = YES;
勾芒's avatar
勾芒 committed
189 190
    fullScreenVC.currentindex = self.imageIndex;
    [self presentViewController:fullScreenVC animated:YES completion:nil];
勾芒's avatar
勾芒 committed
191
}
勾芒's avatar
勾芒 committed
192 193


曹云霄's avatar
曹云霄 committed
194 195
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
勾芒's avatar
勾芒 committed
196 197 198 199 200 201 202
    if (indexPath.section == 0) {
        ProductDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productDetailscell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.datas = self.entity;
        return cell;
        
    }
曹云霄's avatar
曹云霄 committed
203 204 205
    goodsDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goodsdetailscell" forIndexPath:indexPath];
    [cell.detailsImageView sd_setImageWithURL:[NSURL URLWithString:[self.goodsDetailsArray objectAtIndex_opple:indexPath.row][@"image"]] placeholderImage:TCImage(@"bg-big")];
    return cell;
曹云霄's avatar
曹云霄 committed
206 207
}

曹云霄's avatar
曹云霄 committed
208

曹云霄's avatar
曹云霄 committed
209 210
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
勾芒's avatar
勾芒 committed
211 212 213 214
    if (section == 0) {
        return 1;
    }
    return self.goodsDetailsArray.count;
曹云霄's avatar
曹云霄 committed
215 216
}

曹云霄's avatar
曹云霄 committed
217

曹云霄's avatar
曹云霄 committed
218 219
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
勾芒's avatar
勾芒 committed
220 221 222 223
    if (indexPath.section == 0) {
        
        return 170;
    }
224 225
    NSDictionary *dict = [self.goodsDetailsArray objectAtIndex_opple:indexPath.row];
    return [dict[@"height"] floatValue];
226 227
}

曹云霄's avatar
曹云霄 committed
228 229
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
勾芒's avatar
勾芒 committed
230
    return 2;
曹云霄's avatar
曹云霄 committed
231 232 233 234 235 236 237 238 239
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
曹云霄's avatar
曹云霄 committed
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    goodsDetailsSectionview *sectionView = [[[NSBundle mainBundle] loadNibNamed:@"goodsDetailsSectionview" owner:self options:nil] firstObject];
    sectionView.goodSectionLabe.text = section?@"图文详情":@"商品参数";
    return sectionView;
}


#pragma mark -减少商品----增加商品
- (void)reduceGoodsButtonClick:(UIButton *)sender {
    
    NSInteger goodsNumber = [self.headerView.goodsNumber.text integerValue];
    switch (sender.tag) {
        case 100://减少
        {
            if (goodsNumber <= 1) {
                
                //不能小于1
                [self ErrorMBProgressView:@"不能小于1"];
                return;
            }
            goodsNumber --;
勾芒's avatar
勾芒 committed
260
            self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
曹云霄's avatar
曹云霄 committed
261 262 263 264
        }
            break;
        case 101://增加
        {
265
            if (goodsNumber >= self.entity.inv) {
曹云霄's avatar
曹云霄 committed
266 267 268 269 270 271
                
                //不能大于库存
                [self ErrorMBProgressView:@"超过库存"];
                return;
            }
            goodsNumber ++;
勾芒's avatar
勾芒 committed
272
            self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
曹云霄's avatar
曹云霄 committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
        }
            break;
            
        default:
            break;
    }
}


#pragma mark -UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSString *inputString = textField.text;
    if (![self isPureInt:inputString]) {
        
        [self ErrorMBProgressView:@"输入格式错误"];
        textField.text = @"1";
        return;
    }
292
    if ([inputString integerValue] > self.entity.inv) {
曹云霄's avatar
曹云霄 committed
293 294
        
        [self ErrorMBProgressView:@"超过库存数量"];
295
        textField.text = [NSString stringWithFormat:@"%ld",(long)self.entity.inv];
曹云霄's avatar
曹云霄 committed
296 297 298 299 300
        return;
    }
}


勾芒's avatar
勾芒 committed
301
#pragma mark -开发加入购物车动画
302
- (void)StartAddShoppingCarAnimationWithimage:(UIImage *)image withStartpoint:(CGPoint)point withSize:(CGSize)size
勾芒's avatar
勾芒 committed
303
{
曹云霄's avatar
曹云霄 committed
304
    //起点
勾芒's avatar
勾芒 committed
305
    CGPoint startPoint = point;
曹云霄's avatar
曹云霄 committed
306
    //终点
勾芒's avatar
勾芒 committed
307
    CGPoint endPoint = SHARED_APPDELEGATE.shoppingCarPoint;
曹云霄's avatar
曹云霄 committed
308
    //控点
勾芒's avatar
勾芒 committed
309 310 311
    CGPoint controlPoint = CGPointMake(endPoint.x, startPoint.x);
    
    
312 313
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.backgroundColor = kTCColor(252, 248, 239);
勾芒's avatar
勾芒 committed
314
    imageView.image = image;
315
    imageView.center = point;
勾芒's avatar
勾芒 committed
316
    
317
    [self.view.window.layer addSublayer:imageView.layer];
勾芒's avatar
勾芒 committed
318
    
319 320 321 322 323 324 325
    //路径曲线
    UIBezierPath *movePath = [UIBezierPath bezierPath];
    [movePath moveToPoint:imageView.center];
    [movePath addQuadCurveToPoint:endPoint
                     controlPoint:controlPoint];
    //关键帧
    CAKeyframeAnimation *moveAnim = [CAKeyframeAnimation animationWithKeyPath:@"position"];
勾芒's avatar
勾芒 committed
326 327
    
    
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
    moveAnim.path = movePath.CGPath;
    moveAnim.removedOnCompletion = YES;
    
    //旋转变化
    CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
    scaleAnim.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    //x,y轴缩小到0.1,Z 轴不变
    scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)];
    scaleAnim.removedOnCompletion = YES;
    
    //透明度变化
    CABasicAnimation *opacityAnim = [CABasicAnimation animationWithKeyPath:@"alpha"];
    opacityAnim.fromValue = [NSNumber numberWithFloat:1.0];
    opacityAnim.toValue = [NSNumber numberWithFloat:0.1];
    opacityAnim.removedOnCompletion = YES;
勾芒's avatar
勾芒 committed
343
    
344 345 346 347 348 349 350 351 352 353 354 355 356 357
    //关键帧,旋转,透明度组合起来执行
    CAAnimationGroup *animGroup = [CAAnimationGroup animation];
    //当动画完成,停留到结束位置
    animGroup.removedOnCompletion = NO;
    animGroup.fillMode = kCAFillModeForwards;
    animGroup.animations = [NSArray arrayWithObjects:moveAnim, scaleAnim,opacityAnim, nil];
    animGroup.duration = 1;
    animGroup.delegate = self;
    [imageView.layer addAnimation:animGroup forKey:nil];
    [self performSelector:@selector(removeFromLayer:) withObject:imageView.layer afterDelay:1];
}

#pragma mark -动画完成后移除
- (void)removeFromLayer:(CALayer *)layerAnimation{
勾芒's avatar
勾芒 committed
358
    
359
    [layerAnimation removeFromSuperlayer];
勾芒's avatar
勾芒 committed
360 361 362 363 364
}

#pragma mark -完成加入购物车动画完成后回调
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
365
   [[NSNotificationCenter defaultCenter]postNotificationName:REFRESHSHOPPINGCAR object:@([self.headerView.goodsNumber.text integerValue])];
勾芒's avatar
勾芒 committed
366 367 368
}


曹云霄's avatar
曹云霄 committed
369
#pragma mark -添加至购物车
勾芒's avatar
勾芒 committed
370
- (void)addGoodsShoppingbags:(UIButton *)button
曹云霄's avatar
曹云霄 committed
371
{
勾芒's avatar
勾芒 committed
372 373 374 375 376 377
    //判断是否有当前客户
    if (![Shoppersmanager manager].currentCustomer) {
        
        [self ErrorMBProgressView:@"必须设置当前客户"];
        return;
    }
378
    if ([self.headerView.goodsNumber.text integerValue] > self.entity.inv) {
曹云霄's avatar
曹云霄 committed
379 380 381
        [self ErrorMBProgressView:@"超过库存"];
        return;
    }
勾芒's avatar
勾芒 committed
382
    SaveShoppingCartRequest *shopCar = [[SaveShoppingCartRequest alloc]init];
勾芒's avatar
勾芒 committed
383
    shopCar.consumerId = [Customermanager manager].model.fid;
勾芒's avatar
勾芒 committed
384
    shopCar.goodsId = _goodsID;
385
    shopCar.count = self.headerView.goodsNumber.text;
曹云霄's avatar
曹云霄 committed
386
    WS(weakSelf);
曹云霄's avatar
曹云霄 committed
387
    [self CreateMBProgressHUDLoding];
388
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:SERVERREQUESTURL(ADDSHOPPINGBAG) WithCallClass:weakSelf WithRequestType:0 WithParameter:shopCar WithReturnValueBlock:^(id returnValue) {
曹云霄's avatar
曹云霄 committed
389
    
曹云霄's avatar
曹云霄 committed
390
        [weakSelf RemoveMBProgressHUDLoding];
勾芒's avatar
勾芒 committed
391
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
勾芒's avatar
勾芒 committed
392
            NSLog(@"添加购物车成功");
曹云霄's avatar
曹云霄 committed
393
            [weakSelf StartAddShoppingCarAnimationWithimage:weakSelf.headerView.goodsImageview.image withStartpoint:[weakSelf.headerView convertPoint:[weakSelf.headerView.goodsImageview center] toView:weakSelf.view.window] withSize:weakSelf.headerView.goodsImageview.frame.size];
勾芒's avatar
勾芒 committed
394 395
        }else
        {
曹云霄's avatar
曹云霄 committed
396
            [weakSelf ErrorMBProgressView:returnValue[@"message"]];
勾芒's avatar
勾芒 committed
397
        }
曹云霄's avatar
曹云霄 committed
398 399
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
曹云霄's avatar
曹云霄 committed
400
        [weakSelf RemoveMBProgressHUDLoding];
401
        [weakSelf ErrorMBProgressView:NETWORK];
曹云霄's avatar
曹云霄 committed
402
    } WithFailureBlock:^(id error) {
曹云霄's avatar
曹云霄 committed
403
        [weakSelf RemoveMBProgressHUDLoding];
曹云霄's avatar
曹云霄 committed
404
        [weakSelf ErrorMBProgressView:@"添加购物车失败"];
曹云霄's avatar
曹云霄 committed
405
    }];
曹云霄's avatar
曹云霄 committed
406 407
}

曹云霄's avatar
曹云霄 committed
408

曹云霄's avatar
曹云霄 committed
409 410 411 412 413 414 415
#pragma mark -判断是否为纯数字
- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

勾芒's avatar
勾芒 committed
416 417 418 419 420 421
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


曹云霄's avatar
曹云霄 committed
422
@end