CustomerWebViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // CustomerWebViewController.m
  3. // VclustersGemini
  4. //
  5. // Created by 余衡武 on 2022/8/11.
  6. // Copyright © 2022 APPLE. All rights reserved.
  7. //
  8. #import "CustomerWebViewController.h"
  9. #import <WebKit/WebKit.h>
  10. #import "SafeForKey.h"
  11. #import "iPhone.h"
  12. #import "fileUploadToFileCenterModel.h"
  13. #import <SSZipArchive/SSZipArchive.h>
  14. @interface CustomerWebViewController ()<WKScriptMessageHandler,WKNavigationDelegate,WKUIDelegate>
  15. @property (nonatomic,strong) WKWebView *webView;
  16. @end
  17. @implementation CustomerWebViewController
  18. @synthesize titleStr;
  19. @synthesize needHidenNav;
  20. -(void)viewWillAppear:(BOOL)animated {
  21. [super viewWillAppear:animated];
  22. [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"share"];
  23. [self.webView.configuration.userContentController addScriptMessageHandler:self name:@"getFileKey"];
  24. }
  25. - (void)viewWillDisappear:(BOOL)animated {
  26. [super viewWillDisappear:animated];
  27. [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"share"];
  28. [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"getFileKey"];
  29. }
  30. - (void)viewDidAppear:(BOOL)animated {
  31. [super viewDidAppear:animated];
  32. /**导航栏显示*/
  33. }
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. [self drawView];
  37. }
  38. #pragma mark 点击返回
  39. - (void)backBtnPressed
  40. {
  41. if([self.webView canGoBack] && ![self.webUrl containsString:@"novice?isNovice=true"]){
  42. [self.webView goBack];
  43. }
  44. else {
  45. if(_didClickBackFun){
  46. _didClickBackFun();
  47. }
  48. [super backBtnPressed];
  49. }
  50. }
  51. - (void)drawView {
  52. [self.toolBar setHidden:YES];
  53. [self.navigationBar setHidden:YES];
  54. self.titleLabel.text = titleStr;
  55. // WKWebView
  56. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  57. self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
  58. // 解决闪白问题
  59. self.view.backgroundColor = [UIColor whiteColor];
  60. self.webView.backgroundColor = [UIColor whiteColor];
  61. self.webView.scrollView.backgroundColor = [UIColor whiteColor];
  62. self.webView.opaque = NO;
  63. self.webView.navigationDelegate = self;
  64. self.webView.UIDelegate = self;
  65. //要配置点击事件
  66. [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
  67. if (error == noErr) {
  68. if ([result isKindOfClass:[NSString class]]) {
  69. NSString *userAgent = result;
  70. NSString *appVersion = [iPhone appVersion];
  71. NSString *newUserAgent = [userAgent stringByAppendingString:[NSString stringWithFormat:@"iosVersionNumber/%@",appVersion]];
  72. [self.webView setCustomUserAgent:newUserAgent];
  73. }
  74. }
  75. }];
  76. [self.view addSubview:self.webView];
  77. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  78. if (needHidenNav){
  79. make.top.mas_equalTo(0);
  80. }else{
  81. make.top.equalTo(self.navBarBGView.mas_bottom);
  82. }
  83. make.left.mas_equalTo(0);
  84. make.width.mas_equalTo(SCREEN_W);
  85. make.bottom.mas_equalTo(0);
  86. }];
  87. // 加载网页
  88. NSURL *url = [NSURL URLWithString:self.webUrl];
  89. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  90. [self.webView loadRequest:request];
  91. if (needHidenNav){
  92. [self.navBarBGView setHidden:YES];
  93. [self.view addSubview:self.backBtn];
  94. [self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
  95. make.top.mas_equalTo(H_STATE_BAR + 12.f);
  96. }];
  97. }else{
  98. [self.navBarBGView setHidden:NO];
  99. }
  100. }
  101. //- (void)setWebUrl:(NSString *)webUrl{
  102. // _webUrl = webUrl;
  103. //
  104. // // 加载网页
  105. // NSURL *url = [NSURL URLWithString:_webUrl];
  106. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  107. // [self.webView loadRequest:request];
  108. //
  109. //}
  110. #pragma mark 事件代理
  111. // 页面开始加载时调用
  112. -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  113. [self showWebView0];
  114. }
  115. // 当内容开始返回时调用
  116. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  117. }
  118. // 页面加载完成之后调用
  119. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变
  120. [self removeNewIndicator];
  121. NSString *curWebTitle = webView.title;
  122. //暂时只处理 首页点进来的新手引导
  123. if(curWebTitle && curWebTitle.length>0 && [self.webUrl containsString:@"novice?isNovice=true"]){
  124. self.titleLabel.text = curWebTitle;
  125. }
  126. [self showWebView1];
  127. }
  128. // 页面加载失败时调用
  129. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
  130. [self removeNewIndicator];
  131. [self showWebView3];
  132. }
  133. #pragma mark - 解决wkwebView闪白问题
  134. // 解决wkwebView闪白问题
  135. - (void)showWebView0 {
  136. self.webView.hidden = YES;
  137. [self performSelector:@selector(showWebView1) withObject:self afterDelay:1];
  138. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  139. }
  140. // 1改变网页内容背景颜色
  141. - (void)showWebView1 {
  142. [self performSelector:@selector(showWebView2) withObject:self afterDelay:0.2];
  143. }
  144. // 2加载成功
  145. - (void)showWebView2 {
  146. [self removeNewIndicator];
  147. self.webView.hidden = NO;
  148. }
  149. // 3加载失败
  150. - (void)showWebView3 {
  151. [self removeNewIndicator];
  152. }
  153. // 接收到服务器跳转请求之后再执行
  154. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
  155. }
  156. // 在收到响应后,决定是否跳转
  157. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  158. WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
  159. //这句是必须加上的,不然会异常
  160. decisionHandler(actionPolicy);
  161. // NSArray *arrViewControllers = self.navigationController.viewControllers;
  162. // NSInteger index = [arrViewControllers indexOfObject:self];
  163. }
  164. // 在发送请求之前,决定是否跳转
  165. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
  166. if (navigationAction.targetFrame == nil) {
  167. [webView loadRequest:navigationAction.request];
  168. }
  169. //这句是必须加上的,不然会异常
  170. decisionHandler(WKNavigationActionPolicyAllow);
  171. }
  172. #pragma mark - WKScriptMessageHandler
  173. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  174. HLog(@"%@",message.body);
  175. HLog(@"%@",message.name);
  176. //[[iToast makeText:@"已接收到消息"] show];
  177. if([message.name isEqualToString:@"getFileKey"])
  178. {//帮助反馈 h5调用原生 需要日志的FileKey
  179. [self gotoAddLogFun];
  180. }
  181. }
  182. #pragma mark 获取瑞云日志上传到文件中心
  183. //1.读取日志
  184. //2.上传到文件中心
  185. //3.拿到key上传到盒子服务器
  186. - (void)gotoAddLogFun
  187. {
  188. NSString *zipPath = [NSString stringWithFormat:@"%@/logs/archive.zip",CachesPatch];
  189. NSFileManager *fileManager = [NSFileManager defaultManager];
  190. // 检查文件是否存在
  191. BOOL fileExists = [fileManager fileExistsAtPath:zipPath];
  192. if (fileExists) {
  193. // 删除文件
  194. NSError *error = nil;
  195. BOOL success = [fileManager removeItemAtPath:zipPath error:&error];
  196. if (success) {
  197. HLog(@"文件已成功删除: %@", zipPath);
  198. } else {
  199. HLog(@"删除文件失败: %@", [error localizedDescription]);
  200. }
  201. }
  202. //2.读取日志
  203. //NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
  204. NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/webrtc_log_0",CachesPatch];
  205. NSString *ruiyunLogPath2 = [NSString stringWithFormat:@"%@/logs/app.log",CachesPatch];
  206. //3. 将文件压缩成ZIP
  207. NSArray *filesToZip = @[ruiyunLogPath, ruiyunLogPath2];
  208. [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:filesToZip];
  209. NSData *logData = [NSData dataWithContentsOfFile:zipPath];
  210. //HLog(@"%@",logData);
  211. if(!logData || [logData length]==0){
  212. //1.4.2以及后续 webrtc版本 没有日志
  213. [self getFileKeyToH5By:@""];
  214. return;
  215. }
  216. //4.上传到文件中心
  217. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  218. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  219. if(!snStr){
  220. snStr = [iTools getNowTimeString];
  221. }
  222. NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
  223. [paraDict setValue:filename forKey:@"filename"];
  224. [paraDict setValue:@7 forKey:@"days"];
  225. KWeakSelf
  226. [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
  227. HLog(@"%@",responseObject);
  228. fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
  229. if(model.code == 200){
  230. NSString * fileKey = model.data.fileKey;
  231. if(fileKey && fileKey.length >0){
  232. [weakSelf getFileKeyToH5By:fileKey];
  233. }
  234. else{
  235. [weakSelf getFileKeyToH5By:@""];
  236. }
  237. }
  238. else{
  239. [weakSelf getFileKeyToH5By:@""];
  240. }
  241. } faild:^(NSError * _Nonnull error) {
  242. [weakSelf getFileKeyToH5By:@""];
  243. }];
  244. }
  245. #pragma mark 拿到文件中心的key给h5
  246. - (void)getFileKeyToH5By:(NSString*)fileKey
  247. {
  248. NSString *jsFunction = @"transferFileKey('%@');";
  249. jsFunction = [jsFunction stringByReplacingOccurrencesOfString:@"%@" withString:fileKey];
  250. [self.webView evaluateJavaScript:jsFunction completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  251. if (error != nil) {
  252. NSLog(@"Error evaluating JavaScript: %@", error);
  253. }
  254. }];
  255. }
  256. @end