CustomerWebViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. // 初始化配置对象
  56. WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  57. if (@available(iOS 18.0, *)) {
  58. // 系统版本 ≥ 18.0
  59. // 默认是NO,这个值决定了用内嵌HTML5播放视频还是用本地的全屏控制
  60. config.allowsInlineMediaPlayback = YES;
  61. // 自动播放, 不需要用户采取任何手势开启播放
  62. // WKAudiovisualMediaTypeNone 音视频的播放不需要用户手势触发, 即为自动播放
  63. config.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone;
  64. config.allowsAirPlayForMediaPlayback = YES;
  65. config.allowsPictureInPictureMediaPlayback = YES;
  66. }
  67. // WKWebView
  68. //WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
  69. self.webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
  70. // 解决闪白问题
  71. self.view.backgroundColor = [UIColor whiteColor];
  72. self.webView.backgroundColor = [UIColor whiteColor];
  73. self.webView.scrollView.backgroundColor = [UIColor whiteColor];
  74. self.webView.opaque = NO;
  75. self.webView.navigationDelegate = self;
  76. self.webView.UIDelegate = self;
  77. //要配置点击事件
  78. [self.webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id result, NSError *error) {
  79. if (error == noErr) {
  80. if ([result isKindOfClass:[NSString class]]) {
  81. NSString *userAgent = result;
  82. NSString *appVersion = [iPhone appVersion];
  83. NSString *newUserAgent = [userAgent stringByAppendingString:[NSString stringWithFormat:@"iosVersionNumber/%@",appVersion]];
  84. [self.webView setCustomUserAgent:newUserAgent];
  85. }
  86. }
  87. }];
  88. [self.view addSubview:self.webView];
  89. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  90. if (needHidenNav){
  91. make.top.mas_equalTo(0);
  92. }else{
  93. make.top.equalTo(self.navBarBGView.mas_bottom);
  94. }
  95. make.left.mas_equalTo(0);
  96. make.width.mas_equalTo(SCREEN_W);
  97. make.bottom.mas_equalTo(0);
  98. }];
  99. // 加载网页
  100. NSURL *url = [NSURL URLWithString:self.webUrl];
  101. NSURLRequest *request = [NSURLRequest requestWithURL:url];
  102. [self.webView loadRequest:request];
  103. if (needHidenNav){
  104. [self.navBarBGView setHidden:YES];
  105. [self.view addSubview:self.backBtn];
  106. [self.backBtn mas_updateConstraints:^(MASConstraintMaker *make) {
  107. make.top.mas_equalTo(H_STATE_BAR + 12.f);
  108. }];
  109. }else{
  110. [self.navBarBGView setHidden:NO];
  111. }
  112. }
  113. //- (void)setWebUrl:(NSString *)webUrl{
  114. // _webUrl = webUrl;
  115. //
  116. // // 加载网页
  117. // NSURL *url = [NSURL URLWithString:_webUrl];
  118. // NSURLRequest *request = [NSURLRequest requestWithURL:url];
  119. // [self.webView loadRequest:request];
  120. //
  121. //}
  122. #pragma mark 事件代理
  123. // 页面开始加载时调用
  124. -(void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
  125. [self showWebView0];
  126. }
  127. // 当内容开始返回时调用
  128. - (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation{
  129. }
  130. // 页面加载完成之后调用
  131. - (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{//这里修改导航栏的标题,动态改变
  132. [self removeNewIndicator];
  133. NSString *curWebTitle = webView.title;
  134. //暂时只处理 首页点进来的新手引导
  135. if(curWebTitle && curWebTitle.length>0 && [self.webUrl containsString:@"novice?isNovice=true"]){
  136. self.titleLabel.text = curWebTitle;
  137. }
  138. [self showWebView1];
  139. }
  140. // 页面加载失败时调用
  141. - (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
  142. [self removeNewIndicator];
  143. [self showWebView3];
  144. }
  145. #pragma mark - 解决wkwebView闪白问题
  146. // 解决wkwebView闪白问题
  147. - (void)showWebView0 {
  148. self.webView.hidden = YES;
  149. [self performSelector:@selector(showWebView1) withObject:self afterDelay:1];
  150. [self showNewIndicatorWithCanBack:YES canTouch:NO];
  151. }
  152. // 1改变网页内容背景颜色
  153. - (void)showWebView1 {
  154. [self performSelector:@selector(showWebView2) withObject:self afterDelay:0.2];
  155. }
  156. // 2加载成功
  157. - (void)showWebView2 {
  158. [self removeNewIndicator];
  159. self.webView.hidden = NO;
  160. }
  161. // 3加载失败
  162. - (void)showWebView3 {
  163. [self removeNewIndicator];
  164. }
  165. // 接收到服务器跳转请求之后再执行
  166. - (void)webView:(WKWebView *)webView didReceiveServerRedirectForProvisionalNavigation:(WKNavigation *)navigation{
  167. }
  168. // 在收到响应后,决定是否跳转
  169. - (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
  170. WKNavigationResponsePolicy actionPolicy = WKNavigationResponsePolicyAllow;
  171. //这句是必须加上的,不然会异常
  172. decisionHandler(actionPolicy);
  173. // NSArray *arrViewControllers = self.navigationController.viewControllers;
  174. // NSInteger index = [arrViewControllers indexOfObject:self];
  175. }
  176. // 在发送请求之前,决定是否跳转
  177. - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
  178. if (navigationAction.targetFrame == nil) {
  179. [webView loadRequest:navigationAction.request];
  180. }
  181. //这句是必须加上的,不然会异常
  182. decisionHandler(WKNavigationActionPolicyAllow);
  183. }
  184. #pragma mark - WKScriptMessageHandler
  185. - (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
  186. HLog(@"%@",message.body);
  187. HLog(@"%@",message.name);
  188. //[[iToast makeText:@"已接收到消息"] show];
  189. if([message.name isEqualToString:@"getFileKey"])
  190. {//帮助反馈 h5调用原生 需要日志的FileKey
  191. [self gotoAddLogFun];
  192. }
  193. }
  194. #pragma mark 获取瑞云日志上传到文件中心
  195. //1.读取日志
  196. //2.上传到文件中心
  197. //3.拿到key上传到盒子服务器
  198. - (void)gotoAddLogFun
  199. {
  200. NSString *zipPath = [NSString stringWithFormat:@"%@/logs/archive.zip",CachesPatch];
  201. NSFileManager *fileManager = [NSFileManager defaultManager];
  202. // 检查文件是否存在
  203. BOOL fileExists = [fileManager fileExistsAtPath:zipPath];
  204. if (fileExists) {
  205. // 删除文件
  206. NSError *error = nil;
  207. BOOL success = [fileManager removeItemAtPath:zipPath error:&error];
  208. if (success) {
  209. HLog(@"文件已成功删除: %@", zipPath);
  210. } else {
  211. HLog(@"删除文件失败: %@", [error localizedDescription]);
  212. }
  213. }
  214. //2.读取日志
  215. //NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/debug_0.log",CachesPatch];
  216. NSString *ruiyunLogPath = [NSString stringWithFormat:@"%@/logs/webrtc_log_0",CachesPatch];
  217. NSString *ruiyunLogPath2 = [NSString stringWithFormat:@"%@/logs/app.log",CachesPatch];
  218. //3. 将文件压缩成ZIP
  219. NSArray *filesToZip = @[ruiyunLogPath, ruiyunLogPath2];
  220. [SSZipArchive createZipFileAtPath:zipPath withFilesAtPaths:filesToZip];
  221. NSData *logData = [NSData dataWithContentsOfFile:zipPath];
  222. //HLog(@"%@",logData);
  223. if(!logData || [logData length]==0){
  224. //1.4.2以及后续 webrtc版本 没有日志
  225. [self getFileKeyToH5By:@""];
  226. return;
  227. }
  228. //4.上传到文件中心
  229. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  230. NSString *snStr = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  231. if(!snStr){
  232. snStr = [iTools getNowTimeString];
  233. }
  234. NSString*filename = [[NSString alloc] initWithFormat:@"%@_ios.log",snStr];
  235. [paraDict setValue:filename forKey:@"filename"];
  236. [paraDict setValue:@7 forKey:@"days"];
  237. KWeakSelf
  238. [[netWorkManager shareInstance] doUploadFileToFileServiceWithParams:paraDict data:logData success:^(id _Nonnull responseObject) {
  239. HLog(@"%@",responseObject);
  240. fileUploadToFileCenterModel *model = [[fileUploadToFileCenterModel alloc] initWithDictionary:responseObject error:nil];
  241. if(model.code == 200){
  242. NSString * fileKey = model.data.fileKey;
  243. if(fileKey && fileKey.length >0){
  244. [weakSelf getFileKeyToH5By:fileKey];
  245. }
  246. else{
  247. [weakSelf getFileKeyToH5By:@""];
  248. }
  249. }
  250. else{
  251. [weakSelf getFileKeyToH5By:@""];
  252. }
  253. } faild:^(NSError * _Nonnull error) {
  254. [weakSelf getFileKeyToH5By:@""];
  255. }];
  256. }
  257. #pragma mark 拿到文件中心的key给h5
  258. - (void)getFileKeyToH5By:(NSString*)fileKey
  259. {
  260. NSString *jsFunction = @"transferFileKey('%@');";
  261. jsFunction = [jsFunction stringByReplacingOccurrencesOfString:@"%@" withString:fileKey];
  262. [self.webView evaluateJavaScript:jsFunction completionHandler:^(id _Nullable result, NSError * _Nullable error) {
  263. if (error != nil) {
  264. NSLog(@"Error evaluating JavaScript: %@", error);
  265. }
  266. }];
  267. }
  268. @end