// // YOUPAIHRWebWindow.m // YOUQU // // Created by xiaohaoran on 2021/12/13. // Copyright © 2021 MS. All rights reserved. // #import "YOUPAIHRWebWindow.h" #import #import "YOUPAILCIMSessionVC.h" #import "UIViewController+TFPresent.h" @interface YOUPAIHRWebWindow () @property(nonatomic,strong)WKWebView *webView; @property (nonatomic,weak) UIView *youpaipBgView; @end @implementation YOUPAIHRWebWindow - (void)dealloc{ [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)viewDidLoad { [super viewDidLoad]; self.baseView.hidden = YES; [self youpaifappendURLProperty]; [self youpaifsetupUI]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(youpaifApplePaySuccess:) name:@"yqApplePaySuccess" object:nil]; } - (void)youpaifappendURLProperty{ if ([self.youpaipurl containsString:@"?"]) { self.youpaipurl = [NSString stringWithFormat:@"%@&safeHeight=%@",self.youpaipurl,@(StatusBarHeight)]; }else{ self.youpaipurl = [NSString stringWithFormat:@"%@?safeHeight=%@",self.youpaipurl,@(StatusBarHeight)]; } } - (void)youpaifApplePaySuccess:(NSNotification *)not{ [self.webView evaluateJavaScript:@"vue_onload()" completionHandler:^(id _Nullable result, NSError * _Nullable error) { NSLog(@"%@", error); }]; } -(void)youpaifsetupUI{ //bgView UIView *youpaipBgView = [UIView new]; // qxpBgView.layer.cornerRadius = 8.0f; // qxpBgView.clipsToBounds = YES; youpaipBgView.backgroundColor = [LCBkgColor colorWithAlphaComponent:0.3f]; [self.view addSubview:youpaipBgView]; self.youpaipBgView = youpaipBgView; switch (self.youpaipdirection) { case LZWebDirectionWithCenter:{ [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.center.equalTo(self.view); make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight))); }]; } break; case LZWebDirectionWithTop:{ [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.offset(0.0f); make.centerX.equalTo(self.view); make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight))); }]; } break; case LZWebDirectionWithLeft:{ [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.offset(0.0f); make.centerY.equalTo(self.view); make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight))); }]; } break; case LZWebDirectionWithRight:{ [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.right.offset(0.0f); make.centerY.equalTo(self.view); make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight))); }]; } break; case LZWebDirectionWithBottom:{ [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.bottom.offset(0.0f); make.centerX.equalTo(self.view); make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight))); }]; } break; case LZWebDirectionWithFull:{ [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.bottom.left.right.offset(0.0f); }]; } break; default: break; } //webView WKUserContentController *userContentController = [[WKUserContentController alloc] init]; [userContentController addScriptMessageHandler:self name:@"closeWindow"];// 关闭窗口 [userContentController addScriptMessageHandler:self name:@"openActivity"]; //js调用可跳转app指定内页 [userContentController addScriptMessageHandler:self name:@"showH5Dialog"]; //js调用Web窗口 [userContentController addScriptMessageHandler:self name:@"startP2PSession"];// 客服 [userContentController addScriptMessageHandler:self name:@"openMainActivity"];// js调用可跳转app指定内页 // WKWebView的配置 WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; configuration.preferences = [NSClassFromString(@"WKPreferences") new]; configuration.userContentController = userContentController; //创建wkwebview WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:configuration]; webView.UIDelegate = self; webView.navigationDelegate = self; [youpaipBgView addSubview:webView]; [webView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(0); make.left.mas_equalTo(0); make.right.mas_equalTo(0); make.bottom.mas_equalTo(0); }]; webView.backgroundColor =[UIColor clearColor]; webView.opaque = NO; webView.navigationDelegate = self; [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.youpaipurl]]]; // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回 webView.allowsBackForwardNavigationGestures = NO; webView.scrollView.showsVerticalScrollIndicator = NO; webView.scrollView.showsHorizontalScrollIndicator = NO; webView.scrollView.bounces = NO; webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; } -(void)cancelButtonClick{ [self dismissViewControllerAnimated:YES completion:^{}]; } //oc调js //[webView evaluateJavaScript:promptCode completionHandler:^(id _Nullable response, NSError * _Nullable error) { // if (error) { // NSLog(@"错误是:%@",error); // } // //}]; //js调oc - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{ if ([message.name isEqualToString:@"closeWindow"]) { [self dismissViewControllerAnimated:YES completion:^{}]; }else if ([message.name isEqualToString:@"openActivity"]){ //int linkType,String linkUrl // NSLog(@"openActivity方法名:%@", message.name); // NSLog(@"openActivity参数:%@", message.body); if ([message.body isKindOfClass:[NSDictionary class]]) { NSDictionary *dict = message.body; NSNumber *link_type = dict[@"linkType"]; NSString *link_url = dict[@"linkUrl"]; [self goPageWithLinkType:link_type linkUrl:link_url]; } }else if ([message.name isEqualToString:@"openMainActivity"]){ if ([message.body isKindOfClass:[NSDictionary class]]) { NSDictionary *dict = message.body; NSString *type = dict[@"type"]; [self goPageWithLinkType:@(2) linkUrl:type]; } if ([message.body isKindOfClass:[NSString class]]) { } }else if ([message.name isEqualToString:@"showH5Dialog"]){ //int linkType,String linkUrl // NSLog(@"openActivity方法名:%@", message.name); // NSLog(@"openActivity参数:%@", message.body); if ([message.body isKindOfClass:[NSDictionary class]]) { NSDictionary *dict = message.body; NSString *gravity = dict[@"gravity"]; CGFloat width = [dict[@"width"] floatValue]; CGFloat height = [dict[@"height"] floatValue]; NSString *loadUrl = dict[@"loadUrl"]; YOUPAIHRWebWindow *window = [[YOUPAIHRWebWindow alloc] init]; window.youpaipgravity = gravity; window.youpaipwidth = width; window.youpaipheight = height; window.youpaipurl = loadUrl; [self TFPresentVC:window completion:^{}]; } }else if([message.name isEqualToString:@"startP2PSession"]){ NIMSession *session = [NIMSession session:[LCSaveData getServerId] type:NIMSessionTypeP2P]; if (session) { @weakify(self); [ZCHUDHelper show]; [[[NIMSDK sharedSDK] userManager] fetchUserInfos:@[session.sessionId] completion:^(NSArray * _Nullable users, NSError * _Nullable error) { @strongify(self); [ZCHUDHelper dismiss]; YOUPAILCIMSessionVC *vc = [[YOUPAILCIMSessionVC alloc] initWithSession:session]; [[LCTools getContainNavigationControllerCurrentVC].navigationController pushViewController:vc animated:YES]; }]; } } } - (void)goPageWithLinkType:(NSNumber *)linkType linkUrl:(NSString *)linkUrl{ if ([linkType isEqualToNumber:@1]) { ZCBaseWebVC* baseWeb = [[ZCBaseWebVC alloc]init]; baseWeb.contentUrl = linkUrl; [[LCTools getContainNavigationControllerCurrentVC].navigationController pushViewController:baseWeb animated:YES]; }else if ([linkType isEqualToNumber:@2]||[linkType isEqualToNumber:@3]){ [[LCTools getContainNavigationControllerCurrentVC] youpaifpageToStr:linkUrl]; } } // 当内容开始返回时调用 - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{ } // 页面加载完成之后调用 - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{ self.youpaipBgView.layer.cornerRadius = 0.0f; self.youpaipBgView.backgroundColor = [UIColor clearColor]; } // 页面加载失败时调用 - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{ } // 接收到服务器跳转请求之后调用 - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{ } // 在收到响应后,决定是否跳转 - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{ NSLog(@"收到的是:%@",navigationResponse.response); //允许跳转 __weak __typeof(self)wself = self; decisionHandler(WKNavigationResponsePolicyAllow); } - (void)setYoupaipgravity:(NSString *)youpaipgravity{ _youpaipgravity = youpaipgravity; if ([youpaipgravity isEqual:@"center"]) { self.youpaipdirection = LZWebDirectionWithCenter; }else if ([youpaipgravity isEqual:@"left"]) { self.youpaipdirection = LZWebDirectionWithLeft; }else if ([youpaipgravity isEqual:@"right"]) { self.youpaipdirection = LZWebDirectionWithRight; }else if ([youpaipgravity isEqual:@"top"]) { self.youpaipdirection = LZWebDirectionWithTop; }else if ([youpaipgravity isEqual:@"bottom"]) { self.youpaipdirection = LZWebDirectionWithBottom; }else if ([youpaipgravity isEqual:@"full"]) { self.youpaipdirection = LZWebDirectionWithFull; }else{ self.youpaipdirection = LZWebDirectionWithCenter; } } @end