ProductDetailsViewController.m 13.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

曹云霄's avatar
曹云霄 committed
16
@interface ProductDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
曹云霄's avatar
曹云霄 committed
17 18

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

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

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

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

曹云霄's avatar
曹云霄 committed
35 36 37 38
@end

@implementation ProductDetailsViewController

勾芒's avatar
勾芒 committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

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


曹云霄's avatar
曹云霄 committed
63 64
- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
65 66
    
    [self uiConfigAction];
曹云霄's avatar
曹云霄 committed
67 68
    [self getGoodsListDetails];
    
曹云霄's avatar
曹云霄 committed
69 70 71 72 73 74 75
}

#pragma mark -UI
- (void)uiConfigAction
{
    self.productDetilsTableview.dataSource = self;
    self.productDetilsTableview.delegate = self;
勾芒's avatar
勾芒 committed
76
    [self.productDetilsTableview registerNib:[UINib nibWithNibName:@"goodsDetailsTableViewCell" bundle:nil] forCellReuseIdentifier:@"goodsdetailscell"];
曹云霄's avatar
曹云霄 committed
77 78 79
    [self CreateHeaderView];
}

曹云霄's avatar
曹云霄 committed
80

曹云霄's avatar
曹云霄 committed
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
#pragma mark -获取商品详情
- (void)getGoodsListDetails

{
    [self CreateMBProgressHUDLoding];
    NSString *url = [NSString stringWithFormat:@"%@%@%@",ServerAddress,@"/goods/getGoods/",_goodsID];
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:url WithRequestType:1 WithParameter:nil WithReturnValueBlock:^(id returnValue) {
        
        [self RemoveMBProgressHUDLoding];
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
           self.entity = [[TOGoodsEntity alloc]initWithDictionary:returnValue[@"data"] error:nil];
            [self HeaderViewAssignment];
        }
        else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
        
        
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
    } WithFailureBlock:^(id error) {
        
        [self RemoveMBProgressHUDLoding];
        NSLog(@"%@",error);
    }];
}


曹云霄's avatar
曹云霄 committed
113 114 115 116
#pragma mark -头部视图
- (void)CreateHeaderView
{
    self.headerView = [[[NSBundle mainBundle] loadNibNamed:@"ProductDetailsHeaderView" owner:self options:nil] lastObject];
曹云霄's avatar
曹云霄 committed
117 118 119 120
    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
121 122
    //放大
    [self.headerView.amplificationButton addTarget:self action:@selector(amplificationButtonClick) forControlEvents:UIControlEventTouchUpInside];
曹云霄's avatar
曹云霄 committed
123
    self.productDetilsTableview.tableHeaderView = self.headerView;
曹云霄's avatar
曹云霄 committed
124 125
    
    //添加至购物车
勾芒's avatar
勾芒 committed
126
    [self.headerView.addGoodsShoppingbagsButton addTarget:self action:@selector(addGoodsShoppingbags:) forControlEvents:UIControlEventTouchUpInside];
曹云霄's avatar
曹云霄 committed
127 128 129 130 131 132 133 134 135 136
    
}

