YOUPAIHRWebWindow.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // YOUPAIHRWebWindow.m
  3. // YOUQU
  4. //
  5. // Created by xiaohaoran on 2021/12/13.
  6. // Copyright © 2021 MS. All rights reserved.
  7. //
  8. #import "YOUPAIHRWebWindow.h"
  9. #import <WebKit/WebKit.h>
  10. #import "YOUPAILCIMSessionVC.h"
  11. #import "UIViewController+TFPresent.h"
  12. @interface YOUPAIHRWebWindow ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler>
  13. @property(nonatomic,strong)WKWebView *webView;
  14. @property (nonatomic,weak) UIView *youpaipBgView;
  15. @end
  16. @implementation YOUPAIHRWebWindow
  17. - (void)dealloc{
  18. [[NSNotificationCenter defaultCenter] removeObserver:self];
  19. }
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. self.baseView.hidden = YES;
  23. [self youpaifappendURLProperty];
  24. [self youpaifsetupUI];
  25. [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(youpaifApplePaySuccess:) name:@"yqApplePaySuccess" object:nil];
  26. }
  27. - (void)youpaifappendURLProperty{
  28. if ([self.youpaipurl containsString:@"?"]) {
  29. self.youpaipurl = [NSString stringWithFormat:@"%@&safeHeight=%@",self.youpaipurl,@(StatusBarHeight)];
  30. }else{
  31. self.youpaipurl = [NSString stringWithFormat:@"%@?safeHeight=%@",self.youpaipurl,@(StatusBarHeight)];
  32. }
  33. }
  34. - (void)youpaifApplePaySuccess:(NSNotification *)not{
  35. [self.webView evaluateJavaScript:@"vue_onload()" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  36. NSLog(@"%@", error);
  37. }];
  38. }
  39. -(void)youpaifsetupUI{
  40. //bgView
  41. UIView *youpaipBgView = [UIView new];
  42. // qxpBgView.layer.cornerRadius = 8.0f;
  43. // qxpBgView.clipsToBounds = YES;
  44. youpaipBgView.backgroundColor = [LCBkgColor colorWithAlphaComponent:0.3f];
  45. [self.view addSubview:youpaipBgView];
  46. self.youpaipBgView = youpaipBgView;
  47. switch (self.youpaipdirection) {
  48. case LZWebDirectionWithCenter:{
  49. [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  50. make.center.equalTo(self.view);
  51. make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight)));
  52. }];
  53. }
  54. break;
  55. case LZWebDirectionWithTop:{
  56. [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  57. make.top.offset(0.0f);
  58. make.centerX.equalTo(self.view);
  59. make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight)));
  60. }];
  61. }
  62. break;
  63. case LZWebDirectionWithLeft:{
  64. [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.left.offset(0.0f);
  66. make.centerY.equalTo(self.view);
  67. make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight)));
  68. }];
  69. }
  70. break;
  71. case LZWebDirectionWithRight:{
  72. [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  73. make.right.offset(0.0f);
  74. make.centerY.equalTo(self.view);
  75. make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight)));
  76. }];
  77. }
  78. break;
  79. case LZWebDirectionWithBottom:{
  80. [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  81. make.bottom.offset(0.0f);
  82. make.centerX.equalTo(self.view);
  83. make.size.mas_offset(CGSizeMake(ScaleSize(self.youpaipwidth), ScaleSize(self.youpaipheight)));
  84. }];
  85. }
  86. break;
  87. case LZWebDirectionWithFull:{
  88. [youpaipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
  89. make.top.bottom.left.right.offset(0.0f);
  90. }];
  91. }
  92. break;
  93. default:
  94. break;
  95. }
  96. //webView
  97. WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  98. [userContentController addScriptMessageHandler:self name:@"closeWindow"];// 关闭窗口
  99. [userContentController addScriptMessageHandler:self name:@"openActivity"]; //js调用可跳转app指定内页
  100. [userContentController addScriptMessageHandler:self name:@"showH5Dialog"]; //js调用Web窗口
  101. [userContentController addScriptMessageHandler:self name:@"startP2PSession"];// 客服
  102. [userContentController addScriptMessageHandler:self name:@"openMainActivity"];// js调用可跳转app指定内页
  103. // WKWebView的配置
  104. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  105. configuration.preferences = [NSClassFromString(@"WKPreferences") new];
  106. configuration.userContentController = userContentController;
  107. //创建wkwebview
  108. WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:configuration];
  109. webView.UIDelegate = self;
  110. webView.navigationDelegate = self;
  111. [youpaipBgView addSubview:webView];
  112. [webView mas_makeConstraints:^(MASConstraintMaker *make) {
  113. make.top.mas_equalTo(0);
  114. make.left.mas_equalTo(0);
  115. make.right.mas_equalTo(0);
  116. make.bottom.mas_equalTo(0);
  117. }];
  118. webView.backgroundColor =[UIColor clearColor];
  119. webView.opaque = NO;
  120. webView.navigationDelegate = self;
  121. [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.youpaipurl]]];
  122. // 是否允许手势左滑返回上一级, 类似导航控制的左滑返回
  123. webView.allowsBackForwardNavigationGestures = NO;
  124. webView.scrollView.showsVerticalScrollIndicator = NO;
  125. webView.scrollView.showsHorizontalScrollIndicator = NO;
  126. webView.scrollView.bounces = NO;
  127. webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  128. }
  129. -(void)cancelButtonClick{
  130. [self dismissViewControllerAnimated:YES completion:^{}];
  131. }
  132. //oc调js
  133. //[webView evaluateJavaScript:promptCode completionHandler:^(id _Nullable response, NSError * _Nullable error) {
  134. // if (error) {
  135. // NSLog(@"错误是:%@",error);
  136. // }
  137. //
  138. //}];
  139. //js调oc
  140. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
  141. if ([message.name isEqualToString:@"closeWindow"]) {
  142. [self dismissViewControllerAnimated:YES completion:^{}];
  143. }else if ([message.name isEqualToString:@"openActivity"]){
  144. //int linkType,String linkUrl
  145. // NSLog(@"openActivity方法名:%@", message.name);
  146. // NSLog(@"openActivity参数:%@", message.body);
  147. if ([message.body isKindOfClass:[NSDictionary class]]) {
  148. NSDictionary *dict = message.body;
  149. NSNumber *link_type = dict[@"linkType"];
  150. NSString *link_url = dict[@"linkUrl"];
  151. [self goPageWithLinkType:link_type linkUrl:link_url];
  152. }
  153. }else if ([message.name isEqualToString:@"openMainActivity"]){
  154. if ([message.body isKindOfClass:[NSDictionary class]]) {
  155. NSDictionary *dict = message.body;
  156. NSString *type = dict[@"type"];
  157. [self goPageWithLinkType:@(2) linkUrl:type];
  158. }
  159. if ([message.body isKindOfClass:[NSString class]]) {
  160. }
  161. }else if ([message.name isEqualToString:@"showH5Dialog"]){
  162. //int linkType,String linkUrl
  163. // NSLog(@"openActivity方法名:%@", message.name);
  164. // NSLog(@"openActivity参数:%@", message.body);
  165. if ([message.body isKindOfClass:[NSDictionary class]]) {
  166. NSDictionary *dict = message.body;
  167. NSString *gravity = dict[@"gravity"];
  168. CGFloat width = [dict[@"width"] floatValue];
  169. CGFloat height = [dict[@"height"] floatValue];
  170. NSString *loadUrl = dict[@"loadUrl"];
  171. YOUPAIHRWebWindow *window = [[YOUPAIHRWebWindow alloc] init];
  172. window.youpaipgravity = gravity;
  173. window.youpaipwidth = width;
  174. window.youpaipheight = height;
  175. window.youpaipurl = loadUrl;
  176. [self TFPresentVC:window completion:^{}];
  177. }
  178. }else if([message.name isEqualToString:@"startP2PSession"]){
  179. NIMSession *session = [NIMSession session:[LCSaveData getServerId] type:NIMSessionTypeP2P];
  180. if (session) {
  181. @weakify(self);
  182. [ZCHUDHelper show];
  183. [[[NIMSDK sharedSDK] userManager] fetchUserInfos:@[session.sessionId] completion:^(NSArray<NIMUser *> * _Nullable users, NSError * _Nullable error) {
  184. @strongify(self);
  185. [ZCHUDHelper dismiss];
  186. YOUPAILCIMSessionVC *vc = [[YOUPAILCIMSessionVC alloc] initWithSession:session];
  187. [[LCTools getContainNavigationControllerCurrentVC].navigationController pushViewController:vc animated:YES];
  188. }];
  189. }
  190. }
  191. }
  192. - (void)goPageWithLinkType:(NSNumber *)linkType linkUrl:(NSString *)linkUrl{
  193. if ([linkType isEqualToNumber:@1]) {
  194. ZCBaseWebVC* baseWeb = [[ZCBaseWebVC alloc]init];
  195. baseWeb.contentUrl = linkUrl;
  196. [[LCTools getContainNavigationControllerCurrentVC].navigationController pushViewController:baseWeb animated:YES];
  197. }else if ([linkType isEqualToNumber:@2]||[linkType isEqualToNumber:@3]){
  198. [[LCTools getContainNavigationControllerCurrentVC] youpaifpageToStr:linkUrl];
  199. }
  200. }
  201. // 当内容开始返回时调用
  202. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  203. }
  204. // 页面加载完成之后调用
  205. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
  206. self.youpaipBgView.layer.cornerRadius = 0.0f;
  207. self.youpaipBgView.backgroundColor = [UIColor clearColor];
  208. }
  209. // 页面加载失败时调用
  210. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
  211. }
  212. // 接收到服务器跳转请求之后调用
  213. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
  214. }
  215. // 在收到响应后,决定是否跳转
  216. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  217. NSLog(@"收到的是:%@",navigationResponse.response);
  218. //允许跳转
  219. __weak __typeof(self)wself = self;
  220. decisionHandler(WKNavigationResponsePolicyAllow);
  221. }
  222. - (void)setYoupaipgravity:(NSString *)youpaipgravity{
  223. _youpaipgravity = youpaipgravity;
  224. if ([youpaipgravity isEqual:@"center"]) {
  225. self.youpaipdirection = LZWebDirectionWithCenter;
  226. }else if ([youpaipgravity isEqual:@"left"]) {
  227. self.youpaipdirection = LZWebDirectionWithLeft;
  228. }else if ([youpaipgravity isEqual:@"right"]) {
  229. self.youpaipdirection = LZWebDirectionWithRight;
  230. }else if ([youpaipgravity isEqual:@"top"]) {
  231. self.youpaipdirection = LZWebDirectionWithTop;
  232. }else if ([youpaipgravity isEqual:@"bottom"]) {
  233. self.youpaipdirection = LZWebDirectionWithBottom;
  234. }else if ([youpaipgravity isEqual:@"full"]) {
  235. self.youpaipdirection = LZWebDirectionWithFull;
  236. }else{
  237. self.youpaipdirection = LZWebDirectionWithCenter;
  238. }
  239. }
  240. @end