// // PersonalVC.m // JSJPCoach // // Created by apple on 2017/3/20. // Copyright © 2017年 Danson. All rights reserved. // #import "PersonalVC.h" #import "LoginVC.h" #import "MyMsgVC.h" #import "commentVC.h" #import "Tools.h" #import "BindVC.h" @interface PersonalVC () { UILabel *nameLabel, *carTypeLabel, *schoollabel, *cardNumLabel; UIImageView *imgHead; UIButton *btnTel, *btnAddr, *loginBtn; InputView *inputVi; UITextField *psw1; UITextField *psw2; UITextField *RegistField; NSString *password; CGFloat _scrHeight; } @end @implementation PersonalVC -(instancetype)initWithScrFrame:(CGRect)frame{ if (self = [super init]) { _scrHeight = frame.size.height; self.view.frame = frame; } return self; } - (void)viewDidLoad { [super viewDidLoad]; [self myInit]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self refreshUI]; }; -(void)myInit { NSString *str; CGFloat x,y,w,h,bd; NSInteger tag = 1; bd = 15; x = y = 0; w = kSize.width; h = _scrHeight; UIScrollView *sv = [[UIScrollView alloc] setxywh]; [sv setShowsVerticalScrollIndicator:YES]; [self.view addSubview:sv]; h = bd*2 + 80 + 30+30; UIView* vi = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)]; [sv addSubview:vi]; UIImageView* iv; y += bd; w = h = 70; x += bd; iv = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, w, h)]; iv.layer.cornerRadius = iv.height*.5; iv.layer.masksToBounds = YES; [iv setContentMode:UIViewContentModeScaleAspectFill]; str = defUser.userDict[@"photo"]; if (!str || [str isEqualToString:@""]) { [iv setImage:[[UIImage imageNamed:@"noHeadImg.png"] tint:[UIColor whiteColor]]]; }else{ if (![str hasPrefix:@"http"]){ str = [imgPreFix stringByAppendingString:str]; } [iv sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"noHeadImg.png"]]; } [sv addSubview:iv]; imgHead = iv; x += w + bd; w = kSize.width - w; UILabel* label; label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)]; [label setText:defUser.userDict[@"name"] Font:Font21 TextColor:[UIColor whiteColor] Alignment:NSTextAlignmentLeft]; [sv addSubview:label]; nameLabel = label; x = 0; w = kSize.width; UIButton* btn; btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y - bd, w, h + bd)]; [btn target:self Tag:tag++]; [sv addSubview:btn]; y += h + bd; h = 30; w = kSize.width/3.0; NSArray* arr = @[@"准教车型",@"所属驾校",@"职业资格证号"]; for (int i=0; i < arr.count; i++) { label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)]; [label setText:arr[i] Font:Font14 TextColor:[UIColor whiteColor] Alignment:NSTextAlignmentCenter]; [sv addSubview:label]; x += w; } NSString *teachCarType = defUser.userDict[@"teachCarType"]; if (!teachCarType) { teachCarType = @""; } NSString *schoolName = defUser.userDict[@"schoolName"]; if (!schoolName) { schoolName = @""; } NSString *occupationNO = defUser.userDict[@"occupationNO"]; if (!occupationNO) { occupationNO = @""; } x = 0; y += h; w = kSize.width/3.0; h = 44; NSMutableArray *labelArray = [NSMutableArray arrayWithCapacity:3]; arr = @[teachCarType,schoolName,occupationNO]; for (int i=0; i < arr.count; i++) { label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, w, h)]; [label setText:arr[i] Font:Font14 TextColor:[UIColor whiteColor] Alignment:NSTextAlignmentCenter]; [label setNumberOfLines:0]; [sv addSubview:label]; x += w; [labelArray addObject:label]; // label.height = [label.text heightForWid:w Font:Font14]; } carTypeLabel = (UILabel *)labelArray[0]; schoollabel = (UILabel *)labelArray[1]; cardNumLabel = (UILabel *)labelArray[2]; [label addViewWithRect:CGRectMake(w - 1, y - 25, 1, h + 25) Color:newBackGroundColor]; [label addViewWithRect:CGRectMake(2*w, y - 25, 1, h + 25) Color:newBackGroundColor]; UIColor *customLineColor = newBackGroundColor; x = bd; y += h+10; w = kSize.width-bd*2-45; h = 50; btn = [[UIButton alloc] setxywh]; [btn setPersonalImage:[UIImage imageNamed:@"phone.png"] Tit:defUser.userDict[@"mobile"] Font:Font16 State:UIControlStateNormal]; [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [btn target:self Tag:tag++];//2 btnTel= btn; [sv addSubview:btn]; [btn addViewWithRect:CGRectMake(x+45, y+h, w, 1) Color:customLineColor]; y +=h + 2; btn = [[UIButton alloc] setxywh]; [btn setPersonalImage:[UIImage imageNamed:@"address_icon.png"] Tit:defUser.userDict[@"address"] Font:Font16 State:UIControlStateNormal]; [btn.titleLabel setNumberOfLines:0]; [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [btn target:self Tag:tag++];//3 [btn setEnabled:NO]; [sv addSubview:btn]; btnAddr = btn; [btn addViewWithRect:CGRectMake(x+45, y+h, w, 1) Color:customLineColor]; //我的消息 先放在主页显示 y += h + 2; btn = [[UIButton alloc] setxywh]; [btn setPersonalImage:[UIImage imageNamed:@"myNews.png"] Tit:@"我的消息" Font:Font16 State:UIControlStateNormal]; [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [btn target:self Tag:10]; [sv addSubview:btn]; [btn addViewWithRect:CGRectMake(x+45, y+h, w, 1) Color:customLineColor]; y += h + 2; btn = [[UIButton alloc] setxywh]; [btn setPersonalImage:[UIImage imageNamed:@"fixKey.png"] Tit:@"修改密码" Font:Font16 State:UIControlStateNormal]; [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [btn target:self Tag:12]; [sv addSubview:btn]; [btn addViewWithRect:CGRectMake(x+45, y+h, w, 1) Color:customLineColor]; //我的评论 y += h + 2; btn = [[UIButton alloc] setxywh]; [btn setPersonalImage:[[UIImage imageNamed:@"myComment.png"] tint:[UIColor cyanColor]] Tit:@"学员评论" Font:Font16 State:UIControlStateNormal]; [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; [btn target:self Tag:11]; [sv addSubview:btn]; [btn addViewWithRect:CGRectMake(x+45, y+h, w, 1) Color:customLineColor]; // y += h; // btn = [[UIButton alloc] setxywh]; // [btn setPersonalImage:[UIImage imageNamed:@"terminalRegist.png"] Tit:@"终端注册" Font:Font16 State:UIControlStateNormal]; // [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; // [btn target:self Tag:13]; // [sv addSubview:btn]; // [btn addViewWithRect:CGRectMake(x+45, y+h, w, 1) Color:customLineColor]; // // y += h; // btn = [[UIButton alloc] setxywh]; // [btn setPersonalImage:[UIImage imageNamed:@"terminalCancel.png"] Tit:@"终端注销" Font:Font16 State:UIControlStateNormal]; // [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft]; // [btn target:self Tag:14]; // [sv addSubview:btn]; // [btn addViewWithRect:CGRectMake(x+45, y+h, w, 1) Color:customLineColor]; y += h + 20; x = bd; w = kSize.width - bd*2; h = 50; btn = [[UIButton alloc] setxywh]; [btn setTitle:@"退出登录" textColor:[UIColor whiteColor] font:Font18 fotState:UIControlStateNormal]; [btn setBackgroundColor:defGreen]; [btn corner:h*.5]; [btn target:self Tag:tag++]; [sv addSubview:btn]; loginBtn = btn; UIView* aView; aView = sv.subviews.lastObject; [sv setContentSize:CGSizeMake(kSize.width, aView.bottom + 20)]; if (defUser.isLogin) { [self coachLogin];//判断登录是否过期 // [self refreshUI]; } } - (void)refreshUI { NSString *str = defUser.userDict[@"photo"]; if (!str) { str = @""; } [imgHead sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"noHeadImg.png"]]; nameLabel.text = defUser.userDict[@"name"]; NSString *teachCarType = defUser.userDict[@"teachCarType"]; if (!teachCarType) { teachCarType = @""; } carTypeLabel.text = teachCarType; NSString *schoolName = defUser.userDict[@"schoolName"]; if (!schoolName) { schoolName = @""; } schoollabel.text = schoolName; NSString *occupationNO = defUser.userDict[@"occupationNO"]; if (!occupationNO) { occupationNO = @""; } cardNumLabel.text = occupationNO; if (defUser.isLogin) { [loginBtn setTitle:@"退出登录" forState:UIControlStateNormal]; }else{ [loginBtn setTitle:@"登录" forState:UIControlStateNormal]; } [btnAddr setTitle:defUser.userDict[@"address"] forState:UIControlStateNormal]; [btnTel setTitle:defUser.userDict[@"mobile"] forState:UIControlStateNormal]; } /**如果这些按钮的事件。都有某些共同点。那么可以把他们指向同一个sel. 又或者这些按钮的事件都很简单,那么也可以指向同一个sel; 否则,还是不要比较好。 */ -(void)btnClick:(UIButton*)sender { if (!defUser.isLogin) { [LoginVC loginFromVC:self]; return ; } int tag = (int)sender.tag; if (tag == 4 || tag == 13 || tag == 14) { if (myDelegate.isPeriodIng) { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"正在带教学员,不能进行此操作" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; return; } } if (1 == tag) { InputView* vi = [[InputView alloc] initWithTitle:@"请选择头像"]; [vi setDelegate:self]; [vi show]; inputVi = vi; return; } if (3 == tag) { //地址 return; } if (4 == tag) { //退出登录 if (!defUser.isLogin) { [LoginVC loginFromVC:self]; return; } defUser.isLogin = NO; defUser.userDict = nil; [self refreshUI]; ShowMsgSuc(); return; } if (tag == 10) { //NSLog(@"我的消息"); MyMsgVC *vc = [[MyMsgVC alloc] init]; [vc.view setBackgroundColor:[UIColor whiteColor]]; [self.navigationController pushViewController:vc animated:YES]; return; } if (tag == 11) { //查看评论 commentVC *vc = [[commentVC alloc] init]; [vc.view setBackgroundColor:[UIColor whiteColor]]; [self.navigationController pushViewController:vc animated:YES]; return; } if (tag == 12) { [self fixKey];//修改密码 return; } if (tag == 13) { // 取消判断IMEI // if ([defUser.userDict[@"imei"] length] < 1) { // //先绑定imei 然后才能终端注册 // // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您的账号还未绑定手机,绑定后当前手机只能用于当前帐号带教学员,绑定后可以进行终端注册操作" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去绑定", nil]; // alert.tag = 21; // [alert show]; // // }else{ [self showRegistTextField];//终端注册 // } } if (tag == 14) { 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 doDeviceLogOut];//终端注销 }]]; [self presentViewController:alert animated:true completion:nil]; } } #pragma mark - 终端注册 - (void)showRegistTextField { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"手机注册" message:@"手机未注册为终端设备,请输入当前手机带练车辆的车牌号码完成注册" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; alert.alertViewStyle = UIAlertViewStylePlainTextInput; RegistField = [alert textFieldAtIndex:0]; RegistField.placeholder = @"请输入车牌号码"; alert.tag = 20; [alert show]; } - (void)doDeviceReg { if (RegistField.text.length < 1) { ShowMsg(@"请输入车牌号码"); [self showRegistTextField]; return; } [LoadingView showHUD]; if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"sim" Value:defUser.userDict[@"mobile"]]; [arr addPro:@"licnum" Value:RegistField.text]; [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; [arr addPro:@"jxbh" Value:defUser.userDict[@"jxbh"]]; NSString* method = @"doDeviceReg"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); if (!root) { ShowMsg(@"注册失败!"); return ; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } ShowMsg(@"注册成功!"); reLogin(); }]; } #pragma mark - 终端注销 -(void)doDeviceLogOut { [LoadingView showHUD]; if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"sim" Value:defUser.userDict[@"mobile"]]; [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; [arr addPro:@"jxbh" Value:defUser.userDict[@"jxbh"]]; NSString* method = @"doDeviceLogOut"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); if (!root) { ShowMsg(@"注销失败!"); return ; } if ([root[@"code"] isEqualToString:@"1"]) { ShowMsg(root[@"body"]); return; } ShowMsg(@"注销成功!"); }]; } #pragma mark 修改密码 -(void)fixKey { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"验证原密码" message:@"如果忘了原密码,请重新登录即可利用找回密码设置新密码" preferredStyle:UIAlertControllerStyleAlert]; [alert addTextFieldWithConfigurationHandler:^(UITextField * textField) { textField.placeholder = @"密码"; }]; [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]]; [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) { NSString *text = [[alert.textFields firstObject] text]; NSString *key = defUser.userDict[@"initPwd"]; if (password.length > 1) { key = password; } if (![text isEqualToString:key]) { ShowMsg(@"原密码错误!"); return; } UIAlertView *alertKey = [[UIAlertView alloc] initWithTitle:@"修改密码" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认修改",nil]; [alertKey setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput]; alertKey.tag = 11; psw1 = [alertKey textFieldAtIndex:0]; psw2 = [alertKey textFieldAtIndex:1]; psw1.placeholder = @"请输入新密码"; psw2.placeholder = @"请确认密码"; [psw1 setSecureTextEntry:YES]; [alertKey show]; }]]; [self presentViewController:alert animated:true completion:nil]; } -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (alertView.tag == 1) { [LoginVC loginFromVC:self]; } if (alertView.tag == 11) { if (buttonIndex == 1) { [self submit]; } }else if (alertView.tag == 20){ if (buttonIndex != alertView.cancelButtonIndex) { [self doDeviceReg]; } }else if (alertView.tag == 21){ if (buttonIndex != alertView.cancelButtonIndex) { //手机绑定 BindVC *bindVC = [[BindVC alloc] init]; [bindVC bindSuccessWithBlock:^{ reLogin(); }]; [self.navigationController pushViewController:bindVC animated:YES]; } } } -(void)submit { //NSLog(@"修改密码"); if (![psw1.text isEqualToString:psw2.text]) { dispatch_async(dispatch_get_main_queue(), ^{ UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"密码不一致,请您核查" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil]; [alert show]; }); return; } NSMutableArray *arr=[NSMutableArray array]; [arr addPro:@"loginCode" Value:defUser.sfzmhm]; [arr addPro:@"pwd" Value:psw1.text]; [arr addPro:@"dqbh" Value:defUser.userDict[@"cityId"]]; NSString* method = @"saveCoachPwd"; //NSLog(@"密码信息---->%@",arr); ShowHUD(); [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { if (!root) { ShowMsgFailed(); return; } if ( 1 == [root[@"code"] integerValue]) { ShowMsgFailed(); return; } ShowMsg(@"修改成功,请登录!"); //此时的密码 password = psw1.text; defUser.isLogin = NO; NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:defUser.userDict]; if ([[dic allKeys] containsObject:@"initPwd"]) { [dic setValue:@"" forKey:@"initPwd"]; } defUser.userDict = dic; //跳到登陆页面 [LoginVC loginFromVC:self]; }]; } #pragma mark 上传头像 -(void)InputView:(InputView *)view didGetImage:(UIImage *)image { [self uploadCoachHeadImg:image]; } -(void)InputViewWillPickImage:(InputView *)view { UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:view.type]) { pickerImage.sourceType = view.type; pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType]; } pickerImage.delegate = self; pickerImage.allowsEditing = NO; [self presentViewController:pickerImage animated:YES completion:nil]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [inputVi cancelAction]; [picker dismissViewControllerAnimated:YES completion:^{ UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage]; [self uploadCoachHeadImg:image]; }]; } -(void)uploadCoachHeadImg:(UIImage*)image { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSData *data = UIImagePNGRepresentation([image scaledToWid:100]); NSArray *dataArray = [NSArray arrayWithObject:data]; ShowHUD(); [jiaPeiManager requestAnythingWithURL:@"uploadUserImg" array:dataArray data:data completion:^(NSDictionary *root) { RemoveHUD(); if (!root) { ShowMsg(@"上传失败"); return; } if (![root[@"code"] isEqualToString:@"0"]) { ShowMsg(root[@"body"]); return; } ShowHUD(); NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"coachId" Value:defUser.userDict[@"id"]]; [arr addPro:@"photo" Value:root[@"body"]]; [jiaPeiManager requestAnythingWithURL:@"uploadHeadImg" array:arr data:nil completion:^(NSDictionary *root) { RemoveHUD(); if (!root) { ShowMsg(@"上传失败"); return; } ShowMsg(root[@"body"]); if (![root[@"code"] isEqualToString:@"0"]) { return; } //上传头像后 在获取下最新信息 reLogin(); imgHead.image = [UIImage imageWithData:data]; }]; }]; } //更新登录信息 -(void)coachLogin { if (![Util connectedToNetWork]) { showMsgUnconnect(); return; } NSMutableArray *arr = [NSMutableArray array]; [arr addPro:@"sfzhm" Value:defUser.sfzmhm]; [arr addPro:@"pwd" Value:defUser.userDict[@"pwd"]]; NSString* method = @"coachLogin"; [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * root) { RemoveHUD(); if (!root || [root[@"code"] isEqualToString:@"1"]) { defUser.isLogin = NO; defUser.userDict = nil; UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"登录信息已过期,请重新登陆" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil]; alert.tag = 1; [alert show]; return; } //更新信息 defUser.isLogin = YES; defUser.userDict = root[@"body"]; [self refreshUI]; //NSLog(@"----><>%@",defUser.userDict); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } @end