admin
2022-01-17 b63ed504f6d1e652d9fbd67c1490e7821b159c75
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
//
//  PPTVController.m
//  BuWanVideo2.0
//
//  Created by Aeline on 2021/2/27.
//  Copyright © 2021 com.yeshi.buwansheque.ios. All rights reserved.
//
 
#import "PPTVController.h"
#import "LoggingViewController.h"
#import "BuyVipController.h"
 
#import "PPTVNavView.h"
#import "PPTVPppView.h"
#import "PPTVBottomVIpVIew.h"
#import <WebKit/WebKit.h>
 
#import "dsbridge.h"
#import "JSAPI.h"
#import <BUAdSDK/BUAdSDK.h>
 
@interface PPTVController () <WKNavigationDelegate, WKUIDelegate, PPTVDelegate, BUNativeExpressFullscreenVideoAdDelegate>
 
@property (nonatomic, nullable, strong) DWKWebView *webView;
@property (nonatomic, nullable, strong) PPTVNavView *viewPPTVNav;
@property (nonatomic, nullable, strong) PPTVPppView *viewPPTVPop;
@property (nonatomic, nullable, strong) PPTVBottomVIpVIew *viewPPTVVip;
 
 
@property (nonatomic, nullable, strong) JSAPI *jsAPI;
 
@property (nonatomic, nullable, copy) NSString *name;
@property (nonatomic, nullable, copy) NSString *content;
@property (nonatomic, strong) BUNativeExpressFullscreenVideoAd *fullscreenAd;
@end
 
@implementation PPTVController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObservers];
    [self loadViewConfig];
    
    NSDictionary *dictionary = [[NSUserDefaults standardUserDefaults] objectForKey:@"buUserInfo"];
    NSDate *datenow = [NSDate date]; // 现在时间
    NSInteger timeSp = [datenow timeIntervalSince1970]*1000;
    if(dictionary)
    {
        if ([dictionary[@"vipExpireTime"] integerValue] <= timeSp) {
            if ([YTHsharedManger startManger].ad[@"videoDetailFullVideo"] && [YTHsharedManger startManger].ad[@"videoDetailFullVideo"]!=[NSNull null]  && [[YTHsharedManger startManger].ad[@"videoDetailFullVideo"][@"type"] isEqualToString:@"csj"]) {
                    [self loadFullscreenVideoAd];
            }
         }
    }else{
        if ([YTHsharedManger startManger].ad[@"videoDetailFullVideo"] && [YTHsharedManger startManger].ad[@"videoDetailFullVideo"]!=[NSNull null] && [[YTHsharedManger startManger].ad[@"videoDetailFullVideo"][@"type"] isEqualToString:@"csj"]) {
                [self loadFullscreenVideoAd];
        }
    }
    //[self tryPlayFinish];
}
 
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    //是否登录
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"userOnLine"]){
        [self refrshData];
    }
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    
}
 
- (void)addObservers {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(login) name:@"h5Login" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tryPlayFinish) name:@"tryPlayFinish" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginSuccess) name:@"RELOAD_DATA" object:nil];
}
 
- (void)loadViewConfig {
    [self.view addSubview:self.viewPPTVNav];
    self.viewPPTVNav.name = _parms[@"name"];
    self.viewPPTVNav.content = _parms[@"playUrl"];
    [self.view addSubview:self.webView];
    [[UIApplication sharedApplication].keyWindow addSubview:self.viewPPTVPop];
    [[UIApplication sharedApplication].keyWindow addSubview:self.viewPPTVVip];
    
    NSString *url = self.parms[@"playUrl"];
    [self loadWebView:url];
    
    if (@available(iOS 13, *)) {
        
    } else {
        //将要进入全屏
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(startFullScreen) name:UIWindowDidResignKeyNotification object:nil];
        //退出全屏
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];
    }
    
    //[self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:NULL];
    //修改滚动速度
}
 
- (void)loadFullscreenVideoAd {
    self.fullscreenAd = [[BUNativeExpressFullscreenVideoAd alloc] initWithSlotID:@"946515710"];
    self.fullscreenAd.delegate = self;
    [self.fullscreenAd loadAdData];
}
 
