VideoDetailViewController.m 28.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
//
//  VideoDetailViewController.m
//  CNLiveScioLive
//
//  Created by CNLive-zxw on 2018/9/21.
//  Copyright © 2018年 CNLive. All rights reserved.
//

#import "VideoDetailViewController.h"
#import "CNHomeModel.h"
#import "CNLiveNewsLeftPicTableViewCell.h"
#import "CNLiveNewsDetailsView.h"
#import <WebKit/WebKit.h>

@interface VideoDetailViewController ()<WKUIDelegate,WKNavigationDelegate,UITableViewDelegate,UITableViewDataSource>{
    UIScrollView *_backgroundView;
    NSURL *_movieUrl;
    NSString *_url;
    UIImage *_bgImg;
    CGFloat _videoHeight;
    NSMutableArray *_heightArr;
}
//@property (strong, nonatomic) UILabel *errorLabel;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) ZXVideoPlayerController *videoController;
@property (strong, nonatomic) CNLivePlayerController *liveController;

@property (copy, nonatomic) NSString *authorName;
@property (strong, nonatomic) NSMutableArray *data;
@property (strong, nonatomic) WKWebView *webView;

@property (nonatomic, assign) NSInteger pageNo;
@property (nonatomic, assign) BOOL isRefreshing;

@property (nonatomic, assign) CGFloat height;

@property (nonatomic, strong) CNLiveNewsDetailsView *detailsView;

@property (nonatomic, strong) NSArray *fontArray;
@property (nonatomic, assign) NSInteger fontIndex;

@end