#pragma mark -header赋值
- (void)HeaderViewAssignment
{
    self.headerView.serialNumber.text = self.entity.series;
    self.headerView.nameLabe.text = self.entity.name;
    self.headerView.brandName.text = self.entity.brandId;
    self.headerView.dorpPriceLabe.text = [self.entity.tagPrice stringValue];
勾芒's avatar
勾芒 committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    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++) {
        UIImageView *subImageview = [[UIImageView alloc]initWithFrame:CGRectMake(0, i*100, 80, 80)];
        subImageview.backgroundColor = [UIColor redColor];
        subImageview.tag = 100+i;
        [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;
    
    //图文大图
    self.goodsDetailsArray = [NSMutableArray arrayWithArray:[self.entity.detailedIntro componentsSeparatedByString:@","]];
    [self.productDetilsTableview reloadData];
    
曹云霄's avatar
曹云霄 committed
158 159
}

勾芒's avatar
勾芒 committed
160 161 162 163 164
#pragma mark -小图点击手势
- (void)SubimageViewClickAction:(UITapGestureRecognizer *)tap
{
    
}
曹云霄's avatar
曹云霄 committed
165

勾芒's avatar
勾芒 committed
166 167 168 169 170 171
#pragma mark -图片放大点击
- (void)amplificationButtonClick
{
    FullScreenViewController *fullScreenVC = [[FullScreenViewController alloc]init];
    [self presentViewController:fullScreenVC animated:YES completion:nil];
}
勾芒's avatar
勾芒 committed
172 173


曹云霄's avatar
曹云霄 committed
174 175
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
勾芒's avatar
勾芒 committed
176 177 178 179 180 181 182 183 184 185 186 187 188
    if (indexPath.section == 0) {
        ProductDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"productDetailscell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.datas = self.entity;
        return cell;
        
    }else if (indexPath.section == 1)
    {
        goodsDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"goodsdetailscell" forIndexPath:indexPath];
        [cell.detailsImageView sd_setImageWithURL:[NSURL URLWithString:[self.goodsDetailsArray objectAtIndex_opple:indexPath.row]] placeholderImage:REPLACEIMAGE];
        return cell;
    }
    return nil;
曹云霄's avatar
曹云霄 committed
189 190
}

曹云霄's avatar
曹云霄 committed
191

曹云霄's avatar
曹云霄 committed
192 193
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
勾芒's avatar
勾芒 committed
194 195 196 197
    if (section == 0) {
        return 1;
    }
    return self.goodsDetailsArray.count;
曹云霄's avatar
曹云霄 committed
198 199
}

曹云霄's avatar
曹云霄 committed
200

曹云霄's avatar
曹云霄 committed
201 202
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
勾芒's avatar
勾芒 committed
203 204 205 206 207
    if (indexPath.section == 0) {
        
        return 170;
    }
    return ScreenWidth;
曹云霄's avatar
曹云霄 committed
208 209
}

曹云霄's avatar
曹云霄 committed
210

曹云霄's avatar
曹云霄 committed
211 212
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
勾芒's avatar
勾芒 committed
213
    return 2;
曹云霄's avatar
曹云霄 committed
214 215 216 217 218 219 220 221 222
}

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

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
曹云霄's avatar
曹云霄 committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
    
    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
244
            self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
曹云霄's avatar
曹云霄 committed
245 246 247 248 249 250 251 252 253 254 255
        }
            break;
        case 101://增加
        {
            if (goodsNumber >= [self.entity.number integerValue]) {
                
                //不能大于库存
                [self ErrorMBProgressView:@"超过库存"];
                return;
            }
            goodsNumber ++;
勾芒's avatar
勾芒 committed
256
            self.headerView.goodsNumber.text = [NSString stringWithFormat:@"%ld",(long)goodsNumber];
曹云霄's avatar
曹云霄 committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
        }
            break;
            
        default:
            break;
    }
}





#pragma mark -UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    NSLog(@"%@",textField.text);
    NSString *inputString = textField.text;
    if (![self isPureInt:inputString]) {
        
        [self ErrorMBProgressView:@"输入格式错误"];
        textField.text = @"1";
        return;
    }
    
    if ([inputString integerValue] > [self.entity.number integerValue]) {
        
        [self ErrorMBProgressView:@"超过库存数量"];
勾芒's avatar
勾芒 committed
284
        textField.text = [NSString stringWithFormat:@"%ld",(long)[self.entity.number integerValue]];
曹云霄's avatar
曹云霄 committed
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
        return;
    }
}

//
//@synthesize fid;
//@synthesize createName;
//@synthesize createBy;
//@synthesize createDate;
//@synthesize updateName;
//@synthesize updateBy;
//@synthesize updateDate;
//@synthesize sysOrgCode;
////@synthesize goodsId;
//@synthesize goodsCode;
//@synthesize goodsName;
//@synthesize goodsCover;
//@synthesize goodsSpec;
//@synthesize goodsBrand;
//@synthesize goodsSellerPrice;
//@synthesize goodsNum;
//@synthesize goodsPrice;
//@synthesize goodsTotalPrice;
//@synthesize consumerId;

