// // RQTimingView.m // XinShouJiaDao // // Created by 张嵘 on 2021/7/13. // Copyright © 2021 JCZ. All rights reserved. // #import "RQTimingView.h" #import "RQConfigureManager.h" #import "RQControllerHelper.h" #import "FLAnimatedImageView.h" #import "FLAnimatedImage.h" /// 显示状态的key static NSString * const RQTimingViewStatusKey = @"RQTimingViewStatusKey"; @interface RQTimingView () /// closeBtn @property (nonatomic, readwrite, weak) UIButton *closeBtn; /// 开始点 @property (nonatomic, readwrite, assign) CGPoint startPoint; /// 背景图片 @property (nonatomic, readwrite, strong) FLAnimatedImageView *bgImageView; /// GIF图片 @property (nonatomic, readwrite, strong) FLAnimatedImage *animatedImage; /// Timer @property (nonatomic, readwrite, strong) NSTimer *timer; /// Count @property (nonatomic, readwrite, assign) NSUInteger seconds; /// Label @property (nonatomic, readwrite, strong) UILabel *timeLabel; /// Label @property (nonatomic, readwrite, strong) NSMutableArray *titleArr; /// 是否在计时 @property (nonatomic, readwrite, assign , getter = isTiming) BOOL timing; /// 是否在动画 @property (nonatomic, readwrite, assign , getter = isAnimated) BOOL animated; @property (nonatomic, readwrite, copy) NSString *secondString; @end @implementation RQTimingView @def_singleton(RQTimingView); #pragma mark - SystemMethods - (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.userInteractionEnabled = YES; self.backgroundColor = RQ_MAIN_COLOR; NSBundle *bundle = [NSBundle mainBundle]; NSString *resourcePath = [bundle resourcePath]; NSString *filePath = [resourcePath stringByAppendingPathComponent:@"1.gif"]; self.bgImageView.image = [UIImage imageWithContentsOfFile:filePath]; [self addSubview:self.bgImageView]; [self addSubview:self.titleLabel]; /// 添加tap手势 [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapAction:)]]; UIWindow *window = [UIApplication sharedApplication].keyWindow; [window addSubview:self]; self.rq_x = [UIScreen mainScreen].bounds.size.width - self.rq_width - 20; self.rq_y = RQ_APPLICATION_TAB_BAR_HEIGHT + RQ_APPLICATION_NAV_BAR_HEIGHT + 44.f + 54.f; self.hidden = YES; } return self; } - (void)dealloc { [self.timer invalidate]; self.timer = nil; } - (void)setHide:(BOOL)hide { if (self.isAnimated) return; [[NSUserDefaults standardUserDefaults] setBool:hide forKey:RQTimingViewStatusKey]; [[NSUserDefaults standardUserDefaults] synchronize]; hide?[self close]:[self open]; } - (BOOL)isHide{ BOOL temp = [[NSUserDefaults standardUserDefaults] boolForKey:RQTimingViewStatusKey]; NSLog(@"++++ Touch View Is Hide %d ++++" , temp); return temp; } - (void)open{ /// 动画开始,禁止交互 self.userInteractionEnabled = NO; self.animated = YES; self.hidden = NO; self.transform = CGAffineTransformIdentity; self.transform = CGAffineTransformMakeScale(.2, .2); [UIView animateWithDuration:.25f delay:0 usingSpringWithDamping:.3f initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{ self.transform = CGAffineTransformMakeScale(1.0, 1.0); } completion:^(BOOL finished) { self.transform = CGAffineTransformIdentity; /// 动画结束,允许交互 self.userInteractionEnabled = YES; self.animated = NO; }]; } - (void)close{ /// 动画开始,禁止交互 self.animated = YES; self.userInteractionEnabled = NO; [UIView animateWithDuration:.25f animations:^{ self.transform = CGAffineTransformMakeScale(.2, .2); } completion:^(BOOL finished) { self.transform = CGAffineTransformIdentity; self.hidden = YES; /// 动画结束,允许交互 self.userInteractionEnabled = YES; self.animated = NO; }]; } #pragma mark - Action - (void)_tapAction:(UITapGestureRecognizer *)tapGr { NSLog(@"%@",RQControllerHelper.currentViewController); BOOL isTakePhoto = [RQControllerHelper.currentViewController.className isEqualToString:@"CAMImagePickerCameraViewController"]; if (isTakePhoto) { return; } LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:nil cancelButtonTitle:@"取消" didDismiss:^(LCActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) { @weakify(self) switch (buttonIndex) { case 1: { if (_timer) { if (self.isTiming) { /// 暂停计时 [self pauseTiming]; } else { /// 继续计时 [self continueTiming]; } } else { //开始计时 if (defUser.isycbd == 1) { [self startTimingWithResultBlock:^(BOOL isSuccess) { @strongify(self) if (isSuccess) { [self beginTiming]; } }]; } else { [self beginTime]; } } break; } case 2: { if (defUser.isycbd == 1) { [self stopTimingWithResultBlock:^(BOOL isSuccessed) { @strongify(self) if (isSuccessed) { [self endTiming]; } }]; } else { [self stopTiming]; } break; } default: break; } } otherButtonTitleArray:self.titleArr.copy]; [actionSheet addSubview:self.timeLabel]; UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, RQ_SCREEN_WIDTH, 100)]; label.backgroundColor = [UIColor clearColor]; label.text = @"注:退出当前页面将结束计时,学时详情在“个人-我的学时”查看"; label.textColor = UIColor.whiteColor; label.font = [UIFont systemFontOfSize:20 weight:UIFontWeightMedium]; label.numberOfLines = 0; label.textAlignment = NSTextAlignmentCenter; [actionSheet addSubview:label]; [actionSheet show]; } #pragma mark - PrivateMethods - (void)rq_timerValueChanged { @weakify(self) self.seconds++; [self setSecondString:[NSString stringWithFormat:@"%d",(int)self.seconds]]; if (defUser.isycbd == 1) { // NSString *pxkm = defUser.userDict[@"pxjd"]; NSString *pxkm = myDelegate.subject; if (pxkm.length < 1) { pxkm = @"1"; } if (([pxkm isEqualToString:@"2"] || [pxkm isEqualToString:@"3"])) { pxkm = @"4"; } int remainder1 = (int)(self.seconds)%900; if (remainder1 == 0) { [self.timer setFireDate:[NSDate distantFuture]]; SCLAlertView *alert = [[SCLAlertView alloc] initWithNewWindow]; alert.backgroundViewColor = backGroundColor; alert.customViewColor = RQ_MAIN_COLOR; [alert addButton:@"立即开始 " actionBlock:^(void) { dispatch_async(dispatch_get_main_queue(), ^{ @strongify(self) /// 1.本地活体检测 [RQ_CHECKBODY_MANAGER beginCheckBodyWithCheckNum:defUser.ycbdFaceCount completeBlock:^(BOOL success, NSDictionary * _Nullable dic) { @strongify(self) if (success) { /// 2.线上活体检测 [RQ_RemoteTheory_MANAGER edufaceTofaceWithPhotoStr:dic[@"normalImg"] loginFlagType:LoginFlagType_Process resultBlock:^(BOOL isSuccess, id dict) { @strongify(self) if (isSuccess) { NSString *timeStr = [self getTimes:self.seconds]; /// 3.2上传过程照片 if (dict) { NSDictionary *myDict = dict; NSString *imageUrlStr = myDict[@"body"]; if (RQStringIsEmpty(imageUrlStr)) { ShowMsg(@"路径为空"); } [RQ_RemoteTheory_MANAGER uploadEduPicWithClassidStr:self.classIdStr timeStr:timeStr actionPhotoStr:imageUrlStr loginFlagType:LoginFlagType_Process resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull uploadEduPicDict) { @strongify(self) if (isSuccess) { [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"过程验证成功" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"确定" otherButtonTitles:nil otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) { @strongify(self) [self.timer setFireDate:[NSDate distantPast]]; }]; } else { self.seconds--; [self setSecondString:[NSString stringWithFormat:@"%d",(int)self.seconds]]; [self.timer setFireDate:[NSDate distantPast]]; } }]; } else { self.seconds--; [self setSecondString:[NSString stringWithFormat:@"%d",(int)self.seconds]]; [self.timer setFireDate:[NSDate distantPast]]; } } else { self.seconds--; [self setSecondString:[NSString stringWithFormat:@"%d",(int)self.seconds]]; [self.timer setFireDate:[NSDate distantPast]]; } }]; } else { self.seconds--; [self setSecondString:[NSString stringWithFormat:@"%d",(int)self.seconds]]; [self.timer setFireDate:[NSDate distantPast]]; } }]; }); }]; [alert addTimerToButtonIndex:0 reverse:YES]; [alert showNotice:@"温馨提示" subTitle:@"即将开始活体检测,请做好准备" closeButtonTitle:nil duration:5.0f]; } } int hour = 0; int min = 0; int second = 0; int remainder = (int)self.seconds%60; if (remainder == 0) { //每1分钟保存一次 [self saveTrain]; } hour = (int)self.seconds / 3600; min = (int)self.seconds % 3600 / 60; second = (int)self.seconds % 3600 % 60; NSString *min_s; NSString *second_s; if (min<10) { min_s=[NSString stringWithFormat:@"0%d",min]; }else{ min_s=[NSString stringWithFormat:@"%d",min]; } if (second<10) { second_s=[NSString stringWithFormat:@"0%d",second]; }else{ second_s=[NSString stringWithFormat:@"%d",second]; } //更新UI上的时间 self.timeLabel.text = [NSString stringWithFormat:@"0%d:%@:%@",hour,min_s,second_s]; self.titleLabel.text = self.timeLabel.text; } #pragma mark - PublicMethods // 开始计时 - (void)beginTiming { [self.timer fire]; self.timing = YES; [_titleArr replaceObjectAtIndex:0 withObject:@"暂停计时"]; self.bgImageView.animatedImage = self.animatedImage; } // 暂停计时 - (void)pauseTiming { if (_timer) { [_timer setFireDate:[NSDate distantFuture]]; self.timing = NO; [_titleArr replaceObjectAtIndex:0 withObject:@"继续计时"]; [self.bgImageView stopAnimating]; } } // 继续计时 - (void)continueTiming { if (_timer) { [_timer setFireDate:[NSDate distantPast]]; self.timing = YES; [_titleArr replaceObjectAtIndex:0 withObject:@"暂停计时"]; [self.bgImageView startAnimating]; } } // 结束计时 - (void)endTiming { if (_timer) { [_timer invalidate]; _timer = nil; self.timing = NO; self.seconds = 0; self.titleLabel.text = @"计时"; self.timeLabel.text = @"00:00:00"; [_titleArr replaceObjectAtIndex:0 withObject:@"开始计时"]; } } #pragma mark - Override - (void)setFrame:(CGRect)frame{ frame.size.width = 80; frame.size.height = 80; [super setFrame:frame]; self.layer.cornerRadius = frame.size.width / 2.f; self.layer.masksToBounds = YES; } - (void)layoutSubviews{ [super layoutSubviews]; CGFloat closeBtnWH = 36; self.closeBtn.frame = CGRectMake(self.frame.size.width - closeBtnWH , 0, closeBtnWH, closeBtnWH); } #pragma mark - touch move - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { //保存触摸起始点位置 CGPoint point = [[touches anyObject] locationInView:self]; self.startPoint = point; //该view置于最前 [[self superview] bringSubviewToFront:self]; } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { //计算位移=当前位置-起始位置 CGPoint point = [[touches anyObject] locationInView:self]; float dx = point.x - self.startPoint.x; float dy = point.y - self.startPoint.y; //计算移动后的view中心点 CGPoint newcenter = CGPointMake(self.center.x + dx, self.center.y + dy); /* 限制用户不可将视图托出屏幕 */ float halfx = CGRectGetMidX(self.bounds); //x坐标左边界 newcenter.x = MAX(halfx, newcenter.x); //x坐标右边界 newcenter.x = MIN(self.superview.bounds.size.width - halfx, newcenter.x); //y坐标同理 float halfy = CGRectGetMidY(self.bounds); newcenter.y = MAX(halfy, newcenter.y); newcenter.y = MIN(self.superview.bounds.size.height - halfy, newcenter.y); /// 动画过度 [UIView animateWithDuration:.25f delay:0 usingSpringWithDamping:.3f initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{ //移动view self.center = newcenter; } completion:^(BOOL finished) { }]; } #pragma mark - LazyLoad - (FLAnimatedImageView *)bgImageView { if (!_bgImageView) { _bgImageView = [[FLAnimatedImageView alloc] initWithFrame:self.frame]; @weakify(self); @weakify(_bgImageView); _bgImageView.loopCompletionBlock = ^(NSUInteger loopCountRemaining) { @strongify(self); @strongify(_bgImageView); if (!self.isTiming) { NSBundle *bundle = [NSBundle mainBundle]; NSString *resourcePath = [bundle resourcePath]; NSString *filePath = [resourcePath stringByAppendingPathComponent:@"1.gif"]; _bgImageView.image = [UIImage imageWithContentsOfFile:filePath]; } }; } return _bgImageView; } - (UILabel *)timeLabel { if (!_timeLabel) { _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, RQ_SCREEN_WIDTH, 54)]; _timeLabel.text = @"00:00:00"; _timeLabel.textColor = UIColor.whiteColor; _timeLabel.textAlignment = NSTextAlignmentCenter; _timeLabel.font = [UIFont systemFontOfSize:50 weight:UIFontWeightBold]; } return _timeLabel; } - (NSTimer *)timer { if (!_timer) { _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(rq_timerValueChanged) userInfo:nil repeats:YES]; } return _timer; } - (NSMutableArray *)titleArr { if (!_titleArr) { _titleArr = @[@"开始计时",@"结束计时"].mutableCopy; } return _titleArr; } - (UILabel *)titleLabel { if (!_titleLabel) { _titleLabel = [[UILabel alloc] initWithFrame:self.frame]; _titleLabel.text = @"计时"; _titleLabel.textColor = UIColor.whiteColor; _titleLabel.textAlignment = NSTextAlignmentCenter; _titleLabel.font = [UIFont systemFontOfSize:10 weight:UIFontWeightLight]; } return _titleLabel; } - (FLAnimatedImage *)animatedImage { if (!_animatedImage) { NSString *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:@"1" ofType:@"gif"]; NSData *gifImageData = [NSData dataWithContentsOfFile:filePath]; _animatedImage = [FLAnimatedImage animatedImageWithGIFData:gifImageData]; } return _animatedImage; } #pragma mark - TimeModule - (void)startTimingWithResultBlock:(void (^)(BOOL isSuccess))resultBlock { @weakify(self) if (defUser.isycbd == 1) { [RQ_RemoteTheory_MANAGER getTheoryStatusWithResultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull statusDict) { @strongify(self) if (isSuccess) { NSString *statusStr = statusDict[@"body"]; if (!statusStr && [statusStr isEqualToString:@""]) { ShowMsg(@"获取学员状态异常!"); !resultBlock? : resultBlock(NO); return; } BOOL statusIsSignIn = [statusStr isEqualToString:@"1"]; // NSString *pxkm = defUser.userDict[@"pxjd"]; NSString *pxkm = myDelegate.subject; if (pxkm.length < 1) { pxkm = @"1"; } if (([pxkm isEqualToString:@"2"] || [pxkm isEqualToString:@"3"])) { pxkm = @"4"; } NSArray* array = [DB_Helper quearyTrain:RQStringIsNotEmpty(defUser.userStuId)? defUser.userStuId : @"" Subject:pxkm]; __block NSInteger trainTime = 0; [array.rac_sequence.signal subscribeNext:^(TrainRecord *record) { trainTime = trainTime + record.trainTime.integerValue; } completed:^{ dispatch_async(dispatch_get_main_queue(), ^{ @strongify(self) if (trainTime > 0) { [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:statusIsSignIn? @"检测到上次未正常签退,请签退后再开始理论计时" : @"检测到学员已签退!本地存在异常学时,是否删除异常学时?" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"取消" otherButtonTitles:@[statusIsSignIn? @"签退" : @"删除"] otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) { @strongify(self) !resultBlock? : resultBlock(NO); if (selectedOtherButtonIndex == 0) { if (statusIsSignIn) { TrainRecord *lastRecord = [array lastObject]; /// 1.本地活体检测 [RQ_CHECKBODY_MANAGER beginCheckBodyWithCheckNum:defUser.ycbdFaceCount completeBlock:^(BOOL success, NSDictionary * _Nullable dic) { @strongify(self) if (success) { /// 2.线上活体检测 [RQ_RemoteTheory_MANAGER edufaceTofaceWithPhotoStr:dic[@"normalImg"] loginFlagType:LoginFlagType_SignOut resultBlock:^(BOOL isSuccess, id dict) { @strongify(self) if (isSuccess) { if (dict) { NSDictionary *myDict = dict; NSString *imageUrlStr = myDict[@"body"]; if (RQStringIsEmpty(imageUrlStr)) { ShowMsg(@"路径为空"); } /// 4.2上传签退照片 [RQ_RemoteTheory_MANAGER uploadEduPicWithClassidStr:lastRecord.classid timeStr:lastRecord.endTime actionPhotoStr:imageUrlStr loginFlagType:LoginFlagType_SignOut resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull uploadEduPicDict) { @strongify(self) if (isSuccess) { NSLog(@"上传签退照片成功!"); /// 3.签退 [RQ_RemoteTheory_MANAGER uploadEduSignOutWithArray:array resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull signOutDict) { @strongify(self) if (success) { [self signOutSuccessWithTrainArray:array signOutSuccess:YES uploadImageSuccess:YES completion:nil]; } !resultBlock? : resultBlock(NO); }]; } else { NSLog(@"上传签退照片失败!"); [self signOutSuccessWithTrainArray:array signOutSuccess:YES uploadImageSuccess:NO completion:^{ }]; !resultBlock? : resultBlock(NO); } }]; } else { !resultBlock? : resultBlock(NO); } } else { !resultBlock? : resultBlock(NO); } }]; } else { !resultBlock? : resultBlock(NO); } }]; } else { for (TrainRecord *record in array) { [DB_Helper deleteTrainRecord:record]; } } } }]; } else { if (statusIsSignIn) { [RQ_RemoteTheory_MANAGER uploadEduSignOutWithArray:@[] resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull signOutDict) { @strongify(self) if (isSuccess) { [self signOutSuccessWithTrainArray:@[] signOutSuccess:YES uploadImageSuccess:NO completion:nil]; } !resultBlock? : resultBlock(isSuccess); }]; } else { /// 1.本地活体检测 [RQ_CHECKBODY_MANAGER beginCheckBodyWithCheckNum:defUser.ycbdFaceCount completeBlock:^(BOOL success, NSDictionary * _Nullable dic) { @strongify(self) if (success) { /// 2.线上活体检测 [RQ_RemoteTheory_MANAGER edufaceTofaceWithPhotoStr:dic[@"normalImg"] loginFlagType:LoginFlagType_SignIn resultBlock:^(BOOL isSuccess, id dict) { @strongify(self) if (isSuccess) { if (dict) { NSDictionary *myDict = dict; NSString *imageUrlStr = myDict[@"body"]; if (RQStringIsEmpty(imageUrlStr)) { ShowMsg(@"路径为空"); } /// 3.获取线上时间 [RQ_RemoteTheory_MANAGER getCurrentTimeWithResultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull timeDict) { @strongify(self) if (isSuccess) { // NSString *timeStr = @"2022-03-13 22:46:59"; NSDate *date = [NSDate rq_dateWithTimestamp:timeDict[@"body"]]; // NSDate *date = [NSDate rq_dateWithTimestamp:timeStr]; NSInteger timeStamp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue]; NSString *timeStampStr = [NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:timeStamp]]; self.classIdStr = timeStampStr; if (self.classIdStr.length < 10) { /// 如果生成时间戳错误 用一个随机十位数代替 self.classIdStr = [NSString stringWithFormat:@"%@",[NSNumber numberWithInteger:[NSString rq_randomNumberWithFrom:1647311377 to:9999999999]]]; } /// 4.签到 [RQ_RemoteTheory_MANAGER uploadEduSignInWithClassidStr:self.classIdStr resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull signInDict) { if (isSuccess) { @strongify(self) NSString *timeStr = signInDict[@"body"]; [self signInSuccessWithBeginTime:timeStr]; /// 5.上传照片 [RQ_RemoteTheory_MANAGER uploadEduPicWithClassidStr:self.classIdStr timeStr:timeStr actionPhotoStr:imageUrlStr loginFlagType:LoginFlagType_SignIn resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull uploadEduPicDict) { // @strongify(self) }]; } !resultBlock? : resultBlock(isSuccess); }]; } else { !resultBlock? : resultBlock(NO); } }]; } else { !resultBlock? : resultBlock(NO); [self.timer setFireDate:[NSDate distantPast]]; } } else { !resultBlock? : resultBlock(NO); } }]; } else { !resultBlock? : resultBlock(NO); } }]; } } }); }]; } else { !resultBlock? : resultBlock(NO); } }]; } else { !resultBlock? : resultBlock(YES); [self beginTime]; } } - (void)stopTimingWithResultBlock:(void (^)(BOOL isSuccessed))resultBlock { @weakify(self) [self.timer setFireDate:[NSDate distantFuture]]; [self saveTrain]; if (defUser.isycbd == 1) { /// 1.本地活体检测 [RQ_CHECKBODY_MANAGER beginCheckBodyWithCheckNum:defUser.ycbdFaceCount completeBlock:^(BOOL success, NSDictionary * _Nullable dic) { @strongify(self) if (success) { /// 2.线上活体检测 [RQ_RemoteTheory_MANAGER edufaceTofaceWithPhotoStr:dic[@"normalImg"] loginFlagType:LoginFlagType_SignOut resultBlock:^(BOOL isSuccess, id dict) { @strongify(self) if (isSuccess) { if (dict) { NSDictionary *myDict = dict; NSString *imageUrlStr = myDict[@"body"]; if (RQStringIsEmpty(imageUrlStr)) { ShowMsg(@"路径为空"); } /// 3.签退 // NSString *pxkm = defUser.userDict[@"pxjd"]; NSString *pxkm = myDelegate.subject; if (pxkm.length < 1) { pxkm = @"1"; } if (([pxkm isEqualToString:@"2"] || [pxkm isEqualToString:@"3"])) { pxkm = @"4"; } NSArray* array = [DB_Helper quearyTrain:RQStringIsNotEmpty(defUser.userStuId)? defUser.userStuId : @"" Subject:pxkm]; if (array.count < 1) { !resultBlock? : resultBlock(YES); ShowMsg(@"本地无学时明细!"); return; } /// 4.2上传签退照片 NSString *timeStr = [self getTimes:self.seconds]; [RQ_RemoteTheory_MANAGER uploadEduPicWithClassidStr:self.classIdStr timeStr:timeStr actionPhotoStr:imageUrlStr loginFlagType:LoginFlagType_SignOut resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull uploadEduPicDict) { @strongify(self) if (isSuccess) { NSLog(@"上传签退照片成功!"); [RQ_RemoteTheory_MANAGER uploadEduSignOutWithArray:array resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull signOutDict) { @strongify(self) if (isSuccess) { !resultBlock? : resultBlock(YES); [self signOutSuccessWithTrainArray:array signOutSuccess:YES uploadImageSuccess:YES completion:nil]; } else { !resultBlock? : resultBlock(NO); [self.timer setFireDate:[NSDate distantPast]]; } }]; } else { NSLog(@"上传签退照片失败!"); !resultBlock? : resultBlock(NO); [self.timer setFireDate:[NSDate distantPast]]; } }]; } else { !resultBlock? : resultBlock(NO); [self.timer setFireDate:[NSDate distantPast]]; } } else { !resultBlock? : resultBlock(NO); [self.timer setFireDate:[NSDate distantPast]]; } }]; } else { !resultBlock? : resultBlock(NO); [self.timer setFireDate:[NSDate distantPast]]; } }]; } else { !resultBlock? : resultBlock(YES); [self setSecondString:@"0"]; [self.timer setFireDate:[NSDate distantFuture]]; [self.timer invalidate]; self.timer = nil; // myDelegate.timer = nil; myDelegate.isTrain = NO; self.timeLabel.text = @"00:00:00"; // if (self.seconds < 60) { // ShowMsg(@"学时未满一分钟不统计"); // return; // } [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"结束计时,是否上传学时?" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"暂不上传" otherButtonTitles:@[@"确定"] otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) { @strongify(self) if (selectedOtherButtonIndex == 0) { [self upLoadTrainRecord]; }else if (selectedOtherButtonIndex == NSNotFound){ ShowMsg(@"学时已保存在本地"); } }]; } } //远程理论签到成功 - (void)signInSuccessWithBeginTime:(NSString *)beginTime { myDelegate.begin_Time = beginTime; [Tools playAudioWithString:@"操作成功,开始计时"]; self.seconds = 0; // myDelegate.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; myDelegate.isUseriPhone = YES; myDelegate.isTrain = YES; // self.timer = myDelegate.timer; // begin_endBtn.selected = YES; //防止前五分钟内退出软件 造成理论无本地学时 无法签退的问题 [self saveTrain]; // myDelegate.tPeriodVC = self; ShowMsgSuc(); [RQ_COMMON_MANAGER keepIdleTimerDisabledisOpen:YES]; } //远程理论签退成功 - (void)signOutSuccessWithTrainArray:(NSArray *)trainArray signOutSuccess:(BOOL)signOutSuccess uploadImageSuccess:(BOOL)uploadImageSuccess completion:(void (^)(void))completion { for (TrainRecord *record in trainArray) { [DB_Helper updateTrainState:record]; } if (trainArray.count > 0) { if (signOutSuccess) { if (![_secondString isEqualToString:@"0"]) { [self setSecondString:@"0"]; } if (self.timer.isValid) { [self.timer invalidate]; } if (self.timer) { self.timer = nil; } // if (myDelegate.timer) { // myDelegate.timer = nil; // } // if (myDelegate.tPeriodVC) { // myDelegate.tPeriodVC = nil; // } if (myDelegate.isTrain) { myDelegate.isTrain = NO; } if (![self.timeLabel.text isEqualToString:@"00:00:00"]) { self.timeLabel.text = @"00:00:00"; } } if (signOutSuccess && uploadImageSuccess) { [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"签退成功!" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"确定" otherButtonTitles:nil otherButtonStyles:nil showInWindow:NO completion:nil]; } else if (signOutSuccess && !uploadImageSuccess && completion) { [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"上传照片失败!" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"重新上传" otherButtonTitles:nil otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) { completion(); }]; } } else { // 强制签退后再去调签到 [self startTimingWithResultBlock:nil]; } [RQ_COMMON_MANAGER keepIdleTimerDisabledisOpen:NO]; } - (void)reUploadPhotoWithClassidStr:(NSString *)classidStr timeStr:(NSString *)timeStr actionPhotoStr:(NSString *)actionPhotoStr loginFlagType:(LoginFlagType)loginFlagType { __block BOOL reUploadSuccess = YES; __block BOOL isUploading = NO; int i = 0; do { if (i == 0) { if (!isUploading) { isUploading = YES; [RQ_RemoteTheory_MANAGER uploadEduPicWithClassidStr:classidStr timeStr:timeStr actionPhotoStr:actionPhotoStr loginFlagType:loginFlagType resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull uploadEduPicDict) { if (isSuccess) { ShowMsg(@"重新上传照片成功!"); reUploadSuccess = YES; } else { reUploadSuccess = NO; } isUploading = NO; }]; } } else { if (!isUploading) { isUploading = YES; [RQ_SHARE_FUNCTION showAlertWithTitle:@"温馨提示" message:@"上传照片失败!" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"重新上传" otherButtonTitles:nil otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) { [RQ_RemoteTheory_MANAGER uploadEduPicWithClassidStr:classidStr timeStr:timeStr actionPhotoStr:actionPhotoStr loginFlagType:loginFlagType resultBlock:^(BOOL isSuccess, NSDictionary * _Nonnull uploadEduPicDict) { if (isSuccess) { ShowMsg(@"重新上传照片成功!"); reUploadSuccess = YES; } else { reUploadSuccess = NO; } isUploading = NO; }]; }]; } } i ++; } while (reUploadSuccess == NO); } - (NSString *)getTimes:(int)second { NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; NSString *beginTimeStr = myDelegate.begin_Time; [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; NSDate *beginDate = [formatter dateFromString:beginTimeStr]; NSDate *newDate = [NSDate dateWithTimeInterval:second sinceDate:beginDate]; NSString *nowString = [formatter stringFromDate:newDate]; return nowString; } - (void)beginTime { @weakify(self) if (![Util connectedToNetWork]) { ShowMsg(@"请检查网络连接。"); return; } NSMutableArray *arr=[NSMutableArray array]; [arr property:defUser.userDict[@"outId"] forKey:@"outId"]; [arr property:defUser.userDict[@"city"] forKey:@"dqbh"]; NSString* method = @"isUsePhone"; [QMUITips showLoadingInView:DefaultTipsParentView]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) { @strongify(self) [QMUITips hideAllTips]; if (!dict) { ShowMsgFailed(); return; } if ([dict[@"code"] isEqualToString:@"90"]) { UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:nil message:dict[@"msg"] preferredStyle:UIAlertControllerStyleAlert]; [alertFind addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { [LoginViewController loginFromVC:RQControllerHelper.currentViewController]; }]]; [RQControllerHelper.currentViewController presentViewController:alertFind animated:true completion:nil]; return; } if ([dict[@"code"] isEqualToString:@"0"]) { myDelegate.begin_Time = dict[@"body"]; [Tools playAudioWithString:@"操作成功,开始计时"]; [self beginTiming]; self.seconds = 0; // myDelegate.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeFireMethod) userInfo:nil repeats:YES]; myDelegate.isUseriPhone = YES; myDelegate.isTrain = YES; // begin_endBtn.selected = YES; //防止前五分钟内退出软件 造成理论无本地学时 无法签退的问题 [self saveTrain]; } else { NSString *msg = dict[@"msg"]; if (msg.length < 1) { msg = dict[@"body"]; } ShowMsg(msg); [Tools playAudioWithString:msg]; } }]; } - (void)stopTiming { // [myDelegate.timer invalidate]; // myDelegate.timer = nil; myDelegate.isUseriPhone = NO; myDelegate.isTrain = NO; self.timeLabel.text = @"00:00:00"; // if (seconds < 60) { // ShowMsg(@"学时未满一分钟不统计"); // } [self endTiming]; [self saveTrain]; [self upLoadTrainRecord]; } - (void)saveTrain { NSDateFormatter *formatter = [NSDateFormatter rq_defaultDateFormatter]; TrainRecord *train = [[TrainRecord alloc] init]; train.studentId = defUser.userStuId; train.beginTime = myDelegate.begin_Time; NSDate *date=[formatter dateFromString:myDelegate.begin_Time]; date=[date dateByAddingTimeInterval:self.seconds]; train.state = @"0"; train.endTime=[formatter stringFromDate:date]; NSInteger mins = self.seconds / 60; train.trainTime=[NSString stringWithFormat:@"%d",(int)mins]; train.subject = myDelegate.subject; train.classid = (self.classIdStr && ![self.classIdStr isEqualToString:@""])? self.classIdStr : @"0"; [DB_Helper saveTrain:train]; } - (void)upLoadTrainRecord { // NSString *pxkm = defUser.userDict[@"pxjd"]; // // if (pxkm.length < 1) { // pxkm = @"1"; // } NSArray* array = [DB_Helper quearyTrain:defUser.userStuId Subject:myDelegate.subject]; if (array.count < 1) { ShowMsg(@"本地无学时明细!"); return; } //这里是对分段上传学时做的处理 如果学时上传有问题 查看这里 [self uploadMoreTrainRecordWithArray:array]; } - (void)uploadMoreTrainRecordWithArray:(NSArray *)trainArray { @weakify(self) if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSString* str = @""; for (TrainRecord *record in trainArray) { str = [str stringByAppendingString:[NSString stringWithFormat:@"%@,%@,%@,%@;",record.studentId,record.beginTime,record.endTime,record.trainTime]]; } TrainRecord *record = [trainArray firstObject]; NSMutableArray *arr=[NSMutableArray array]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:@"ios",@"trainType", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:[DES3Util encrypt:str ],@"trainRecord", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:record.subject,@"pxkm", nil]]; [arr addObject:[NSDictionary dictionaryWithObjectsAndKeys:defUser.userDict[@"outId"],@"stuOutid", nil]]; NSString* method = @"uploadMoreTrainRecord"; [QMUITips showLoadingInView:DefaultTipsParentView]; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary *dict) { [QMUITips hideAllTips]; @strongify(self) if (!dict) { ShowMsg(@"操作失败"); return; } if ([dict[@"code"] isEqualToString:@"90"]) { UIAlertController *alertFind = [UIAlertController alertControllerWithTitle:nil message:dict[@"msg"] preferredStyle:UIAlertControllerStyleAlert]; [alertFind addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { @strongify(self) [LoginViewController loginFromVC:RQControllerHelper.currentViewController]; }]]; [RQControllerHelper.currentViewController presentViewController:alertFind animated:true completion:nil]; return; } if ( [dict[@"code"] isEqualToString:@"1"]) { ShowMsg(dict[@"body"]); return; } if ( [dict[@"code"] isEqualToString:@"0"]) { for (TrainRecord *record in trainArray) { [DB_Helper updateTrainState:record]; } UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"上传学时成功!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show]; } }]; } @end