@implementation VideoDetailViewController
static NSString *newsLeftPicTableViewCell = @"CNLiveNewsLeftPicTableViewCell";
#pragma mark - Data
- (void)loadRelatedInformation {
    if (![[Reachability reachabilityForInternetConnection] currentReachabilityStatus]) {
        [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"NetworkiInterruption")];
        return;
    }
    NSString *strUrl = @"http://app.phonemovie.cnlive.com/gxb/api/programRelative";
    NSLog(@"%@",strUrl);
    NSString *categoryId = [NSString stringWithFormat:@"{\"plat\":\"i\",\"version\":\"5.0\",\"appid\":\"gxb\",\"cmsContentId\":\"%@\"}",self.model.cmsContentId];
    NSLog(@"%@",categoryId);
    NSDictionary *parameters = @{@"params":categoryId};
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [XWHTTPTool POST:strUrl params:parameters success:^(id json) {
        self->_isRefreshing = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        // 请求成功,解析数据
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:json];
        
        if ([dic[@"errorCode"] isEqualToString:@"0"]) {
            self->_pageNo++;
            if ([dic[@"programs"] isKindOfClass:[NSArray class]]) {
                NSArray *lists = dic[@"programs"];
                for (NSDictionary *dics in lists) {
                    [CNHomeModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
                        return @{ @"ID" : @"id" ,@"Description" : @"description" };
                    }];
                    NSLog(@"%@",dics);
                    [self.data addObject:[CNHomeModel mj_objectWithKeyValues:dics]];
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    if (self.data.count != 0) {
                        [self.tableView setFrame:CGRectMake(0, [self->_heightArr[self->_fontIndex] floatValue], MainScreenWidth, 40 + 100*self.data.count)];
                    }
                    [self->_backgroundView setContentSize:CGSizeMake(MainScreenWidth, [self->_heightArr[self->_fontIndex] floatValue] + self.tableView.frame.size.height)];
                    
                });
              
            }
            
        } else {
            
            [AlertViewTool showHUDAddedToView:self.view text:dic[@"errorMessage"]];
            
        }
        [self.tableView.mj_header endRefreshing];
        
        if ([dic[@"nextCursor"] isEqualToString:@"0"]) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            
        } else {
            [self.tableView.mj_footer endRefreshing];
            
        }
        [self.tableView reloadData];
        
        
    } failure:^(NSError *error) {
        [AlertViewTool showHUDAddedToView:self.view text:CustomStr(@"ServerBusy")];
        self->_isRefreshing = NO;
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    }];
}
- (void)loadMoreData{
    if(!_isRefreshing){
        _isRefreshing = YES;
        [self loadRelatedInformation];
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[JGMusicManager shareMusicManager] playStop];
    [self.navigationController setNavigationBarHidden:YES];
    
}
- (void)seDefaultValues{
    self.navigationItem.title = self.model.cmsColumnName;

    _fontIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"FontSize"];
    _heightArr = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil];
    
    if(self.model.videoUrl){
        _url = self.model.videoUrl;

    }else if (self.model.uploadVideoUrl){
        _url = self.model.uploadVideoUrl;

    }
    _url = [_url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    _movieUrl = [NSURL URLWithString:_url];
    _videoHeight = MainScreenWidth * (9.0 / 16.0);
    
    if(self.model.img){
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.model.img]];
        _bgImg = [UIImage imageWithData:data];
    }
}
- (NSString *)setHTMLString {
    NSString *htmlHeader = [[NSString alloc] initWithFormat:@"<html><p><font style=\"font-size:20px;\"><strong>%@</strong></font></p><body>",self.model.title];
    
    NSString *htmldate = @"";
    NSString *htmlForm = @"";
    
    if (self.model.publishDate && self.model.author) {
        htmldate = [[NSString alloc] initWithFormat:@"<html><p>%@</p><body>",[NSString stringWithFormat:@"%@",self.model.publishDate]];
        htmlForm = [[NSString alloc] initWithFormat:@"<html><p>来源:%@</p><body>", self.model.author];
        self.authorName = self.model.author;
    } else {
        htmldate = [[NSString alloc] initWithFormat:@"<html><p>%@</p><body>",[NSString stringWithFormat:@"%@",self.model.publishDate]];
        NSDictionary *author = [NSDictionary dictionaryWithContentsOfFile:FavouriteAuthors_PATH];
        self.authorName = author[self.model.pageUrl];
        htmlForm = [[NSString alloc] initWithFormat:@"<html><p>来源:%@</p><body>", self.authorName];
    }
    
    NSString* htmlBody = [[NSString alloc] initWithFormat:@"<div class=\"logo\" style=\"text-indent:2em\"><font style=\"font-size:%@px;\">%@</font></div>",_fontArray[_fontIndex],self.model.desc];
    NSString* htmlFooter = @"</body></html>";
    NSString* strHtml = [[NSString alloc] initWithFormat:@"%@%@%@%@%@",htmlHeader,htmldate,htmlForm,htmlBody,htmlFooter];
    return strHtml;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self seDefaultValues];
    [self createUI];
    _pageNo = 1;
    _isRefreshing = NO;
    
    // Do any additional setup after loading the view.
}
#pragma mark - UI
- (void)createUI{
    if (iPhoneX) {
        _backgroundView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _videoHeight + 44, MainScreenWidth, MainScreenHeight - _videoHeight - 44 - 44 - 34)];
    } else {
        _backgroundView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _videoHeight, MainScreenWidth, MainScreenHeight - _videoHeight-TabBarHeight)];

    }
    [self.view addSubview:_backgroundView];
    if ([self.model.playType isEqualToString:@"1"]) {
//        [self addCNLivePlayer];

    } else {
        [self addZXVideo:_movieUrl];
    }
    
    [_backgroundView addSubview:self.webView];
    [self.webView loadHTMLString:[self setHTMLString] baseURL:nil];
    [_backgroundView addSubview:self.tableView];
    self.detailsView.dic = [self.model toDictionary];
    self.detailsView.pics = @[@"cnlive_fontsize", @"cnlive_isfavorite" ,@"cnlive_share"];
    __weak typeof(self) weakSelf = self;
    self.detailsView.block = ^(NSInteger tag) {
        switch (tag) {
            case 1000://字体
            {
                if (weakSelf.fontArray.count - 1 == weakSelf.fontIndex) {
                    weakSelf.fontIndex = 0;
                } else {
                    weakSelf.fontIndex++;
                }
                if (0 == weakSelf.fontIndex) {
                    NSLog(@"0");
                    [weakSelf.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'" completionHandler:nil];
                } else if (1 == weakSelf.fontIndex) {
                    NSLog(@"1");
                    [weakSelf.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '120%'" completionHandler:nil];
                } else if (2 == weakSelf.fontIndex) {
                    NSLog(@"2");
                    [weakSelf.webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '140%'" completionHandler:nil];
                }
            }
                break;
            case 1001://收藏
            {
                NSDictionary *dic = [weakSelf.model toDictionary];
                TableData *notice = [[TableData alloc] initWithJSONDictionary:dic];
                BOOL isIn = [[DBAction sharedDBAction] isInTable:notice];
                if (!isIn) {
                    BOOL isInsert = [[DBAction sharedDBAction] insertTable:notice];
                    if (isInsert) {
                        [AlertViewTool showHUDAddedToView:weakSelf.view text:CustomStr(@"CollectSuccess")];
                        [weakSelf.detailsView.btn setImage:[UIImage imageNamed:@"cnlive_isfavorited"] forState:UIControlStateNormal];
                        
                    }
                    
                } else {
                    BOOL isDelete = [[DBAction sharedDBAction] deleteTable:notice];
                    if (isDelete) {
                        [AlertViewTool showHUDAddedToView:weakSelf.view text:CustomStr(@"CancelCollect")];
                        [weakSelf.detailsView.btn setImage:[UIImage imageNamed:@"cnlive_isfavorite"] forState:UIControlStateNormal];
                        
                    }
                }
                
            }
                break;
            case 1002://分享
            {
                [[TemplateShareManager manager] shareImage:self.model.img title:self.model.title text:self.model.desc?self.model.desc:self.model.title url:self.model.pageUrl controller:self];
                return;
            }
                break;
                
            default:
                break;
        }
        
    };
    [self.view addSubview:self.detailsView];
}
// CNLivePlayer
- (void)addCNLivePlayer {
    
    if (!self.liveController) {
//        CGRect frame = CGRectMake(0, iPhoneX ? 44 : 0, MainScreenWidth, _videoHeight);
        CGRect frame = CGRectMake(0, 0, MainScreenWidth, _videoHeight);
        UIView *bgView = [[UIView alloc] initWithFrame:frame];
        bgView.backgroundColor = [UIColor blackColor];
        
        UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        backButton.frame = CGRectMake(10, 20, 25, 25);
        [backButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:backButton];
        [self.view addSubview:bgView];
        
        // 点播
        self.liveController = [[CNLivePlayerController alloc] initVodPlayWithVId:self.model.uuid authSuccess:^{
            NSError *error = nil;
            [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryAmbient error: &error];
            [[AVAudioSession sharedInstance] setActive:YES error:nil];
            
            [bgView removeFromSuperview];
            [self.liveController configPlayerWithFrame:frame isLiving:NO];
            [self.liveController showInView:self.view];
            
        } authFailed:^(NSDictionary *errorInfo) {
            NSLog(@"初始化点播播放器失败%@", errorInfo);
        }];
        
        [[NSUserDefaults standardUserDefaults] setObject:@0 forKey:@"ZXVideoPlayer_DidLockScreen"];
        
        __weak typeof(self) weakSelf = self;
        self.liveController.videoPlayerGoBackBlock = ^{
            __strong typeof(self) strongSelf = weakSelf;

            [[NSUserDefaults standardUserDefaults] setObject:@0 forKey:@"ZXVideoPlayer_DidLockScreen"];
            [strongSelf.liveController destroyPlayer];
            strongSelf.liveController = nil;
            [strongSelf goBack];
        };
    }
    ZXVideo *video = [[ZXVideo alloc] init];
    video.title = self.model.title;
    self.liveController.video = video;
}

- (void)addZXVideo:(NSURL *)videoUrl {
    if (!self.videoController) {
        CGRect frame = CGRectMake(0, iPhoneX ? 44 : 0, MainScreenWidth, _videoHeight);
        self.videoController = [[ZXVideoPlayerController alloc] initWithFrame:frame];
        __weak typeof(self) weakSelf = self;
        self.videoController.videoPlayerGoBackBlock = ^{
            __strong typeof(self) strongSelf = weakSelf;
            
            [[NSUserDefaults standardUserDefaults] setObject:@0 forKey:@"ZXVideoPlayer_DidLockScreen"];
            strongSelf.videoController = nil;
            [strongSelf goBack];
            
            //时长统计结束
            if ([strongSelf.model.cmsColumnId isEqualToString:@"spzl/djzw/"]) {
                //记者问答
                [CNLiveStat endEvent:@"djzwsc"];
            }else if ([strongSelf.model.cmsColumnId isEqualToString:@"spzl/zgsfz/"]){
                //视界
                [CNLiveStat endEvent:@"sjsc"];
            }else if ([strongSelf.model.cmsColumnId isEqualToString:@"spzl/gxzf/"]){
                //国新联播
                [CNLiveStat endEvent:@"gxlbsc"];
            }else if ([strongSelf.model.cmsColumnId isEqualToString:@"spzl/jlzg/"]){
                //记录中国
                [CNLiveStat endEvent:@"jlzgsc"];
            }else if ([strongSelf.model.cmsColumnId isEqualToString:@"spzl/fbhhf/"]){
                //发布会回放
                [CNLiveStat endEvent:@"fbhhfsc"];
            }else if ([strongSelf.model.cmsColumnId isEqualToString:@"spzl/icons/"]){
                //CHINA ICONS
                [CNLiveStat endEvent:@"iconssc"];
            }else if ([strongSelf.model.cmsColumnId isEqualToString:@"spzl/gxzs/"]){
                //CHINA ICONS
                [CNLiveStat endEvent:@"gxzssc"];
            }
        };
        
        //增加播放器背景图片
        if (!(nil == _bgImg)) {
            self.videoController.view.contentMode = UIViewContentModeScaleAspectFit;
            self.videoController.view.layer.contents = (__bridge id _Nullable)(_bgImg.CGImage);
        }
        
        [self.videoController showInView:self.view];
    }
    
        //时长统计开始
    if ([self.model.cmsColumnId isEqualToString:@"spzl/djzw/"]) {
        //记者问答
        [CNLiveStat beginEvent:@"djzwsc"];
    }else if ([self.model.cmsColumnId isEqualToString:@"spzl/zgsfz/"]){
        //视界
        [CNLiveStat beginEvent:@"sjsc"];
    }else if ([self.model.cmsColumnId isEqualToString:@"spzl/gxzf/"]){
        //国新联播
        [CNLiveStat beginEvent:@"gxlbsc"];
    }else if ([self.model.cmsColumnId isEqualToString:@"spzl/jlzg/"]){
        //记录中国
        [CNLiveStat beginEvent:@"jlzgsc"];
    }else if ([self.model.cmsColumnId isEqualToString:@"spzl/fbhhf/"]){
        //发布会回放
        [CNLiveStat beginEvent:@"fbhhfsc"];
    }else if ([self.model.cmsColumnId isEqualToString:@"spzl/icons/"]){
        //CHINA ICONS
        [CNLiveStat beginEvent:@"iconssc"];
    }else if ([self.model.cmsColumnId isEqualToString:@"spzl/gxzs/"]){
        //CHINA ICONS
        [CNLiveStat beginEvent:@"gxzssc"];
    }
    
    ZXVideo *video = [[ZXVideo alloc] init];
    video.playUrl = _url;
    video.title = self.model.title;
    self.videoController.video = video;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.videoController stop];
    [self.liveController stop];
    self.liveController = nil;
    [self.navigationController setNavigationBarHidden:NO];
    NSError *error = nil;
    [[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayback error: &error];
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
}

#pragma mark - UI
- (void)setUpNavigationBar{
    
    self.navigationController.navigationBar.barTintColor = Color_Blue;
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize:18]};

    //左
    UIButton *headerButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 44)];
    headerButton.layer.cornerRadius = 2.0;
    headerButton.layer.masksToBounds = true;
    [headerButton setImage:[UIImage imageNamed:@"cnlive_white_back"] forState:UIControlStateNormal];
    [headerButton addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:headerButton];
    
    self.navigationItem.title = self.model.cmsColumnName;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - WebViewDelegate
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
    NSURL *requestURL = webView.URL;
    if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString:@"https"] || [[requestURL scheme] isEqualToString: @"mailto" ]) && (navigationAction.navigationType == WKNavigationTypeLinkActivated)) {
        [[UIApplication sharedApplication] openURL:requestURL];
        decisionHandler(WKNavigationActionPolicyCancel);
    }
    decisionHandler(WKNavigationActionPolicyAllow);
}
//- (void)webViewDidFinishLoad:(IWebView *)webView{
//    CGFloat width = MainScreenWidth - 20;
//    NSString *string = [NSString stringWithFormat:
//                        @"var script = document.createElement('script');"
//                        "script.type = 'text/javascript';"
//                        "script.text = \"function ResizeImages() { "
//                        "var myimg,oldwidth;"
//                        "var maxwidth=%f;" //缩放系数
//                        "for(i=0;i <document.images.length;i++){"
//                        "myimg = document.images[i];"
//                        "if(myimg.width > maxwidth){"
//                        "oldwidth = myimg.width;"
//                        "myimg.width = maxwidth;"
//                        "myimg.height = myimg.height * (maxwidth/oldwidth);"
//                        "}"
//                        "}"
//                        "}\";"
//                        "document.getElementsByTagName('head')[0].appendChild(script);",width];
//    [webView stringByEvaluatingJavaScriptFromString:string];
//    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
//    CGRect frame = _webView.frame;
//    frame.size.width = MainScreenWidth;
//    frame.size.height = 1;
//    _webView.frame = frame;
//    CGFloat graphicLiveHeight = _webView.scrollView.contentSize.height;
//    frame.size.height = _webView.scrollView.contentSize.height;
//    _webView.frame = frame;
//    if (0 == [_heightArr[_fontIndex] floatValue]) {
//        [_heightArr replaceObjectAtIndex:_fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]];
//    }
//    [self loadRelatedInformation];
//}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation*)navigation {
    CGFloat width = MainScreenWidth - 20;
    NSString *string = [NSString stringWithFormat:
                        @"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var myimg,oldwidth;"
                        "var maxwidth=%f;" //缩放系数
                        "for(i=0;i <document.images.length;i++){"
                        "myimg = document.images[i];"
                        "if(myimg.width > maxwidth){"
                        "oldwidth = myimg.width;"
                        "myimg.width = maxwidth;"
                        "myimg.height = myimg.height * (maxwidth/oldwidth);"
                        "}"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('head')[0].appendChild(script);",width];
    [webView evaluateJavaScript:@"ResizeImages();" completionHandler:nil];
    [webView evaluateJavaScript:string completionHandler:^(id _Nullable response, NSError * _Nullable error) {
        NSLog(@"value: %@ error: %@", response, error);
        dispatch_async(dispatch_get_main_queue(), ^{
            CGRect frame = self->_webView.frame;
            frame.size.width = MainScreenWidth;
            frame.size.height = 1;
            self->_webView.frame = frame;
            CGFloat graphicLiveHeight = self->_webView.scrollView.contentSize.height;
            frame.size.height = self->_webView.scrollView.contentSize.height;
            self->_webView.frame = frame;
            if (0 == [self->_heightArr[self->_fontIndex] floatValue]) {
                [self->_heightArr replaceObjectAtIndex:self->_fontIndex withObject:[NSString stringWithFormat:@"%f",graphicLiveHeight]];
            }
        });
    }];
    [self loadRelatedInformation];
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 40;
    
}

