// // MyOrderVC.m // jiaPei // // Created by apple on 16/5/27. // Copyright © 2016年 JCZ. All rights reserved. // #import "MyOrderVC.h" #import "STSegView.h" #import "DateView.h" #import "OrderCell.h" @interface MyOrderVC () { STSegView *seg; UIScrollView *scroll; UITableView *mainTableView; UIBarButtonItem *item; UIView *backView; UIButton *beginBtn; UIButton *endBtn; HolderView *holder; NSMutableArray *dataArray;; NSString *beginDate; NSString *endDate; NSInteger currentPage; NSString *orderState; //生成收款二维码 UIView *scanView; NSTimer *timer; NSString *payOrderId; NSString *studentId; NSString *studentOutId; NSString *reserveId; BOOL isAfterDelay; NSInteger timeI; } @end @implementation MyOrderVC - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; [self getMyOrderInfo]; } -(void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [LoadingView removeHUD]; } -(void)myInit { currentPage = 1; orderState = @"0"; beginDate = @""; endDate = @""; dataArray = [NSMutableArray array]; item = [[UIBarButtonItem alloc] initWithTitle:@"查找" style:UIBarButtonItemStyleDone target:self action:@selector(clickToSearch)]; [item setTintColor:defGreen]; [self.navigationItem setRightBarButtonItem:item]; CGFloat x,y,w,h; x = 0; y = kNavOffSet; w = kSize.width; h = 50; seg = [[STSegView alloc] initWithFrame:CGRectMake(x, y, w, h)]; //0 进行中 2待支付 1 待评价/投诉 3 交易完成 4 投诉中 5 待结算 [seg setTitles:@[@"计时中",@"待支付",@"待评价",@"已完成",@"已投诉",@"待结算"]]; seg.selectedIndex = 0; [self.view addSubview:seg]; y += h; h = kSize.height - y; mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(x, y, w, h) style:UITableViewStyleGrouped]; mainTableView.delegate = self; mainTableView.dataSource = self; mainTableView.rowHeight = 115; [self.view addSubview:mainTableView]; [seg click:^(NSString* sInd) { currentPage = 1; beginDate = @""; endDate = @""; orderState = sInd; [self getMyOrderInfo]; }]; holder = [[HolderView alloc] initWithFrame:mainTableView.frame]; [holder freshBlock:^{ currentPage = 1; [self getMyOrderInfo]; }]; [self.view addSubview:holder]; } -(void)clickToSearch { item.enabled = NO; backView = [[UIView alloc] initWithFrame:kFrame]; backView.backgroundColor = [UIColor colorWithWhite:.1 alpha:.5]; [self.view addSubview:backView]; CGFloat x, y, w, h, bd; x = 20; y = (kSize.height - 250)/2.0; w = kSize.width - 40; h = 250; UIView *searchView = [[UIView alloc] setxywh]; searchView.backgroundColor = backGroundColor; searchView.layer.masksToBounds = YES; searchView.layer.cornerRadius = 5; [backView addSubview:searchView]; x = y = bd = 15; w = searchView.width - 30; h = 50; NSArray *imgArray = @[@"beginDate",@"endDate"]; NSString *beginDateStr = @"考试通过开始时间"; NSString *endDateStr = @"考试通过结束时间"; if (beginDate.length > 0) { beginDateStr = beginDate; } if (endDate.length > 0) { endDateStr = endDate; } NSArray *titles = @[beginDateStr,endDateStr]; NSMutableArray *btns = [NSMutableArray arrayWithCapacity:3]; for (int i = 0; i < 2; i ++) { UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x + bd, 2*y + i*(h + bd), 20, 20)]; imageView.image = [[UIImage imageNamed:imgArray[i]] tint:defGreen]; [searchView addSubview:imageView]; UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y + i*(h + bd) , w, h)]; [button setTitle:titles[i] textColor:contentTextColor font:16 fotState:UIControlStateNormal]; button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft; button.titleEdgeInsets = UIEdgeInsetsMake(0, 3*bd, 0, 0); [button borderColor:lineColor width:2 cornorRadios:5]; [button target:self Tag:i]; [searchView addSubview:button]; [btns addObject:button]; } beginBtn = btns[0]; endBtn = btns[1]; y += 2*(h + bd); w = (w - 2*bd)/2.0; NSArray *titleArrays = @[@"取消",@"查询"]; for (int i = 0; i < 2; i ++) { UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x + i*(w + 2*bd), y , w, h)]; [button setTitle:titleArrays[i] textColor:defGreen font:16 fotState:UIControlStateNormal]; [button borderColor:lineColor width:2 cornorRadios:5]; [button target:self Tag:i + 2]; [searchView addSubview:button]; } } -(void)btnClick:(UIButton *)sender { switch (sender.tag) { case 0: { DateView *dateV = [[DateView alloc] init]; [dateV setStyle:0]; [dateV showWithComplete:^(NSString * result) { beginDate = result; [beginBtn setTitle:beginDate forState:UIControlStateNormal]; }]; } break; case 1: { DateView *dateV = [[DateView alloc] init]; [dateV setStyle:0]; [dateV showWithComplete:^(NSString * result) { endDate = result; [endBtn setTitle:endDate forState:UIControlStateNormal]; }]; } break; case 2: { if (backView) { [backView removeFromSuperview]; } item.enabled = YES; } break; case 3: { if (backView) { [backView removeFromSuperview]; } item.enabled = YES; currentPage = 1; [self getMyOrderInfo]; } break; default: break; } } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 1) { if (buttonIndex != alertView.cancelButtonIndex) { [self orderPayWithType:buttonIndex]; } } } #pragma mark tableView -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return dataArray.count; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 1; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { OrderCell *cell = [OrderCell cellForTabelView:tableView]; cell.skipVC = self; cell.type = orderState; [cell setDataDic:dataArray[indexPath.section]]; [cell payClickToBlock:^(NSDictionary *dic) { payOrderId = dic[@"RO_ID"]; studentId = dic[@"RO_USER_ID"]; studentOutId = dic[@"OUTID"]; reserveId = dic[@"RO_RESERVE_ID"]; //弹出选择框 微信 QQ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"请选择支付方式" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"支付宝支付",@"微信支付",@"本地支付", nil]; alert.tag = 1; [alert show]; }]; return cell; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 15)]; view.backgroundColor = lineColor; return view; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 15; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *view = [UIView new]; return view; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return .1; } -(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { CGPoint off = scrollView.contentOffset; if (scrollView.contentSize.height - off.y - scrollView.frame.size.height < 1) { [scrollView setContentOffset:CGPointMake(off.x, off.y - 10) animated:YES]; [self getMyOrderInfo]; } } #pragma mark 数据请求 -(void)getMyOrderInfo { [LoadingView showHUD]; if (![Util connectedToNetWork]) { return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"type" Value:@"2"]; [arr addPro:@"userId" Value:defUser.userDict[@"id"]]; [arr addPro:@"startTime" Value:beginDate]; [arr addPro:@"endTime" Value:endDate]; [arr addPro:@"status" Value:orderState]; [arr addPro:@"isPage" Value:@"1"]; [arr addPro:@"pageSize" Value:@"20"]; [arr addPro:@"currentPage" Value:[NSString stringWithFormat:@"%d",(int)currentPage]]; NSString *method = @"getMyReserveOrders"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); //执行同步并取得结果 if (currentPage == 1) { holder.hidden = NO; } //NSLog(@"我的订单-->%@-->%@",arr,root); if (!root) { ShowMsgFailed(); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } NSArray *array = root[@"body"]; if ([array count] < 1) { ShowMsg(@"已加载全部"); return; } if (currentPage == 1) { [dataArray removeAllObjects]; } [dataArray addObjectsFromArray:array]; currentPage += 1; holder.hidden = YES; [mainTableView reloadData]; }]; } //- (void)payReserveOrder { // [LoadingView showHUD]; // if (![Util connectedToNetWork]) { // showMsgUnconnect(); // return; // } // NSMutableArray *arr=[NSMutableArray array]; // // [arr addPro:@"userId" Value:studentId]; // [arr addPro:@"coachId" Value:defUser.userDict[@"id"]]; // [arr addPro:@"orderId" Value:payOrderId]; // // NSString *method = @"payReserveOrder"; // // [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { // if (!root) { // RemoveHUD(); // ShowMsg(@"操作失败,请重试!"); // return; // } // if ([root[@"code"] isEqualToString:@"1"]) { // RemoveHUD(); // ShowMsg(root[@"body"]); // return; // } // [self addStudentTrainOrder]; // }]; //} /// 生成建行支付订单 - (void)addStudentTrainOrder { [LoadingView showHUD]; if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; [arr addPro:@"orderId" Value:payOrderId]; NSString *method = @"addStudentTrainOrder"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { if (!root) { RemoveHUD(); ShowMsg(@"操作失败,请重试!"); return; } if ([root[@"code"] isEqualToString:@"1"]) { RemoveHUD(); ShowMsg(root[@"body"]); return; } [self getReserveSweepQrcode]; }]; } /// 获取付款码 - (void)getReserveSweepQrcode { NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"stuOutId" Value:studentOutId]; [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; [arr addPro:@"reserveId" Value:reserveId]; [arr addPro:@"orderId" Value:payOrderId]; NSString *method = @"getReserveSweepQrcode"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); if (!root) { ShowMsg(@"操作失败,请重试!"); return; } if ([root[@"code"] isEqualToString:@"1"] || [root[@"code"] isEqualToString:@"-1"]) { ShowMsg(root[@"body"]); return; } [self makeScanViewWithString:root[@"body"][@"qrCode"] withType:0]; // [self getMyOrderInfo]; }]; } - (void)getReserveOrderStatus { NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; [arr addPro:@"orderId" Value:payOrderId]; NSString *method = @"getReserveOrderStatus"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { if (!root) { ShowMsg(@"操作失败,请重试!"); return; } if ([root[@"code"] isEqualToString:@"1"] || [root[@"code"] isEqualToString:@"-1"]) { ShowMsg(root[@"body"]); return; } }]; } /** 订单支付 @param type 1为QQ 2为微信 3本地 */ - (void)orderPayWithType:(NSInteger)type { if (type == 3) { UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"本地支付" message:@"是否本地支付完毕?" preferredStyle:UIAlertControllerStyleAlert]; [alert addAction:[UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self addStudentTrainOrder]; }]]; [self presentViewController:alert animated:true completion:nil]; return; } [LoadingView showHUD]; if (![Util connectedToNetWork]) { return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"id" Value:payOrderId]; NSString *method = @"aliSweepPay"; if (type == 2) { method = @"wechatSweepPay"; } [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); if (!root) { ShowMsg(@"操作失败,请重试!"); return; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } if ([root[@"body"] length] < 1) { ShowMsg(@"数据错误,请重试!"); return; } [self makeScanViewWithString:root[@"body"] withType:type]; }]; } - (void)getOrderPayStatus { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSString* method = [NSString stringWithFormat:@"getOrderPayStatus-%@",payOrderId]; [jiaPeiManager requestGetAnythingWithURL:method completion:^(NSDictionary *dict) { if (!dict) { return; } if ([dict[@"code"] isEqualToString:@"1"] || [dict[@"code"] isEqualToString:@"3"]) { ShowMsg(dict[@"body"]); [self removeScanView]; return; } if ([dict[@"code"] isEqualToString:@"0"]) { //交易完成 ShowMsg(@"支付成功!"); [self removeScanView]; currentPage = 1; [self getMyOrderInfo]; return; } }]; } #pragma mark 生成二维码 -(void)makeScanViewWithString:(NSString *)scanString withType:(NSInteger)type { [self closeTimer]; CGFloat x,y,w,h; x = 20; w = kSize.width - 40; h = (w - 250)/2.0 + 300; y = (kSize.height - h)/2.0 - 30; scanView = [[UIView alloc] setxywh]; scanView.backgroundColor = [UIColor whiteColor]; [scanView borderColor:lineColor width:1 cornorRadios:5]; [self.view addSubview:scanView]; x = 0; y = 10; h = 30; UILabel *label = [[UILabel alloc] setxywh]; [label setText:@"请扫下图二维码进行付费" Font:Font16 TextColor:[UIColor orangeColor] Alignment:NSTextAlignmentCenter]; [scanView addSubview:label]; UIImageView *cancelImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cancel"]]; cancelImg.frame = CGRectMake(w - 25, 5, 20, 20); [scanView addSubview:cancelImg]; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(w - 50, 0, 50, 50); btn.backgroundColor = [UIColor clearColor]; [btn addTarget:self action:@selector(removeScanViewAndIsCanTime) forControlEvents:UIControlEventTouchUpInside]; [scanView addSubview:btn]; x = (w - 250)/2.0; y += h + 10; w = h = 250; UIImageView *scanImgView = [[UIImageView alloc] setxywh]; [scanView addSubview:scanImgView]; //二维码 UIImage *qrcode = [self createNonInterpolatedUIImageFormCIImage:[self createQRForString:scanString] withSize:250.0f]; if (type == 1) { qrcode = [self addIconToQRCodeImage:qrcode withIcon:[UIImage imageNamed:@"AliPay.png"] withIconSize:CGSizeMake(40, 40)]; }else if (type == 2){ qrcode = [self addIconToQRCodeImage:qrcode withIcon:[UIImage imageNamed:@"WechatPay.png"] withIconSize:CGSizeMake(40, 40)]; } scanImgView.image = qrcode; timeI = 0; timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeRun) userInfo:nil repeats:YES]; } - (void)timeRun { timeI ++; if (timeI%5 == 0) { [self getOrderPayStatus]; if (timeI == 1000) { timeI = 0; } } } - (void)removeScanViewAndIsCanTime { //不要立即关闭定时器 做30秒的延迟 if (scanView) { [scanView removeFromSuperview]; scanView = nil; } isAfterDelay = YES; [self performSelector:@selector(closeTimer) withObject:nil afterDelay:30]; } - (void)closeTimer { if (isAfterDelay == YES) { if (timer) { [timer invalidate]; timer = nil; } isAfterDelay = NO; } } - (void)removeScanView { if (timer) { [timer invalidate]; timer = nil; } if (scanView) { [scanView removeFromSuperview]; scanView = nil; } } - (CIImage *)createQRForString:(NSString *)qrString { NSData *stringData = [qrString dataUsingEncoding:NSUTF8StringEncoding]; // 创建filter CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"]; // 设置内容和纠错级别 [qrFilter setValue:stringData forKey:@"inputMessage"]; [qrFilter setValue:@"M" forKey:@"inputCorrectionLevel"]; // 返回CIImage return qrFilter.outputImage; } - (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size { CGRect extent = CGRectIntegral(image.extent); CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent)); // 创建bitmap; size_t width = CGRectGetWidth(extent) * scale; size_t height = CGRectGetHeight(extent) * scale; CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray(); CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone); CIContext *context = [CIContext contextWithOptions:nil]; CGImageRef bitmapImage = [context createCGImage:image fromRect:extent]; CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone); CGContextScaleCTM(bitmapRef, scale, scale); CGContextDrawImage(bitmapRef, extent, bitmapImage); // 保存bitmap到图片 CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef); CGColorSpaceRelease(cs); CGContextRelease(bitmapRef); CGImageRelease(bitmapImage); UIImage * newImage = (UIImage *)[UIImage imageWithCGImage:scaledImage]; CGImageRelease(scaledImage); return newImage; } -(UIImage *)addIconToQRCodeImage:(UIImage *)image withIcon:(UIImage *)icon withIconSize:(CGSize)iconSize { UIGraphicsBeginImageContext(image.size); //通过两张图片进行位置和大小的绘制,实现两张图片的合并;其实此原理做法也可以用于多张图片的合并 CGFloat widthOfImage = image.size.width; CGFloat heightOfImage = image.size.height; CGFloat widthOfIcon = iconSize.width; CGFloat heightOfIcon = iconSize.height; [image drawInRect:CGRectMake(0, 0, widthOfImage, heightOfImage)]; [icon drawInRect:CGRectMake((widthOfImage-widthOfIcon)/2, (heightOfImage-heightOfIcon)/2, widthOfIcon, heightOfIcon)]; UIImage *img = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return img; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end