// // FKAllOrderController.m // FirstLink // // Created by ascii on 15/5/29. // Copyright (c) 2015年 FirstLink. All rights reserved. // #import "FKPersonOrderController.h" #import "FKShareListController.h" #import "WebViewController.h" #import "FKPersonOrderRequest.h" #import "FKPersonOrderReform.h" #import "SubmitPaymentItem.h" #import "FKOrderPaymentItem.h" #import "FKShareTargetItem.h" #import "FKOrderSupplierItem.h" #import "FKOrderGoodItem.h" #import "FKPersonOrderStatusCell.h" #import "FKPersonOrderCountryCell.h" #import "FKPersonOrderProductCell.h" #import "FKPersonOrderSummaryCell.h" #import "UIExtendAlertView.h" #import "FKShareView.h" #import "FLStringHelper.h" #import "FLThirdPayHelper.h" #import "FLShareHelper.h" #import "FLImageHelper.h" #import "FLControllerHelper.h" #import #import "FKShareListRequest.h" #import "FKShareListReform.h" @interface FKPersonOrderController () @property (nonatomic, weak ) id delegate; @property (nonatomic, assign) FKPersonOrderClassify classify; @property (nonatomic, strong) FKPersonOrderViewModel *viewModel; @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, assign) NSInteger shareTargetIndex; @end static NSString * const FKPersonOrderCancelAction = @"FKPersonOrderCancelAction"; static NSString * const FKPersonOrderDeletelAction = @"FKPersonOrderDeletelAction"; @implementation FKPersonOrderController - (instancetype)initWithDelegate:(id)delegate orderClassify:(FKPersonOrderClassify)classify viewModel:(FKPersonOrderViewModel *)viewModel { self = [super init]; if (self) { _delegate = delegate; _classify = classify; _viewModel = viewModel; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.right.top.bottom.equalTo(self.view); }]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. [self configTableView]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self requestTotalCountOfPage]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #pragma mark - Request - (void)refreshControl:(FKRefreshControl *)refreshControl didEngageRefreshDirection:(RefreshDirection)direction { // [self.hudView show:YES]; if (direction==RefreshDirectionTop) { [FKPersonOrderRequest requestItems:FKPersonOrderRequestNew startRow:@"0" pageSize:[self.viewModel getPageRowCount:self.classify] keyword:self.viewModel.searchKeyword tradeStatus:[self.viewModel getRequestTradeStatus:self.classify] deleagate:self]; } else if (direction==RefreshDirectionBottom) { [FKPersonOrderRequest requestItems:FKPersonOrderRequestNextPage startRow:[self.viewModel getStartRow:self.classify] pageSize:[self.viewModel getPageRowCount:self.classify] keyword:self.viewModel.searchKeyword tradeStatus:[self.viewModel getRequestTradeStatus:self.classify] deleagate:self]; } } - (void)requestTotalCountOfPage { [self.hudView show:YES]; [FKPersonOrderRequest requestItems:FKPersonOrderRequestNew startRow:@"0" pageSize:[self.viewModel getMaxRowCount:self.classify] keyword:self.viewModel.searchKeyword tradeStatus:[self.viewModel getRequestTradeStatus:self.classify] deleagate:self]; } /* #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. } */ #pragma mark - Response - (void)networkDidSuccessResponse:(NSDictionary *)response identify:(int)identify header:(MSGHeader *)header { WeakSelf(weakSelf); dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.hudView hide:YES]; [weakSelf.hudView removeFromSuperview]; weakSelf.hudView = nil; }); [self finishLoadingData]; [self.statusView removeFromSuperview]; if ([header.code intValue] == RESPONSE_MSG_NORMAL) { if (identify == FKPersonOrderRequestShareSingle){ // 分享 FKShareTargetItem *item = [FKShareListReform parserShareListWithDict:response].firstObject; if (item){ FKShareView *shareView = [[FKShareView alloc]init]; [shareView showInView:self.view.window actionBlock:^(kShareViewActionType type, FKShareView *shareView) { [weakSelf shareSingleGoodWithItem:item type:type shareView:shareView]; }]; } return; } NSArray *orders = [FKPersonOrderReform parseOrderItems:response]; if (identify == FKPersonOrderRequestNew) { [self.viewModel resetPersonOrders:orders classify:_classify]; } if (identify == FKPersonOrderRequestNextPage) { [self.viewModel appendPersonOrders:orders classify:_classify]; } if (identify == FKPersonOrderRequestCancelOrder || identify == FKPersonOrderRequestDeleteOrder) { [self requestTotalCountOfPage]; if ([self.delegate respondsToSelector:@selector(orderControllerDidNeedRefreshBadge)]) { [self.delegate orderControllerDidNeedRefreshBadge]; } return; } [self.tableView reloadData]; // 显示无内容提示 if ([self.viewModel numberOfSectionsInTableView:_classify] == 0) { [self showStatusTipInView:self.tableView image:[UIImage imageNamed:@"StatusNoOrderIcon"] title:@"没有发现订单哦"]; self.refreshControl.bottomEnabled = NO; } else { [self.statusView removeFromSuperview]; } self.refreshControl.bottomEnabled = orders.count > 0 ? YES : NO; } else { [FLProgressHUDHelper showText:header.msg inView:self.view]; } } - (void)networkDidReceiveError:(NSError *)error identify:(int)identify header:(MSGHeader *)header { WeakSelf(weakSelf); dispatch_async(dispatch_get_main_queue(), ^{ [weakSelf.hudView hide:YES]; [weakSelf.hudView removeFromSuperview]; weakSelf.hudView = nil; }); [self finishLoadingData]; [FLProgressHUDHelper showText:header.msg inView:self.view]; } #pragma mark - Table View Delegate - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.viewModel numberOfSectionsInTableView:_classify]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [self.viewModel numberOfRowsInSection:section classify:_classify]; } - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 10; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return CGFLOAT_MIN; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return [self.viewModel heightAtIndexPath:indexPath classify:_classify]; } - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *identify = [self.viewModel cellIdentifyAtIndexPath:indexPath classify:_classify]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify]; cell.selectionStyle = UITableViewCellSelectionStyleNone; if ([cell respondsToSelector:@selector(configPersonOrderCell:classify:viewModel:)]) { [cell configPersonOrderCell:indexPath classify:_classify viewModel:self.viewModel]; } if ([cell isKindOfClass:[FKPersonOrderSummaryCell class]]) { [self configCellAction:(FKPersonOrderSummaryCell*)cell indexPath:indexPath]; } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:indexPath.section classify:_classify]; if (item&& self.delegate && [self.delegate respondsToSelector:@selector(orderController:didClickOrderDetailAction:orderType:)]) { [self.delegate orderController:self didClickOrderDetailAction:item.paymentID orderType:kOrderTypeProduct]; } } #pragma mark - Alert View Delegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 1) { if ([alertView isKindOfClass:[UIExtendAlertView class]]) { UIExtendAlertView *exAlertView = (UIExtendAlertView*)alertView; NSInteger index = alertView.tag; FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:index classify:_classify]; if (item) { if ([exAlertView.userInfo[@"type"] isEqualToString:FKPersonOrderCancelAction]) { [FKPersonOrderRequest requestCancelOrder:FKPersonOrderRequestCancelOrder orderID:item.paymentID deleagate:self]; } if ([exAlertView.userInfo[@"type"] isEqualToString:FKPersonOrderDeletelAction]) { [FKPersonOrderRequest requestDeleteOrder:FKPersonOrderRequestDeleteOrder orderID:item.paymentID deleagate:self]; } } } } } #pragma mark - Action - (IBAction)clickCancelOrderAction:(UIButton*)sender { NSInteger index = sender.tag; FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:index classify:_classify]; if (item) { UIExtendAlertView *alert = [[UIExtendAlertView alloc] initWithTitle:nil message:@"确认取消该订单?" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil]; alert.delegate = self; alert.userInfo = @{@"type": FKPersonOrderCancelAction}; alert.tag = index; [alert show]; } } - (IBAction)clickDeleteOrderAction:(UIButton*)sender { NSInteger index = sender.tag; FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:index classify:_classify]; if (item) { UIExtendAlertView *alert = [[UIExtendAlertView alloc] initWithTitle:nil message:@"确认删除该订单?" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil]; alert.delegate = self; alert.userInfo = @{@"type": FKPersonOrderDeletelAction}; alert.tag = index; [alert show]; } } - (IBAction)clickPaymentAction:(UIButton*)sender { FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:sender.tag classify:_classify]; [self pushOrderPayController:item]; } - (void)pushOrderPayController:(FKOrderPaymentItem*)item { if (item) { SubmitPaymentItem *paymentItem = [SubmitPaymentItem new]; paymentItem.orderID = item.paymentID; paymentItem.successController = FKOrderPaymentSuccessControllerRedpacket; if ([self.delegate respondsToSelector:@selector(orderController:didClickPaymentAction:orderType:)]) { [self.delegate orderController:self didClickPaymentAction:paymentItem orderType:kOrderTypeProduct]; } } } - (void)pushWebViewControllerWithUrl:(NSString *)url { WebViewController *newViewController = [[FLControllerHelper currentStoryBoard] instantiateViewControllerWithIdentifier:@"WebViewController"]; newViewController.url = url; newViewController.hidesBottomBarWhenPushed = YES; [self pushController:newViewController]; } - (void)clickUploadIDCardAction:(UIButton *)sender { FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:sender.tag classify:_classify]; if (item.paymentID.length > 0) { if ([self.delegate respondsToSelector:@selector(orderController:didClickUploadIDCardAction:)]) { [self.delegate orderController:self didClickUploadIDCardAction:item.paymentID]; } } } - (void)clickShareAction:(UIButton *)sender{ FKOrderPaymentItem *item = [self.viewModel paymentItemAtIndex:sender.tag classify:_classify]; self.shareTargetIndex = sender.tag; if ([item hasOnlyOneGood]){ // 直接弹出分享 [self.hudView show:YES]; [FKShareListRequest requestShareList:FKPersonOrderRequestShareSingle orderID:item.paymentID deleagate:self]; }else{ FKShareListController *controller = [[FKShareListController alloc]init]; controller.orderId = item.paymentID; [self pushController:controller]; } } #pragma mark - Method - (void)pushController:(UIViewController *)controller{ if (!controller) return; if ([self.delegate respondsToSelector:@selector(orderController:pushController:)]) { [self.delegate orderController:self pushController:controller]; } } - (void)shareSingleGoodWithItem:(FKShareTargetItem *)item type:(kShareViewActionType)type shareView:(FKShareView *)shareView{ switch (type) { case kShareViewActionTypeQuestion:{ NSString *qsURL = [NSString stringWithFormat:@"%@/support/about-points.html", [[FKServerUtil sharedInstance] webServer]]; WeakSelf(weakSelf); [shareView dismissWithCompletion:^(BOOL finish) { [weakSelf pushWebViewControllerWithUrl:qsURL]; }]; } break; case kShareViewActionTypeFriend:{ UIImage *image = [self getShareImgWithSize:CGSizeMake(SHARE_IMAGE_LENGTH, SHARE_IMAGE_LENGTH)]; [FLShareHelper shareToWeixin:item.shareDesc description:item.title thumbImage:image webURL:item.shareUrl scene:WeixinSceneFriend]; } break; case kShareViewActionTypeCircle:{ UIImage *image = [self getShareImgWithSize:CGSizeMake(SHARE_IMAGE_LENGTH, SHARE_IMAGE_LENGTH)]; [FLShareHelper shareToWeixin:item.shareDesc description:item.title thumbImage:image webURL:item.shareUrl scene:WeixinSceneCicle]; } break; case kShareViewActionTypeWeibo:{ UIImage *image = [self getShareImgWithSize:CGSizeMake(SHARE_IMAGE_LENGTH_WEIBO, SHARE_IMAGE_LENGTH_WEIBO)]; [FLShareHelper shareToWeiboWithTitle:item.shareDesc content:item.title image:image urlString:item.shareUrl tagID:item.itemID]; } break; case kShareViewActionTypeQQzone:{ [FLShareHelper shareToQQZoneWithTitle:item.title content:item.shareDesc imgUrl:item.picUrl urlString:item.shareUrl]; } break; default: break; } } - (UIImage *)getShareImgWithSize:(CGSize)size{ UIImage *image = nil; if (!image){ for (FKPersonOrderProductCell *proCell in self.tableView.visibleCells) { if ([proCell isKindOfClass:[FKPersonOrderProductCell class]] && proCell.tag == self.shareTargetIndex){ image = proCell.productImgView.image; break; } } } if (image) { image = [FLImageHelper generateThumbnailFromCenter:image frame:size]; } else { image = [FLImageHelper generateThumbnailFromCenter:[FLImageHelper getAppIcon] frame:size]; } return image; } - (void)configCellAction:(FKPersonOrderSummaryCell *)cell indexPath:(NSIndexPath *)indexPath { cell.cancelButton.tag = indexPath.section; cell.paymentButton.tag = indexPath.section; cell.deleteButton.tag = indexPath.section; cell.uploadIDCardButton.tag = indexPath.section; cell.shareButton.tag = indexPath.section; [cell.cancelButton addTarget:self action:@selector(clickCancelOrderAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.paymentButton addTarget:self action:@selector(clickPaymentAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.deleteButton addTarget:self action:@selector(clickDeleteOrderAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.uploadIDCardButton addTarget:self action:@selector(clickUploadIDCardAction:) forControlEvents:UIControlEventTouchUpInside]; [cell.shareButton addTarget:self action:@selector(clickShareAction:) forControlEvents:UIControlEventTouchUpInside]; } - (void)configTableView { [self initRefreshControlWithTableView:self.tableView]; [self.tableView registerClass:[FKPersonOrderStatusCell class] forCellReuseIdentifier:FKPersonOrderStatusCellIdentify]; [self.tableView registerClass:[FKPersonOrderCountryCell class] forCellReuseIdentifier:FKPersonOrderCountryCellIdentify]; [self.tableView registerClass:[FKPersonOrderProductCell class] forCellReuseIdentifier:FKPersonOrderProductCellIdentify]; [self.tableView registerClass:[FKPersonOrderSummaryCell class] forCellReuseIdentifier:FKPersonOrderSummaryCellIdentify]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; } #pragma mark - property - (UITableView *)tableView { if (!_tableView) { _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped]; _tableView.dataSource = self; _tableView.delegate = self; _tableView.separatorStyle = UITableViewCellSeparatorStyleNone; _tableView.backgroundColor = UIColorFromRGB(0xf4f4f4); } return _tableView; } @end