//此方法在视频广告素材加载成功时调用。
- (void)nativeExpressFullscreenVideoAdDidLoad:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
    
}
 
//当视频广告素材加载失败时调用此方法。
- (void)nativeExpressFullscreenVideoAd:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error {
    
}
 
//此方法在成功呈现nativeExpressAdView时调用。
- (void)nativeExpressFullscreenVideoAdViewRenderSuccess:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd {
    
}
 
//当nativeExpressAdView无法呈现时,将调用此方法
- (void)nativeExpressFullscreenVideoAdViewRenderFail:(BUNativeExpressFullscreenVideoAd *)rewardedVideoAd error:(NSError *_Nullable)error {
    
}
//当视频缓存成功时调用此方法
- (void)nativeExpressFullscreenVideoAdDidDownLoadVideo:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
    [fullscreenVideoAd showAdFromRootViewController:self];
}
 
//此方法在用户单击“跳过”按钮时调用
- (void)nativeExpressFullscreenVideoAdDidClickSkip:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
    
}
 
//此方法在视频广告即将关闭时调用。
- (void)nativeExpressFullscreenVideoAdWillClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
    
}
 
//关闭视频广告时调用此方法
- (void)nativeExpressFullscreenVideoAdDidClose:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd {
    
}
 
//此方法在视频广告播放完成或发生错误时调用。
- (void)nativeExpressFullscreenVideoAdDidPlayFinish:(BUNativeExpressFullscreenVideoAd *)fullscreenVideoAd didFailWithError:(NSError *_Nullable)error {
    
    
}
 
- (void)refrshData {
   @ weakify(self)
    [[YTHNetInterface startInterface] getLoginUserInfoWithUid:[YTHsharedManger startManger].Uid WithSystem:@"1" WithLoginUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] withBlock:^(BOOL isSuccessful, id result, NSString *error) {
        @strongify(self)
        if (isSuccessful) {
            //首先判断数据是否获成功
            NSString *err = [NSString stringWithFormat:@"%@",[result objectForKey:@"Error"]];
            if (err.length == 0) {
                [[NSUserDefaults standardUserDefaults] setObject:[result objectForKey:@"Data"] forKey:@"buUserInfo"];
                [[NSUserDefaults standardUserDefaults] synchronize];
                
                [self.webView removeFromSuperview];
                self.webView.DSUIDelegate = nil;
                self.webView.navigationDelegate = nil;
                self.webView = nil;
                [self.view addSubview:self.webView];
                [self loadWebView:self.content];
            }
        }
    }
     ];
}
 
- (void)loadWebView:(NSString *)url {
    [self.webView addJavascriptObject:[[JSAPI alloc] init] namespace:@"yestv"];
    // open debug mode, Release mode should disable this.
    [self.webView setDebugMode:true];
    [self.webView customJavascriptDialogLabelTitles:@{@"alertTitle":@"Notification",@"alertBtn":@"OK"}];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [self.webView loadRequest:request];
}
 
-(void)startFullScreen {
    //[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    UIApplication *application = [UIApplication sharedApplication];
    [application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];
    application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI/2);
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    application.keyWindow.bounds = CGRectMake(0, 0, frame.size.height + 20, frame.size.width);
}
 
-(void)endFullScreen {
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    UIApplication *application = [UIApplication sharedApplication];
    [application setStatusBarOrientation: UIInterfaceOrientationLandscapeRight];
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    application.keyWindow.bounds = CGRectMake(0, 0, frame.size.width, frame.size.height + 20);
    [UIView animateWithDuration:0 animations:^{
        application.keyWindow.transform=CGAffineTransformMakeRotation(M_PI * 2);
    }];
}
 
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    //网页title
    if ([keyPath isEqualToString:@"title"]) {
        if (object == self.webView) {
            self.viewPPTVNav.name = self.webView.title;
            
        } else {
            [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
        }
        
    } else {
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}
 
#pragma mark --------- PPTVDelegate ---------
- (void)backVc {
    [_viewPPTVPop removeFromSuperview];
    [_viewPPTVVip removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}
 
- (void)moreSelect {
    self.viewPPTVPop.hidden = NO;
}
 
- (void)selectType:(NSString *)type {
    if ([type isEqualToString:@"刷新"]) {
        [self.webView reload];
        
    } else  if ([type isEqualToString:@"复制链接"]) {
        [self copyUrl];
        
    }else  if ([type isEqualToString:@"用其他浏览器打开"]) {
        if (@available(iOS 10, *)) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_content?:@""] options:@{} completionHandler:^(BOOL success) {}];
            
        } else {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_content?:@""]];
        }
    }
}
 
