// // EditSocietyViewController.m // BuguLive // // Created by 王珂 on 17/1/22. // Copyright © 2017年 xfg. All rights reserved. // #import "EditSocietyViewController.h" #import "AFNetworking.h" //#import "BGOssManager.h" @interface EditSocietyViewController () { // BGOssManager *_ossManager; NSString *_uploadFilePath; NSString *_urlString; NSString *_timeString;//时间戳的字符串 } @property(nonatomic,strong) UIView * view1; @property(nonatomic,strong) UIView * view2; @property(nonatomic,strong) UIView * view3; @property(nonatomic,strong) UIView * view4; @property(nonatomic,strong) UIView * view5; @property(nonatomic,strong) UIButton * headBtn; @property(nonatomic,strong) UILabel * societyLabel; @property(nonatomic,strong) UITextView * nameTextView;//公会名称 @property(nonatomic,strong) UITextView * desTextView;//公会宣言 @property(nonatomic,strong) UIButton * editBtn; //编辑按钮 @property(nonatomic,strong) UIButton * managerBtn; //成员管理 @property(nonatomic,strong) UIButton * ensureBtn; //确认 @property(nonatomic,strong) NSMutableDictionary * dataDic; @property(nonatomic,copy) NSString * urlStr; @property (nonatomic, strong) MBProgressHUD *HUD; @end @implementation EditSocietyViewController - (void)viewDidLoad { [super viewDidLoad]; _dataDic = [NSMutableDictionary dictionary]; // if (self.BuguLive.appModel.open_sts == 1) // { // _ossManager = [[BGOssManager alloc]initWithDelegate:self]; // } [self createView]; } -(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [IQKeyboardManager sharedManager].enable = YES; [IQKeyboardManager sharedManager].enableAutoToolbar = YES; } -(void)createView { // self.automaticallyAdjustsScrollViewInsets = NO; self.navigationItem.title = ASLocalizedString(@"公会资料修改"); self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(comeBack) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"]; _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, (kScreenH-64)*375/1200)]; _view1.backgroundColor = kFamilyBackGroundColor; [self.view addSubview:_view1]; _view2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view1.frame), kScreenW, (kScreenH-64)*95/1200)]; _view2.backgroundColor= [UIColor whiteColor]; [self.view addSubview:_view2]; _view3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view2.frame), kScreenW, (kScreenH-64)*20/1200)]; _view3.backgroundColor = kFamilyBackGroundColor; [self.view addSubview:_view3]; _view4 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view3.frame), kScreenW, (kScreenH-64)*310/1200)]; _view4.backgroundColor = [UIColor whiteColor]; [self.view addSubview:_view4]; _view5 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view4.frame), kScreenW, (kScreenH-64)*410/1200)]; _view5.backgroundColor = kFamilyBackGroundColor; [self.view addSubview:_view5]; _nameTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, CGRectGetHeight(_view2.frame))]; // _nameTextView.textAlignment = NSTextAlignmentCenter; // _nameTextView.insertDictationResultPlaceholder = @"" _nameTextView.delegate = self; // _nameTextView.text =ASLocalizedString(@"请输入公会名称"); // _nameTextView.textColor = [UIColor lightGrayColor]; _nameTextView.font = [UIFont systemFontOfSize:15]; // _nameTextView.editable = NO; [_view2 addSubview:_nameTextView]; _desTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, CGRectGetHeight(_view4.frame))]; // _desTextView.text = ASLocalizedString(@"请输入公会宣言"); // _desTextView.textColor = [UIColor lightGrayColor]; _desTextView.delegate = self; _desTextView.font = [UIFont systemFontOfSize:15]; [_view4 addSubview:_desTextView]; [self createBtn]; } -(void)viewWillDisappear:(BOOL)animated { [IQKeyboardManager sharedManager].enable = NO; [IQKeyboardManager sharedManager].enableAutoToolbar = NO; } - (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName { NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5); NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName]; [imageData writeToFile:fullPath atomically:NO]; _uploadFilePath = fullPath; NSLog(@"uploadFilePath : %@", _uploadFilePath); } #pragma mark 代理回调 - (void)uploadImageWithUrlStr:(NSString *)imageUrlStr withUploadStateCount:(int)stateCount { if (_HUD) { _HUD.hidden = YES; } if (stateCount == 0) { _urlStr = [NSString stringWithFormat:@"./%@",_timeString]; NSLog(@"_urlString==%@",_urlStr); } } -(void)createBtn { _headBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _headBtn.frame = CGRectMake((kScreenW-90)/2, (CGRectGetHeight(_view1.frame)-117)/2, 90, 90); _headBtn.backgroundColor = [UIColor whiteColor]; _headBtn.layer.cornerRadius = 45; _headBtn.layer.masksToBounds = YES; _headBtn.backgroundColor = [UIColor whiteColor]; [_headBtn addTarget:self action:@selector(clickHeadImage) forControlEvents:UIControlEventTouchUpInside]; [_view1 addSubview:_headBtn]; _societyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_headBtn.frame)+10, kScreenW, 17)]; _societyLabel.text = ASLocalizedString(@"编辑公会头像"); _societyLabel.textAlignment = NSTextAlignmentCenter; _societyLabel.font = [UIFont systemFontOfSize:17]; _societyLabel.textColor = kAppGrayColor2; [_view1 addSubview:_societyLabel]; _ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _ensureBtn.frame = CGRectMake(25, 25, kScreenW-50, 40); _ensureBtn.backgroundColor = kAppMainColor; _ensureBtn.layer.cornerRadius = 20; _ensureBtn.layer.masksToBounds = YES; [_ensureBtn setTitle:ASLocalizedString(@"确认")forState:UIControlStateNormal]; [_ensureBtn addTarget:self action:@selector(ensureBtnAction) forControlEvents:UIControlEventTouchUpInside]; [_view5 addSubview:_ensureBtn]; [self createDes]; } -(void)createDes { //如果是创建公会 if (_type==0) { [_headBtn setBackgroundImage:[UIImage imageNamed:@"me_addPhoto"] forState:UIControlStateNormal]; _nameTextView.text =ASLocalizedString(@"请输入公会名称"); _nameTextView.textColor = [UIColor lightGrayColor]; _desTextView.text = ASLocalizedString(@"请输入公会宣言"); _desTextView.textColor = [UIColor lightGrayColor]; } //如果是重新编辑公会资料 else if (_type==1) { // [_headBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:_model.family_logo] forState:UIControlStateNormal]; [_headBtn sd_setImageWithURL:[NSURL URLWithString:self.sociaHead] forState:UIControlStateNormal placeholderImage:kDefaultPreloadHeadImg]; _nameTextView.text = self.sociatyName; //如果是未通过审核则可以编辑公会名称 _nameTextView.editable = YES; // _nameTextView.editable = _canEditAll==1?YES:NO; _nameTextView.textAlignment = _canEditAll==1? NSTextAlignmentLeft:NSTextAlignmentCenter; // _nameTextView.textAlignment = NSTextAlignmentCenter; _nameTextView.textColor = [UIColor blackColor]; _desTextView.text = self.sociatyManifasto; _desTextView.textColor = [UIColor blackColor]; } } //确认按钮 -(void)ensureBtnAction { if ([_nameTextView.text isEqualToString:ASLocalizedString(@"请输入公会名称")]&&[_nameTextView.textColor isEqual:[UIColor lightGrayColor]]) { [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会名称")]; return; } if ([_desTextView.text isEqualToString:ASLocalizedString(@"请输入公会宣言")]&&[_desTextView.textColor isEqual:[UIColor lightGrayColor]]) { [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会宣言")]; return; } // _desTextView.insertDictationResultPlaceholder //如果是创建公会 if (_type==0) { if (_urlStr.length<1) { [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请编辑头像")]; return; } if (_desTextView.text.length<1) { [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会宣言")]; return; } NSMutableDictionary *mDict = [NSMutableDictionary dictionary]; [mDict setObject:@"society_app" forKey:@"ctl"]; [mDict setObject:@"create" forKey:@"act"]; [mDict setObject:_urlStr forKey:@"logo"]; [mDict setObject:_nameTextView.text forKey:@"name"]; [mDict setObject:_desTextView.text forKey:@"manifesto"]; [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在创建公会请等待")]; [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) { if ([responseJson toInt:@"status"]==1) { [[BGHUDHelper sharedInstance] syncStopLoading]; [self.navigationController popViewControllerAnimated:YES]; } else { [[BGHUDHelper sharedInstance] syncStopLoading]; } } FailureBlock:^(NSError *error) { [[BGHUDHelper sharedInstance] syncStopLoading]; }]; } else if (_type==1) // 如果是修改公会资料 { if (_canEditAll==1&&_nameTextView.text.length<1) { [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会名称")]; return; } if (_desTextView.text.length<1) { [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会宣言")]; return; } NSMutableDictionary *mDict = [NSMutableDictionary dictionary]; [mDict setObject:@"society_app" forKey:@"ctl"]; // [mDict setObject:@"society" forKey:@"ctl"]; [mDict setObject:@"save" forKey:@"act"]; [mDict setObject:[NSNumber numberWithInt:self.sociatyID] forKey:@"id"]; if(_urlStr!=nil) { [mDict setObject:_urlStr forKey:@"logo"]; } else { [mDict setObject:@"" forKey:@"logo"]; } if (_nameTextView.text!= nil) { [mDict setObject:_nameTextView.text forKey:@"name"]; } if (_desTextView.text!= nil) { [mDict setObject:_desTextView.text forKey:@"manifesto"]; } [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在上传修改请等待")]; [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) { if ([responseJson toInt:@"status"]==1) { [[BGHUDHelper sharedInstance] syncStopLoading]; [self.navigationController popViewControllerAnimated:YES]; } else { [[BGHUDHelper sharedInstance] syncStopLoading]; } } FailureBlock:^(NSError *error) { [[BGHUDHelper sharedInstance] syncStopLoading]; }]; } } //编辑公会头像 -(void)clickHeadImage { UIActionSheet *headImgSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; [headImgSheet addButtonWithTitle:ASLocalizedString(@"相机")]; [headImgSheet addButtonWithTitle:ASLocalizedString(@"从手机相册选择")]; [headImgSheet addButtonWithTitle:ASLocalizedString(@"取消")]; headImgSheet.cancelButtonIndex = headImgSheet.numberOfButtons-1; headImgSheet.delegate = self; [headImgSheet showInView:[UIApplication sharedApplication].keyWindow]; } - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { UIImagePickerController *picker = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { picker.sourceType = UIImagePickerControllerSourceTypeCamera; picker.delegate = self; picker.allowsEditing = YES; picker.modalTransitionStyle = UIModalPresentationOverCurrentContext; } [self presentViewController:picker animated:YES completion:nil]; }else if (buttonIndex == 1){ UIImagePickerController *picker = [[UIImagePickerController alloc] init]; if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) { picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; picker.delegate = self; picker.allowsEditing = YES; picker.modalPresentationStyle = UIModalPresentationOverCurrentContext; } [self presentViewController:picker animated:YES completion:nil]; } } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage]; [self.headBtn setImage:image forState:UIControlStateNormal]; // if (self.BuguLive.appModel.open_sts == 1) // { // if ([_ossManager isSetRightParameter]) // { // [self saveImage:image withName:@"1.png"]; // _timeString= [_ossManager getObjectKeyString]; // [_ossManager asyncPutImage:_timeString localFilePath:_uploadFilePath]; // } // }else // { NSString *nameImage = [NSString stringWithFormat:@"societyHeadImage.jpg"]; [self saveImage:image withName:nameImage]; NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:nameImage]; NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file:%@",fullPath]]; [self pictureToNet:fileUrl]; // } [picker dismissViewControllerAnimated:YES completion:nil]; } - (void)pictureToNet:(NSURL *)pictureUrl { [[BGHUDHelper sharedInstance] syncLoading:nil]; NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString * photoName=[imageFile stringByAppendingPathComponent:@"societyHeadImage.jpg"]; NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file:%@",photoName]]; NSMutableDictionary *parmDict = [NSMutableDictionary dictionary]; [parmDict setObject:@"avatar" forKey:@"ctl"]; [parmDict setObject:@"uploadImage" forKey:@"act"]; [parmDict setObject:self.user_id forKey:@"id"]; [self.httpsManager POSTWithDict:parmDict andFileUrl:fileUrl SuccessBlock:^(NSDictionary *responseJson) { if ([responseJson toInt:@"status"] == 1) { NSString *nameImage = [NSString stringWithFormat:@"societyHeadImage.jpg"];; NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:nameImage]; UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath]; [_headBtn setImage:savedImage forState:UIControlStateNormal]; _urlStr = [responseJson toString:@"path"]; [[BGHUDHelper sharedInstance] syncStopLoading]; } else { [[BGHUDHelper sharedInstance] syncStopLoading]; } } FailureBlock:^(NSError *error) { [[BGHUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:@"%@",error]]; [[BGHUDHelper sharedInstance] syncStopLoading]; }]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { [self dismissViewControllerAnimated:YES completion:^{}]; } //点击返回按钮 - (void)comeBack { [self.navigationController popViewControllerAnimated:NO]; } -(BOOL)textViewShouldBeginEditing:(UITextView *)textView { if (textView==_nameTextView) { if ([_nameTextView.text isEqualToString:ASLocalizedString(@"请输入公会名称")]&&[_nameTextView.textColor isEqual:[UIColor lightGrayColor]]) { _nameTextView.text = @""; _nameTextView.textColor = [UIColor blackColor]; } _nameTextView.textAlignment = NSTextAlignmentLeft; } else if (textView== _desTextView) { if ([_desTextView.text isEqualToString:ASLocalizedString(@"请输入公会宣言")]&&[_desTextView.textColor isEqual:[UIColor lightGrayColor]]) { _desTextView.text = @""; _desTextView.textColor= [UIColor blackColor]; } } return YES; } -(void)textViewDidEndEditing:(UITextView *)textView { [textView resignFirstResponder]; if (textView==_nameTextView) { if (_nameTextView.text.length<1) { _nameTextView.text =ASLocalizedString(@"请输入公会名称"); _nameTextView.textColor = [UIColor lightGrayColor]; [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会名称")]; _ensureBtn.enabled = NO; _ensureBtn.backgroundColor = kAppGrayColor3; } else { _ensureBtn.enabled = YES; _ensureBtn.backgroundColor = kAppMainColor; } } else if (textView==_desTextView) { if (_desTextView.text.length<1) { _desTextView.text = ASLocalizedString(@"请输入公会宣言"); _desTextView.textColor = [UIColor lightGrayColor]; [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入公会宣言")]; _ensureBtn.enabled = NO; _ensureBtn.backgroundColor = kAppGrayColor3; } else { _ensureBtn.enabled = YES; _ensureBtn.backgroundColor = kAppMainColor; } } } // 限制字数140 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if (_desTextView.text.length > 140) { _desTextView.text = [_desTextView.text substringWithRange:NSMakeRange(0, 140)]; [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"超过最大字数限制")]; } return YES; } -(BOOL)canPerformAction:(SEL)action withSender:(id)sender { [UIMenuController sharedMenuController].menuVisible = NO; //donot display the menu [_desTextView resignFirstResponder]; //do not allow the user to selected anything [_nameTextView resignFirstResponder]; return NO; } @end