LPhoneRegistVC.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. //
  2. // LPhoneRegistVC.m
  3. // BuguLive
  4. //
  5. // Created by 丁凯 on 2017/6/15.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. #import "LPhoneRegistVC.h"
  8. //#import "BGOssManager.h"
  9. #import "LoginRecommendVC.h"
  10. @interface LPhoneRegistVC ()<UIActionSheetDelegate,UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
  11. //@property (nonatomic, strong) BGOssManager *ossManager; //oss 类
  12. @property (nonatomic, assign) int sexNum; //1-男,2-女
  13. @property (nonatomic, copy) NSString *timeString; //时间戳的字符串
  14. @property (nonatomic, copy) NSString *uploadFilePath; //
  15. @property (nonatomic, copy) NSString *urlString; //
  16. @end
  17. @implementation LPhoneRegistVC
  18. - (void)viewDidLoad
  19. {
  20. [super viewDidLoad];
  21. }
  22. - (void)viewWillAppear:(BOOL)animated
  23. {
  24. [super viewWillAppear:animated];
  25. [IQKeyboardManager sharedManager].enable = YES;
  26. [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
  27. [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
  28. [IQKeyboardManager sharedManager].keyboardDistanceFromTextField = 5.0f;
  29. self.topConstraint.constant = kTopHeight;
  30. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  31. }
  32. - (void)viewWillDisappear:(BOOL)animated
  33. {
  34. [super viewWillDisappear:animated];
  35. [IQKeyboardManager sharedManager].enable = NO;
  36. [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  37. }
  38. - (void)initFWUI
  39. {
  40. [super initFWUI];
  41. // if (self.BuguLive.appModel.open_sts == 1)
  42. // {
  43. // _ossManager = [[BGOssManager alloc]initWithDelegate:self];
  44. // }
  45. _urlString = @"";
  46. self.sexNum = 1;
  47. self.view.backgroundColor = kBackGroundColor;
  48. self.navigationItem.leftBarButtonItem=[UIBarButtonItem itemWithTarget:self action:@selector(comeBack) image:@"com_arrow_vc_back" highImage:@"com_arrow_vc_back"];
  49. self.navigationItem.title = ASLocalizedString(@"手机注册");
  50. self.headImgView.layer.cornerRadius = 30;
  51. self.headImgView.layer.masksToBounds = YES;
  52. self.nameFiled.delegate = self;
  53. self.headLabel.textColor = kAppGrayColor3;
  54. self.textLabel.textColor = kAppGrayColor3;
  55. self.sexLabel.textColor = kAppGrayColor3;
  56. self.nextBtn.layer.cornerRadius = 20;
  57. self.nextBtn.layer.masksToBounds = YES;
  58. self.nextBtn.backgroundColor = kAppMainColor;
  59. self.nameFiled.placeholder = ASLocalizedString(@"填写昵称");
  60. // [self.nameFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
  61. if ([[_userInfoDic allKeys] containsObject:@"head_image"] && [_userInfoDic toString:@"head_image"].length>0)
  62. {
  63. [self.headImgView sd_setImageWithURL:[NSURL URLWithString:[_userInfoDic toString:@"head_image"]] placeholderImage:kDefaultPreloadHeadImg];
  64. }
  65. if (self.userName.length>0)
  66. {
  67. self.nameFiled.text = self.userName;
  68. self.textLabel.text = [NSString stringWithFormat:@"%d/16",(int)self.nameFiled.text.length];
  69. }
  70. }
  71. - (void)comeBack
  72. {
  73. [self.navigationController popViewControllerAnimated:YES];
  74. }
  75. - (IBAction)btnClickBack:(UIButton *)sender {
  76. [self dismissViewControllerAnimated:YES completion:nil];
  77. }
  78. - (IBAction)btnClick:(UIButton *)sender
  79. {
  80. [self.nameFiled resignFirstResponder];
  81. switch (sender.tag) {
  82. case 0: //点击头像
  83. {
  84. UIActionSheet *headImgSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
  85. [headImgSheet addButtonWithTitle:ASLocalizedString(@"相机")];
  86. [headImgSheet addButtonWithTitle:ASLocalizedString(@"从手机相册选择")];
  87. [headImgSheet addButtonWithTitle:ASLocalizedString(@"取消")];
  88. headImgSheet.cancelButtonIndex = headImgSheet.numberOfButtons-1;
  89. headImgSheet.delegate = self;
  90. [headImgSheet showInView:[UIApplication sharedApplication].keyWindow];
  91. }
  92. break;
  93. case 1: //选择男性
  94. {
  95. _sexNum = 1;
  96. [_rightSexBtn setImage:[UIImage imageNamed:@"com_female_normal"] forState:UIControlStateNormal];
  97. [_leftSexBtn setImage:[UIImage imageNamed:@"com_new_sex_male"] forState:UIControlStateNormal];
  98. }
  99. break;
  100. case 2://选择女性
  101. {
  102. _sexNum = 2;
  103. [_leftSexBtn setImage:[UIImage imageNamed:@"com_male_normal"] forState:UIControlStateNormal];
  104. [_rightSexBtn setImage:[UIImage imageNamed:@"com_new_sex_female"] forState:UIControlStateNormal];
  105. }
  106. break;
  107. case 3://下一步
  108. {
  109. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  110. [parmDict setObject:@"login" forKey:@"ctl"];
  111. [parmDict setObject:@"do_update" forKey:@"act"];
  112. [parmDict setObject:self.used_id forKey:@"id"];
  113. if (self.BuguLive.appModel.open_sts == 1)
  114. {
  115. if (_urlString.length < 1)
  116. {
  117. if ([[_userInfoDic allKeys] containsObject:@"head_image"] && [_userInfoDic toString:@"head_image"].length>0)
  118. {
  119. [parmDict setObject:_userInfoDic[@"head_image"] forKey:@"head_image"];
  120. }
  121. else
  122. {
  123. [parmDict setObject:@"" forKey:@"head_image"];
  124. }
  125. }
  126. else
  127. {
  128. [parmDict setObject:_urlString forKey:@"head_image"];
  129. }
  130. }
  131. else
  132. {
  133. if (_urlString.length < 1 && [_urlString isEqualToString:@""] )
  134. {
  135. [parmDict setObject:@"" forKey:@"head_image"];
  136. }
  137. else
  138. {
  139. [parmDict setObject:_urlString forKey:@"head_image"];
  140. }
  141. }
  142. if (self.nameFiled.text.length < 1)
  143. {
  144. [FanweMessage alert:ASLocalizedString(@"请输入你的昵称")];
  145. return;
  146. }
  147. [parmDict setObject:self.nameFiled.text forKey:@"nick_name"];
  148. [parmDict setObject:[NSString stringWithFormat:@"%d",_sexNum] forKey:@"sex"];
  149. [self showMyHud];
  150. FWWeakify(self)
  151. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  152. {
  153. FWStrongify(self)
  154. if ([responseJson toInt:@"status"] == 1)
  155. {
  156. NSString *token = [NSString stringWithFormat:@"%@",responseJson[@"user_info"][@"token"]];
  157. [GlobalVariables sharedInstance].token = token;
  158. //进入登录推荐用户页面
  159. // LoginRecommendVC *phoneRegist = [[LoginRecommendVC alloc]init];
  160. //
  161. //
  162. // [self presentViewController:phoneRegist animated:YES completion:nil];
  163. [self hideMyHud];
  164. if ([BGIMLoginManager sharedInstance].loginParam.identifier.length) {
  165. [[AppDelegate sharedAppDelegate] enterMainUI];
  166. [BGIMLoginManager sharedInstance].loginParam.identifier = [responseJson toString:@"user_id"];
  167. }else{
  168. [BGIMLoginManager sharedInstance].loginParam.identifier = [responseJson toString:@"user_id"];
  169. [[BGIMLoginManager sharedInstance] getUserSig:^{
  170. [[AppDelegate sharedAppDelegate] enterMainUI];
  171. // [self hideMyHud];
  172. //
  173. } failed:^(int errId, NSString *errMsg) {
  174. // [self hideMyHud];
  175. }];
  176. }
  177. }else
  178. {
  179. [self hideMyHud];
  180. [FanweMessage alertHUD:[responseJson toString:@"error"]];
  181. }
  182. } FailureBlock:^(NSError *error)
  183. {
  184. FWStrongify(self)
  185. [self hideMyHud];
  186. }];
  187. }
  188. break;
  189. default:
  190. break;
  191. }
  192. }
  193. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  194. {
  195. if (buttonIndex == 0)
  196. {
  197. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  198. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  199. {
  200. picker.sourceType=UIImagePickerControllerSourceTypeCamera;
  201. picker.delegate = self;
  202. picker.allowsEditing = YES;
  203. picker.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
  204. }
  205. [self presentViewController:picker animated:YES completion:nil];
  206. }
  207. else if (buttonIndex == 1)
  208. {
  209. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  210. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
  211. {
  212. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  213. picker.delegate = self;
  214. picker.allowsEditing = YES;
  215. picker.modalPresentationStyle = UIModalPresentationOverCurrentContext;
  216. }
  217. [self presentViewController:picker animated:YES completion:nil];
  218. }
  219. }
  220. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  221. {
  222. [self.nameFiled resignFirstResponder];
  223. }
  224. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  225. {
  226. NSString *mediaType = [info objectForKey:UIImagePickerControllerEditedImage];
  227. if (mediaType){
  228. UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
  229. if (!image)
  230. {
  231. image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  232. }
  233. if(image){
  234. [_headImgView setImage:image];
  235. }
  236. // if (self.BuguLive.appModel.open_sts == 1)
  237. // {
  238. // if ([_ossManager isSetRightParameter])
  239. // {
  240. // [self saveImage:image withName:@"1.png"];
  241. // [self showMyHud];
  242. // _timeString = [_ossManager getObjectKeyString];
  243. // [_ossManager asyncPutImage:_timeString localFilePath:_uploadFilePath];
  244. // }
  245. //
  246. // }else
  247. // {
  248. NSData *data=UIImageJPEGRepresentation(image, 1);
  249. _headImgView.image = [UIImage imageWithData:data];
  250. [self saveImage:_headImgView.image WithName:@"image_head3.jpg"];
  251. [self performSelector:@selector(uploadAvatar) withObject:nil afterDelay:0.8];
  252. // }
  253. }
  254. [picker dismissViewControllerAnimated:YES completion:nil];
  255. }
  256. #pragma mark 代理回调
  257. - (void)uploadImageWithUrlStr:(NSString *)imageUrlStr withUploadStateCount:(int)stateCount
  258. {
  259. [self hideMyHud];
  260. if (stateCount == 0)
  261. {
  262. _urlString = [NSString stringWithFormat:@"./%@",_timeString];
  263. }
  264. else
  265. {
  266. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"上传头像失败")];
  267. }
  268. }
  269. #pragma mark 保存图片到document
  270. - (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
  271. {
  272. NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
  273. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
  274. [imageData writeToFile:fullPath atomically:NO];
  275. _uploadFilePath = fullPath;
  276. }
  277. - (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName
  278. {
  279. NSData* imageData = UIImagePNGRepresentation(tempImage);
  280. NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  281. NSString* documentsDirectory = [paths objectAtIndex:0];
  282. NSString* fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
  283. [imageData writeToFile:fullPathToFile atomically:NO];
  284. }
  285. - (void)uploadAvatar
  286. {
  287. [self showMyHud];
  288. NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  289. NSString *photoName=[imageFile stringByAppendingPathComponent:@"image_head3.jpg"];
  290. NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file:%@",photoName]];
  291. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  292. [parmDict setObject:@"avatar" forKey:@"ctl"];
  293. [parmDict setObject:@"uploadImage" forKey:@"act"];
  294. [parmDict setObject:self.used_id forKey:@"id"];
  295. FWWeakify(self)
  296. [self.httpsManager POSTWithDict:parmDict andFileUrl:fileUrl SuccessBlock:^(NSDictionary *responseJson)
  297. {
  298. FWStrongify(self)
  299. [self hideMyHud];
  300. if ([responseJson toInt:@"status"] == 1)
  301. {
  302. _urlString = [responseJson toString:@"path"];
  303. }
  304. } FailureBlock:^(NSError *error)
  305. {
  306. FWStrongify(self)
  307. [self hideMyHud];
  308. }];
  309. }
  310. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  311. {
  312. [self dismissViewControllerAnimated:YES completion:^{}];
  313. }
  314. #pragma mark
  315. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  316. {
  317. [textField resignFirstResponder];
  318. return YES;
  319. }
  320. //限制字符
  321. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
  322. {
  323. NSString *comcatstr = [textField.text stringByReplacingCharactersInRange:range withString:string];
  324. NSInteger caninputlen;
  325. caninputlen = 16 - comcatstr.length;
  326. if (caninputlen >= 0)
  327. {
  328. return YES;
  329. }
  330. else
  331. {
  332. NSInteger len = string.length + caninputlen;
  333. //防止当text.length + caninputlen < 0时,使得rg.length为一个非法最大正数出错
  334. NSRange rg = {0,MAX(len,0)};
  335. if (rg.length > 0)
  336. {
  337. NSString *s = [string substringWithRange:rg];
  338. [textField setText:[textField.text stringByReplacingCharactersInRange:range withString:s]];
  339. }
  340. return NO;
  341. }
  342. }
  343. - (void)textFieldDidEndEditing:(UITextField *)textField
  344. {
  345. self.textLabel.text = [NSString stringWithFormat:@"%d/16",(int)textField.text.length];
  346. }
  347. @end