SignUpVC.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. //
  2. // SignUpVC.m
  3. // LN_School
  4. //
  5. // Created by apple on 2017/4/11.
  6. // Copyright © 2017年 Danson. All rights reserved.
  7. //
  8. #import "SignUpVC.h"
  9. #import "applyCell.h"
  10. @interface SignUpVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIActionSheetDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
  11. {
  12. UITableView *mainTableView;
  13. NSMutableArray *titleArray;
  14. NSArray *dataArray;
  15. NSArray *sheetsArray;
  16. NSString *bustype;
  17. NSString *headImgPath;
  18. }
  19. @end
  20. @implementation SignUpVC
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self myInit];
  24. }
  25. -(void)myInit
  26. {
  27. [self setTitle:@"学员报名"];
  28. self.view.backgroundColor = KBackGroundColor;
  29. [self goBackByNavigation];
  30. //报名类型 默认为 初领
  31. bustype = @"0";
  32. NSArray *array = @[@[@"培训车型",@"证件类型",@"报名类型",@"培训模式",@"获取照片"],@[@"姓名",@"性别",@"证件号码",@"手机号码",@"地址"],@[@"推荐人"]];
  33. titleArray = [NSMutableArray arrayWithArray:array];
  34. dataArray = @[[NSMutableArray arrayWithArray:@[@"C1 >",@"身份证 >",@"初领 >",@"传统模式 >",@"点击拍照 >"]],[NSMutableArray arrayWithArray:@[@"请输入姓名",@"请输入性别",@"请输入证件号码",@"请输入手机号码",@"请输入地址"]],[NSMutableArray arrayWithArray:@[@"请输入推荐人"]],[NSMutableArray arrayWithArray:@[@"请输入原车型(例如:C1)",@"请输入原驾驶证号",@"请输入初领证日期"]]];
  35. mainTableView = [[UITableView alloc] initWithFrame:kFrame style:UITableViewStyleGrouped];
  36. mainTableView.height -= kNavOffSet;
  37. mainTableView.estimatedSectionHeaderHeight = 0;
  38. mainTableView.estimatedSectionFooterHeight = 0;
  39. [mainTableView setDelegate:self];
  40. [mainTableView setDataSource:self];
  41. [self.view addSubview:mainTableView];
  42. // UIView *view = [[UIView alloc] initWithFrame:CGRectZero];
  43. // mainTableView.tableHeaderView = view;
  44. UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kSize.width, 100)];
  45. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(10, 25, kSize.width - 20, 50)];
  46. btn.backgroundColor = defGreen;
  47. [btn borderCornorRadios:5];
  48. [btn setTitle:@"提交" textColor:[UIColor whiteColor] font:Font18 fotState:UIControlStateNormal];
  49. [btn target:self Tag:1];
  50. [view addSubview:btn];
  51. mainTableView.tableFooterView = view;
  52. }
  53. - (void)btnClick:(UIButton *)sender
  54. {
  55. if (sender.tag == 1) {
  56. //提交
  57. [self.view endEditing:YES];
  58. // [mainTableView setEditing:YES];
  59. // [sender becomeFirstResponder];
  60. // if (!myDelegate.isSchool) {
  61. // ShowMsg(@"驾校管理员才可以进行此操作!");
  62. // return;
  63. // }
  64. [self addStudent];
  65. }
  66. }
  67. #pragma mark 拍照
  68. - (void)takePhoto { //100*120
  69. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  70. //创建pickerVC
  71. UIImagePickerController * pickerVC = [[UIImagePickerController alloc]init];
  72. //设置图片来源类型
  73. pickerVC.sourceType = UIImagePickerControllerSourceTypeCamera;
  74. //是否可编辑。
  75. pickerVC.allowsEditing = YES;
  76. //设置代理
  77. pickerVC.delegate=self;
  78. //前置摄像头
  79. pickerVC.cameraDevice = UIImagePickerControllerCameraDeviceRear;
  80. //拍照模式
  81. pickerVC.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
  82. //弹出pickerVC
  83. [self presentViewController:pickerVC animated:YES completion:nil];
  84. }else{
  85. ShowMsg(@"相机打开失败");
  86. }
  87. }
  88. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  89. {
  90. if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:@"public.image"])
  91. {
  92. //找到源图片
  93. //UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
  94. //找到编辑后的图片
  95. UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
  96. NSData *data = UIImageJPEGRepresentation([editedImage scaledAndCutToSize:CGSizeMake(100, 120)], 0.5);
  97. NSString *imgString = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  98. [self uploadHeadImg:imgString];
  99. }
  100. [self dismissViewControllerAnimated:YES completion:nil];
  101. }
  102. #pragma mark - tableview delegate
  103. -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  104. {
  105. return titleArray.count;
  106. }
  107. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  108. {
  109. return [titleArray[section] count];
  110. }
  111. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  112. {
  113. applyCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  114. if (cell == nil)
  115. {
  116. cell = [[applyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
  117. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  118. }
  119. [cell.titLabel setText:[titleArray[indexPath.section] objectAtIndex:indexPath.row]];
  120. NSArray *detailArray = dataArray[indexPath.section];
  121. cell.detailField.delegate = self;
  122. cell.detailField.tag = indexPath.section * 10 + indexPath.row;
  123. if (indexPath.section == 0)
  124. {
  125. cell.detailField.userInteractionEnabled = NO;
  126. cell.detailField.textColor = defGreen;
  127. cell.detailField.text = detailArray[indexPath.row];
  128. }else {
  129. cell.detailField.userInteractionEnabled = YES;
  130. NSArray *array = @[@"请输入姓名",@"请输入性别",@"请输入证件号码",@"请输入手机号码",@"请输入地址",@"请输入推荐人",@"请输入原车型(例如:C1)",@"请输入原驾驶证号",@"请输入初领证日期"];
  131. if ([array containsObject:detailArray[indexPath.row]]) {
  132. cell.detailField.text = @"";
  133. cell.detailField.placeholder = detailArray[indexPath.row];
  134. }else {
  135. cell.detailField.textColor = KTitleColor;
  136. cell.detailField.text = detailArray[indexPath.row];
  137. }
  138. }
  139. return cell;
  140. }
  141. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. [self.view endEditing:YES];
  144. if (indexPath.section == 0)
  145. {
  146. if (indexPath.row == 4) {
  147. //拍照
  148. [self takePhoto];
  149. return;
  150. }
  151. UIActionSheet *myActionSheet;
  152. if (indexPath.row == 0) {
  153. // sheetsArray = @[@"A1-大型客车", @"A2-牵引车",@"A3-城市公交车",@"B1-中型客车",@"B2-大型货车",@"C1-小型汽车",@"C2-小型自动挡汽车",@"C3-低速载货汽车",@"C4-三轮汽车",@"C5-残疾人专用小型自动挡载客汽车",@"D-普通三轮摩托车",@"E-普通二轮摩托车",@"F-轻便摩托车",@"M-轮式自行机械车",@"N-无轨电车",@"P-有轨电车"];
  154. sheetsArray = [defUser.userDict[@"busiscope"] componentsSeparatedByString:@","];
  155. myActionSheet = [[UIActionSheet alloc] initWithTitle:@"选择培训车型" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
  156. for (NSString *title in sheetsArray) {
  157. [myActionSheet addButtonWithTitle:title];
  158. }
  159. [myActionSheet addButtonWithTitle:@"取消"];
  160. myActionSheet.cancelButtonIndex = myActionSheet.numberOfButtons - 1;
  161. }else if (indexPath.row == 1) {
  162. sheetsArray = @[@"身份证",@"护照",@"军官证",@"其它"];
  163. myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"身份证",@"护照",@"军官证",@"其它", nil];
  164. }else if (indexPath.row == 2) {
  165. sheetsArray = @[@"初领-0",@"增驾-1",@"其它-9"];
  166. myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"初领",@"增驾",@"其它", nil];
  167. }else if (indexPath.row == 3) {
  168. sheetsArray = @[@"传统模式",@"先学后付",@"分段式收费"];
  169. myActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"传统模式",@"先学后付",@"分段式收费", nil];
  170. }
  171. myActionSheet.tag = indexPath.row;
  172. [myActionSheet showInView:self.view];
  173. }
  174. }
  175. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  176. {
  177. return .1;
  178. }
  179. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  180. {
  181. return 25;
  182. }
  183. -(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
  184. //NSLog(@"1");
  185. [self.view endEditing:YES];
  186. }
  187. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  188. {
  189. if (buttonIndex == actionSheet.cancelButtonIndex) {
  190. return;
  191. }
  192. NSMutableArray *array = [dataArray firstObject];
  193. NSString *detailString = sheetsArray[buttonIndex];
  194. if (actionSheet.tag == 2) { //报名类型
  195. detailString = [[sheetsArray[buttonIndex] componentsSeparatedByString:@"-"] firstObject];
  196. bustype = [[sheetsArray[buttonIndex] componentsSeparatedByString:@"-"] lastObject];
  197. if ([bustype isEqualToString:@"1"]) { //mainTableView新增一个section
  198. [titleArray addObject:@[@"原车型",@"原驾驶证号",@"初领证日期"]];
  199. [array replaceObjectAtIndex:actionSheet.tag withObject:[NSString stringWithFormat:@"%@ >",detailString]];
  200. [mainTableView reloadData];
  201. return;
  202. }else{
  203. if (titleArray.count > 3) {
  204. [titleArray removeLastObject];
  205. }
  206. }
  207. }
  208. [array replaceObjectAtIndex:actionSheet.tag withObject:[NSString stringWithFormat:@"%@ >",detailString]];
  209. // NSIndexPath *indexpath = [NSIndexPath indexPathForRow:actionSheet.tag inSection:0];
  210. // [mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexpath,nil] withRowAnimation:UITableViewRowAnimationNone];
  211. [mainTableView reloadData];//上面方法二次调用引起崩溃
  212. }
  213. #pragma mark textField
  214. -(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
  215. {
  216. NSIndexPath *indexpath = indexpath= [NSIndexPath indexPathForRow:textField.tag%10 inSection:textField.tag/10];;
  217. //获取当前cell在tableview中的位置
  218. CGRect rectintableview=[mainTableView rectForRowAtIndexPath:indexpath];
  219. [mainTableView setContentOffset:CGPointMake(mainTableView.contentOffset.x,rectintableview.origin.y-25) animated:YES];
  220. return YES;
  221. }
  222. -(BOOL)textFieldShouldEndEditing:(UITextField *)textField
  223. {
  224. NSString *textString = textField.text;
  225. if (textString.length < 1) {
  226. textString = @"请输入";
  227. }
  228. NSMutableArray *array = dataArray[textField.tag/10];
  229. [array replaceObjectAtIndex:textField.tag%10 withObject:textString];
  230. [mainTableView reloadData];
  231. return YES;
  232. }
  233. -(BOOL)textFieldShouldReturn:(UITextField *)textField
  234. {
  235. [textField endEditing:YES];
  236. return YES;
  237. }
  238. #pragma mark 数据请求
  239. -(void)uploadHeadImg:(NSString *)image
  240. {
  241. if (![NetManager connectedToNetWork]) {
  242. showMsgUnconnect();
  243. return;
  244. }
  245. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  246. [dic setObject:image forKey:@"content"];
  247. [dic setObject:@"" forKey:@"keyword"];
  248. [dic setObject:@"2" forKey:@"type"];
  249. NSString *method = @"upload-2";
  250. [MBProgressHUD showLoadToView:self.view];
  251. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
  252. [MBProgressHUD hideHUDForView:self.view];
  253. if (!root) {
  254. [self showUPloadImgAlertWithImg:image];
  255. return;
  256. }
  257. if ([root[@"code"] isEqualToString:@"1"]) {
  258. [self showUPloadImgAlertWithImg:image];
  259. return;
  260. }
  261. //上传头像成功
  262. headImgPath = root[@"body"];
  263. NSMutableArray *array = [dataArray firstObject];
  264. [array replaceObjectAtIndex:4 withObject:@"拍照成功,点击重拍 >"];
  265. NSIndexPath *indexpath = [NSIndexPath indexPathForRow:3 inSection:0];
  266. [mainTableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexpath,nil] withRowAnimation:UITableViewRowAnimationNone];
  267. }];
  268. }
  269. - (void)showUPloadImgAlertWithImg:(NSString *)image {
  270. UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"图片上传失败!" preferredStyle:UIAlertControllerStyleAlert];
  271. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
  272. UIAlertAction *reGoAction = [UIAlertAction actionWithTitle:@"再试一次" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  273. [self uploadHeadImg:image];
  274. }];
  275. [alert addAction:cancelAction];
  276. [alert addAction:reGoAction];
  277. [self presentViewController:alert animated:YES completion:nil];
  278. }
  279. -(void)addStudent
  280. {
  281. if (headImgPath.length < 1) {
  282. ShowMsg(@"请采集照片");
  283. headImgPath = @"";
  284. return;
  285. }
  286. NSInteger num = 3;
  287. if ([bustype isEqualToString:@"1"]) {
  288. num = 4;
  289. }
  290. NSString *name, *idcard, *sex, *phone, *source, *cardtype, *traintype, *perdritype, *drilicnum, *fstdrilicdate, *address, *trainMode;
  291. name=idcard=sex=phone=source=cardtype=traintype=perdritype=drilicnum=fstdrilicdate=address = @"";
  292. for (int i = 0; i < num; i ++) {
  293. NSMutableArray *array = dataArray[i];
  294. for (int j = 0; j < array.count; j ++) {
  295. NSString *string = array[j];
  296. if ([string containsString:@"请输入"] || string.length == 0) {
  297. ShowMsg(@"您还有信息未录入");
  298. return;
  299. }
  300. switch (i) {
  301. case 0:
  302. if (j == 0) {
  303. traintype = [[string componentsSeparatedByString:@" "] firstObject];
  304. }else if (j == 1) {
  305. cardtype = [[string componentsSeparatedByString:@" "] firstObject];
  306. //@[@"身份证",@"护照",@"军官证",@"其他"];
  307. if ([cardtype isEqualToString:@"身份证"]) {
  308. cardtype = @"1";
  309. }else if ([cardtype isEqualToString:@"护照"]) {
  310. cardtype = @"2";
  311. }else if ([cardtype isEqualToString:@"军官证"]) {
  312. cardtype = @"3";
  313. }else if ([cardtype isEqualToString:@"其它"]) {
  314. cardtype = @"4";
  315. }
  316. }else if (j == 3){
  317. trainMode = [[string componentsSeparatedByString:@" "] firstObject];
  318. //培训模式 0 传统模式 1 先学后付 2 分段式收费
  319. if ([trainMode isEqualToString:@"传统模式"]) {
  320. trainMode = @"0";
  321. }else if ([trainMode isEqualToString:@"先学后付"]) {
  322. trainMode = @"1";
  323. }else if ([trainMode isEqualToString:@"分段式收费"]) {
  324. trainMode = @"2";
  325. }
  326. }
  327. break;
  328. case 1:
  329. if (j == 0) {
  330. name = string;
  331. }
  332. if (j == 1) {
  333. if (![string isEqualToString:@"男"] && ![string isEqualToString:@"女"]) {
  334. ShowMsg(@"性别输入错误");
  335. return;
  336. }
  337. sex = [string isEqualToString:@"男"]?@"1":@"2";
  338. }
  339. if (j == 2) {
  340. idcard = string;
  341. }
  342. if (j == 3) {
  343. if (![Tools isMobileNumber:string]) {
  344. ShowMsg(@"请输入正确的手机号码");
  345. return;
  346. }
  347. phone = string;
  348. }
  349. if (j == 4) {
  350. address = string;
  351. }
  352. break;
  353. case 2:
  354. source = string;
  355. break;
  356. case 3:
  357. if (j == 0) {
  358. perdritype = string;
  359. }
  360. if (j == 1) {
  361. drilicnum = string;
  362. }
  363. if (j == 2) {
  364. fstdrilicdate = string;
  365. }
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. }
  372. if (![NetManager connectedToNetWork]) {
  373. showMsgUnconnect();
  374. return;
  375. }
  376. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  377. [dic setObject:defUser.userDict[@"id"] forKey:@"userId"];
  378. [dic setObject:name forKey:@"name"];
  379. [dic setObject:idcard forKey:@"idcard"];
  380. [dic setObject:sex forKey:@"sex"];
  381. [dic setObject:phone forKey:@"phone"];
  382. [dic setObject:source forKey:@"source"];
  383. [dic setObject:bustype forKey:@"bustype"];
  384. [dic setObject:traintype forKey:@"traintype"];
  385. [dic setObject:perdritype forKey:@"perdritype"];
  386. [dic setObject:drilicnum forKey:@"drilicnum"];
  387. [dic setObject:fstdrilicdate forKey:@"fstdrilicdate"];
  388. [dic setObject:address forKey:@"address"];
  389. [dic setObject:headImgPath forKey:@"photo"];
  390. [dic setObject:cardtype forKey:@"cardtype"];
  391. [dic setObject:trainMode forKey:@"trainMode"];
  392. NSString *method = @"addStudent";
  393. [MBProgressHUD showLoadToView:self.view];
  394. [NetManager requestAnythingWithURL:method dictionary:dic dataArray:nil completion:^(NSDictionary *root) {
  395. [MBProgressHUD hideHUDForView:self.view];
  396. if (!root) {
  397. ShowMsg(@"提交失败,请重试");
  398. return;
  399. }
  400. if ([root[@"code"] isEqualToString:@"1"]) {
  401. ShowMsg(root[@"msg"]);
  402. return;
  403. }
  404. ShowMsg(@"报名成功!");
  405. [self.navigationController popViewControllerAnimated:YES];
  406. }];
  407. }
  408. #pragma mark - 限制输入字符
  409. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  410. NSCharacterSet *cs;
  411. //invertedSet方法是去反字符,把所有的除了NUMBERS 里的字符都找出来(包含去空格功能)
  412. cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789n"]invertedSet];
  413. // componentsJoinedByString 用于根据一个字符串来将数组连接成一个新的字符串。
  414. NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
  415. BOOL canChange = [string isEqualToString:filtered];
  416. if (textField.tag == 12 || textField.tag == 13) {
  417. return canChange;
  418. }
  419. return YES;
  420. //这样写了以后,输入非数字时不能输入
  421. }
  422. - (void)didReceiveMemoryWarning {
  423. [super didReceiveMemoryWarning];
  424. }
  425. @end