NYWPViewModel.m 11 KB

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