// // KXMainNewWebViewController.m // CAISHEN // // Created by jikaipeng on 2017/10/16. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXMainNewWebViewController.h" #import "KXSelectTableViewCell.h" #import "KXLogginViewController.h" #import #import "KXMainWebView2ViewController.h" @interface KXMainNewWebViewController () { UIView *_selectView; // BOOL _selected; } @property (nonatomic,strong)UITableView *selectTableView; @property (nonatomic,strong)NSArray *monthArray; @property (nonatomic,assign)NSInteger month; @property (nonatomic,strong)UIView *proView; @property (nonatomic,assign)BOOL agreeBool; @property (nonatomic,strong)UIView *topView; @property (nonatomic,strong)UIView *backView; @property (nonatomic,strong)NSArray *protocolArray; @end @implementation KXMainNewWebViewController - (void)viewDidLoad { [super viewDidLoad]; self.agreeBool=YES; // Do any additional setup after loading the view. self.name = @"贷款申请"; [self setUpUI]; [self createllSelectView]; // CGPoint point=self.view.center; // point.y=self.view.bounds.size.height-220; // self.proView.center=point; [self setPro]; [self addViews]; } /**********************************************/ -(void)addViews{ [self.view addSubview:self.backView]; [self.view addSubview:self.topView]; self.topView.hidden=YES; self.backView.hidden=YES; } -(void)agreeAction:(UIButton *)sender{ self.agreeBool=!self.agreeBool; sender.selected=self.agreeBool; } -(void)protocolAction:(UIButton *)sender{ self.backView.hidden=NO; self.topView.hidden=NO; long indexSender=sender.tag-1000; UILabel *titleLabel=[self.topView viewWithTag:764]; titleLabel.text=sender.titleLabel.text; UIWebView *content=[self.topView viewWithTag:766]; content.delegate=self; NSString *path = [[NSBundle mainBundle] bundlePath]; NSURL *baseURL = [NSURL fileURLWithPath:path]; NSString * htmlPath = [[NSBundle mainBundle] pathForResource:self.protocolArray[indexSender][@"html"] ofType:@"html"]; // NSLog(@"--%@--%@--",self.protocolArray[indexSender][@"html"],htmlPath); NSString * htmlCont = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil]; [content loadHTMLString:htmlCont baseURL:baseURL]; } -(void)setPro{ UIButton *agreeButton=[UIButton buttonWithType:UIButtonTypeCustom]; agreeButton.frame=CGRectMake(0, 0, 16, 16); agreeButton.tag=2000; [agreeButton addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside]; agreeButton.selected=YES; [agreeButton setImage:[UIImage imageNamed:@"disagree_protocol"] forState:UIControlStateNormal]; [agreeButton setImage:[UIImage imageNamed:@"agree_protocol"] forState:UIControlStateSelected]; UIView *protocolView=[[UIView alloc]init]; protocolView.frame=CGRectMake(0, 0, 240, 20); for (int i=0; i<3; i++) { UIButton *button=[[UIButton alloc]init]; button.titleLabel.font=[UIFont systemFontOfSize:12.0f]; [button addTarget:self action:@selector(protocolAction:) forControlEvents:UIControlEventTouchUpInside]; [button setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal]; button.tag=1000+i; switch (i) { case 0: { [button setTitle:@"《委托授权协议》" forState:UIControlStateNormal]; button.frame=CGRectMake(0-10, -3, 100, 20); } break; case 1: { [button setTitle:@"《借款协议》" forState:UIControlStateNormal]; button.frame=CGRectMake(90-10, -3, 65, 20); } break; case 2: { [button setTitle:@"《平台服务协议》" forState:UIControlStateNormal]; button.frame=CGRectMake(90+65-10, -3, 70, 20); } break; default: break; } [button sizeToFit]; [protocolView addSubview:button]; } UITextField *protocolText=[[UITextField alloc]initWithFrame:self.proView.bounds]; protocolText.text=@"同意"; protocolText.font = [UIFont systemFontOfSize:12.0]; protocolText.textAlignment=NSTextAlignmentCenter; protocolText.leftView=agreeButton; protocolText.leftViewMode=UITextFieldViewModeAlways; protocolText.rightView=protocolView; protocolText.rightViewMode=UITextFieldViewModeAlways; [self.proView addSubview:protocolText]; UIView * views=[[UIView alloc]initWithFrame:CGRectMake(13, 0, 35, 16)]; views.backgroundColor=[UIColor clearColor]; [protocolText addSubview:views]; } -(UIView *)proView{ if (!_proView) { _proView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 290, 25)]; } return _proView; } -(UIView *)topView{ if (!_topView) { _topView=[[UIView alloc]initWithFrame:CGRectMake((self.backView.bounds.size.width-300)/2,10, 300, 350)]; // CGPoint point=self.view.center; // point.x=_topView.center.x; // point. // _topView.center=self.view.center; _topView.backgroundColor=[UIColor whiteColor]; UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 300, 49.5)]; titleLabel.textColor=[UIColor baseColor]; titleLabel.textAlignment=NSTextAlignmentCenter; UIView *line1=[[UIView alloc]initWithFrame:CGRectMake(0, 49.5, 300, 0.5)]; line1.backgroundColor=[UIColor baseColor]; UIView *line2=[[UIView alloc]initWithFrame:CGRectMake(0, 300, 300, 0.5)]; line2.backgroundColor=[UIColor baseColor]; titleLabel.tag=764; // UIScrollView *contentScrollView=[[UIScrollView alloc]initWithFrame:]; // contentScrollView.tag=765; UIWebView*content=[[UIWebView alloc]initWithFrame:CGRectMake(0, 50, 300, 250)]; content.tag=766; UIButton *closeButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 300.5, 300, 50)]; content.backgroundColor=[UIColor whiteColor]; [closeButton setTitle:@"关闭" forState:UIControlStateNormal]; [closeButton setTitleColor:[UIColor baseColor] forState:UIControlStateNormal]; [closeButton addTarget:self action:@selector(closeTop) forControlEvents:UIControlEventTouchUpInside]; closeButton.tag=767; [_topView addSubview:titleLabel]; // [contentScrollView addSubview:content]; // [_topView addSubview:contentScrollView]; [_topView addSubview:content]; [_topView addSubview:closeButton]; [_topView addSubview:line1]; [_topView addSubview:line2]; } return _topView; } -(void)closeTop{ self.topView.hidden=YES; self.backView.hidden=YES; UIWebView *content=[self.topView viewWithTag:766]; [content loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@""]]]; } -(UIView *)backView{ if (!_backView) { _backView=[[UIView alloc]initWithFrame:self.view.bounds]; _backView.backgroundColor=[UIColor KXColorWithHex:0x000000 alpha:0.3]; } return _backView; } -(NSArray *)protocolArray{ if (!_protocolArray) { NSMutableArray *arr=[NSMutableArray array]; NSString*path=[[NSBundle mainBundle] pathForResource:@"protocolList" ofType:@"plist"]; NSMutableArray *dataList=[NSMutableArray arrayWithContentsOfFile:path]; for (NSDictionary *dic in dataList) { [arr addObject:dic]; } _protocolArray=[NSArray arrayWithArray:arr]; } return _protocolArray; } /**********************/ -(void)createllSelectView { _month=0; _selectView=[[UIView alloc]initWithFrame:self.view.bounds]; _selectView.hidden=YES; _selectView.backgroundColor=[UIColor colorWithWhite:0 alpha:0.5]; [self.view addSubview:_selectView]; UITapGestureRecognizer *tapGesturRecognizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenAction:)]; tapGesturRecognizer.delegate = self; [_selectView addGestureRecognizer:tapGesturRecognizer]; self.selectTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,SCREEN_HEIGHT-220-110, SCREEN_WIDTH, 220) style:UITableViewStylePlain]; self.selectTableView.separatorStyle=UITableViewCellSeparatorStyleNone; [self.selectTableView registerClass:[KXSelectTableViewCell class] forCellReuseIdentifier:@"selected"]; self.selectTableView.tableFooterView=[[UIView alloc]init]; self.selectTableView.delegate = self; self.selectTableView.dataSource = self; [_selectView addSubview:self.selectTableView]; } -(void)hiddenAction:(UITapGestureRecognizer*) g{ _selectView.hidden=YES; } - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{ CGPoint point = [gestureRecognizer locationInView:_selectView]; if(point.y8) { return NO; } if([self isPureInt:string]){ if(range.location==0&&[string hasPrefix:@"0"]){ return NO; } }else{ if(string.length!=0){ return NO; } } return YES; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 6; } -(NSArray *)monthArray { if (!_monthArray) { _monthArray=@[@"1",@"3",@"6",@"12",@"24",@"36"]; } return _monthArray; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KXSelectTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"selected"]; if (!cell) { cell=[[KXSelectTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"selected"]; } cell.detailStr=self.monthArray[indexPath.row]; return cell; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath { return 45; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { _month=[self.monthArray[indexPath.row] integerValue]; UIButton *button=[self.view viewWithTag:8866]; [button setTitle:self.monthArray[indexPath.row] forState:UIControlStateNormal]; UITextField* text=(UITextField*)[self.view viewWithTag:8800]; if(text.text.length==0){ [self calculate: 5000]; }else{ [self calculate: text.text.integerValue]; } _selectView.hidden=YES; } -(void)buttonllAction:(UIButton *)btn{ _selectView.hidden=NO; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self checkLog]; } - (void)checkLog{ UIButton *nextBtn=[self.view viewWithTag:1000]; UITextField *allFiled = [self.view viewWithTag:8800]; UIButton *button = [self.view viewWithTag:8866]; UILabel *label = [self.view viewWithTag:8801]; NSString *urlString=[NSString stringWithFormat:@"%@/user/personalCenter",URL]; [KXHTTP post:urlString params:nil success:^(id json) { NSString *username= [[NSUserDefaults standardUserDefaults]valueForKey:USER_NAME]; if (!username) { allFiled.enabled = YES; button.enabled = YES; nextBtn.enabled = YES; allFiled.text = @"5000"; [button setTitle:@"24" forState:UIControlStateNormal]; [nextBtn setTitle:@"下一步" forState:UIControlStateNormal]; [nextBtn setBackgroundColor:[UIColor baseColor]]; label.text=@"210.95"; return; } if ([[json[@"user"] valueForKey:@"user_level_id"] isEqual:@(YES)]) { NSString *urlSt=[NSString stringWithFormat:@"%@/user/getIosApplyById",URL]; [KXHTTP post:urlSt params:@{@"id":[json[@"user"] valueForKey:@"id"]} success:^(id json) { _month=[json[@"term"] integerValue]; if (json[@"amount"]) { allFiled.enabled = NO; allFiled.text = json[@"amount"]; } if (json[@"term"]) { button.enabled = NO; [button setTitle:json[@"term"] forState:UIControlStateNormal]; } label.text=[NSString stringWithFormat:@"%.2f",[json[@"amount"] integerValue]*0.005*pow(1.005,(int)_month)/(pow(1.005,(int)_month)-1)]; [nextBtn setBackgroundColor:[UIColor redColor]]; [nextBtn setTitle:@"审核中......" forState:UIControlStateNormal]; nextBtn.enabled = NO; } failure:^(NSError *error) { }]; } } failure:^(NSError *error) { }]; } - (void) setUpUI{ UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150*SCREEN_MUTI)]; imageview.image = [UIImage imageNamed:@"main_banner2"]; [self.view addSubview:imageview]; UIView *backview = [[UIView alloc] init]; backview.backgroundColor = [UIColor whiteColor]; [self.view addSubview:backview]; // UIView *view1 = [[UIView alloc] init]; // view1.backgroundColor = [UIColor baseColor]; // [backview addSubview:view1]; UILabel* labelAll=[[UILabel alloc] init]; labelAll.textColor=[UIColor titleColor]; labelAll.font=FONT_SYS(22); labelAll.text=@"贷款金额:"; UITextField* textAll=[[UITextField alloc] init]; textAll.textColor=[UIColor baseColor]; textAll.font=FONT_SYS(22); textAll.placeholder=@"请输入金额"; textAll.text = @"5000"; textAll.tag=8800; textAll.keyboardType = UIKeyboardTypeNumberPad; textAll.delegate=self; UILabel* labelAll1=[[UILabel alloc] init]; labelAll1.textColor=[UIColor titleColor]; labelAll1.font=FONT_SYS(22); labelAll1.text=@"元"; UILabel *deadlineLabel = [[UILabel alloc] init]; deadlineLabel.text = @"贷款期数:"; deadlineLabel.textColor = [UIColor titleColor]; deadlineLabel.font = FONT_SYS(22); UIButton *button2=[UIButton buttonWithType:UIButtonTypeCustom]; [button2 setTitle:@"24" forState:UIControlStateNormal]; [button2 setTitleColor:[UIColor baseColor] forState:UIControlStateNormal]; button2.titleLabel.font=FONT_SYS(22); button2.tag=8866; [button2 addTarget:self action:@selector(buttonllAction:) forControlEvents:UIControlEventTouchUpInside]; button2.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft; UILabel* labeldeadline=[[UILabel alloc] init]; labeldeadline.textColor=[UIColor titleColor]; labeldeadline.font=FONT_SYS(22); labeldeadline.text=@"月"; UILabel *repayLabel = [[UILabel alloc] init]; repayLabel.font = FONT_SYS(18); repayLabel.text =@"每月还款金额:"; UILabel *label2 = [[UILabel alloc] init]; label2.font = FONT_SYS(18); label2.textColor = [UIColor baseColor]; label2.text=@"210.95"; label2.tag = 8801; UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.backgroundColor = [UIColor baseColor]; button.layer.cornerRadius = 3; button.layer.masksToBounds = YES; [button setTitle:@"立即申请" forState:UIControlStateNormal]; [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; button.layer.masksToBounds = YES; button.tag = 1000; [button addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside]; UIView *line1 = [[UIView alloc] init]; line1.backgroundColor = [UIColor lineColor]; UIView *line2 = [[UIView alloc] init]; line2.backgroundColor = [UIColor lineColor]; [backview addSubview:labelAll]; [backview addSubview:labelAll1]; [backview addSubview:textAll]; [backview addSubview:deadlineLabel]; [backview addSubview:button2]; [backview addSubview:labeldeadline]; [backview addSubview:repayLabel]; [backview addSubview:label2]; [backview addSubview:button]; [backview addSubview:line1]; [backview addSubview:line2]; [backview addSubview:self.proView]; WS(weakSelf); [backview mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(weakSelf.view.mas_left).offset(14); make.top.equalTo(imageview.mas_bottom).offset(14); make.right.equalTo(weakSelf.view.mas_right).offset(-14); make.height.equalTo(240*SCREEN_MUTI); }]; // [view1 mas_makeConstraints:^(MASConstraintMaker *make) { // make.left.equalTo(backview.mas_left); // make.right.equalTo(backview.mas_right); // make.top.equalTo(backview.mas_top); // make.height.equalTo(@16); // }]; [labelAll mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(backview.mas_left).offset(22); make.top.equalTo(backview.mas_top).offset(10); }]; [labelAll1 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(backview.mas_right).offset(-22); make.top.equalTo(labelAll.mas_top); }]; [textAll mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(labelAll.mas_right); make.top.equalTo(labelAll.mas_top); make.right.equalTo(labelAll1.mas_left); make.width.equalTo(SCREEN_WIDTH-28-44-22-99); }]; [line1 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(textAll.mas_left); make.top.equalTo(textAll.mas_bottom); make.width.equalTo(SCREEN_WIDTH-28-44-22-99); make.height.equalTo(1); }]; [deadlineLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(labelAll.mas_left); make.top.equalTo(labelAll.mas_bottom).offset(18); }]; [labeldeadline mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(labelAll1.mas_right); make.top.equalTo(deadlineLabel.mas_top); }]; [button2 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(deadlineLabel.mas_right); make.top.equalTo(deadlineLabel.mas_top); make.right.equalTo(labeldeadline.mas_left); make.size.equalTo(CGSizeMake(SCREEN_WIDTH-28-44-22-99, 20)); }]; [line2 mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(button2.mas_left); make.top.equalTo(button2.mas_bottom); make.height.equalTo(1); make.width.equalTo(SCREEN_WIDTH-28-44-22-99); }]; [label2 mas_makeConstraints:^(MASConstraintMaker *make) { make.right.equalTo(labeldeadline.mas_right); make.top.equalTo(labeldeadline.mas_bottom).offset(38); }]; [repayLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(label2.mas_top); make.right.equalTo(label2.mas_left); }]; [button mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(labelAll.mas_left); make.right.equalTo(label2.mas_right); make.bottom.equalTo(backview.mas_bottom).offset(-17); make.height.equalTo(44); }]; [self.proView mas_makeConstraints:^(MASConstraintMaker *make) { make.centerX.equalTo(backview.mas_centerX); make.top.equalTo(deadlineLabel).offset(30); make.width.mas_equalTo(290); make.height.equalTo(30); }]; UILabel *littleLabel=[[UILabel alloc]init]; littleLabel.text=[NSString stringWithFormat:@"%@为您提供高品质金融服务",KXCOMPANY]; littleLabel.textColor=[UIColor detailTitleColor]; littleLabel.textAlignment=NSTextAlignmentCenter; littleLabel.font=FONT_SYS(12); [self.view addSubview:littleLabel]; [littleLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(backview.mas_bottom).offset(2*SCREEN_MUTI); make.centerX.equalTo(backview.mas_centerX); }]; } - (void)nextAction{ UIButton *nextBtn = [self.view viewWithTag:1000]; NSString *urlString=[NSString stringWithFormat:@"%@/user/personalCenter",URL]; [KXHTTP post:urlString params:nil success:^(id json) { NSString *username= [[NSUserDefaults standardUserDefaults]valueForKey:USER_NAME]; if (!username) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请登录" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { KXLogginViewController *loggin=[[KXLogginViewController alloc]init]; [self.navigationController pushViewController:loggin animated:YES]; [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"ISPOP"]; [[NSUserDefaults standardUserDefaults]synchronize]; }]]; [self presentViewController:alertController animated:YES completion:nil]; nextBtn.enabled=YES; }else if (!self.agreeBool){//请勾选同意协议 UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请勾选同意协议" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [[NSUserDefaults standardUserDefaults]setObject:nil forKey:@"ISPOP"]; [[NSUserDefaults standardUserDefaults]synchronize]; }]]; [self presentViewController:alertController animated:YES completion:nil]; nextBtn.enabled=YES; }else{ UITextField* textfield=[self.view viewWithTag:8800]; UIButton *button = [self.view viewWithTag:8866]; if(textfield.text.length==0){ [self createPromptWhit:@"请输入金额后再试"]; nextBtn.enabled=YES; return; } if(!button.titleLabel.text){ [self createPromptWhit:@"请选择期限后再试"]; nextBtn.enabled=YES; return; } KXMainWebView2ViewController *mainWeb=[[KXMainWebView2ViewController alloc]init]; mainWeb.amount=textfield.text.integerValue ; mainWeb.term=_month; [self.navigationController pushViewController:mainWeb animated:YES]; nextBtn.enabled=YES; } } failure:^(NSError *error) { nextBtn.enabled=YES; }]; } -(void)createPromptWhit:(NSString*) message{ UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:message preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; } - (void)webViewDidFinishLoad:(UIWebView *)webView{ NSString *company=[NSString stringWithFormat:@"document.getElementById('company').innerHTML='%@'",KXCOMPANY]; [webView stringByEvaluatingJavaScriptFromString:company]; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"]; NSString *appName=[NSString stringWithFormat:@"document.getElementById('appName').innerHTML='%@'",app_Name]; [webView stringByEvaluatingJavaScriptFromString:appName]; } @end