EditSocietyViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. //
  2. // EditSocietyViewController.m
  3. // BuguLive
  4. //
  5. // Created by 王珂 on 17/1/22.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "EditSocietyViewController.h"
  9. #import "AFNetworking.h"
  10. //#import "BGOssManager.h"
  11. @interface EditSocietyViewController ()<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 * societyLabel;
  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 EditSocietyViewController
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. _dataDic = [NSMutableDictionary dictionary];
  39. // if (self.BuguLive.appModel.open_sts == 1)
  40. // {
  41. // _ossManager = [[BGOssManager alloc]initWithDelegate:self];
  42. // }
  43. [self createView];
  44. }
  45. -(void)viewWillAppear:(BOOL)animated
  46. {
  47. [super viewWillAppear:animated];
  48. [IQKeyboardManager sharedManager].enable = YES;
  49. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  50. }
  51. -(void)createView
  52. {
  53. // self.automaticallyAdjustsScrollViewInsets = NO;
  54. self.navigationItem.title = ASLocalizedString(@"公会资料修改");
  55. self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(comeBack) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  56. _view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, (kScreenH-64)*375/1200)];
  57. _view1.backgroundColor = kFamilyBackGroundColor;
  58. [self.view addSubview:_view1];
  59. _view2 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view1.frame), kScreenW, (kScreenH-64)*95/1200)];
  60. _view2.backgroundColor= [UIColor whiteColor];
  61. [self.view addSubview:_view2];
  62. _view3 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view2.frame), kScreenW, (kScreenH-64)*20/1200)];
  63. _view3.backgroundColor = kFamilyBackGroundColor;
  64. [self.view addSubview:_view3];
  65. _view4 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view3.frame), kScreenW, (kScreenH-64)*310/1200)];
  66. _view4.backgroundColor = [UIColor whiteColor];
  67. [self.view addSubview:_view4];
  68. _view5 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_view4.frame), kScreenW, (kScreenH-64)*410/1200)];
  69. _view5.backgroundColor = kFamilyBackGroundColor;
  70. [self.view addSubview:_view5];
  71. _nameTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, CGRectGetHeight(_view2.frame))];
  72. // _nameTextView.textAlignment = NSTextAlignmentCenter;
  73. // _nameTextView.insertDictationResultPlaceholder = @""
  74. _nameTextView.delegate = self;
  75. // _nameTextView.text =ASLocalizedString(@"请输入公会名称");
  76. // _nameTextView.textColor = [UIColor lightGrayColor];
  77. _nameTextView.font = [UIFont systemFontOfSize:15];
  78. // _nameTextView.editable = NO;
  79. [_view2 addSubview:_nameTextView];
  80. _desTextView = [[UITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, CGRectGetHeight(_view4.frame))];
  81. // _desTextView.text = ASLocalizedString(@"请输入公会宣言");
  82. // _desTextView.textColor = [UIColor lightGrayColor];
  83. _desTextView.delegate = self;
  84. _desTextView.font = [UIFont systemFontOfSize:15];
  85. [_view4 addSubview:_desTextView];
  86. [self createBtn];
  87. }
  88. -(void)viewWillDisappear:(BOOL)animated
  89. {
  90. [IQKeyboardManager sharedManager].enable = NO;
  91. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  92. }
  93. - (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
  94. {
  95. NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
  96. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
  97. [imageData writeToFile:fullPath atomically:NO];
  98. _uploadFilePath = fullPath;
  99. NSLog(@"uploadFilePath : %@", _uploadFilePath);
  100. }
  101. #pragma mark 代理回调
  102. - (void)uploadImageWithUrlStr:(NSString *)imageUrlStr withUploadStateCount:(int)stateCount
  103. {
  104. if (_HUD)
  105. {
  106. _HUD.hidden = YES;
  107. }
  108. if (stateCount == 0)
  109. {
  110. _urlStr = [NSString stringWithFormat:@"./%@",_timeString];
  111. NSLog(@"_urlString==%@",_urlStr);
  112. }
  113. }
  114. -(void)createBtn
  115. {
  116. _headBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  117. _headBtn.frame = CGRectMake((kScreenW-90)/2, (CGRectGetHeight(_view1.frame)-117)/2, 90, 90);
  118. _headBtn.backgroundColor = [UIColor whiteColor];
  119. _headBtn.layer.cornerRadius = 45;
  120. _headBtn.layer.masksToBounds = YES;
  121. _headBtn.backgroundColor = [UIColor whiteColor];
  122. [_headBtn addTarget:self action:@selector(clickHeadImage) forControlEvents:UIControlEventTouchUpInside];
  123. [_view1 addSubview:_headBtn];
  124. _societyLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_headBtn.frame)+10, kScreenW, 17)];
  125. _societyLabel.text = ASLocalizedString(@"编辑公会头像");
  126. _societyLabel.textAlignment = NSTextAlignmentCenter;
  127. _societyLabel.font = [UIFont systemFontOfSize:17];
  128. _societyLabel.textColor = kAppGrayColor2;
  129. [_view1 addSubview:_societyLabel];
  130. _ensureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  131. _ensureBtn.frame = CGRectMake(25, 25, kScreenW-50, 40);
  132. _ensureBtn.backgroundColor = kAppMainColor;
  133. _ensureBtn.layer.cornerRadius = 20;
  134. _ensureBtn.layer.masksToBounds = YES;
  135. [_ensureBtn setTitle:ASLocalizedString(@"确认")forState:UIControlStateNormal];
  136. [_ensureBtn addTarget:self action:@selector(ensureBtnAction) forControlEvents:UIControlEventTouchUpInside];
  137. [_view5 addSubview:_ensureBtn];
  138. [self createDes];
  139. }
  140. -(void)createDes
  141. {
  142. //如果是创建公会
  143. if (_type==0) {
  144. [_headBtn setBackgroundImage:[UIImage imageNamed:@"me_addPhoto"] forState:UIControlStateNormal];
  145. _nameTextView.text =ASLocalizedString(@"请输入公会名称");
  146. _nameTextView.textColor = [UIColor lightGrayColor];
  147. _desTextView.text = ASLocalizedString(@"请输入公会宣言");
  148. _desTextView.textColor = [UIColor lightGrayColor];
  149. }
  150. //如果是重新编辑公会资料
  151. else if (_type==1)
  152. {
  153. // [_headBtn sd_setBackgroundImageWithURL:[NSURL URLWithString:_model.family_logo] forState:UIControlStateNormal];
  154. [_headBtn sd_setImageWithURL:[NSURL URLWithString:self.sociaHead] forState:UIControlStateNormal placeholderImage:kDefaultPreloadHeadImg];
  155. _nameTextView.text = self.sociatyName;
  156. //如果是未通过审核则可以编辑公会名称
  157. _nameTextView.editable = YES;
  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 = self.sociatyManifasto;
  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:@"society_app" forKey:@"ctl"];
  190. [mDict setObject:@"create" forKey:@"act"];
  191. [mDict setObject:_urlStr forKey:@"logo"];
  192. [mDict setObject:_nameTextView.text forKey:@"name"];
  193. [mDict setObject:_desTextView.text forKey:@"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:@"society_app" forKey:@"ctl"];
  221. // [mDict setObject:@"society" forKey:@"ctl"];
  222. [mDict setObject:@"save" forKey:@"act"];
  223. [mDict setObject:[NSNumber numberWithInt:self.sociatyID] forKey:@"id"];
  224. if(_urlStr!=nil)
  225. {
  226. [mDict setObject:_urlStr forKey:@"logo"];
  227. }
  228. else
  229. {
  230. [mDict setObject:@"" forKey:@"logo"];
  231. }
  232. if (_nameTextView.text!= nil) {
  233. [mDict setObject:_nameTextView.text forKey:@"name"];
  234. }
  235. if (_desTextView.text!= nil) {
  236. [mDict setObject:_desTextView.text forKey:@"manifesto"];
  237. }
  238. [[BGHUDHelper sharedInstance] syncLoading:ASLocalizedString(@"正在上传修改请等待")];
  239. [self.httpsManager POSTWithParameters:mDict SuccessBlock:^(NSDictionary *responseJson) {
  240. if ([responseJson toInt:@"status"]==1)
  241. {
  242. [[BGHUDHelper sharedInstance] syncStopLoading];
  243. [self.navigationController popViewControllerAnimated:YES];
  244. }
  245. else
  246. {
  247. [[BGHUDHelper sharedInstance] syncStopLoading];
  248. }
  249. } FailureBlock:^(NSError *error) {
  250. [[BGHUDHelper sharedInstance] syncStopLoading];
  251. }];
  252. }
  253. }
  254. //编辑公会头像
  255. -(void)clickHeadImage
  256. {
  257. UIActionSheet *headImgSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
  258. [headImgSheet addButtonWithTitle:ASLocalizedString(@"相机")];
  259. [headImgSheet addButtonWithTitle:ASLocalizedString(@"从手机相册选择")];
  260. [headImgSheet addButtonWithTitle:ASLocalizedString(@"取消")];
  261. headImgSheet.cancelButtonIndex = headImgSheet.numberOfButtons-1;
  262. headImgSheet.delegate = self;
  263. [headImgSheet showInView:[UIApplication sharedApplication].keyWindow];
  264. }
  265. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  266. {
  267. if (buttonIndex == 0)
  268. {
  269. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  270. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  271. {
  272. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  273. picker.delegate = self;
  274. picker.allowsEditing = YES;
  275. picker.modalTransitionStyle = UIModalPresentationOverCurrentContext;
  276. }
  277. [self presentViewController:picker animated:YES completion:nil];
  278. }else if (buttonIndex == 1){
  279. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  280. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
  281. {
  282. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  283. picker.delegate = self;
  284. picker.allowsEditing = YES;
  285. picker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  286. }
  287. [self presentViewController:picker animated:YES completion:nil];
  288. }
  289. }
  290. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  291. UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
  292. [self.headBtn setImage:image forState:UIControlStateNormal];
  293. // if (self.BuguLive.appModel.open_sts == 1)
  294. // {
  295. // if ([_ossManager isSetRightParameter])
  296. // {
  297. // [self saveImage:image withName:@"1.png"];
  298. // _timeString= [_ossManager getObjectKeyString];
  299. // [_ossManager asyncPutImage:_timeString localFilePath:_uploadFilePath];
  300. // }
  301. // }else
  302. // {
  303. NSString *nameImage = [NSString stringWithFormat:@"societyHeadImage.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:@"societyHeadImage.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:@"societyHeadImage.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. else
  333. {
  334. [[BGHUDHelper sharedInstance] syncStopLoading];
  335. }
  336. } FailureBlock:^(NSError *error)
  337. {
  338. [[BGHUDHelper sharedInstance] tipMessage:[NSString stringWithFormat:@"%@",error]];
  339. [[BGHUDHelper sharedInstance] syncStopLoading];
  340. }];
  341. }
  342. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  343. {
  344. [self dismissViewControllerAnimated:YES completion:^{}];
  345. }
  346. //点击返回按钮
  347. - (void)comeBack
  348. {
  349. [self.navigationController popViewControllerAnimated:NO];
  350. }
  351. -(BOOL)textViewShouldBeginEditing:(UITextView *)textView
  352. {
  353. if (textView==_nameTextView) {
  354. if ([_nameTextView.text isEqualToString:ASLocalizedString(@"请输入公会名称")]&&[_nameTextView.textColor isEqual:[UIColor lightGrayColor]]) {
  355. _nameTextView.text = @"";
  356. _nameTextView.textColor = [UIColor blackColor];
  357. }
  358. _nameTextView.textAlignment = NSTextAlignmentLeft;
  359. }
  360. else if (textView== _desTextView)
  361. {
  362. if ([_desTextView.text isEqualToString:ASLocalizedString(@"请输入公会宣言")]&&[_desTextView.textColor isEqual:[UIColor lightGrayColor]]) {
  363. _desTextView.text = @"";
  364. _desTextView.textColor= [UIColor blackColor];
  365. }
  366. }
  367. return YES;
  368. }
  369. -(void)textViewDidEndEditing:(UITextView *)textView
  370. {
  371. [textView resignFirstResponder];
  372. if (textView==_nameTextView) {
  373. if (_nameTextView.text.length<1) {
  374. _nameTextView.text =ASLocalizedString(@"请输入公会名称");
  375. _nameTextView.textColor = [UIColor lightGrayColor];
  376. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请填写公会名称")];
  377. _ensureBtn.enabled = NO;
  378. _ensureBtn.backgroundColor = kAppGrayColor3;
  379. }
  380. else
  381. {
  382. _ensureBtn.enabled = YES;
  383. _ensureBtn.backgroundColor = kAppMainColor;
  384. }
  385. }
  386. else if (textView==_desTextView)
  387. {
  388. if (_desTextView.text.length<1) {
  389. _desTextView.text = ASLocalizedString(@"请输入公会宣言");
  390. _desTextView.textColor = [UIColor lightGrayColor];
  391. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"请输入公会宣言")];
  392. _ensureBtn.enabled = NO;
  393. _ensureBtn.backgroundColor = kAppGrayColor3;
  394. }
  395. else
  396. {
  397. _ensureBtn.enabled = YES;
  398. _ensureBtn.backgroundColor = kAppMainColor;
  399. }
  400. }
  401. }
  402. // 限制字数140
  403. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  404. {
  405. if (_desTextView.text.length > 140) {
  406. _desTextView.text = [_desTextView.text substringWithRange:NSMakeRange(0, 140)];
  407. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"超过最大字数限制")];
  408. }
  409. return YES;
  410. }
  411. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender
  412. {
  413. [UIMenuController sharedMenuController].menuVisible = NO; //donot display the menu
  414. [_desTextView resignFirstResponder]; //do not allow the user to selected anything
  415. [_nameTextView resignFirstResponder];
  416. return NO;
  417. }
  418. @end