// // MineController.m // model // // Created by zuxiukuan on 2018/7/15. // Copyright © 2018年 Mine. All rights reserved. // 我的 #import "MineController.h" #import "MyWalletController.h" #import "ModelDataController.h" #import "MySkillsController.h" #import "MyOrderController.h" #import "PlatformActivityController.h" #import "RedEnvelopeController.h" #import "ModelCodeController.h" #import "UIViewController+YJPhoto.h" #import "LoginViewController.h" #import "AboutUsViewController.h" @interface MineController () @property (nonatomic, strong) UIImageView *iconView; //  头像 @property (nonatomic, strong) UILabel *nameLabel; //  名字 @property (nonatomic, strong) UILabel *incomeLabel; //  收入 @property (nonatomic, strong) UIView *notificationView; //  消息红点 @property (nonatomic, strong) UIScrollView *bgScrollView; //  滚动背景 @end @implementation MineController { UIView *outPutView; UILabel *outLabel; UIImageView *cancelBtn; UIImageView *sureBtn; UIImageView *mengBanView; } - (void)viewWillAppear:(BOOL)animated { [self changeLoginState:@"1"]; [super viewWillAppear:animated]; self.navigationController.navigationBar.hidden = YES; // 网络请求 [self getDataFromNetworking]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.navigationController.navigationBar.hidden = NO; } - (void)changeLoginState:(NSString *)state{ NSString *str = [NSString stringWithFormat:@"%@modelInfo?action=doPutOnline&modelpk=%@&online=%@",imageURl,[ModelUser user].modelpk,state]; [[AHHttpManager sharedManager]POST:str parameters:nil success:^(id responseObject) { NSLog(@"%@",responseObject); if ([[responseObject objectForKey:@"msg"]isEqualToString:@"success"]) { }else{ } } failure:^(NSError *error) { NSLog(@"error = %@",error); }]; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; self.extendedLayoutIncludesOpaqueBars = YES; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orderNotification) name:@"orderMsg" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(hideNavigationBar) name:@"hideNavigationBar" object:nil]; //布局子视图 [self createSubviews]; mengBanView = [[UIImageView alloc]init]; mengBanView.frame = self.view.bounds; [mengBanView setImage:[UIImage imageNamed:@"graymengban"]]; outPutView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight-140, ScreenWidth, 140)]; outPutView.backgroundColor = [UIColor whiteColor]; outLabel = [[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth/2-50, 30, 140, 30)]; outLabel.text = @"是否确定退出?"; cancelBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 80, ScreenWidth/2, 60)]; [cancelBtn setImage:[UIImage imageNamed:@"quxiao"]]; sureBtn = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth/2, 80, ScreenWidth/2, 60)]; [sureBtn setImage:[UIImage imageNamed:@"queding"]]; [outPutView addSubview:cancelBtn]; [outPutView addSubview:sureBtn]; [outPutView addSubview:outLabel]; [mengBanView addSubview:outPutView]; UITapGestureRecognizer *cancelTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelAction:)]; [cancelBtn addGestureRecognizer:cancelTap]; UITapGestureRecognizer *sureTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sureAction:)]; [sureBtn addGestureRecognizer:sureTap]; cancelBtn.userInteractionEnabled = YES; sureBtn.userInteractionEnabled = YES; outPutView.userInteractionEnabled = YES; mengBanView.userInteractionEnabled = YES; [self.navigationController.view addSubview:mengBanView]; [self hideOutPut]; } -(void)hideNavigationBar{ if ([[self getCurrentVC] isKindOfClass:[MineController class]]) { self.navigationController.navigationBar.hidden = YES; } } //获取当前屏幕显示的viewcontroller -(UIViewController *)getCurrentVC { UIViewController *result = nil; UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController; do { if ([rootVC isKindOfClass:[UINavigationController class]]) { UINavigationController *navi = (UINavigationController *)rootVC; UIViewController *vc = [navi.viewControllers lastObject]; result = vc; rootVC = vc.presentedViewController; continue; } else if([rootVC isKindOfClass:[UITabBarController class]]) { UITabBarController *tab = (UITabBarController *)rootVC; result = tab; rootVC = [tab.viewControllers objectAtIndex:tab.selectedIndex]; continue; } else if([rootVC isKindOfClass:[UIViewController class]]) { result = rootVC; rootVC = nil; } } while (rootVC != nil); return result; } - (void)hideOutPut{ mengBanView.hidden = YES; outPutView.hidden = YES; outLabel.hidden = YES; cancelBtn.hidden = YES; sureBtn.hidden = YES; } - (void)showOutPut{ mengBanView.hidden = NO; outPutView.hidden = NO; outLabel.hidden = NO; cancelBtn.hidden = NO; sureBtn.hidden = NO; } //隐藏tabbar - (void)hideTabBar { if (self.tabBarController.tabBar.hidden == YES) { return; } UIView *contentView; if ( [[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]){ contentView = [self.tabBarController.view.subviews objectAtIndex:1]; }else{ contentView = [self.tabBarController.view.subviews objectAtIndex:0]; } contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height + self.tabBarController.tabBar.frame.size.height); self.tabBarController.tabBar.hidden = YES; } //显示tabbar - (void)showTabBar { if (self.tabBarController.tabBar.hidden == NO) { return; } UIView *contentView; if ([[self.tabBarController.view.subviews objectAtIndex:0] isKindOfClass:[UITabBar class]]){ contentView = [self.tabBarController.view.subviews objectAtIndex:1]; }else{ contentView = [self.tabBarController.view.subviews objectAtIndex:0]; } contentView.frame = CGRectMake(contentView.bounds.origin.x, contentView.bounds.origin.y, contentView.bounds.size.width, contentView.bounds.size.height - self.tabBarController.tabBar.frame.size.height); self.tabBarController.tabBar.hidden = NO; } - (void)cancelAction:(UITapGestureRecognizer*)recognizer{ [self showTabBar]; [self hideOutPut]; } - (void)sureAction:(UITapGestureRecognizer*)recognizer{ [self changeLoginState]; [self setupLoginViewController]; [[[NIMSDK sharedSDK] loginManager] logout:^(NSError *error) { NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier]; [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain]; }]; } - (void)setupLoginViewController { [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil]; LoginViewController *loginController = [[LoginViewController alloc] init]; UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginController]; self.view.window.rootViewController = nav; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } #pragma mark -- 网络请求 - (void)getDataFromNetworking { [MBProgressHUD showHUDAddedTo:self.view animated:YES]; NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[ModelUser user].modelpk,@"modelpk", nil]; [YanCNetWorkManager requestPostWithURLStr:Url_refreshLogin(PublicUrl) parameters:dic finish:^(id dataDic) { [MBProgressHUD hideHUDForView:self.view animated:YES]; NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { ModelUser *user = [ModelUser modelUser]; [user setValuesForKeysWithDictionary:dataDic[@"data"]]; user.modelpk = dataDic[@"data"][@"modelpk"]; user.coin_a = dataDic[@"data"][@"coin_a"]; user.coin_ir = dataDic[@"data"][@"coin_ir"]; user.pk = dataDic[@"data"][@"pk"]; user.qrcode = dataDic[@"data"][@"qrcode"]; [ModelUser save:user]; self.nameLabel.text = [NSString stringWithFormat:@"%@", dataDic[@"data"][@"pet"]]; [self.iconView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", PublicUrl, dataDic[@"data"][@"hphoto"]]] placeholderImage:[UIImage imageNamed:@"morentouxiang"]]; if ([dataDic[@"data"][@"hasred"] isEqualToString:@"1"]) { self.notificationView.hidden = NO; }else{ self.notificationView.hidden = YES; } [self getIncomeFromNetworking]; } else { [MBProgressHUD showInfo:@"请求失败!"]; } } enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } -(void)getIncomeFromNetworking{ NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[ModelUser modelUser].pk,@"memberpk", nil]; [YanCNetWorkManager requestPostWithURLStr:Url_getIncomeToday(PublicUrl) parameters:dic finish:^(id dataDic) { NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { self.incomeLabel.text = [NSString stringWithFormat:@"%@元", dataDic[@"count"]]; }else { [MBProgressHUD showInfo:@"请求失败!"]; } }enError:^(NSError *error) { [MBProgressHUD hideHUDForView:self.view animated:YES]; }]; } #pragma mark -- 布局子视图 - (void)createSubviews { self.bgScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds]; self.bgScrollView.backgroundColor = [UIColor whiteColor]; self.bgScrollView.contentSize = CGSizeMake(ScreenWidth, 600); self.bgScrollView.showsVerticalScrollIndicator = NO; [self.view addSubview:self.bgScrollView]; //头像 self.iconView = [[UIImageView alloc] initWithFrame:CGRectMake(ScreenWidth/2 - 40, 40, 80, 80)]; self.iconView.layer.masksToBounds = YES; self.iconView.layer.cornerRadius = 40; [self.bgScrollView addSubview:self.iconView]; UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickUploadHeadPhoto)]; self.iconView.userInteractionEnabled = YES; [self.iconView addGestureRecognizer:tap]; //姓名 self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.iconView.frame) + 18, ScreenWidth, 16)]; self.nameLabel.font = [UIFont systemFontOfSize:16.f]; self.nameLabel.textColor = RGBValueColor(0x333333, 1.0); self.nameLabel.textAlignment = NSTextAlignmentCenter; [self.bgScrollView addSubview:self.nameLabel]; //线 UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.nameLabel.frame) + 20, ScreenWidth, 0.5)]; lineLabel.backgroundColor = RGBValueColor(0xdfdfdf, 1.0); [self.bgScrollView addSubview:lineLabel]; //收入金额 self.incomeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lineLabel.frame) + 10, ScreenWidth, 16)]; self.incomeLabel.font = [UIFont systemFontOfSize:16.f]; self.incomeLabel.textColor = RGBValueColor(0x333333, 1.0); self.incomeLabel.textAlignment = NSTextAlignmentCenter; [self.bgScrollView addSubview:self.incomeLabel]; //今日收入 UILabel *incomeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.incomeLabel.frame) + 8, ScreenWidth, 13)]; incomeTitleLabel.font = [UIFont systemFontOfSize:13.f]; incomeTitleLabel.textColor = RGBValueColor(0x999999, 1.0); incomeTitleLabel.textAlignment = NSTextAlignmentCenter; incomeTitleLabel.text = @"今日收入"; [self.bgScrollView addSubview:incomeTitleLabel]; //粗线条 UILabel *greyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(incomeTitleLabel.frame) + 10, ScreenWidth, 10)]; greyLabel.backgroundColor = RGBValueColor(0xf7f7f7, 1.0); [self.bgScrollView addSubview:greyLabel]; //  九宫格 CGFloat btnWidth = ScreenWidth / 3; CGFloat btnHeight = 95 * AutoSizeSalseX; NSArray *imagesArr = @[@"qianbao",@"jineng",@"dingdan",@"mokaziliao",@"pingtaihuodong",@"erweima",@"aboutUs",@"tuichu"]; NSArray *titlesArr = @[@"我的钱包",@"我的技能",@"我的订单",@"模卡资料",@"平台活动",@"邀请二维码",@"联系我们",@"退出账号"]; for (int i = 0; i < 8; i++) {// 0 1 2 3 4 5 6 7 int x = i % 3; int y = i / 3; UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; btn.frame = CGRectMake(x * btnWidth, CGRectGetMaxY(greyLabel.frame) + y * btnHeight, btnWidth, btnHeight); [btn setImage:[UIImage imageNamed:imagesArr[i]] forState:UIControlStateNormal]; btn.adjustsImageWhenHighlighted = NO; [btn setTitle:titlesArr[i] forState:UIControlStateNormal]; [btn setTitleColor:RGBValueColor(0x333333, 1.0) forState:UIControlStateNormal]; btn.titleLabel.font = [UIFont systemFontOfSize:13]; // button标题的偏移量 btn.titleEdgeInsets = UIEdgeInsetsMake(btn.imageView.frame.size.height+12, -btn.imageView.bounds.size.width, 0,0); // button图片的偏移量 btn.imageEdgeInsets = UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width/2, btn.titleLabel.frame.size.height+12, -btn.titleLabel.frame.size.width/2); btn.tag = i; [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; [self.bgScrollView addSubview:btn]; if (i == 2) { self.notificationView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(btn.imageView.frame)-3, CGRectGetMinY(btn.imageView.frame)-5, 10, 10)]; self.notificationView.backgroundColor = RGB(254, 64, 134); self.notificationView.layer.cornerRadius = 5; [btn addSubview:self.notificationView]; self.notificationView.hidden = YES; } if (x == 1) { UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(btnWidth * x, CGRectGetMaxY(greyLabel.frame), 0.5, btnHeight * 3)]; lineLabel.backgroundColor = RGBValueColor(0xefefef, 1.0); [self.bgScrollView addSubview:lineLabel]; } if (x == 2) { UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(btnWidth * x, CGRectGetMaxY(greyLabel.frame), 0.5, btnHeight * 3)]; lineLabel.backgroundColor = RGBValueColor(0xefefef, 1.0); [self.bgScrollView addSubview:lineLabel]; } if (y == 0 || y == 1) { UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(greyLabel.frame) +btnHeight * (y + 1), ScreenWidth, 0.5)]; lineLabel.backgroundColor = RGBValueColor(0xefefef, 1.0); [self.bgScrollView addSubview:lineLabel]; } if (y == 2) { UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(greyLabel.frame) +btnHeight * 3, btnWidth * 2, 0.5)]; lineLabel.backgroundColor = RGBValueColor(0xefefef, 1.0); [self.bgScrollView addSubview:lineLabel]; } } } -(void)clickUploadHeadPhoto{ [self showCanEdit:NO title:@"上传头像" wscale:0.7 hscale:0.7 photo:^(UIImage *photo) { NSData *photoData = UIImageJPEGRepresentation(photo, 0.2); NSInteger length = [photoData length] / 1000; NSLog(@"图片大小:%ld Kb",(long)length); UIImage *smallPhoto = [UIImage imageWithData:photoData]; ModelUser *user = [ModelUser modelUser]; AFHTTPSessionManager * manger =[AFHTTPSessionManager manager]; manger.responseSerializer = [AFHTTPResponseSerializer serializer]; manger.requestSerializer = [AFJSONRequestSerializer serializer]; manger.securityPolicy.allowInvalidCertificates = YES; NSDictionary *dict =@{@"memberpk":user.pk, @"action":@"hp" }; [manger POST:Url_uploadImage(PublicUrl) parameters:dict constructingBodyWithBlock:^(id _Nonnull formData) { if (photoData==NULL) { }else{ [formData appendPartWithFileData:photoData name:@"img" fileName:@"head.png" mimeType:@"image/png"]; } } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { self.iconView.image = smallPhoto; NSDictionary *dic =[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers| NSJSONReadingMutableLeaves error:nil]; NSString * img = [dic objectForKey:@"img"]; [[NIMSDK sharedSDK].userManager updateMyUserInfo:@{@(NIMUserInfoUpdateTagAvatar) : [NSString stringWithFormat:@"%@%@",imageURl,img]} completion:nil]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { }]; }]; } - (void)btnAction:(UIButton *)sender { NSLog(@"sender.tag = %ld", sender.tag); switch (sender.tag) { case 0: //   我的钱包 { MyWalletController *myWalletVC = [[MyWalletController alloc] init]; myWalletVC.hidesBottomBarWhenPushed = YES; myWalletVC.amount = [ModelUser modelUser].coin_a; [self.navigationController pushViewController:myWalletVC animated:YES]; } break; case 1://   我的技能 { MySkillsController *mySkillsVC = [[MySkillsController alloc] init]; mySkillsVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:mySkillsVC animated:YES]; } break; case 2://   我的订单 { [self doRemoveModelHasRed]; MyOrderController *myOrderVC = [[MyOrderController alloc] init]; myOrderVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:myOrderVC animated:YES]; } break; case 3://   模卡资料 { ModelDataController *modelDataVC = [[ModelDataController alloc] init]; modelDataVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:modelDataVC animated:YES]; } break; case 4://   平台活动 { PlatformActivityController *activityVC = [[PlatformActivityController alloc] init]; activityVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:activityVC animated:YES]; } break; case 5://   邀请二维码 { ModelCodeController *modelCodeVC = [[ModelCodeController alloc] init]; modelCodeVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:modelCodeVC animated:YES]; } break; case 6://   关于我们 { AboutUsViewController *aboutUsVC = [[AboutUsViewController alloc] init]; aboutUsVC.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:aboutUsVC animated:YES]; } break; case 7://   退出账号 { // UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定退出" preferredStyle:UIAlertControllerStyleAlert]; // UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; // UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { // [self.navigationController pushViewController:[UIViewController new] animated:YES]; // }]; // [alertVc addAction:cancelAction]; // [alertVc addAction:sureAction]; // [self presentViewController:alertVc animated:YES completion:nil]; [self hideTabBar]; [self showOutPut]; } break; default: break; } } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { if (![outPutView isExclusiveTouch]) { [self showTabBar]; [self hideOutPut]; } } - (void)changeLoginState{ NSString *str = [NSString stringWithFormat:@"%@modelInfo?action=doPutOnline&modelpk=%@&online=0",imageURl,[ModelUser user].modelpk]; [[AHHttpManager sharedManager]POST:str parameters:nil success:^(id responseObject) { NSLog(@"%@",responseObject); if ([[responseObject objectForKey:@"msg"]isEqualToString:@"success"]) { }else{ } } failure:^(NSError *error) { NSLog(@"error = %@",error); }]; } -(void)doRemoveModelHasRed{ NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:[ModelUser modelUser].modelpk,@"modelpk", nil]; [YanCNetWorkManager requestPostWithURLStr:Url_doRemoveModelHasRed(PublicUrl) parameters:dic finish:^(id dataDic) { NSString *issuccess = dataDic[@"msg"]; if ([issuccess isEqualToString:@"success"]) { }else { } }enError:^(NSError *error) { }]; } -(void)orderNotification{ [self getDataFromNetworking]; } @end