BindPhoneController.m 12 KB

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