- (void)copyUrl {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = _content?:@"";
    
    //[SVProgressHUD showWithStatus:@"复制成功"];
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"复制成功" preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        //延时执行
        dispatch_queue_t q = dispatch_get_main_queue();
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.2 * NSEC_PER_SEC), q, ^{
            [alert dismissViewControllerAnimated:YES completion:nil];
        });
    }];
}
 
- (void)login {
    LoggingViewController *loginVC=[[LoggingViewController alloc] init];
    loginVC.backNotRoot = YES;
    [self.navigationController pushViewController:loginVC animated:YES];
}
 
- (void)tryPlayFinish {
    [SVProgressHUD show];
    
    NSString *vid = [self getParamByName:@"vid" URLString:_content];
    NSString *cid = [self getParamByName:@"cid" URLString:_content];
    [[YTHNetInterface startInterface] fetchPptvBuyResult:[YTHsharedManger startManger].Uid WithSystem:@"1" WithLoginUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] url:_content vid:vid cid:cid withBlock:^(BOOL isSuccessful, id result, NSString *error) {
        
        if (isSuccessful) {
            self.viewPPTVVip.data = result[@"Data"];
            BuyVipController *vc = [[BuyVipController alloc] init];
            vc.url =[[NSUserDefaults standardUserDefaults] objectForKey:VIPLINK];
            vc.refrshData = ^{
                [self refrshData];
            };
            [self.navigationController pushViewController:vc animated:YES];
            
            //self.viewPPTVVip.hidden = NO;
        }
        [SVProgressHUD dismiss];
    }];
    
    //self.viewPPTVVip.hidden = NO;
}
 
- (void)loginSuccess {
    [_webView removeFromSuperview];
    _webView.DSUIDelegate = nil;
    _webView.navigationDelegate = nil;
    _webView = nil;
    [self.view addSubview:self.webView];
    [self loadWebView:_content];
}
 
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [self jsAdds];
}
 
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
    
}
 
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
    [self jsAdds];
    [self performSelector:@selector(afterDelay) withObject:nil afterDelay:0.5];
    
    NSString *vid = [self getParamByName:@"vid" URLString:_content];
    NSString *cid = [self getParamByName:@"cid" URLString:_content];
    
    [[YTHNetInterface startInterface] fetchPPTVVideoPlayStateWith:[YTHsharedManger startManger].Uid WithSystem:@"1" WithLoginUid:[[NSUserDefaults standardUserDefaults] objectForKey:@"LoginUid"] url:_content vid:vid cid:cid withBlock:^(BOOL isSuccessful, id result, NSString *error) {
        if (isSuccessful) {
            if (result[@"Data"][@"title"]) {
                self.viewPPTVNav.name = result[@"Data"][@"title"];
            }
        }
    }];
}
 
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void(^)(WKNavigationActionPolicy))decisionHandler {
    
    NSString *strRequest = navigationAction.request.URL.absoluteString;
    
    if ([strRequest containsString:@"acmd.api.pptv.com"] && [strRequest containsString:@"bwysdqmovie"]) {
        if (![_content isEqualToString:strRequest] && _content) {
            decisionHandler(WKNavigationActionPolicyCancel);
            self.content = strRequest;
            [self loginSuccess];
            return;
        }
        self.content = strRequest;
        self.viewPPTVNav.content = strRequest;
        [self jsAdds];
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);//允许跳转
}
 
- (void)afterDelay {
    [self.webView evaluateJavaScript:@"javascript:adPlayFinish()" completionHandler:^(id _Nullable object, NSError * _Nullable error) {}];
}
 
- (void)jsAdds {
    NSString *str = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"js"ofType:@"txt"]   encoding:NSUTF8StringEncoding error:nil];
    [self.webView evaluateJavaScript:str completionHandler:^(id _Nullable object, NSError * _Nullable error) {}];
}
 
