Нет описания

KXCustomServiceViewController.m 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. //
  2. // KXCustomServiceViewController.m
  3. // CAISHEN
  4. //
  5. // Created by xiaoxi on 2017/12/6.
  6. // Copyright © 2017年 kuxuan. All rights reserved.
  7. //
  8. #import "KXCustomServiceViewController.h"
  9. #import "KXCustomServiceTableViewCell.h"
  10. #import "KXCommentBottomBar.h"
  11. #import "KXCommentPutBar.h"
  12. #import "IQKeyboardManager.h"
  13. #import "KXTime.h"
  14. @interface KXCustomServiceViewController () <UITableViewDelegate,UITableViewDataSource,KXBottomInputBarDelegate> {
  15. UIView *_maskView;
  16. KXCommentPutBar *_putBar;
  17. }
  18. @property (nonatomic, strong) UITableView *tableView;
  19. @property (nonatomic, strong) NSMutableArray *dataSource;
  20. @property (nonatomic, strong) NSDateFormatter *fmt;
  21. @property (nonatomic, assign) NSInteger first;
  22. @property (nonatomic, strong) NSTimer *timer;
  23. @property (nonatomic, strong) UIButton *writeBtn;
  24. @property (nonatomic, assign) CGRect keyboardFrame;
  25. @property (nonatomic, strong) MBProgressHUD *hud;
  26. @end
  27. @implementation KXCustomServiceViewController
  28. static NSString *const cellID = @"custom";
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. // Do any additional setup after loading the view.
  32. [[IQKeyboardManager sharedManager]setEnable:NO];
  33. [self setupNavView];
  34. [self setupContentView];
  35. [self createBottomBarView];
  36. [self setNotification];
  37. }
  38. - (void)setupNavView {
  39. self.name = @"客服";
  40. [self addLeftBarButtonItemWithImageName:@"main_back" title:nil target:self selector:@selector(backItemAction)];
  41. }
  42. - (void)backItemAction {
  43. [self.navigationController popViewControllerAnimated:YES];
  44. }
  45. - (void)setupContentView {
  46. self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-NavHeight-44) style:UITableViewStylePlain];
  47. self.tableView.backgroundColor = [UIColor KXColorWithHex:0xf8f8f8];
  48. self.tableView.delegate = self;
  49. self.tableView.dataSource = self;
  50. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  51. self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
  52. [self.tableView registerClass:[KXCustomServiceTableViewCell class] forCellReuseIdentifier:cellID];
  53. [self.view addSubview:self.tableView];
  54. [self loadFirstData];
  55. }
  56. - (void)loadFirstData {
  57. if ([KXTime validateWithStartTime:@"09:00" withExpireTime:@"18:00"]) {
  58. KXChatModel *model = [[KXChatModel alloc] init];
  59. model.isLeft = YES;
  60. model.isShowTime = YES;
  61. model.time = [self.fmt stringFromDate:[NSDate date]];
  62. self.first = (arc4random()%31)+20;
  63. model.msg = [NSString stringWithFormat:@"您好,正在等待客服接入,当前有%ld人正在排队,因当前资讯人数较多,请耐心等待~您可以先简单描述所要资讯的问题~",self.first];
  64. CGRect timeRect = [model.time boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(12*SCREEN_MUTI)} context:nil];
  65. 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];
  66. CGFloat timeHeight = ceil(timeRect.size.height);
  67. CGFloat msgHeight = ceil(msgRect.size.height);
  68. if (msgHeight + 20 < 44) {
  69. msgHeight = 24;
  70. }
  71. model.cellHeight = timeHeight+msgHeight+20+14;
  72. [self.dataSource addObject:model];
  73. [self.tableView reloadData];
  74. self.timer = [NSTimer scheduledTimerWithTimeInterval:180.0f target:self selector:@selector(firstChange) userInfo:nil repeats:YES];
  75. } else {
  76. UIView *header = [[UIView alloc] initWithFrame:CGRectZero];
  77. header.backgroundColor = [UIColor KXColorWithHex:0xffed99];
  78. NSString *hello = @"当前客服不在,您可以留言,客服上线后会第一时间回复您(客服时间:周一到周日,09:00-18:00)";
  79. CGRect helloRect = [hello boundingRectWithSize:CGSizeMake(SCREEN_WIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(14*SCREEN_MUTI)} context:nil];
  80. CGFloat helloWidth = ceil(helloRect.size.width);
  81. CGFloat helloHeight = ceil(helloRect.size.height);
  82. UILabel *helloLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  83. helloLabel.backgroundColor = [UIColor clearColor];
  84. helloLabel.font = FONT_SYS(14*SCREEN_MUTI);
  85. helloLabel.textColor = [UIColor titleColor];
  86. helloLabel.numberOfLines = 0;
  87. helloLabel.textAlignment = NSTextAlignmentCenter;
  88. helloLabel.text = hello;
  89. [header addSubview:helloLabel];
  90. [helloLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  91. make.center.equalTo(header);
  92. make.size.mas_equalTo(CGSizeMake(helloWidth, helloHeight));
  93. }];
  94. header.frame = CGRectMake(0, 0, SCREEN_WIDTH, helloHeight+20);
  95. self.tableView.tableHeaderView = header;
  96. }
  97. }
  98. - (void)firstChange {
  99. self.first --;
  100. if (self.first < 31) {
  101. self.first = 31;
  102. }
  103. }
  104. - (void)createBottomBarView {
  105. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-NavHeight-safeBottomHeight-44, SCREEN_WIDTH, 44)];
  106. bottomView.backgroundColor = [UIColor whiteColor];
  107. [self.view addSubview:bottomView];
  108. UILabel *lineLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
  109. lineLabel.backgroundColor=[UIColor KXColorWithHex:0xdddddd];
  110. [bottomView addSubview:lineLabel];
  111. self.writeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
  112. [self.writeBtn setTitle:@"请输入您想咨询的问题..." forState:UIControlStateNormal];
  113. [self.writeBtn setTitleColor:[UIColor detailTitleColor] forState:UIControlStateNormal];
  114. self.writeBtn.backgroundColor=[UIColor KXColorWithHex:0xf4f5f6];
  115. self.writeBtn.titleLabel.font=FONT_SYS(14);
  116. self.writeBtn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
  117. self.writeBtn.titleEdgeInsets=UIEdgeInsetsMake(0, 14, 0, 0);
  118. self.writeBtn.layer.cornerRadius=16;
  119. self.writeBtn.layer.borderColor=[UIColor KXColorWithHex:0xe5e5e5].CGColor;
  120. self.writeBtn.layer.borderWidth=1;
  121. self.writeBtn.layer.masksToBounds=YES;
  122. [self.writeBtn addTarget:self action:@selector(writeCommentAction) forControlEvents:UIControlEventTouchUpInside];
  123. [bottomView addSubview:self.writeBtn];
  124. [self.writeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  125. make.left.equalTo(bottomView).offset(18);
  126. make.centerY.equalTo(bottomView);
  127. make.right.equalTo(bottomView).offset(-18);
  128. make.height.equalTo(@28);
  129. }];
  130. _maskView=[[UIView alloc]initWithFrame:self.view.bounds];
  131. _maskView.backgroundColor=[UIColor colorWithWhite:0 alpha:0.4];
  132. UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideMask)];
  133. [_maskView addGestureRecognizer:tap];
  134. _maskView.hidden=YES;
  135. [self.view addSubview:_maskView];
  136. _putBar=[[KXCommentPutBar alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-44-64, SCREEN_WIDTH, 44)];
  137. _putBar.backgroundColor=[UIColor KXColorWithRed:239 green:239 blue:244];;
  138. _putBar.delegate=self;
  139. [_putBar.sendBtn setTitle:@"发送" forState:UIControlStateNormal];
  140. [_maskView addSubview:_putBar];
  141. }
  142. - (void)writeCommentAction {
  143. [_putBar.inputView becomeFirstResponder];
  144. _maskView.hidden=NO;
  145. }
  146. -(void)hideMask
  147. {
  148. [_putBar.inputView resignFirstResponder];
  149. _maskView.hidden=YES;
  150. }
  151. /**
  152. 设置通知
  153. */
  154. - (void)setNotification {
  155. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  156. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  157. }
  158. #pragma mark - NotificationEventAction
  159. - (void)keyboardWillHide:(NSNotification *)notification {
  160. NSDictionary *info = [notification userInfo];
  161. CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  162. [UIView animateWithDuration:duration animations:^{
  163. _maskView.hidden = YES;
  164. _putBar.inputView.text=@"";
  165. _putBar.frame = CGRectMake(0, SCREEN_HEIGHT-44-64, SCREEN_WIDTH, 44);
  166. } completion:^(BOOL finished) {
  167. }];
  168. }
  169. - (void)keyboardWillShow:(NSNotification *)notification {
  170. NSDictionary *info = [notification userInfo];
  171. self.keyboardFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  172. CGFloat duration = [[info objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
  173. [UIView animateWithDuration:duration animations:^{
  174. _maskView.hidden = NO;
  175. _putBar.frame =CGRectMake(0, self.keyboardFrame.origin.y-44-64, SCREEN_WIDTH, 44) ;
  176. }];
  177. }
  178. #pragma mark KXBottomInputBarDelegate
  179. -(void)textHeightChange:(NSInteger)height
  180. {
  181. CGRect rect=_putBar.frame;
  182. _putBar.frame=CGRectMake(rect.origin.x,self.keyboardFrame.origin.y-height-30-64, rect.size.width, height+30);
  183. }
  184. -(void)sendText:(NSString *)text
  185. {
  186. [_hud show:YES];
  187. NSString *urlString=[NSString stringWithFormat:@"%@/user/personalCenter",URL];
  188. [KXHTTP post:urlString params:nil success:^(id json) {
  189. NSString *username= [[NSUserDefaults standardUserDefaults]valueForKey:USER_NAME];
  190. [_hud hide:YES];
  191. if (!username) {
  192. _maskView.hidden=YES;
  193. [_putBar.inputView resignFirstResponder];
  194. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先去登录" preferredStyle:UIAlertControllerStyleAlert];
  195. [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  196. }]];
  197. [self presentViewController:alertController animated:YES completion:nil];
  198. }else{
  199. _maskView.hidden=YES;
  200. [_putBar.inputView resignFirstResponder];
  201. if (text.length) {
  202. KXChatModel *model = [[KXChatModel alloc] init];
  203. model.isLeft = NO;
  204. model.isShowTime = YES;
  205. model.time = [self.fmt stringFromDate:[NSDate date]];
  206. model.msg = text;
  207. CGRect timeRect = [model.time boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(12*SCREEN_MUTI)} context:nil];
  208. 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];
  209. CGFloat timeHeight = ceil(timeRect.size.height);
  210. CGFloat msgHeight = ceil(msgRect.size.height);
  211. if (msgHeight + 20 < 44) {
  212. msgHeight = 24;
  213. }
  214. model.cellHeight = timeHeight+msgHeight+20+14;
  215. [self.dataSource addObject:model];
  216. if ([KXTime validateWithStartTime:@"09:00" withExpireTime:@"18:00"]) {
  217. KXChatModel *model1 = [[KXChatModel alloc] init];
  218. model1.isLeft = YES;
  219. model1.isShowTime = YES;
  220. model1.time = [self.fmt stringFromDate:[NSDate date]];
  221. model1.msg = [NSString stringWithFormat:@"您好,正在等待客服接入,当前有%ld人正在排队,因当前资讯人数较多,请耐心等待~您可以先简单描述所要资讯的问题~",self.first];
  222. CGRect timeRect1 = [model1.time boundingRectWithSize:CGSizeMake(200, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:FONT_SYS(12*SCREEN_MUTI)} context:nil];
  223. 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];
  224. CGFloat timeHeight1 = ceil(timeRect1.size.height);
  225. CGFloat msgHeight1 = ceil(msgRect1.size.height);
  226. if (msgHeight1 + 20 < 44) {
  227. msgHeight1 = 24;
  228. }
  229. model1.cellHeight = timeHeight1+msgHeight1+20+14;
  230. [self.dataSource addObject:model1];
  231. }
  232. [self.tableView reloadData];
  233. [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.dataSource.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
  234. }
  235. }
  236. } failure:^(NSError *error) {
  237. [_hud hide:YES];
  238. }];
  239. }
  240. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  241. return self.dataSource.count;
  242. }
  243. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  244. KXCustomServiceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
  245. KXChatModel *model = self.dataSource[indexPath.row];
  246. [cell refreshCell:model];
  247. return cell;
  248. }
  249. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  250. KXChatModel *model = self.dataSource[indexPath.row];
  251. return model.cellHeight;
  252. }
  253. - (NSMutableArray *)dataSource {
  254. if (!_dataSource) {
  255. _dataSource = [NSMutableArray array];
  256. }
  257. return _dataSource;
  258. }
  259. - (NSDateFormatter *)fmt {
  260. if (!_fmt) {
  261. _fmt = [[NSDateFormatter alloc] init];
  262. _fmt.dateFormat = @"HH:mm";
  263. }
  264. return _fmt;
  265. }
  266. -(MBProgressHUD *)hud
  267. {
  268. if (!_hud) {
  269. _hud=[[MBProgressHUD alloc]initWithView:self.view];
  270. _hud.labelText=@"发送中...";
  271. [self.view addSubview:_hud];
  272. }
  273. return _hud;
  274. }
  275. - (void)didReceiveMemoryWarning {
  276. [super didReceiveMemoryWarning];
  277. // Dispose of any resources that can be recreated.
  278. }
  279. /*
  280. #pragma mark - Navigation
  281. // In a storyboard-based application, you will often want to do a little preparation before navigation
  282. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  283. // Get the new view controller using [segue destinationViewController].
  284. // Pass the selected object to the new view controller.
  285. }
  286. */
  287. @end