NYWPViewModel.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. //
  2. // NYWPViewModel.m
  3. // jiaPei
  4. //
  5. // Created by Ning.ge on 2024/7/24.
  6. // Copyright © 2024 JCZ. All rights reserved.
  7. //
  8. #import "NYWPViewModel.h"
  9. @interface NYWPViewModel ()
  10. @property (nonatomic,assign) BOOL isLk;
  11. @property (nonatomic,assign) NSInteger code;
  12. @property (nonatomic,strong) NSString *ws;
  13. @end
  14. @implementation NYWPViewModel
  15. - (void)executeMethodWithName:(NSString *)methodName withParam:(NSString *)param {
  16. NSString *value = NYDDov_dict[methodName];
  17. SEL selector = NSSelectorFromString([NSString stringWithFormat:@"%@:", value]);
  18. if ([self respondsToSelector:selector]) {
  19. [self performSelector:selector withObject:param];
  20. } else {
  21. NSLog(@"Method not found: %@", methodName);
  22. }
  23. }
  24. - (WKWebView *)wView{
  25. if(!_wView){
  26. WKUserContentController *userContentController = [[WKUserContentController alloc] init];
  27. NYUSerADD0
  28. NYUSerADD1
  29. NYUSerADD2
  30. NYUSerADD3
  31. NYUSerADD4
  32. NSMutableString *jsString = [NSMutableString string];
  33. [jsString appendString:@"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"];
  34. [jsString appendString:@"document.documentElement.style.webkitTouchCallout='none';"];
  35. [jsString appendString:@"document.documentElement.style.webkitUserSelect='none';"];
  36. [jsString appendString:@"var script = document.createElement('meta');"
  37. "script.name = 'viewport';"
  38. "script.content=\"width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";"
  39. "document.getElementsByTagName('head')[0].appendChild(script);"];
  40. WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jsString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
  41. [userContentController addUserScript:userScript];
  42. NSString *userAgent = @"";
  43. if (!(([[[UIDevice currentDevice] systemVersion] floatValue]) >= 9.0)) [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"userAgent":userAgent}];
  44. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  45. configuration.allowsInlineMediaPlayback = YES;
  46. if (@available(iOS 10.0, *)) {
  47. configuration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
  48. }
  49. configuration.userContentController = userContentController;
  50. WKWebView *view = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 300, 300) configuration:configuration];
  51. view.navigationDelegate = self;
  52. view.UIDelegate = self;
  53. if (@available(iOS 9.0, *)) {
  54. view.customUserAgent = userAgent;
  55. } else {
  56. // Fallback on earlier versions
  57. }
  58. _wView = view;
  59. }
  60. return _wView;
  61. }
  62. #pragma mark - WKNavigationDelegate
  63. - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
  64. }
  65. - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
  66. }
  67. - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
  68. }
  69. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  70. NSLog(@"navigationAction.request.URL: %@", navigationAction.request.URL);
  71. WKNavigationActionPolicy actionPolicy = WKNavigationActionPolicyAllow;//允许
  72. NSString *urlString = [[navigationAction.request URL] absoluteString];
  73. if([urlString containsString:self.ws]) {
  74. [MBProgressHUD rq_hideHUD];
  75. actionPolicy =WKNavigationActionPolicyCancel;//不允许跳转
  76. NSURL*url = [NSURL URLWithString:urlString];
  77. if([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
  78. [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
  79. }];
  80. }else{
  81. [[UIApplication sharedApplication]openURL:webView.URL];
  82. }
  83. //延迟1秒
  84. [self performSelector:@selector(showUpview) withObject:nil afterDelay:1.0];
  85. }
  86. decisionHandler(actionPolicy);
  87. }
  88. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
  89. decisionHandler(WKNavigationResponsePolicyAllow);
  90. }
  91. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler {
  92. NSURLCredential *cred = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  93. completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
  94. }
  95. #pragma mark - WKScriptMessageHandler
  96. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
  97. [MBProgressHUD rq_hideHUD];
  98. if(self.isLk){
  99. return;
  100. }
  101. self.isLk = YES;
  102. NSString *vt = (NSString *)message.body;
  103. [self executeMethodWithName:message.name withParam:vt];
  104. self.isLk = NO;
  105. }
  106. #pragma mark - WKUIDelegate
  107. - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
  108. /// CoderMike Fixed : 解决点击网页的链接 不跳转的Bug。
  109. WKFrameInfo *frameInfo = navigationAction.targetFrame;
  110. if (![frameInfo isMainFrame]) {
  111. [webView loadRequest:navigationAction.request];
  112. }
  113. return nil;
  114. }
  115. #pragma mark runJavaScript
  116. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
  117. // [NSObject rq_showAlertViewWithTitle:nil message:message confirmTitle:@"我知道了"];
  118. completionHandler();
  119. }
  120. - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
  121. completionHandler(YES);
  122. }
  123. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler {
  124. completionHandler(defaultText);
  125. }
  126. - (void)callHph:(int )sf{
  127. NSString *Notification = @"";
  128. if(sf==1){
  129. Notification = RQUserNotificationOrWechatSuccess;
  130. }else{
  131. Notification = RQUserNotificationOrWechatFail;
  132. }
  133. [[NSNotificationCenter defaultCenter] postNotificationName:Notification object:nil userInfo:nil];
  134. }
  135. - (void)callApa:(NSString *)ap{
  136. [MBProgressHUD rq_showProgressHUD:@"购买中..."];
  137. NSString *idStr = [NSString stringWithFormat:@"com.kestrel.JSJPStudent.vip.%ld",self.code];
  138. [[IAPManager shareIAPManager] startPurchaseWithID:idStr completeHandle:^(IAPPurchType type, NSData * _Nonnull data, NSString * _Nonnull transactionIdentifier, BOOL isTest) {
  139. if (type == IAPPurchSuccess) {
  140. [[RQ_HTTP_Service postApplePayWithReceiptData:data transactionIdentifier:transactionIdentifier type:YES] subscribeNext:^(id _Nullable x) {
  141. [MBProgressHUD rq_hideHUD];
  142. [[NSNotificationCenter defaultCenter] postNotificationName:RQUserNotificationOrWechatSuccess object:nil userInfo:nil];
  143. } error:^(NSError * _Nullable error) {
  144. [MBProgressHUD rq_hideHUD];
  145. [MBProgressHUD rq_showErrorTips:error];
  146. }];
  147. }
  148. else {
  149. [MBProgressHUD rq_hideHUD];
  150. }
  151. }];
  152. }
  153. - (void)callSsd:(NSString *)ttl{
  154. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:ttl]];
  155. }
  156. - (void)showUpview
  157. {
  158. [RQ_SHARE_FUNCTION showAlertWithTitle:@"提示" message:@"刷新信息" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"失败" otherButtonTitles:@[@"成功"] otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) {
  159. if (selectedOtherButtonIndex == 0) {
  160. [self callHph:1];
  161. }else{
  162. [self callHph:0];
  163. }
  164. }];
  165. }
  166. - (void)callRee:(NSString *)pur{
  167. NSLog(@"错误码:%@",pur);
  168. ShowMsg([NSString stringWithFormat:@"服务器发生了%@错误",pur]);
  169. }
  170. - (void)callLad:(NSString *)ad{
  171. self.ws = ad;
  172. }
  173. - (NSDictionary *)getPudict{
  174. NSDictionary *obj = @{
  175. @"id":RQ_USER_MANAGER.currentUser._id,
  176. @"loginCode":RQ_USER_MANAGER.currentUser.loginCode==nil?@"":RQ_USER_MANAGER.currentUser.loginCode,
  177. @"nickName":RQ_USER_MANAGER.currentUser.nickName,
  178. @"pxkm":RQ_USER_MANAGER.currentUser.pxkm==nil?@"":RQ_USER_MANAGER.currentUser.pxkm,
  179. @"apsw":RQ_COMMON_MANAGER.APP_SWITCH?@"1":@"0",
  180. @"apt":[NSString stringWithFormat:@"%zd",RQ_COMMON_MANAGER.JSJP_iOS_PT]
  181. };
  182. return obj;
  183. }
  184. -(void)wkvPL:(NSString *)httpUrl{
  185. /// 加载请求数据
  186. NSString *encodedString = (NSString*) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)httpUrl,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL, kCFStringEncodingUTF8));
  187. NSURL *url = [NSURL URLWithString:encodedString];
  188. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  189. [self.wView loadRequest:request];
  190. }
  191. -(void)wwvPLJS:(NSInteger)code{
  192. self.code = code;
  193. [MBProgressHUD rq_showProgressHUD:@""];
  194. NSDictionary *obj = [self getPudict];
  195. [self.wView evaluateJavaScript:NYUCOdw_GO completionHandler:^(id result, NSError *error) {
  196. NSLog(@"is ++++ %@ %@", result,error);
  197. }];
  198. }
  199. @end