勾芒's avatar
勾芒 committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
#pragma mark -开发加入购物车动画
- (void)StartAddShoppingCarAnimationWithimage:(UIImage *)image withStartpoint:(CGPoint)point
{
    //起点
    CGPoint startPoint = point;
    //终点
    CGPoint endPoint = SHARED_APPDELEGATE.shoppingCarPoint;
    //控点
    CGPoint controlPoint = CGPointMake(endPoint.x, startPoint.x);
    
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    imageView.layer.position = point;
    imageView.tag = 100;
    imageView.image = image;
    
    [self.view.layer addSublayer:imageView.layer];
    
    //创建关键帧
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.delegate = self;
    //动画时间
    animation.duration = 1;
    
    //当动画完成,停留到结束位置
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    
    //当方法名字遇到create,new,copy,retain,都需要管理内存
    CGMutablePathRef path = CGPathCreateMutable();
    //设置起点
    CGPathMoveToPoint(path, NULL, startPoint.x, startPoint.y);
    CGPathAddQuadCurveToPoint(path, NULL, controlPoint.x, controlPoint.y, endPoint.x,endPoint.y);
    
    //设置动画路径
    animation.path = path;
    
    //执行动画
    [imageView.layer addAnimation:animation forKey:nil];
    
    //释放路径
    CGPathRelease(path);
}

#pragma mark -完成加入购物车动画完成后回调
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    NSLog(@"动画完成了");
勾芒's avatar
勾芒 committed
358
   [[NSNotificationCenter defaultCenter]postNotificationName:@"ADDGOODSNUMBER" object:@([self.headerView.goodsNumber.text integerValue])];
勾芒's avatar
勾芒 committed
359 360 361
}


曹云霄's avatar
曹云霄 committed
362
#pragma mark -添加至购物车
勾芒's avatar
勾芒 committed
363
- (void)addGoodsShoppingbags:(UIButton *)button
曹云霄's avatar
曹云霄 committed
364
{
勾芒's avatar
勾芒 committed
365 366 367 368 369 370
    //判断是否有当前客户
    if (![Shoppersmanager manager].currentCustomer) {
        
        [self ErrorMBProgressView:@"必须设置当前客户"];
        return;
    }
勾芒's avatar
勾芒 committed
371
    [self StartAddShoppingCarAnimationWithimage:TCImage(@"欧") withStartpoint:[self.headerView convertPoint:[button center] toView:self.view.window]];
勾芒's avatar
勾芒 committed
372 373 374 375
    SaveShoppingCartRequest *shopCar = [[SaveShoppingCartRequest alloc]init];
    shopCar.consumerId = [[Customermanager manager] customerID];
    shopCar.goodsId = _goodsID;
    shopCar.count = [self.headerView.goodsNumber.text intValue];
曹云霄's avatar
曹云霄 committed
376 377
    [[NetworkRequestClassManager Manager] NetworkRequestWithURL:[NSString stringWithFormat:@"%@%@",ServerAddress,@"/shopcart/save"] WithRequestType:0 WithParameter:shopCar WithReturnValueBlock:^(id returnValue) {
    
勾芒's avatar
勾芒 committed
378 379
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            
勾芒's avatar
勾芒 committed
380
            NSLog(@"添加购物车成功");
勾芒's avatar
勾芒 committed
381 382 383 384
        }else
        {
            [self ErrorMBProgressView:returnValue[@"message"]];
        }
曹云霄's avatar
曹云霄 committed
385 386 387 388 389
        
    } WithErrorCodeBlock:^(id errorCodeValue) {
        
        
    } WithFailureBlock:^(id error) {
勾芒's avatar
勾芒 committed
390
       
勾芒's avatar
勾芒 committed
391 392
        [self ErrorMBProgressView:@"添加购物车失败"];
        
曹云霄's avatar
曹云霄 committed
393
    }];
曹云霄's avatar
曹云霄 committed
394 395
}

曹云霄's avatar
曹云霄 committed
396 397


曹云霄's avatar
曹云霄 committed
398 399 400 401 402 403 404
#pragma mark -判断是否为纯数字
- (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

曹云霄's avatar
曹云霄 committed
405 406


勾芒's avatar
勾芒 committed
407 408 409 410 411 412
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


曹云霄's avatar
曹云霄 committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end