// // KXCustomServiceViewController.m // CAISHEN // // Created by xiaoxi on 2017/12/6. // Copyright © 2017年 kuxuan. All rights reserved. // #import "KXCustomServiceViewController.h" #import "KXCustomServiceTableViewCell.h" #import "KXCommentBottomBar.h" #import "KXCommentPutBar.h" #import "IQKeyboardManager.h" #import "KXTime.h" @interface KXCustomServiceViewController () { UIView *_maskView; KXCommentPutBar *_putBar; } @property (nonatomic, strong) UITableView *tableView; @property (nonatomic, strong) NSMutableArray *dataSource; @property (nonatomic, strong) NSDateFormatter *fmt; @property (nonatomic, assign) NSInteger first; @property (nonatomic, strong) NSTimer *timer; @property (nonatomic, strong) UIButton *writeBtn; @property (nonatomic, assign) CGRect keyboardFrame; @property (nonatomic, strong) MBProgressHUD *hud; @end @implementation KXCustomServiceViewController static NSString *const cellID = @"custom"; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [[IQKeyboardManager sharedManager]setEnable:NO]; [self setupNavView]; [self setupContentView]; [self createBottomBarView]; [self setNotification]; } - (void)setupNavView { self.name = @"客服"; [self addLeftBarButtonItemWithImageName:@"main_back" title:nil target:self selector:@selector(backItemAction)]; } - (void)backItemAction { [self.navigationController popViewControllerAnimated:YES]; } - (void)setupContentView { self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-44) style:UITableViewStylePlain]; self.tableView.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero]; [self.tableView registerClass:[KXCustomServiceTableViewCell class] forCellReuseIdentifier:cellID]; [self.view addSubview:self.tableView]; [self loadFirstData]; } - (void)loadFirstData { if ([KXTime validateWithStartTime:@"09:00" withExpireTime:@"18:00"]) { KXChatModel *model = [[KXChatModel alloc] init]; model.isLeft = YES; model.isShowTime = YES; model.time = [self.fmt stringFromDate:[NSDate date]]; self.first = (arc4random()%31)+20; model.msg = [NSString stringWithFormat:@"您好,正在等待客服接入,当前有%ld人正在排队,因当前资讯人数较多,请耐心等待~您可以先简单描述所要资讯的问题~",self.first]; CGRect timeRect = [model.time boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(12*SCREEN_MUTI)} context:nil]; CGRect msgRect = [model.msg boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-(18+44+52)*SCREEN_MUTI, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(14*SCREEN_MUTI)} context:nil]; CGFloat timeHeight = ceil(timeRect.size.height); CGFloat msgHeight = ceil(msgRect.size.height); if (msgHeight + 20 < 44) { msgHeight = 24; } model.cellHeight = timeHeight+msgHeight+20+14; [self.dataSource addObject:model]; [self.tableView reloadData]; self.timer = [NSTimer scheduledTimerWithTimeInterval:180.0f target:self selector:@selector(firstChange) userInfo:nil repeats:YES]; } else { UIView *header = [[UIView alloc] initWithFrame:CGRectZero]; header.backgroundColor = [UIColor KXColorWithHex:0xffed99]; NSString *hello = @"当前客服不在,您可以留言,客服上线后会第一时间回复您(客服时间:周一到周日,09:00-18:00)"; CGRect helloRect = [hello boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(14*SCREEN_MUTI)} context:nil]; CGFloat helloWidth = ceil(helloRect.size.width); CGFloat helloHeight = ceil(helloRect.size.height); UILabel *helloLabel = [[UILabel alloc] initWithFrame:CGRectZero]; helloLabel.backgroundColor = [UIColor clearColor]; helloLabel.font = FONT_SYS(14*SCREEN_MUTI); helloLabel.textColor = [UIColor titleColor]; helloLabel.numberOfLines = 0; helloLabel.textAlignment = NSTextAlignmentCenter; helloLabel.text = hello; [header addSubview:helloLabel]; [helloLabel mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(header); make.size.mas_equalTo(CGSizeMake(helloWidth, helloHeight)); }]; header.frame = CGRectMake(0, 0, SCREEN_WIDTH, helloHeight+20); self.tableView.tableHeaderView = header; } } - (void)firstChange { self.first --; if (self.first < 31) { self.first = 31; } } - (void)createBottomBarView { UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-NavHeight-safeBottomHeight-44, SCREEN_WIDTH, 44)]; bottomView.backgroundColor = [UIColor whiteColor]; [self.view addSubview:bottomView]; UILabel *lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)]; lineLabel.backgroundColor=[UIColor KXColorWithHex:0xdddddd]; [bottomView addSubview:lineLabel]; self.writeBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [self.writeBtn setTitle:@"请输入您想咨询的问题..." forState:UIControlStateNormal]; [self.writeBtn setTitleColor:[UIColor detailTitleColor] forState:UIControlStateNormal]; self.writeBtn.backgroundColor=[UIColor KXColorWithHex:0xf4f5f6]; self.writeBtn.titleLabel.font=FONT_SYS(14); self.writeBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft; self.writeBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 14, 0, 0); self.writeBtn.layer.cornerRadius=16; self.writeBtn.layer.borderColor=[UIColor KXColorWithHex:0xe5e5e5].CGColor; self.writeBtn.layer.borderWidth=1; self.writeBtn.layer.masksToBounds=YES; [self.writeBtn addTarget:self action:@selector(writeCommentAction) forControlEvents:UIControlEventTouchUpInside]; [bottomView addSubview:self.writeBtn]; [self.writeBtn mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(bottomView).offset(18); make.centerY.equalTo(bottomView); make.right.equalTo(bottomView).offset(-18); make.height.equalTo(@28); }]; _maskView=[[UIView alloc]initWithFrame:self.view.bounds]; _maskView.backgroundColor=[UIColor colorWithWhite:0 alpha:0.4]; UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideMask)]; [_maskView addGestureRecognizer:tap]; _maskView.hidden=YES; [self.view addSubview:_maskView]; _putBar=[[KXCommentPutBar alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-44-64, SCREEN_WIDTH, 44)]; _putBar.backgroundColor=[UIColor KXColorWithRed:239 green:239 blue:244];; _putBar.delegate=self; [_putBar.sendBtn setTitle:@"发送" forState:UIControlStateNormal]; [_maskView addSubview:_putBar]; } - (void)writeCommentAction { [_putBar.inputView becomeFirstResponder]; _maskView.hidden=NO; } -(void)hideMask { [_putBar.inputView resignFirstResponder]; _maskView.hidden=YES; } /** 设置通知 */ - (void)setNotification { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } #pragma mark - NotificationEventAction - (void)keyboardWillHide:(NSNotification *)notification { NSDictionary *info = [notification userInfo]; CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; [UIView animateWithDuration:duration animations:^{ _maskView.hidden = YES; _putBar.inputView.text=@""; _putBar.frame = CGRectMake(0, SCREEN_HEIGHT-44-64, SCREEN_WIDTH, 44); } completion:^(BOOL finished) { }]; } - (void)keyboardWillShow:(NSNotification *)notification { NSDictionary *info = [notification userInfo]; self.keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue]; CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]; [UIView animateWithDuration:duration animations:^{ _maskView.hidden = NO; _putBar.frame =CGRectMake(0, self.keyboardFrame.origin.y-44-64, SCREEN_WIDTH, 44) ; }]; } #pragma mark KXBottomInputBarDelegate -(void)textHeightChange:(NSInteger)height { CGRect rect=_putBar.frame; _putBar.frame=CGRectMake(rect.origin.x,self.keyboardFrame.origin.y-height-30-64, rect.size.width, height+30); } -(void)sendText:(NSString *)text { [_hud show:YES]; NSString *urlString=[NSString stringWithFormat:@"%@/user/personalCenter",URL]; [KXHTTP post:urlString params:nil success:^(id json) { NSString *username= [[NSUserDefaults standardUserDefaults]valueForKey:USER_NAME]; [_hud hide:YES]; if (!username) { _maskView.hidden=YES; [_putBar.inputView resignFirstResponder]; UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先去登录" preferredStyle:UIAlertControllerStyleAlert]; [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { }]]; [self presentViewController:alertController animated:YES completion:nil]; }else{ _maskView.hidden=YES; [_putBar.inputView resignFirstResponder]; if (text.length) { KXChatModel *model = [[KXChatModel alloc] init]; model.isLeft = NO; model.isShowTime = YES; model.time = [self.fmt stringFromDate:[NSDate date]]; model.msg = text; CGRect timeRect = [model.time boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(12*SCREEN_MUTI)} context:nil]; CGRect msgRect = [model.msg boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-(18+44+52)*SCREEN_MUTI, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(14*SCREEN_MUTI)} context:nil]; CGFloat timeHeight = ceil(timeRect.size.height); CGFloat msgHeight = ceil(msgRect.size.height); if (msgHeight + 20 < 44) { msgHeight = 24; } model.cellHeight = timeHeight+msgHeight+20+14; [self.dataSource addObject:model]; if ([KXTime validateWithStartTime:@"09:00" withExpireTime:@"18:00"]) { KXChatModel *model1 = [[KXChatModel alloc] init]; model1.isLeft = YES; model1.isShowTime = YES; model1.time = [self.fmt stringFromDate:[NSDate date]]; model1.msg = [NSString stringWithFormat:@"您好,正在等待客服接入,当前有%ld人正在排队,因当前资讯人数较多,请耐心等待~您可以先简单描述所要资讯的问题~",self.first]; CGRect timeRect1 = [model1.time boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(12*SCREEN_MUTI)} context:nil]; CGRect msgRect1 = [model1.msg boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-(18+44+52)*SCREEN_MUTI, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(14*SCREEN_MUTI)} context:nil]; CGFloat timeHeight1 = ceil(timeRect1.size.height); CGFloat msgHeight1 = ceil(msgRect1.size.height); if (msgHeight1 + 20 < 44) { msgHeight1 = 24; } model1.cellHeight = timeHeight1+msgHeight1+20+14; [self.dataSource addObject:model1]; } [self.tableView reloadData]; [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataSource.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES]; } } } failure:^(NSError *error) { [_hud hide:YES]; }]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataSource.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { KXCustomServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath]; KXChatModel *model = self.dataSource[indexPath.row]; [cell refreshCell:model]; return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { KXChatModel *model = self.dataSource[indexPath.row]; return model.cellHeight; } - (NSMutableArray *)dataSource { if (!_dataSource) { _dataSource = [NSMutableArray array]; } return _dataSource; } - (NSDateFormatter *)fmt { if (!_fmt) { _fmt = [[NSDateFormatter alloc] init]; _fmt.dateFormat = @"HH:mm"; } return _fmt; } -(MBProgressHUD *)hud { if (!_hud) { _hud=[[MBProgressHUD alloc]initWithView:self.view]; _hud.labelText=@"发送中..."; [self.view addSubview:_hud]; } return _hud; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end