From 0fe2be212f074cd7a194f4968d500d379d4c247b Mon Sep 17 00:00:00 2001 From: linyudan <1031378945@qq.com> Date: Wed, 29 Nov 2023 15:18:56 -0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4web=E7=9A=84=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yinmeng-ios.xcodeproj/project.pbxproj | 4 +- .../Base/Tool/YYUtility/YYUtility+App.m | 14 +- .../yinmeng-ios/Main/YMWebViewController.m | 218 +++++++++++++++--- 3 files changed, 196 insertions(+), 40 deletions(-) diff --git a/yinmeng-ios/yinmeng-ios.xcodeproj/project.pbxproj b/yinmeng-ios/yinmeng-ios.xcodeproj/project.pbxproj index c29f5a3..0a84a87 100644 --- a/yinmeng-ios/yinmeng-ios.xcodeproj/project.pbxproj +++ b/yinmeng-ios/yinmeng-ios.xcodeproj/project.pbxproj @@ -2705,7 +2705,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.4.1; + MARKETING_VERSION = 1.0.0; ONLY_ACTIVE_ARCH = YES; PRODUCT_BUNDLE_IDENTIFIER = "linyudan.yinmeng-ios"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -2750,7 +2750,7 @@ "$(inherited)", "@executable_path/Frameworks", ); - MARKETING_VERSION = 1.4.1; + MARKETING_VERSION = 1.0.0; ONLY_ACTIVE_ARCH = YES; PRODUCT_BUNDLE_IDENTIFIER = "linyudan.yinmeng-ios"; PRODUCT_NAME = "$(TARGET_NAME)"; diff --git a/yinmeng-ios/yinmeng-ios/Base/Tool/YYUtility/YYUtility+App.m b/yinmeng-ios/yinmeng-ios/Base/Tool/YYUtility/YYUtility+App.m index 8e24226..94b023a 100644 --- a/yinmeng-ios/yinmeng-ios/Base/Tool/YYUtility/YYUtility+App.m +++ b/yinmeng-ios/yinmeng-ios/Base/Tool/YYUtility/YYUtility+App.m @@ -25,7 +25,7 @@ } + (NSString *)appName { - return @"yinmeng"; + return @"mew"; } + (NSString *)appBuild{ @@ -109,12 +109,12 @@ static NSString *_from = nil; && ![from isEqualToString:@""]) { _from = from; } else { - if (isEnterprise) { - _from = @"yinmeng_Enterprise"; // 企业包 - }else { - _from = @"yinmeng_appstore"; // App Store包 - } - _from = @"yinmeng_Enterprise"; // 企业包 +// if (isEnterprise) { +// _from = @"yinmeng_Enterprise"; // 企业包 +// }else { +// _from = @"mew_appstore"; // App Store包 +// } + _from = @"mew_appstore"; // 企业包 } } diff --git a/yinmeng-ios/yinmeng-ios/Main/YMWebViewController.m b/yinmeng-ios/yinmeng-ios/Main/YMWebViewController.m index d4266a4..8234b22 100644 --- a/yinmeng-ios/yinmeng-ios/Main/YMWebViewController.m +++ b/yinmeng-ios/yinmeng-ios/Main/YMWebViewController.m @@ -6,22 +6,23 @@ // #import "YMWebViewController.h" +#import "MewLoginViewController.h" +#import "BaseNavigationController.h" /// Tool #import "ApiHost.h" +#import "Api+Mine.h" #import "YYUtility.h" #import "AccountInfoStorage.h" #import "ThemeColor.h" #import "YMMacro.h" #import "NewEncryptTool.h" +#import "HttpRequestHelper.h" +#import "AccountModel.h" +#import "YMHUDTool.h" +#import "ClientConfig.h" /// Third #import - - -//#ifdef DEBUG -//static const NSString *CompanyFirstDomainByWeChatRegister = @"api.uat.lecheng163.com"; -//#else -//static const NSString *CompanyFirstDomainByWeChatRegister = @"api.lecheng163.com"; -//#endif +#import @interface WeakWebViewScriptMessageDelegate : NSObject @@ -54,40 +55,41 @@ @end + NSString * const kJSGetUid = @"getUid"; +NSString * const kJSGetDeviceId = @"getDeviceId"; +NSString * const kJSGetTicket = @"getTicket"; +NSString * const kJSGetDeviceInfo = @"getDeviceInfo"; +NSString * const kInitShowNav = @"initShowNav"; +NSString * const kCloseWebView = @"closeWebView"; +NSString * const kCancelAccount = @"cancelAccount"; @interface YMWebViewController () @property (strong, nonatomic) WKWebView *webview; +@property (nonatomic, strong) WKUserContentController *userContentController; +///是否隐藏导航栏 +@property (nonatomic,assign) BOOL isHiddenNav; @end + @implementation YMWebViewController - (void)viewDidLoad { [super viewDidLoad]; - // Do any additional setup after loading the view. + self.isHiddenNav = NO; [self initView]; - NSString *urlString = [NSString stringWithFormat:@"%@/%@", [NewEncryptTool MEW_aesDecrypt: API_HOST_H5_URL], self.url]; - [self.webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]]; } - - -- (void)backButtonClick { - BOOL canGoBack = YES; - if (self.webview.backForwardList.backList.count <= 1) { - canGoBack = NO; - } - - if ([self.webview canGoBack]) { - [self.webview goBack]; - } else { - [self.navigationController popViewControllerAnimated:YES]; - // [self.userContentController removeAllUserScripts]; - } +- (void)viewWillDisappear:(BOOL)animated { + [super viewWillDisappear:animated]; + self.navigationController.interactivePopGestureRecognizer.enabled = YES; } +- (void)viewDidAppear:(BOOL)animated { + [super viewDidAppear:animated]; + [self.navigationController setNavigationBarHidden:self.isHiddenNav animated:animated]; +} -#pragma mark - InitView - (void)initView { if (@available(iOS 11.0, *)) { self.webview.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever; @@ -99,19 +101,115 @@ NSString * const kJSGetUid = @"getUid"; make.left.right.top.bottom.mas_equalTo(self.view); }]; - } -#pragma mark - Set + +- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message { + [self.webview evaluateJavaScript:@"document.location.href" completionHandler:^(id _Nullable response, NSError * _Nullable error) { + NSLog(@"%@", response); + NSString *currentUrl = [NSString stringWithFormat:@"%@", response]; + ///测试环境只要有host就执行,方便h5连接本地调试 +#ifdef DEBUG + if (currentUrl != nil) { +#else + if (currentUrl != nil) { +#endif + if ([message.name isEqualToString:kJSGetUid]) { + NSString *uid = [[AccountInfoStorage instance] getUid]; + NSString *js = [NSString stringWithFormat:@"getMessage(\"uid\",%@)", uid]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) { + NSLog(@"%@",error); + }]; + } else if ([message.name isEqualToString:kJSGetTicket]) { + NSString *ticket = [[AccountInfoStorage instance] getTicket]; + NSString *js = [NSString stringWithFormat:@"getMessage(\"ticket\",\"%@\")",ticket]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) { + NSLog(@"%@",error); + }]; + } else if ([message.name isEqualToString:kJSGetDeviceId]) { + NSString *js = [NSString stringWithFormat:@"getMessage(\"deviceId\",\"%@\")",[YYUtility deviceUniqueIdentification]]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable other, NSError * _Nullable error) { + NSLog(@"%@",error); + }]; + } else if ([message.name isEqualToString:kJSGetDeviceInfo]) { + NSDictionary *basicParmars = [HttpRequestHelper configBaseParmars:[[NSDictionary alloc] init]]; + NSString *json = [basicParmars mj_JSONString]; + NSString *js = [NSString stringWithFormat:@"getMessage(\"deviceInfo\",%@)", json]; + [self.webview evaluateJavaScript:js completionHandler:^(id _Nullable ohter, NSError * _Nullable error) { + NSLog(@"%@", error); + }]; + } else if([message.name isEqualToString:kInitShowNav]) { + if (((NSNumber *)message.body).intValue == 0) { + self.isHiddenNav = YES; + if (self.navigationController) { + [self.navigationController setNavigationBarHidden:YES]; + } + } + } else if([message.name isEqualToString:kCloseWebView]) { + if (self.navigationController) { + [self.navigationController popViewControllerAnimated:YES]; + } + } else if ([message.name isEqualToString:kCancelAccount]) { + [YMHUDTool showLoadingWithMessage:@"正在注销中,请稍等"]; + AccountModel *model = [[AccountInfoStorage instance] accountModel]; + [Api logoutCurrentAccount:^(BaseModel * _Nonnull data, NSInteger code, NSString * _Nonnull msg) { + [YMHUDTool showSuccessWithMessage:@"注销成功"]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [self.navigationController popToRootViewControllerAnimated:NO]; + [[AccountInfoStorage instance] saveAccountInfo:nil]; + [[AccountInfoStorage instance] saveTicket:nil]; + ///关闭心跳 + [[ClientConfig shareConfig] resetHeartBratTimer]; + MewLoginViewController *loginVC = [[MewLoginViewController alloc] init]; + BaseNavigationController * nav = [[BaseNavigationController alloc] initWithRootViewController:loginVC]; + nav.modalPresentationStyle = UIModalPresentationFullScreen; + [self.navigationController presentViewController:nav animated:YES completion:nil]; + }); + + } access_token:model.access_token]; + + } + } + }]; + } + + - (void)setUrl:(NSString *)urlString{ _url = urlString; if (_url == nil) { return; } + if (![_url hasPrefix:@"http"] && ![_url hasPrefix:@"https"]){ + _url = [NSString stringWithFormat:@"%@/%@", [NewEncryptTool MEW_aesDecrypt:API_HOST_H5_URL], _url]; + } + if (![_url containsString:@"alipayh5"]) { + if (![_url containsString:@"?"]) { + _url = [NSString stringWithFormat:@"%@?platform=%@", _url, [YYUtility appName]]; + } else { + _url = [NSString stringWithFormat:@"%@&platform=%@", _url, [YYUtility appName]]; + } + } + + // 去掉 urlString 中的空格。 + NSString *noSpaceTextUrl = [_url stringByReplacingOccurrencesOfString:@" " withString:@""]; + + NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:noSpaceTextUrl]]; + [self.webview loadRequest:request]; } - -#pragma mark - Get +- (void)backButtonClick { + BOOL canGoBack = YES; + if (self.webview.backForwardList.backList.count <= 1) { + canGoBack = NO; + } + + if ([self.webview canGoBack]) { + [self.webview goBack]; + } else { + [self.navigationController popViewControllerAnimated:YES]; + [self.userContentController removeAllUserScripts]; + } +} - (WKWebView *)webview { if (_webview == nil) { @@ -122,18 +220,76 @@ NSString * const kJSGetUid = @"getUid"; } else { // Fallback on earlier versions } - - _webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)]; + configuration.allowsInlineMediaPlayback = YES; + NSString *uid = [[AccountInfoStorage instance] getUid]; + NSString *realCookie = [NSString stringWithFormat:@"%@=%@",@"uid",uid]; + + WKUserScript *cookieScript = [[WKUserScript alloc] initWithSource: [NSString stringWithFormat:@"document.cookie = '%@';", realCookie] injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:NO]; + [self.userContentController addUserScript:cookieScript]; + ///禁止缩放 + NSString *scaleJs = @"$('meta[name=description]').remove(); $('head').append( '' );"; + WKUserScript *scaleScript = [[WKUserScript alloc] initWithSource:scaleJs injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO]; + [self.userContentController addUserScript:scaleScript]; + + //根据生成的WKUserScript对象,初始化WKWebViewConfiguration + configuration.preferences.javaScriptEnabled = YES; + configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES; + configuration.preferences.minimumFontSize = 10; + configuration.selectionGranularity = WKSelectionGranularityCharacter; + configuration.userContentController = self.userContentController; + + CGSize size = [UIScreen mainScreen].bounds.size; + _webview = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, size.width,size.height) configuration:configuration]; + + _webview.navigationDelegate = self; + UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(backButtonClick)]; [_webview addGestureRecognizer:swipeGesture]; [_webview.scrollView setShowsVerticalScrollIndicator:NO]; [_webview.scrollView setShowsHorizontalScrollIndicator:NO]; + + //set useragent + __weak typeof(self) weakSelf = self; + [_webview evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) { + NSString *userAgent = result; + + if (![userAgent containsString:@"tutuAppIos erbanAppIos"]){ + NSString *newUserAgent = [userAgent stringByAppendingString:@" tutuAppIos erbanAppIos"]; + NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:newUserAgent, @"UserAgent", nil]; + [[NSUserDefaults standardUserDefaults] registerDefaults:dictionary]; + [[NSUserDefaults standardUserDefaults] synchronize]; + [weakSelf.webview setCustomUserAgent:newUserAgent]; + } + + }]; _webview.scrollView.bounces = NO; } return _webview; } +- (WKUserContentController *)userContentController{ + if (!_userContentController) { + //自定义的WKScriptMessageHandler 是为了解决内存不释放的问题 + WeakWebViewScriptMessageDelegate *weakScriptMessageDelegate = [[WeakWebViewScriptMessageDelegate alloc] initWithDelegate:self]; + _userContentController = [[WKUserContentController alloc] init]; + // 获取uid + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetUid]; + // 获取设备id + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetDeviceId]; + // 获取Ticket + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetTicket]; + // 获取设备info + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kJSGetDeviceInfo]; + // 隐藏导航栏 + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kInitShowNav]; + // 隐藏导航栏 + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kCloseWebView]; + // 注销账号 + [_userContentController addScriptMessageHandler:weakScriptMessageDelegate name:kCancelAccount]; + } + return _userContentController; +} @end