BindPhoneController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //
  2. // BindPhoneController.m
  3. // model
  4. //
  5. // Created by JuYi on 2018/7/16.
  6. // Copyright © 2018年 Mine. All rights reserved.
  7. // 绑定手机
  8. #import "BindPhoneController.h"
  9. #import "TableDefaultCell.h"
  10. #import "TableWithVerificationCodeCell.h"
  11. @interface BindPhoneController ()<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
  12. {
  13. NSString *zhifubaoAccount;
  14. NSString *zhifubaoName;
  15. NSString *phoneNum;
  16. NSString *verifyCode;
  17. NSString *randomCode;
  18. }
  19. @property (nonatomic, strong) UITableView *tableView;
  20. @property (nonatomic, strong) TableDefaultCell *accountCell;
  21. @property (nonatomic, strong) TableDefaultCell *nameCell;
  22. @property (nonatomic, strong) TableDefaultCell *phoneCell;
  23. @property (nonatomic, strong) TableWithVerificationCodeCell *verificationCodeCell;
  24. @end
  25. @implementation BindPhoneController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. // Do any additional setup after loading the view.
  29. self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  30. UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];
  31. //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。
  32. tapGestureRecognizer.cancelsTouchesInView = NO;
  33. //将触摸事件添加到当前view
  34. [self.view addGestureRecognizer:tapGestureRecognizer];
  35. //设置导航条
  36. [self creatNavBar];
  37. //设置子试图
  38. [self creatSubViews];
  39. }
  40. #pragma mark -- 收起键盘
  41. -(void)keyboardHide:(UITapGestureRecognizer*)tap{
  42. [self.view endEditing:YES];
  43. }
  44. - (void)didReceiveMemoryWarning {
  45. [super didReceiveMemoryWarning];
  46. // Dispose of any resources that can be recreated.
  47. }
  48. - (void)creatNavBar {
  49. self.title = @"绑定手机";
  50. //
  51. UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
  52. btn.frame = CGRectMake(0, 0, 40, 40);
  53. [btn setImage:[UIImage imageNamed:@"fanhui2"] forState:UIControlStateNormal];
  54. btn.imageEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
  55. [btn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
  56. UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
  57. UIBarButtonItem *nagetiveSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
  58. // nagetiveSpacer.width = -12;//这个值可以根据自己需要自己调整
  59. self.navigationItem.leftBarButtonItems = @[nagetiveSpacer, leftItem];
  60. }
  61. - (void)backClick {
  62. [self.navigationController popViewControllerAnimated:YES];
  63. }
  64. - (void)creatSubViews {
  65. //
  66. UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 16)];
  67. headerView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  68. //
  69. UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight - 220)];
  70. footerView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  71. UIButton *bindingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  72. bindingBtn.frame = CGRectMake(20, 39, ScreenWidth - 40, 45);
  73. [bindingBtn setBackgroundImage:[UIImage imageNamed:@"tixian-1"] forState:UIControlStateNormal];
  74. [bindingBtn setTitle:@"绑定" forState:UIControlStateNormal];
  75. [bindingBtn setTitleColor:RGBValueColor(0xffffff, 1.0) forState:UIControlStateNormal];
  76. [bindingBtn addTarget:self action:@selector(bindingBtnAction) forControlEvents:UIControlEventTouchUpInside];
  77. [footerView addSubview:bindingBtn];
  78. //
  79. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
  80. self.tableView.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
  81. self.tableView.delegate = self;
  82. self.tableView.dataSource = self;
  83. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  84. self.tableView.tableHeaderView = headerView;
  85. self.tableView.tableFooterView = footerView;
  86. [self.view addSubview:self.tableView];
  87. [self.tableView registerClass:[TableDefaultCell class] forCellReuseIdentifier:@"DefaultCell"];
  88. [self.tableView registerClass:[TableWithVerificationCodeCell class] forCellReuseIdentifier:@"VerificationCodeCell"];
  89. }
  90. #pragma mark -- UITableViewDelegate, UITableViewDataSource
  91. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  92. return 4;
  93. }
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  95. if (indexPath.row == 0) {
  96. TableDefaultCell *accountCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell" forIndexPath:indexPath];
  97. accountCell.titleLabel.text = @"提现账户";
  98. accountCell.contentTF.placeholder = @"请输入支付宝账户";
  99. accountCell.contentTF.keyboardType = UIKeyboardTypeDefault;
  100. accountCell.contentTF.delegate = self;
  101. accountCell.contentTF.tag = 1000;
  102. self.accountCell = accountCell;
  103. return accountCell;
  104. } else if (indexPath.row == 1) {
  105. TableDefaultCell *nameCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell" forIndexPath:indexPath];
  106. nameCell.titleLabel.text = @"账户姓名";
  107. nameCell.contentTF.placeholder = @"请输入账户姓名";
  108. nameCell.contentTF.keyboardType = UIKeyboardTypeDefault;
  109. nameCell.contentTF.delegate = self;
  110. nameCell.contentTF.tag = 1001;
  111. self.nameCell = nameCell;
  112. return nameCell;
  113. } else if (indexPath.row == 2) {
  114. TableDefaultCell *phoneCell = [tableView dequeueReusableCellWithIdentifier:@"DefaultCell" forIndexPath:indexPath];
  115. phoneCell.titleLabel.text = @"手机号";
  116. phoneCell.contentTF.placeholder = @"请输入手机号";
  117. phoneCell.contentTF.keyboardType = UIKeyboardTypePhonePad;
  118. phoneCell.contentTF.delegate = self;
  119. phoneCell.contentTF.tag = 1002;
  120. self.phoneCell = phoneCell;
  121. return phoneCell;
  122. } else {
  123. TableWithVerificationCodeCell *verificationCodeCell = [tableView dequeueReusableCellWithIdentifier:@"VerificationCodeCell" forIndexPath:indexPath];
  124. verificationCodeCell.titleLabel.text = @"验证码";
  125. verificationCodeCell.contentTF.placeholder = @"请输入手机验证码";
  126. verificationCodeCell.contentTF.keyboardType = UIKeyboardTypeNumberPad;
  127. verificationCodeCell.contentTF.delegate = self;
  128. verificationCodeCell.contentTF.tag = 1003;
  129. [verificationCodeCell.codeBtn addTarget:self action:@selector(codeBtnAction) forControlEvents:UIControlEventTouchUpInside];
  130. self.verificationCodeCell = verificationCodeCell;
  131. return verificationCodeCell;
  132. }
  133. }
  134. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  135. return 50;
  136. }
  137. #pragma mark -- 绑定按钮
  138. - (void)bindingBtnAction {
  139. if (zhifubaoAccount.length == 0) {
  140. [MBProgressHUD showInfo:@"请输入支付宝账户"];
  141. return;
  142. }
  143. if (zhifubaoName.length == 0) {
  144. [MBProgressHUD showInfo:@"请输入账户姓名"];
  145. return;
  146. }
  147. if (phoneNum.length == 0) {
  148. [MBProgressHUD showInfo:@"请填写正确的手机号"];
  149. return;
  150. }
  151. if (verifyCode.length == 0 || ![verifyCode isEqualToString:randomCode]) {
  152. [MBProgressHUD showInfo:@"请填写正确的验证码"];
  153. return;
  154. }
  155. [self postBindingRequestToNetworking];
  156. }
  157. #pragma mark -- 网络请求
  158. -(void)postBindingRequestToNetworking{
  159. [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  160. ModelUser *modelUser = [ModelUser user];
  161. NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:modelUser.modelpk,@"modelpk",zhifubaoAccount,@"alipayid",zhifubaoName,@"name", nil];
  162. [YanCNetWorkManager requestPostWithURLStr:Url_alipayoutcashmodify(PublicUrl) parameters:dic finish:^(id dataDic) {
  163. [MBProgressHUD hideHUDForView:self.view animated:YES];
  164. NSString *issuccess = dataDic[@"msg"];
  165. if ([issuccess isEqualToString:@"success"]) {
  166. [MBProgressHUD showSuccess:@"绑定成功"];
  167. [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
  168. } else {
  169. [MBProgressHUD showInfo:@"请求失败!"];
  170. }
  171. } enError:^(NSError *error) {
  172. [MBProgressHUD hideHUDForView:self.view animated:YES];
  173. }];
  174. }
  175. #pragma mark -- 倒计时
  176. - (void)codeBtnAction {
  177. if ([Helper valiMobile:phoneNum]) {
  178. [self getVerifyCode];
  179. [self startCountdownAction];
  180. }else{
  181. [MBProgressHUD showInfo:@"请填写正确的手机号"];
  182. }
  183. }
  184. -(void)getVerifyCode{
  185. NSString *sn = @"SDK-FHD-010-00328";
  186. //NSString *md5Pwd = [MD5String md5String:[NSString stringWithFormat:@"%@%@",sn,password]];
  187. NSString *Md5Pwd = @"54774CB6E781A142206F756B3E18142C";
  188. int a = arc4random() % 100000;
  189. randomCode = [NSString stringWithFormat:@"%06d", a];
  190. NSString *content = [NSString stringWithFormat:@"【千模科技】您正在进行提现账号绑定,验证码%@,5分钟内有效。请勿泄露。来千模多挣钱,有钱日子天天过年。非本人操作,请勿理会。",randomCode];
  191. NSString *str = [NSString stringWithFormat:@"http://sdk.entinfo.cn:8061/mdsmssend.ashx?sn=%@&pwd=%@&mobile=%@&content=%@&ext=&stime=&rrid=&msgfmt=",sn,Md5Pwd,phoneNum,content];
  192. AFHTTPSessionManager *manager =[AFHTTPSessionManager manager];
  193. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  194. [manager GET:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
  195. NSString *result = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
  196. NSLog(@"%@",result);
  197. } failure:^(NSURLSessionDataTask *task, NSError *error) {
  198. NSLog(@"%@",error);
  199. }];
  200. }
  201. //获取验证码按钮开始倒计时响应事件
  202. - (void)startCountdownAction{
  203. __block int timeout = 60;//倒计时时间
  204. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
  205. dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER,0, 0,queue);
  206. dispatch_source_set_timer(_timer,dispatch_walltime(NULL,0),1.0*NSEC_PER_SEC,0); //每秒执行
  207. dispatch_source_set_event_handler(_timer, ^{
  208. if(timeout <= 1){//倒计时结束,关闭
  209. dispatch_source_cancel(_timer);
  210. dispatch_async(dispatch_get_main_queue(), ^{
  211. //设置界面的按钮显示根据自己需求设置
  212. [self.verificationCodeCell.codeBtn setTitle:@"重新获取"forState:UIControlStateNormal];
  213. self.verificationCodeCell.codeBtn.userInteractionEnabled = YES;
  214. });
  215. }else{
  216. int seconds = timeout;
  217. NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
  218. dispatch_async(dispatch_get_main_queue(), ^{
  219. //让按钮变为不可点击的灰色
  220. //并且关掉按钮的交互
  221. self.verificationCodeCell.codeBtn.userInteractionEnabled = NO;
  222. //设置界面的按钮显示根据自己需求设置
  223. [UIView beginAnimations:nil context:nil];
  224. [UIView setAnimationDuration:1];
  225. [self.verificationCodeCell.codeBtn setTitle:[NSString stringWithFormat:@"%@s",strTime]forState:UIControlStateNormal];
  226. [UIView commitAnimations];
  227. });
  228. timeout--;
  229. }
  230. });
  231. dispatch_resume(_timer);
  232. }
  233. #pragma mark -- UITextFieldDelegate
  234. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  235. if([textField viewWithTag:1002]){
  236. if(string.length > 0 && (![NSString amtchString:string checkString:@"[0-9]"]||[textField.text length]==11)){
  237. return NO;
  238. }
  239. } else if([textField viewWithTag:1003]) {
  240. if(string.length > 0 && [textField.text length] == 6){
  241. return NO;
  242. }
  243. }
  244. return YES;
  245. }
  246. - (void)textFieldDidEndEditing:(UITextField *)textField{
  247. if([textField viewWithTag:1000]){
  248. zhifubaoAccount = textField.text;
  249. }else if ([textField viewWithTag:1001]){
  250. zhifubaoName = textField.text;
  251. }else if ([textField viewWithTag:1002]){
  252. phoneNum = textField.text;
  253. }else if ([textField viewWithTag:1003]){
  254. verifyCode = textField.text;
  255. }
  256. }
  257. @end