- (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section {
    return 0.00000001;
    
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.data.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //一张图 左边
    CNLiveNewsLeftPicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:newsLeftPicTableViewCell];
    if (!cell) {
        cell = [[CNLiveNewsLeftPicTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:newsLeftPicTableViewCell];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.contentView.backgroundColor = [UIColor whiteColor];
    cell.model = self.data[indexPath.row];
    return cell;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *lineImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 1)];
    lineImage.backgroundColor = Color_Line;
    [view addSubview:lineImage];
    UILabel *relatedInformation = [[UILabel alloc] initWithFrame:CGRectMake(12, 5, MainScreenWidth - 24, 35)];
    relatedInformation.backgroundColor = [UIColor clearColor];
    relatedInformation.textColor = HEXCOLOR(0x333333);
    relatedInformation.font = [UIFont systemFontOfSize:15];
    [view addSubview:relatedInformation];
    relatedInformation.text = CustomStr(@"RelevantInformation");
    return view;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CNHomeModel *model = self.data[indexPath.row];
    if ([model.type isEqualToString:@"program"]) {
        VideoDetailViewController *vc = [[VideoDetailViewController alloc] init];
        vc.model = model;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    } else if ([model.type isEqualToString:@"article"]) {
//        ArticleDetailViewController *vc = [[ArticleDetailViewController alloc] init];
//        vc.model = model;
//        vc.hidesBottomBarWhenPushed = YES;
//        [self.navigationController pushViewController:vc animated:YES];
    }
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([cell respondsToSelector:@selector(setSeparatorInset:)])
        [cell setSeparatorInset:UIEdgeInsetsZero];
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)])
        [cell setLayoutMargins:UIEdgeInsetsZero];
}