- (DWKWebView *)webView {
    if (!_webView) {
        // 0.网页配置对象
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
        // 1.原生与JS交互管理
        WKUserContentController *userContentController = [[WKUserContentController alloc] init];
        
        // 添加
        config.userContentController = userContentController;
        
        // 3.WKWebview设置
        WKPreferences *prefer = [[WKPreferences alloc] init];
        //设置是否支持javaScript 默认是支持的
        prefer.javaScriptEnabled = YES;
        // // 在iOS上默认为NO,表示是否允许不经过用户交互由javaScript自动打开窗口
        prefer.javaScriptCanOpenWindowsAutomatically = YES;
        // 添加
        config.preferences = prefer;
        config.preferences.minimumFontSize = 0.0f;
        // 是使用h5的视频播放器在线播放, 还是使用原生播放器全屏播放
        config.allowsInlineMediaPlayback = YES;
        //设置视频是否需要用户手动播放  设置为NO则会允许自动播放
        config.mediaTypesRequiringUserActionForPlayback = YES;
        //设置是否允许画中画技术 在特定设备上有效
        config.allowsPictureInPictureMediaPlayback = YES;
        //设置请求的User-Agent信息中应用程序名称 iOS9后可用
        config.applicationNameForUserAgent = @"ChinaDailyForiPad";
        
        config.suppressesIncrementalRendering = YES;
        
        _webView = [[DWKWebView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.viewPPTVNav.frame), KScreenW, KScreenH - CGRectGetHeight(self.viewPPTVNav.frame)) configuration:config];
        
        _webView.DSUIDelegate = self;
        _webView.navigationDelegate = self;
        
        // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
        _webView.allowsBackForwardNavigationGestures = YES;
        //可返回的页面列表, 存储已打开过的网页
        //WKBackForwardList * backForwardList = [_webView backForwardList];
    }
    return _webView;
}
 
- (PPTVNavView *)viewPPTVNav {
    if (!_viewPPTVNav) {
        _viewPPTVNav = [[PPTVNavView alloc] initWithFrame:CGRectMake(0, 0, KScreenW, kStatusBarH + ALNavBarH)];
        _viewPPTVNav.delegate = self;
    }
    return _viewPPTVNav;
}
 
- (PPTVPppView *)viewPPTVPop {
    if (!_viewPPTVPop) {
        _viewPPTVPop = [[PPTVPppView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.viewPPTVNav.frame), KScreenW, KScreenH - CGRectGetHeight(self.viewPPTVNav.frame))];
        _viewPPTVPop.hidden = YES;
        _viewPPTVPop.delegate = self;
    }
    return _viewPPTVPop;
}
 
- (PPTVBottomVIpVIew *)viewPPTVVip {
    if (!_viewPPTVVip) {
        _viewPPTVVip = [[PPTVBottomVIpVIew alloc] initWithFrame:CGRectMake(0, 0, KScreenW, KScreenH)];
        _viewPPTVVip.hidden = YES;
        @weakify(self)
        _viewPPTVVip.buyVip = ^{
            @strongify(self)
            BuyVipController *vc = [[BuyVipController alloc] init];
            vc.url = [[NSUserDefaults standardUserDefaults] objectForKey:VIPLINK];
            @weakify(self)
            vc.refrshData = ^{
                @strongify(self)
                [self refrshData];
            };
            [self.navigationController pushViewController:vc animated:YES];
        };
        // _viewPPTVVip.delegate = self;
    }
    return _viewPPTVVip;
}
 
- (NSString *)getParamByName:(NSString *)name URLString:(NSString *)url {
 
    NSError *error;
    NSString *regTags=[[NSString alloc] initWithFormat:@"(^|&|\\?)+%@=+([^&]*)(&|$)", name];
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regTags
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
     
    // 执行匹配的过程
    NSArray *matches = [regex matchesInString:url
                                      options:0
                                        range:NSMakeRange(0, [url length])];
    for (NSTextCheckingResult *match in matches) {
        NSString *tagValue = [url substringWithRange:[match rangeAtIndex:2]];  // 分组2所对应的串
        return tagValue;
    }
    return @"";
}
@end