NYWPViewModel.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. _wView = view;
  60. }
  61. return _wView;
  62. }
  63. #pragma mark - WKNavigationDelegate
  64. - (void)webView:(WKWebView *)webView didCommitNavigation:(null_unspecified WKNavigation *)navigation {
  65. }
  66. - (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
  67. NSLog(@"didFinishNavigation");
  68. }
  69. - (void)webView:(WKWebView *)webView didFailNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error {
  70. }
  71. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {
  72. NSLog(@"navigationAction.request.URL: %@", navigationAction.request.URL);
  73. WKNavigationActionPolicy actionPolicy = WKNavigationActionPolicyAllow;//允许
  74. NSString *urlString = [[navigationAction.request URL] absoluteString];
  75. if([urlString containsString:self.ws]) {
  76. [MBProgressHUD rq_hideHUD];
  77. actionPolicy =WKNavigationActionPolicyCancel;//不允许跳转
  78. NSURL*url = [NSURL URLWithString:urlString];
  79. if([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
  80. [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
  81. }];
  82. }else{
  83. [[UIApplication sharedApplication]openURL:webView.URL];
  84. }
  85. //延迟1秒
  86. [self performSelector:@selector(showUpview) withObject:nil afterDelay:1.0];
  87. }
  88. decisionHandler(actionPolicy);
  89. }
  90. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler {
  91. decisionHandler(WKNavigationResponsePolicyAllow);
  92. }
  93. - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *__nullable credential))completionHandler {
  94. NSURLCredential *cred = [[NSURLCredential alloc] initWithTrust:challenge.protectionSpace.serverTrust];
  95. completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
  96. }
  97. #pragma mark - WKScriptMessageHandler
  98. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
  99. [MBProgressHUD rq_hideHUD];
  100. if(self.isLk){
  101. return;
  102. }
  103. self.isLk = YES;
  104. NSString *vt = (NSString *)message.body;
  105. [self executeMethodWithName:message.name withParam:vt];
  106. self.isLk = NO;
  107. }
  108. #pragma mark - WKUIDelegate
  109. - (nullable WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
  110. /// CoderMike Fixed : 解决点击网页的链接 不跳转的Bug。
  111. WKFrameInfo *frameInfo = navigationAction.targetFrame;
  112. if (![frameInfo isMainFrame]) {
  113. [webView loadRequest:navigationAction.request];
  114. }
  115. return nil;
  116. }
  117. #pragma mark runJavaScript
  118. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler {
  119. // [NSObject rq_showAlertViewWithTitle:nil message:message confirmTitle:@"我知道了"];
  120. completionHandler();
  121. }
  122. - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(BOOL result))completionHandler {
  123. completionHandler(YES);
  124. }
  125. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(NSString * __nullable result))completionHandler {
  126. completionHandler(defaultText);
  127. }
  128. - (void)callHph:(int )sf{
  129. NSString *Notification = @"";
  130. if(sf==1){
  131. Notification = RQUserNotificationOrWechatSuccess;
  132. }else{
  133. Notification = RQUserNotificationOrWechatFail;
  134. }
  135. [[NSNotificationCenter defaultCenter] postNotificationName:Notification object:nil userInfo:nil];
  136. }
  137. - (void)callApa:(NSString *)ap{
  138. [MBProgressHUD rq_showProgressHUD:@"购买中..."];
  139. NSString *idStr = [NSString stringWithFormat:@"com.kestrel.JSJPStudent.vip.%ld",self.code];
  140. [[IAPManager shareIAPManager] startPurchaseWithID:idStr completeHandle:^(IAPPurchType type, NSData * _Nonnull data, NSString * _Nonnull transactionIdentifier, BOOL isTest) {
  141. if (type == IAPPurchSuccess) {
  142. [[RQ_HTTP_Service postApplePayWithReceiptData:data transactionIdentifier:transactionIdentifier type:YES] subscribeNext:^(id _Nullable x) {
  143. [MBProgressHUD rq_hideHUD];
  144. [[NSNotificationCenter defaultCenter] postNotificationName:RQUserNotificationOrWechatSuccess object:nil userInfo:nil];
  145. } error:^(NSError * _Nullable error) {
  146. [MBProgressHUD rq_hideHUD];
  147. [MBProgressHUD rq_showErrorTips:error];
  148. }];
  149. }
  150. else {
  151. [MBProgressHUD rq_hideHUD];
  152. }
  153. }];
  154. }
  155. - (void)callSsd:(NSString *)ttl{
  156. if([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
  157. NSURL*url = [NSURL URLWithString:ttl];
  158. [[UIApplication sharedApplication] openURL:url options:@{UIApplicationOpenURLOptionUniversalLinksOnly: @NO} completionHandler:^(BOOL success) {
  159. }];
  160. }else{
  161. [[UIApplication sharedApplication]openURL:ttl];
  162. }
  163. }
  164. - (void)showUpview
  165. {
  166. [RQ_SHARE_FUNCTION showAlertWithTitle:@"提示" message:@"刷新信息" alertControllerStyle:UIAlertControllerStyleAlert cancelButtonTitle:@"失败" otherButtonTitles:@[@"成功"] otherButtonStyles:nil showInWindow:NO completion:^(NSUInteger selectedOtherButtonIndex) {
  167. if (selectedOtherButtonIndex == 0) {
  168. [self callHph:1];
  169. }else{
  170. [self callHph:0];
  171. }
  172. }];
  173. }
  174. - (void)callRee:(NSString *)pur{
  175. NSLog(@"错误码:%@",pur);
  176. ShowMsg([NSString stringWithFormat:@"服务器发生了%@错误",pur]);
  177. }
  178. - (void)callLad:(NSString *)ad{
  179. self.ws = ad;
  180. }
  181. - (NSDictionary *)getPudict{
  182. NSDictionary *obj = @{
  183. @"id":RQ_USER_MANAGER.currentUser._id,
  184. @"loginCode":RQ_USER_MANAGER.currentUser.loginCode==nil?@"":RQ_USER_MANAGER.currentUser.loginCode,
  185. @"nickName":RQ_USER_MANAGER.currentUser.nickName,
  186. @"pxkm":RQ_USER_MANAGER.currentUser.pxkm==nil?@"":RQ_USER_MANAGER.currentUser.pxkm,
  187. @"apsw":@"1" ,//RQ_COMMON_MANAGER.APP_SWITCH?@"1":@"0",
  188. @"apt":[NSString stringWithFormat:@"%zd",RQ_COMMON_MANAGER.JSJP_iOS_PT], // 1 苹果
  189. };
  190. return obj;
  191. }
  192. -(void)wkvPL:(NSString *)httpUrl{
  193. /// 加载请求数据
  194. NSString *encodedString = (NSString*) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)httpUrl,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL, kCFStringEncodingUTF8));
  195. NSURL *url = [NSURL URLWithString:encodedString];
  196. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  197. [self.wView loadRequest:request];
  198. }
  199. -(void)wwvPLJS:(NSInteger)code goodType:(NSInteger)goodtype {
  200. self.code = code;
  201. self.goodtype = goodtype;
  202. [MBProgressHUD rq_showProgressHUD:@""];
  203. NSDictionary *obj = [self getPudict];
  204. NSLog(@"%@",NYUCOdw_GO);
  205. [self.wView evaluateJavaScript:NYUCOdw_GO completionHandler:^(id result, NSError *error) {
  206. NSLog(@"is ++++ %@ %@", result,error);
  207. }];
  208. }
  209. @end