/*
#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.
}
*/
#pragma mark - Action
- (void)goBack{
    if(self.videoController){
        [self.videoController stop];
    }
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - Lazy Laoding
- (WKWebView *)webView {
    if (!_webView) {
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";

        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];

        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, MainScreenWidth, 0) configuration:wkWebConfig];
        _webView.scrollView.backgroundColor = [UIColor clearColor];
//        _webView.dataDetectorTypes = UIDataDetectorTypeNone;
        _webView.backgroundColor = [UIColor clearColor];
        _webView.scrollView.bounces = NO;
        _webView.navigationDelegate = self;
        _webView.UIDelegate = self;
        _webView.backgroundColor = [UIColor whiteColor];
        _webView.opaque = NO;
        _webView.scrollView.scrollEnabled = NO;
    }
    return _webView;
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerClass:[CNLiveNewsLeftPicTableViewCell class] forCellReuseIdentifier:newsLeftPicTableViewCell];
        if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [_tableView setSeparatorInset:UIEdgeInsetsZero];
        }
        //  设置上拉加载
//        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
//        _tableView.mj_footer = footer;
        
    }
    return _tableView;
}
- (NSMutableArray *)data{
    if (!_data) {
        _data = [[NSMutableArray alloc] init];
    }
    return _data;
}
- (NSArray *)fontArray{
    if (!_fontArray) {
        _fontArray = [[NSArray alloc] initWithObjects:@"16", @"20", @"24", nil];
    }
    return _fontArray;
}
- (void)viewDidLayoutSubviews{
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)])
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0,0,0,0)];
    
    if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)])
        [self.tableView setLayoutMargins:UIEdgeInsetsMake(0,0,0,0)];
}

- (BOOL)shouldAutorotate {
    return ![[[NSUserDefaults standardUserDefaults] objectForKey:@"ZXVideoPlayer_DidLockScreen"] boolValue];
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskAllButUpsideDown;
}
- (CNLiveNewsDetailsView *)detailsView{
    if (!_detailsView) {
        _detailsView = [[CNLiveNewsDetailsView alloc] initWithFrame:CGRectMake(0, MainScreenHeight-TabBarHeight, MainScreenWidth, TabBarHeight)];
    }
    return _detailsView;
}
@end