// // ShopCartViewController.m // IBOSSmini // // Created by guan hong hou on 2017/5/18. // Copyright © 2017年 elongtian. All rights reserved. // #import "ShopCartViewController.h" #import "CartTableViewCell.h" #import "CartFrame.h" #import "ShopCartModel.h" #import "SubmitOrderVC.h" @interface ShopCartViewController () @end @implementation ShopCartViewController @synthesize vCustomTableView; #pragma mark - 公共函数 /** viewDidLoad函数 */ - (void)viewDidLoad { [super viewDidLoad]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; [self initUI]; _detailArr=[[NSMutableArray alloc] init]; _cartM=[ShopCartModel new]; _cartM.rootVC=self; [self reloadData]; } /** 修改:2017-9-25 适配机型 安全区视图发生变化 */ -(void)viewSafeAreaInsetsDidChange{ self.view.backgroundColor = [UIColor whiteColor]; vCustomTableView.frame = CGRectMake(0, 0, SCREENWIDTH, self.view.safeAreaLayoutGuide.layoutFrame.size.height - 50); _bottomView.frame=CGRectMake(0, self.view.safeAreaLayoutGuide.layoutFrame.size.height - 50, self.view.frame.size.width, 50); [super viewSafeAreaInsetsDidChange]; } #pragma mark - 委托函数 //文本编辑回調 -(void)textValueChange:(CartTableViewCell *)cell txtField:(UITextField*)txtfield { [_cartM updateCellSaleText:txtfield checkeId:cell.lblCartID.text]; } //商品删除成功回调函数 - (void)onDeleteFinish:(ASIDownManager *)sender { NSDictionary *dic = [sender.mWebStr JSONValue]; [self cancel]; if (dic && [dic isKindOfClass:[NSDictionary class]]) { int iStatus = [[dic objectForKey:@"Status"] intValue]; NSString *message=[dic objectForKey:@"Message"]; if (iStatus == 0) { NSArray * approvArr=[dic objectForKey:@"Result"]; if(approvArr!=nil&&approvArr.count>0) { [_detailArr removeAllObjects]; _detailArr= [[NSMutableArray alloc] initWithArray:approvArr]; [_cartM parseWorkFlowArr:_detailArr]; if([_cartM isCheckedAll]) { [self changeCheckALLSelect]; } else { [self changeCheckAllUnSelect]; } int orderCount=[self getOrderCount]; NSString *balance=[NSString stringWithFormat:@"去结算(%d)",orderCount]; [_btnBalance setTitle:balance forState:UIControlStateNormal]; [vCustomTableView reloadData]; } else{ _detailArr= [[NSMutableArray alloc] initWithArray:approvArr]; [_cartM parseWorkFlowArr:_detailArr]; vCustomTableView.backgroundView = nil; UIView *noDataView=[[UIView alloc]init]; noDataView.frame=vCustomTableView.bounds; UIImageView *nodataImgView=[[UIImageView alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16,noDataView.frame.size.height/2-16,32,32)]; [nodataImgView setImage:[UIImage imageNamed:@"icon_no_data"]]; [noDataView addSubview:nodataImgView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16-12,CGRectGetMaxY(nodataImgView.frame)+3,70, 25)]; label.font=[UIFont systemFontOfSize:NoDataFontOfSize]; label.text = @"无数据"; label.numberOfLines = 2; label.textColor = [UIColor lightGrayColor]; [noDataView addSubview:label]; vCustomTableView.backgroundView=noDataView; [vCustomTableView reloadData]; [_btnBalance setTitle:@"去结算(0)" forState:UIControlStateNormal]; } } else if(iStatus==ActionResultStatusAuthError ||iStatus==ActionResultStatusNoLogin ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){ [self showReLoginDialog:message]; return; } else { [self showAlertViewText:message]; } } } //商品删除失败回调函数 - (void)onDeleteFail:(ASIDownManager *)sender { [self cancel]; [self showAlertViewText:@"操作失败"]; } //购物车加载成功函数 - (void)onLoadFinish:(ASIDownManager *)sender { NSDictionary *dic = [sender.mWebStr JSONValue]; [self cancel]; if (dic && [dic isKindOfClass:[NSDictionary class]]) { int iStatus = [[dic objectForKey:@"Status"] intValue]; NSString *message=[dic objectForKey:@"Message"]; vCustomTableView.backgroundView=nil; if (iStatus == 0) { NSArray * approvArr=[dic objectForKey:@"Result"]; if(approvArr!=nil&&approvArr.count>0) { _detailArr= [[NSMutableArray alloc] initWithArray:approvArr]; [_cartM parseWorkFlowArr:_detailArr]; if([_cartM isCheckedAll]) { [self changeCheckALLSelect]; int orderCount=[self getOrderCount]; NSString *balance=[NSString stringWithFormat:@"去结算(%d)",orderCount]; [_btnBalance setTitle:balance forState:UIControlStateNormal]; } else { [_btnBalance setTitle:@"去结算(0)" forState:UIControlStateNormal]; [self changeCheckAllUnSelect]; } [self.vCustomTableView reloadData]; } else{ vCustomTableView.backgroundView = nil; UIView *noDataView=[[UIView alloc]init]; noDataView.frame=vCustomTableView.bounds; UIImageView *nodataImgView=[[UIImageView alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16,noDataView.frame.size.height/2-16,32,32)]; [nodataImgView setImage:[UIImage imageNamed:@"icon_no_data"]]; [noDataView addSubview:nodataImgView]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(noDataView.frame.size.width/2-16-12,CGRectGetMaxY(nodataImgView.frame)+3,70, 25)]; label.font=[UIFont systemFontOfSize:NoDataFontOfSize]; label.text = @"无数据"; label.numberOfLines = 2; label.textColor = [UIColor lightGrayColor]; [noDataView addSubview:label]; vCustomTableView.backgroundView =noDataView; [_btnBalance setTitle:@"去结算(0)" forState:UIControlStateNormal]; } } else if(iStatus==ActionResultStatusAuthError ||iStatus==ActionResultStatusNoLogin ||iStatus==ActionResultStatusLogined ||iStatus == ActionResultStatusLoginedInvalid){ [self showReLoginDialog:message]; return; } else { [self showAlertViewText:message]; } } } //购物车加载失败函数 - (void)onLoadFail:(ASIDownManager *)sender { [self cancel]; [self showAlertViewText:@"加载失败"]; } //屏幕触摸回调函数 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } // scrollview回调函数 - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ [self.view endEditing:YES]; } //checkbox点击回调函数 -(void) btnCheckPressed:(CartTableViewCell *)cell { BOOL checked = [_cartM updateCellChecked:cell.cartId]; [cell setCheckBackground:checked]; [self changeCheckAllState]; } //tableview cell的高度 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { CartFrame *cartFrame=_cartM.workflowArr[indexPath.row]; return cartFrame.cellHeight; } //tableview的行数 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _detailArr.count; } //获取tableviewcell - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"ShopCartCell"; CartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; cell=[[CartTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; cell.selectionStyle=UITableViewCellSelectionStyleNone; CartFrame *cartFrame=_cartM.workflowArr[indexPath.row]; [cell parseCartInfo:cartFrame]; cell.delegate=self; cell.cellIndex=(int)indexPath.row; BOOL checked = cartFrame.cartModel.isChecked; [cell setCheckBackground:checked]; return cell; } //弹出消息回调 -(void)alertMessage:(NSString*)message{ [self showAlertViewText:message]; return; } //刷新数据 -(void)refreshData{ if(_detailArr!=nil){ [_detailArr removeAllObjects]; [vCustomTableView reloadData]; } [self reloadData]; } #pragma mark - 私有函数 //初始化UI -(void)initUI{ _detailArr=[[NSMutableArray alloc] init]; self.navigationItem.title=@"购物车"; vCustomTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-50)]; vCustomTableView.autoresizingMask = UIViewAutoresizingFlexibleHeight; vCustomTableView.backgroundColor = [UIColor whiteColor]; vCustomTableView.separatorStyle=UITableViewCellSeparatorStyleNone; vCustomTableView.delegate = self; vCustomTableView.dataSource=self; [self.view addSubview:vCustomTableView]; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageNamed:@"icon_back.png"] forState:UIControlStateNormal]; [button addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside]; button.frame = CGRectMake(0, 0, 15, 18); UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithCustomView:button]; self.navigationItem.leftBarButtonItem = menuButton; //保存 _bottomView=[UIView new]; _bottomView.frame=CGRectMake(0, Screen_Height-110, self.view.frame.size.width, 50); [self.view addSubview:_bottomView]; _btnCheckAll=[UIButton buttonWithType:UIButtonTypeCustom]; _btnCheckAll.frame=CGRectMake(15,15, 25, 25); [_btnCheckAll addTarget:self action:@selector(btnCheckall) forControlEvents:UIControlEventTouchUpInside]; [_bottomView addSubview:_btnCheckAll]; UILabel *lblall=[UILabel new]; lblall.frame=CGRectMake(CGRectGetMaxX(_btnCheckAll.frame)+3,15, 60, 25); lblall.text=@"全选"; [_bottomView addSubview:lblall]; //提交结算 _btnBalance=[UIButton buttonWithType:UIButtonTypeCustom]; [_btnBalance setTitle:@"去结算" forState:UIControlStateNormal]; [_btnBalance setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; _btnBalance.frame=CGRectMake(Screen_Width-114,0, 114,50); [_btnBalance setBackgroundColor:[UIColor colorWithRed:189.0/255.0 green:0 blue:7.0/255.0 alpha:1]]; [_btnBalance addTarget:self action:@selector(submitOrder) forControlEvents:UIControlEventTouchUpInside]; [_bottomView addSubview:_btnBalance]; [self changeCheckALLSelect]; } //结算函数 -(void)submitOrder { [self.view endEditing:YES]; if(![_cartM isSelectAtLeastOne]) { [self showAlertViewText:@"至少选择一种商品"]; return; } if(![_cartM checkFormat]) { [self showAlertViewText:@"每件商品的数量不能为0"]; return; } [self gotoOrder]; } //全部选中 -(void)changeCheckALLSelect { [_btnCheckAll setBackgroundImage:[UIImage imageNamed:@"order_checked"] forState:UIControlStateNormal]; } //全部未选中 -(void)changeCheckAllUnSelect { [_btnCheckAll setBackgroundImage:[UIImage imageNamed:@"order_unchecked"] forState:UIControlStateNormal]; } /** * 返回函数 */ -(void)goBack { [self.navigationController popViewControllerAnimated:YES]; } //到提交订单函数 -(void)gotoOrder { self.hidesBottomBarWhenPushed=YES; SubmitOrderVC *detailVC=[[SubmitOrderVC alloc] init]; detailVC.cartModel=_cartM; detailVC.detailArr=[_cartM checkedOriginalArr]; detailVC.refreshDelegate=self; [self.navigationController pushViewController:detailVC animated:YES]; } /** 键盘弹出 @param note <#note description#> */ -(void)keyboardWillShow:(NSNotification *)note { CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; vCustomTableView.contentInset = UIEdgeInsetsMake(0, 0, keyBoardRect.size.height, 0); } /** 键盘隐藏 @param note <#note description#> */ -(void)keyboardWillHide:(NSNotification *)note { vCustomTableView.contentInset = UIEdgeInsetsZero; } //删除订单 -(void)deleteOrderItem:(ShopCartItemModel*)cartItem{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"确定要删除吗?" preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { _mDownManager = [[ASIDownManager alloc] init]; _mDownManager.delegate = self; _mDownManager.OnImageDown = @selector(onDeleteFinish:); _mDownManager.OnImageFail = @selector(onDeleteFail:); [self startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"DeleteOrderShoppingCartIphone" forKey:@"Action"]; [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [dict setObject:cartItem.cartId forKey:@"CartID"]; [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil]; }]; UIColor *cancelColor=[UIColor blackColor]; UIColor *sureColor=[UIColor redColor]; [cancelAction setValue:cancelColor forKey:@"titleTextColor"]; [otherAction setValue:sureColor forKey:@"titleTextColor"]; [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [self presentViewController:alertController animated:YES completion:nil]; } //加载订单数据函数 -(void)reloadData { self.mDownManager = [[ASIDownManager alloc] init]; _mDownManager.delegate = self; _mDownManager.OnImageDown = @selector(onLoadFinish:); _mDownManager.OnImageFail = @selector(onLoadFail:); [self startLoading]; NSString *urlStr = ServerURL; NSMutableDictionary *dict = [NSMutableDictionary dictionary]; [dict setObject:@"GetShoppingCartListIphone" forKey:@"Action"]; [dict setObject:[NSString stringWithFormat:@"%@",kkAccountCode]forKey:@"AccountCode"]; [dict setObject:kkUserCode forKey:@"UserCode"]; [dict setObject:kkUserPwd forKey:@"UserPassword"]; [dict setObject:kkSessionKey forKey:@"SessionKey"]; [_mDownManager postHttpRequest:urlStr dic:dict path:nil fileName:nil];} //获得订单数 -(int) getOrderCount{ NSMutableArray *orderArray= _cartM.workflowArr; int count=0; for(CartFrame *frame in orderArray){ if(frame.cartModel.isChecked){ ++count; } } return count; } //改变订单选中状态 -(void)changeCheckAllState { if([_cartM isCheckedAll]) { [self changeCheckALLSelect]; } else { [self changeCheckAllUnSelect]; } long orderCount= [self getOrderCount]; NSString *balance=[NSString stringWithFormat:@"去结算(%ld)",orderCount]; [_btnBalance setTitle:balance forState:UIControlStateNormal]; } //选中或反选所有订单 -(void)btnCheckall { [_cartM updateAllCellCheck]; [vCustomTableView reloadData]; [self changeCheckAllState]; } //进度条取消 - (void)cancel { [self stopLoading]; } @end