EditFamilyViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. //
  2. // EditFamilyViewController.m
  3. // BuguLive
  4. //
  5. // Created by 王珂 on 16/9/26.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "EditFamilyViewController.h"
  9. #import "AFNetworking.h"
  10. //#import "BGOssManager.h"
  11. @interface EditFamilyViewController ()<UITextViewDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,MBProgressHUDDelegate,UINavigationControllerDelegate>
  12. {
  13. // BGOssManager *_ossManager;
  14. NSString *_uploadFilePath;
  15. NSString *_urlString;
  16. NSString *_timeString;//时间戳的字符串
  17. }
  18. @property (nonatomic, strong) UIView * view1;
  19. @property (nonatomic, strong) UIView * view2;
  20. @property (nonatomic, strong) UIView * view3;
  21. @property (nonatomic, strong) UIView * view4;
  22. @property (nonatomic, strong) UIView * view5;
  23. @property (nonatomic, strong) UIButton * headBtn;
  24. @property (nonatomic, strong) UILabel * familyLabel;
  25. @property (nonatomic, strong) UITextView * nameTextView;//公会名称
  26. @property (nonatomic, strong) UITextView * desTextView;//公会宣言
  27. @property (nonatomic, strong) UIButton * editBtn; //编辑按钮
  28. @property (nonatomic, strong) UIButton * managerBtn; //成员管理
  29. @property (nonatomic, strong) UIButton * ensureBtn; //确认
  30. @property (nonatomic, strong) NSMutableDictionary * dataDic;
  31. @property (nonatomic, copy) NSString * urlStr;
  32. @property (nonatomic, strong) MBProgressHUD *HUD;
  33. @end
  34. @implementation EditFamilyViewController
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. _dataDic = [NSMutableDictionary dictionary];
  39. _urlStr = @"";
  40. // if (self.BuguLive.appModel.open_sts == 1)
  41. // {
  42. // _ossManager = [[BGOssManager alloc]initWithDelegate:self];
  43. // }
  44. [self createView];
  45. }
  46. - (void)viewWillAppear:(BOOL)animated
  47. {
  48. [super viewWillAppear:animated];
  49. [IQKeyboardManager sharedManager].enable = YES;
  50. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  51. }
  52. - (void)createView
  53. {
  54. // self.automaticallyAdjustsScrollViewInsets = NO;
  55. self.navigationItem.title = ASLocalizedString(@"公会资料修改");
  56. self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(comeBack) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  57. _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, (kScreenH-64)*375/1200)];
  58. _view1.backgroundColor = kFamilyBackGroundColor;
  59. [self.view addSubview:_view1];
  60. _view2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view1.frame), kScreenW, (kScreenH-64)*95/1200)];
  61. _view2.backgroundColor= [UIColor whiteColor];
  62. [self.view addSubview:_view2];
  63. _view3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view2.frame), kScreenW, (kScreenH-64)*20/1200)];
  64. _view3.backgroundColor = kFamilyBackGroundColor;
  65. [self.view addSubview:_view3];
  66. _view4 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view3.frame), kScreenW, (kScreenH-64)*310/1200)];
  67. _view4.backgroundColor = [UIColor whiteColor];
  68. [self.view addSubview:_view4];
  69. _view5 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view4.frame), kScreenW, (kScreenH-64)*410/1200)];
  70. _view5.backgroundColor = kFamilyBackGroundColor;
  71. [self.view addSubview:_view5];
  72. _nameTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, CGRectGetHeight(_view2.frame))];
  73. // _nameTextView.textAlignment = NSTextAlignmentCenter;
  74. // _nameTextView.insertDictationResultPlaceholder = @""
  75. _nameTextView.delegate = self;
  76. // _nameTextView.text =ASLocalizedString(@"请输入公会名称");
  77. // _nameTextView.textColor = [UIColor lightGrayColor];
  78. _nameTextView.font = [UIFont systemFontOfSize:15];
  79. // _nameTextView.editable = NO;
  80. [_view2 addSubview:_nameTextView];
  81. _desTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, CGRectGetHeight(_view4.frame))];
  82. // _desTextView.text = ASLocalizedString(@"请输入公会宣言");
  83. // _desTextView.textColor = [UIColor lightGrayColor];
  84. _desTextView.delegate = self;
  85. _desTextView.font = [UIFont systemFontOfSize:15];
  86. [_view4 addSubview:_desTextView];
  87. [self createBtn];
  88. }
  89. - (void)viewWillDisappear:(BOOL)animated
  90. {
  91. [IQKeyboardManager sharedManager].enable = NO;
  92. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  93. }
  94. - (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
  95. {
  96. NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
  97. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
  98. [imageData writeToFile:fullPath atomically:NO];
  99. _uploadFilePath = fullPath;
  100. NSLog(@"uploadFilePath : %@", _uploadFilePath);
  101. }
  102. #pragma mark 代理回调
  103. - (void)uploadImageWithUrlStr:(NSString *)imageUrlStr withUploadStateCount:(int)stateCount
  104. {
  105. if (_HUD)
  106. {
  107. _HUD.hidden = YES;
  108. }
  109. if (stateCount == 0)
  110. {
  111. _urlStr = [NSString stringWithFormat:@"./%@",_timeString];
  112. NSLog(@"_urlString==%@",_urlStr);
  113. }
  114. }
  115. - (void)createBtn
  116. {
  117. _headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  118. _headBtn.frame = CGRectMake((kScreenW-90)/2, (CGRectGetHeight(_view1.frame)-117)/2, 90, 90);
  119. _headBtn.backgroundColor = [UIColor whiteColor];
  120. _headBtn.layer.cornerRadius = 45;
  121. _headBtn.layer.masksToBounds = YES;
  122. _headBtn.backgroundColor = [UIColor whiteColor];
  123. [_headBtn addTarget:self action:@selector(clickHeadImage) forControlEvents:UIControlEventTouchUpInside];
  124. [_view1 addSubview:_headBtn];
  125. _familyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_headBtn.frame)+10, kScreenW, 17)];
  126. _familyLabel.text = ASLocalizedString(@"编辑公会头像");
  127. _familyLabel.textAlignment = NSTextAlignmentCenter;
  128. _familyLabel.font = [UIFont systemFontOfSize:17];
  129. _familyLabel.textColor = kAppGrayColor2;
  130. [_view1 addSubview:_familyLabel];
  131. _ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  132. _ensureBtn.frame = CGRectMake(25, 25, kScreenW-50, 40);
  133. _ensureBtn.backgroundColor = kAppMainColor;
  134. _ensureBtn.layer.cornerRadius = 20;
  135. _ensureBtn.layer.masksToBounds = YES;
  136. [_ensureBtn setTitle:ASLocalizedString(@"确认")forState:UIControlStateNormal];
  137. [_ensureBtn addTarget:self action:@selector(ensureBtnAction) forControlEvents:UIControlEventTouchUpInside];
  138. [_view5 addSubview:_ensureBtn];
  139. [self createDes];
  140. }
  141. - (void)createDes
  142. {
  143. //如果是创建公会
  144. if (_type==0) {
  145. [_headBtn setBackgroundImage:[UIImage imageNamed:@"me_addPhoto"] forState:UIControlStateNormal];
  146. _nameTextView.text =ASLocalizedString(@"请输入公会名称");
  147. _nameTextView.textColor = [UIColor lightGrayColor];
  148. _desTextView.text = ASLocalizedString(@"请输入公会宣言");
  149. _desTextView.textColor = [UIColor lightGrayColor];
  150. }
  151. //如果是重新编辑公会资料
  152. else if (_type==1)
  153. {
  154. // [_headBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:_model.family_logo] forState:UIControlStateNormal];
  155. [_headBtn sd_setImageWithURL:[NSURL URLWithString:_model.family_logo] forState:UIControlStateNormal placeholderImage:kDefaultPreloadHeadImg];
  156. _nameTextView.text = _model.family_name;
  157. //如果是未通过审核则可以编辑公会名称
  158. _nameTextView.editable = _canEditAll==1?YES:NO;
  159. _nameTextView.textAlignment = _canEditAll==1? NSTextAlignmentLeft:NSTextAlignmentCenter;
  160. // _nameTextView.textAlignment = NSTextAlignmentCenter;
  161. _nameTextView.textColor = [UIColor blackColor];
  162. _desTextView.text = _model.family_manifesto;
  163. _desTextView.textColor = [UIColor blackColor];
  164. }
  165. }
  166. //确认按钮
  167. - (void)ensureBtnAction
  168. {
  169. if ([_nameTextView.text isEqualToString:ASLocalizedString(@"请输入公会名称")]&&[_nameTextView.textColor isEqual:[UIColor lightGrayColor]]) {
  170. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会名称")];
  171. return;
  172. }
  173. if ([_desTextView.text isEqualToString:ASLocalizedString(@"请输入公会宣言")]&&[_desTextView.textColor isEqual:[UIColor lightGrayColor]]) {
  174. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会宣言")];
  175. return;
  176. }
  177. // _desTextView.insertDictationResultPlaceholder
  178. //如果是创建公会
  179. if (_type==0) {
  180. if (_urlStr.length<1) {
  181. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请编辑头像")];
  182. return;
  183. }
  184. if (_desTextView.text.length<1) {
  185. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会宣言")];
  186. return;
  187. }
  188. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  189. [mDict setObject:@"family" forKey:@"ctl"];
  190. [mDict setObject:@"create" forKey:@"act"];
  191. [mDict setObject:_urlStr forKey:@"family_logo"];
  192. [mDict setObject:_nameTextView.text forKey:@"family_name"];
  193. [mDict setObject:_desTextView.text forKey:@"family_manifesto"];
  194. [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在创建公会请等待")];
  195. [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  196. if ([responseJson toInt:@"status"]==1)
  197. {
  198. [[BGHUDHelper sharedInstance] syncStopLoading];
  199. [self.navigationController popViewControllerAnimated:YES];
  200. }
  201. else
  202. {
  203. [[BGHUDHelper sharedInstance] syncStopLoading];
  204. }
  205. } FailureBlock:^(NSError *error) {
  206. [[BGHUDHelper sharedInstance] syncStopLoading];
  207. }];
  208. }
  209. else if (_type==1) // 如果是修改公会资料
  210. {
  211. if (_canEditAll==1&&_nameTextView.text.length<1) {
  212. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会名称")];
  213. return;
  214. }
  215. if (_desTextView.text.length<1) {
  216. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会宣言")];
  217. return;
  218. }
  219. NSMutableDictionary *mDict = [NSMutableDictionary dictionary];
  220. [mDict setObject:@"family" forKey:@"ctl"];
  221. [mDict setObject:@"save" forKey:@"act"];
  222. [mDict setObject:_model.family_id forKey:@"family_id"];
  223. if(_urlStr!=nil)
  224. {
  225. [mDict setObject:_urlStr forKey:@"family_logo"];
  226. }
  227. else
  228. {
  229. [mDict setObject:@"" forKey:@"family_logo"];
  230. }
  231. if (_nameTextView.text!= nil) {
  232. [mDict setObject:_nameTextView.text forKey:@"family_name"];
  233. }
  234. if (_desTextView.text!= nil) {
  235. [mDict setObject:_desTextView.text forKey:@"family_manifesto"];
  236. }
  237. [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在上传修改请等待")];
  238. [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  239. if ([responseJson toInt:@"status"]==1)
  240. {
  241. [[BGHUDHelper sharedInstance] syncStopLoading];
  242. [self.navigationController popViewControllerAnimated:YES];
  243. }
  244. else
  245. {
  246. [[BGHUDHelper sharedInstance] syncStopLoading];
  247. }
  248. } FailureBlock:^(NSError *error) {
  249. [[BGHUDHelper sharedInstance] syncStopLoading];
  250. }];
  251. }
  252. }
  253. //编辑公会头像
  254. - (void)clickHeadImage
  255. {
  256. UIActionSheet *headImgSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
  257. [headImgSheet addButtonWithTitle:ASLocalizedString(@"相机")];
  258. [headImgSheet addButtonWithTitle:ASLocalizedString(@"从手机相册选择")];
  259. [headImgSheet addButtonWithTitle:ASLocalizedString(@"取消")];
  260. headImgSheet.cancelButtonIndex = headImgSheet.numberOfButtons-1;
  261. headImgSheet.delegate = self;
  262. [headImgSheet showInView:[UIApplication sharedApplication].keyWindow];
  263. }
  264. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  265. {
  266. if (buttonIndex == 0)
  267. {
  268. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  269. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  270. {
  271. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  272. picker.delegate = self;
  273. picker.allowsEditing = YES;
  274. picker.modalTransitionStyle = UIModalPresentationOverCurrentContext;
  275. }
  276. [self presentViewController:picker animated:YES completion:nil];
  277. }else if (buttonIndex == 1){
  278. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  279. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
  280. {
  281. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  282. picker.delegate = self;
  283. picker.allowsEditing = YES;
  284. picker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  285. }
  286. [self presentViewController:picker animated:YES completion:nil];
  287. }
  288. }
  289. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  290. UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
  291. [self.headBtn setImage:image forState:UIControlStateNormal];
  292. // if (self.BuguLive.appModel.open_sts == 1)
  293. // {
  294. // if ([_ossManager isSetRightParameter])
  295. // {
  296. // [self saveImage:image withName:@"1.png"];
  297. // _timeString= [_ossManager getObjectKeyString];
  298. // [_ossManager asyncPutImage:_timeString localFilePath:_uploadFilePath];
  299. // }
  300. //
  301. // }else
  302. // {
  303. NSString *nameImage = [NSString stringWithFormat:@"familyHeadImage.jpg"];
  304. [self saveImage:image withName:nameImage];
  305. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:nameImage];
  306. NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file:%@",fullPath]];
  307. [self pictureToNet:fileUrl];
  308. // }
  309. [picker dismissViewControllerAnimated:YES completion:nil];
  310. }
  311. - (void)pictureToNet:(NSURL *)pictureUrl
  312. {
  313. [[BGHUDHelper sharedInstance] syncLoading:nil];
  314. NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  315. NSString * photoName=[imageFile stringByAppendingPathComponent:@"familyHeadImage.jpg"];
  316. NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file:%@",photoName]];
  317. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  318. [parmDict setObject:@"avatar" forKey:@"ctl"];
  319. [parmDict setObject:@"uploadImage" forKey:@"act"];
  320. [parmDict setObject:self.user_id forKey:@"id"];
  321. [self.httpsManager POSTWithDict:parmDict andFileUrl:fileUrl SuccessBlock:^(NSDictionary *responseJson)
  322. {
  323. if ([responseJson toInt:@"status"] == 1)
  324. {
  325. NSString *nameImage = [NSString stringWithFormat:@"familyHeadImage.jpg"];;
  326. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:nameImage];
  327. UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
  328. [_headBtn setImage:savedImage forState:UIControlStateNormal];
  329. _urlStr = [responseJson toString:@"path"];
  330. [[BGHUDHelper sharedInstance] syncStopLoading];
  331. }
  332. } FailureBlock:^(NSError *error)
  333. {
  334. [[BGHUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:@"%@",error]];
  335. [[BGHUDHelper sharedInstance] syncStopLoading];
  336. }];
  337. }
  338. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  339. {
  340. [self dismissViewControllerAnimated:YES completion:^{}];
  341. }
  342. //点击返回按钮
  343. - (void)comeBack
  344. {
  345. [self.navigationController popViewControllerAnimated:YES];
  346. }
  347. - (BOOL)textViewShouldBeginEditing:(UITextView *)textView
  348. {
  349. if (textView==_nameTextView) {
  350. if ([_nameTextView.text isEqualToString:ASLocalizedString(@"请输入公会名称")]&&[_nameTextView.textColor isEqual:[UIColor lightGrayColor]]) {
  351. _nameTextView.text = @"";
  352. _nameTextView.textColor = [UIColor blackColor];
  353. }
  354. _nameTextView.textAlignment = NSTextAlignmentLeft;
  355. }
  356. else if (textView== _desTextView)
  357. {
  358. if ([_desTextView.text isEqualToString:ASLocalizedString(@"请输入公会宣言")]&&[_desTextView.textColor isEqual:[UIColor lightGrayColor]]) {
  359. _desTextView.text = @"";
  360. _desTextView.textColor= [UIColor blackColor];
  361. }
  362. }
  363. return YES;
  364. }
  365. - (void)textViewDidEndEditing:(UITextView *)textView
  366. {
  367. [textView resignFirstResponder];
  368. if (textView==_nameTextView) {
  369. if (_nameTextView.text.length<1) {
  370. _nameTextView.text =ASLocalizedString(@"请输入公会名称");
  371. _nameTextView.textColor = [UIColor lightGrayColor];
  372. // [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会名称")];
  373. _ensureBtn.enabled = NO;
  374. _ensureBtn.backgroundColor = kAppGrayColor3;
  375. }
  376. else
  377. {
  378. _ensureBtn.enabled = YES;
  379. _ensureBtn.backgroundColor = kAppMainColor;
  380. }
  381. }
  382. else if (textView==_desTextView)
  383. {
  384. if (_desTextView.text.length<1) {
  385. _desTextView.text = ASLocalizedString(@"请输入公会宣言");
  386. _desTextView.textColor = [UIColor lightGrayColor];
  387. // [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会宣言")];
  388. _ensureBtn.enabled = NO;
  389. _ensureBtn.backgroundColor = kAppGrayColor3;
  390. }
  391. else
  392. {
  393. _ensureBtn.enabled = YES;
  394. _ensureBtn.backgroundColor = kAppMainColor;
  395. }
  396. }
  397. }
  398. //- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
  399. //{
  400. // [UIMenuController sharedMenuController].menuVisible = NO; //donot display the menu
  401. // [_desTextView resignFirstResponder]; //do not allow the user to selected anything
  402. // [_nameTextView resignFirstResponder];
  403. // return NO;
  404. //
  405. //}
  406. @end