H6ViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // H6ViewController.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/2/21.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "H6ViewController.h"
  9. #import "LYLPhotoTailoringTool.h"
  10. #import "FWZGetNoteBookTool.h"
  11. #import "FWZFileGetTool.h"
  12. #import "H5ViewController.h"
  13. #import "NoticeUnreadVC.h"
  14. @interface H6ViewController ()<WKUIDelegate,WKNavigationDelegate,UIScrollViewDelegate,WKScriptMessageHandler>
  15. @property (nonatomic,strong) WKWebView *webView;
  16. @property (nonatomic,assign) CGFloat keyBoardH;
  17. @end
  18. @implementation H6ViewController
  19. - (void)viewDidLoad{
  20. [super viewDidLoad];
  21. [self.view addSubview:self.webView];
  22. [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
  23. make.edges.mas_offset(UIEdgeInsetsMake(64, 0, 0, 0));
  24. }];
  25. // NSURL *pathUrl = [NSURL URLWithString:@"http://192.168.0.118:8080/#/mobile/editor"];
  26. NSURL *pathUrl = [NSURL URLWithString:@"http://192.168.0.119:8080/#/mobile/noticeDetails"];
  27. NSURLRequest *request = [NSURLRequest requestWithURL:pathUrl];
  28. // [self getData];
  29. [self.webView loadRequest:request];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillShow:) name:UIKeyboardWillShowNotification object:nil];
  31. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoardWillHide:) name:UIKeyboardWillHideNotification object:nil];
  32. }
  33. - (void)getData
  34. {
  35. WS(weakSelf);
  36. [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Topic_App_Detail) parameters:@{@"Id":@(712),@"UserId":@([AppUserModel sharedAppUserModel].Id)} responseStyle:JOSN success:^(id _Nonnull responseObject) {
  37. NSLog(@"话题正文%@",responseObject);
  38. // NSMutableDictionary * mdict = [NSMutableDictionary dictionaryWithDictionary:responseObject];
  39. // [mdict removeObjectForKey:@"HtmlContent"];
  40. NSString *jsonString = [NSString stringWithFormat:@"getData('%@')",USERDEFAULTSGET(@"LOGINTOKEN")];
  41. [weakSelf.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  42. NSLog(@"data:%@",data);
  43. NSLog(@"error:%@",error);
  44. }];
  45. } failure:^(NSError * _Nonnull error) {
  46. SHOWERROR([ZYCTool handerResultData:error]);
  47. }];
  48. }
  49. - (void)upDateAction:(NSString *)action
  50. {
  51. [self.webView evaluateJavaScript:action completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  52. NSLog(@"上传成功");
  53. }];
  54. }
  55. - (WKWebView *)webView
  56. {
  57. if (!_webView) {
  58. WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  59. // 实例化对象
  60. WKUserContentController* userContent = [[WKUserContentController alloc] init];
  61. [userContent addScriptMessageHandler:self name:@"chosePic"];
  62. [userContent addScriptMessageHandler:self name:@"toPZ"];
  63. [userContent addScriptMessageHandler:self name:@"toBJ"];
  64. [userContent addScriptMessageHandler:self name:@"toSC"];
  65. [userContent addScriptMessageHandler:self name:@"toWJ"];
  66. [userContent addScriptMessageHandler:self name:@"delelteAction"];
  67. [userContent addScriptMessageHandler:self name:@"editAction"];
  68. [userContent addScriptMessageHandler:self name:@"postTopicDetailsData"];
  69. [userContent addScriptMessageHandler:self name:@"alreadyRead"];
  70. configuration.userContentController = userContent;
  71. WKPreferences *preferences = [WKPreferences new];
  72. preferences.javaScriptCanOpenWindowsAutomatically = YES;
  73. preferences.minimumFontSize = 40.0;
  74. configuration.preferences = preferences;
  75. // 初始化WKWebView
  76. _webView = [[WKWebView alloc]initWithFrame:CGRectZero configuration:configuration];
  77. // UI代理
  78. _webView.UIDelegate = self;
  79. // 导航代理
  80. _webView.navigationDelegate = self;
  81. _webView.scrollView.delegate = self;
  82. }
  83. return _webView;
  84. }
  85. #pragma mark - KeyBoard
  86. - (void)keyBoardWillShow:(NSNotification *)note
  87. {
  88. // 获取用户信息
  89. NSDictionary *userInfo = [NSDictionary dictionaryWithDictionary:note.userInfo];
  90. // 获取键盘高度
  91. CGRect keyBoardBounds = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  92. self.keyBoardH = keyBoardBounds.size.height;
  93. [self.webView evaluateJavaScript:@"textFocus()" completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  94. NSLog(@"keyShow");
  95. }];
  96. }
  97. - (void)keyBoardWillHide:(NSNotification *)note
  98. {
  99. self.keyBoardH = 0.f;
  100. [self.webView evaluateJavaScript:@"blur()" completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  101. NSLog(@"keyHidden");
  102. }];
  103. }
  104. #pragma mark - WKScriptMessageHandler
  105. - (void)userContentController:(nonnull WKUserContentController *)userContentController didReceiveScriptMessage:(nonnull WKScriptMessage *)message {
  106. WS(weakSelf);
  107. if ([message.name isEqualToString:@"chosePic"]) {
  108. [self chatBarDidPhotoAction];
  109. }
  110. if ([message.name isEqualToString:@"toPZ"]) {
  111. [[LYLPhotoTailoringTool sharedTool] selectPhotoWithPhoroOrCamera:SelectPhotoWithTypePhoroOrCamera2 crop:YES showImgBlock:^(NSString *imageUrlStr) {
  112. } choosImgBlock:^(UIImage *image) {
  113. [weakSelf didSelectImage:@[image]];
  114. }];
  115. }
  116. if ([message.name isEqualToString:@"toBJ"]) {
  117. [[FWZGetNoteBookTool sharedTool] openNoteBookListChooseNoteBookBlock:^(NSMutableArray<MyNoteBookSubModel *> * _Nonnull noteArr) {
  118. NSLog(@"SmartBarNotes %@",noteArr);
  119. [weakSelf disSelectNoteBook:noteArr];
  120. }];
  121. }
  122. if ([message.name isEqualToString:@"toSC"]) {
  123. [[FWZGetNoteBookTool sharedTool] openCollectListChooseCollectBlock:^(NSMutableArray<MyFavoriteSubModel *> * _Nonnull collectArr) {
  124. NSLog(@"SmartBarCollection %@",collectArr);
  125. [weakSelf disSelectCollect:collectArr];
  126. }];
  127. }
  128. if ([message.name isEqualToString:@"toWJ"]) {
  129. [[FWZFileGetTool sharedTool] openFileWithCrop:YES showImgBlock:^(NSString * _Nonnull fileUrlStr) {
  130. } choosFileBlock:^(NSData * _Nonnull fileData, NSString * _Nonnull fileName) {
  131. SHOWLOADING
  132. [[HttpManager sharedHttpManager] HeaderUploadFileUrl:Host(Modify_UserImages_Post) parameters:@{} fileData:fileData fileKey:@"file" fileName:fileName mimeType:@"multipart/form-data" success:^(id _Nonnull responseObject) {
  133. REMOVESHOW
  134. NSLog(@"上传成功的文件%@",responseObject);
  135. NSString *jsonString = [NSString stringWithFormat:@"getAndroidImg('%@','%@','%ld')",responseObject[@"AbsolutePath"],responseObject[@"FileName"],(long)CollectDataType_File];
  136. [weakSelf upDateAction:jsonString];
  137. } failure:^(NSError * _Nonnull error) {
  138. REMOVESHOW
  139. }];
  140. }];
  141. }
  142. if ([message.name isEqualToString:@"delelteAction"]) {
  143. [self deleteButtonHander:714];
  144. }
  145. if ([message.name isEqualToString:@"editAction"]) {
  146. [self editorButtonHander:714];
  147. }
  148. if ([message.name isEqualToString:@"postTopicDetailsData"]) {
  149. NSString *jsonString = [NSString stringWithFormat:@"getNoticeDetailsData('%ld','%@','%d')",(long)622,USERDEFAULTSGET(@"LOGINTOKEN"),1];
  150. // NSString *jsonString = [NSString stringWithFormat:@"getData('%ld','%@')",(long)714,USERDEFAULTSGET(@"LOGINTOKEN")];
  151. [self.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  152. NSLog(@"data:%@",data);
  153. NSLog(@"error:%@",error);
  154. }];
  155. }
  156. if ([message.name isEqualToString:@"alreadyRead"]) {
  157. NSLog(@"msgBody:%@",message.body);
  158. NSDictionary * dict = (NSDictionary *)message.body;
  159. NoticeUnreadVC *vc = [NoticeUnreadVC initNoticeUnreadVC];
  160. vc.Id = 622;
  161. vc.type = ReadNoticeType;
  162. vc.readCount = [dict[@"ReadCount"] integerValue];
  163. vc.unReadCount = [dict[@"AllCount"] integerValue] - [dict[@"ReadCount"] integerValue];
  164. [self.navigationController pushViewController:vc animated:YES];
  165. }
  166. }
  167. #pragma mark - 选择图片
  168. - (void)chatBarDidPhotoAction
  169. {
  170. WS(weakSelf);
  171. [self.view endEditing:YES];
  172. TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:9 delegate:nil];
  173. [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){
  174. leftButton.hidden = YES;
  175. }];
  176. imagePickerVc.showSelectBtn = NO;
  177. imagePickerVc.allowCrop = YES;
  178. imagePickerVc.cropRect = CGRectMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH) * 0.5, SCREEN_WIDTH, SCREEN_WIDTH);
  179. imagePickerVc.allowTakePicture = NO;
  180. imagePickerVc.allowTakeVideo = NO;
  181. imagePickerVc.allowPickingOriginalPhoto = NO;
  182. imagePickerVc.allowPickingGif = NO;
  183. [imagePickerVc setIsStatusBarDefault:YES];
  184. [imagePickerVc setNaviTitleColor:[UIColor blackColor]];
  185. [imagePickerVc setBarItemTextColor:[UIColor blackColor]];
  186. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
  187. PHAsset *asset = assets.firstObject;
  188. switch (asset.mediaType) {
  189. case PHAssetMediaTypeVideo: {
  190. } break;
  191. case PHAssetMediaTypeImage: {
  192. [weakSelf didSelectImage:photos];
  193. } break;
  194. case PHAssetMediaTypeAudio:
  195. break;
  196. case PHAssetMediaTypeUnknown:
  197. break;
  198. default: break;
  199. }
  200. }];
  201. [self presentViewController:imagePickerVc animated:YES completion:nil];
  202. }
  203. - (void)didSelectImage:(NSArray *)images
  204. {
  205. NSMutableArray * array = [NSMutableArray array];
  206. for (UIImage * image in images) {
  207. NSData *data = UIImageJPEGRepresentation(image, 1);
  208. [array addObject:data];
  209. }
  210. WS(weakSelf);
  211. NSString *urlString = [[NSString alloc] initWithFormat:@"%@%@",BaseUrl,Modify_UserImages_Post];
  212. [[HttpManager sharedHttpManager] HeaderUploadUrl:urlString parameters:@{} pictureArray:array pictureKey:@"file" success:^(id _Nonnull responseObject) {
  213. NSLog(@"%@",responseObject);
  214. for (NSDictionary * dict in responseObject) {
  215. NSString *jsonString = [NSString stringWithFormat:@"getAndroidImg('%@','%@','%ld')",dict[@"AbsolutePath"],dict[@"FileName"],(long)CollectDataType_IMG];
  216. [weakSelf upDateAction:jsonString];
  217. }
  218. } failure:^(NSError * _Nonnull error) {
  219. }];
  220. }
  221. #pragma mark - 选择笔记
  222. - (void)disSelectNoteBook:(NSMutableArray<MyNoteBookSubModel *>*_Nonnull )noteArr{
  223. for (MyNoteBookSubModel *dataModel in noteArr ) {
  224. if (dataModel.AttributeValue == 1) {
  225. NSString *jsonString = [NSString stringWithFormat:@"getAndroidNoteCollection('%ld','%@','%ld','%@')",(long)CollectDataType_Note,dataModel.Title,(long)dataModel.MiddleId,dataModel.Name];
  226. [self upDateAction:jsonString];
  227. }else{
  228. NSString *jsonString = [NSString stringWithFormat:@"getAndroidNoteCollection('%ld','%@','%ld','%@')",(long)CollectDataType_NoteFile,dataModel.FolderName,(long)dataModel.Id,[AppUserModel sharedAppUserModel].Name];
  229. [self upDateAction:jsonString];
  230. }
  231. }
  232. }
  233. #pragma mark - 选择收藏
  234. - (void)disSelectCollect:(NSMutableArray<MyFavoriteSubModel *> * _Nonnull) collectArr{
  235. for (MyFavoriteSubModel *dataModel in collectArr ) {
  236. if(dataModel.AttributeValue == 1)
  237. {
  238. NSString *jsonString = [NSString stringWithFormat:@"getAndroidNoteCollection('%ld','%@','%ld','%@')",(long)[ZYCTool returnDataType:dataModel.CollectionType],dataModel.Title,(long)dataModel.CollectionDataId,dataModel.Data.Author];
  239. [self upDateAction:jsonString];
  240. }else{
  241. NSString *jsonString = [NSString stringWithFormat:@"getAndroidNoteCollection('%ld','%@','%ld','%@')",(long)CollectDataType_CollectFile,dataModel.FolderName,(long)dataModel.Id,[AppUserModel sharedAppUserModel].Name];
  242. [self upDateAction:jsonString];
  243. }
  244. }
  245. }
  246. #pragma mark - WKScriptMessageHandler
  247. -(NSString *)convertToJson:(NSDictionary *)dict
  248. {
  249. NSError *error;
  250. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
  251. NSString *jsonString;
  252. if (!jsonData) {
  253. NSLog(@"%@",error);
  254. }else{
  255. jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
  256. }
  257. NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
  258. NSRange range = {0,jsonString.length};
  259. //去掉字符串中的空格
  260. [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
  261. NSRange range2 = {0,mutStr.length};
  262. //去掉字符串中的换行符
  263. [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
  264. return mutStr;
  265. }
  266. #pragma mark - WKUIDelegete
  267. - (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(void))completionHandler
  268. {
  269. NSLog(@"结果:%@",message);
  270. completionHandler();
  271. }
  272. - (void)webView:(WKWebView *)webView runJavaScriptConfirmPanelWithMessage:(nonnull NSString *)message initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(BOOL))completionHandler
  273. {
  274. NSLog(@"223123");
  275. completionHandler(YES);
  276. }
  277. - (void)webView:(WKWebView *)webView runJavaScriptTextInputPanelWithPrompt:(nonnull NSString *)prompt defaultText:(nullable NSString *)defaultText initiatedByFrame:(nonnull WKFrameInfo *)frame completionHandler:(nonnull void (^)(NSString * _Nullable))completionHandler
  278. {
  279. NSLog(@"323123");
  280. completionHandler(@"111");
  281. }
  282. #pragma mark - 提交富文本
  283. - (void)submit
  284. {
  285. NSString *jsonString = [NSString stringWithFormat:@"getEditorText()"];
  286. [self.webView evaluateJavaScript:jsonString completionHandler:^(id _Nullable data, NSError * _Nullable error) {
  287. NSLog(@"submit:%@",data);
  288. NSDictionary * dict = (NSDictionary *)data;
  289. NSString * html = dict[@"html"];
  290. NSArray * jsonArray = dict[@"json"];
  291. NSMutableDictionary * paraDict = [NSMutableDictionary dictionary];
  292. [paraDict setObject:@"话题" forKey:@"Title"];
  293. [paraDict setObject:@([AppUserModel sharedAppUserModel].Id) forKey:@"UserId"];
  294. [paraDict setObject:@(0) forKey:@"ReadCount"];//没有
  295. [paraDict setObject:@(0) forKey:@"SourceType"];//没有
  296. [paraDict setObject:@(0) forKey:@"SourceId"];//没有
  297. [paraDict setObject:@(0) forKey:@"FolderId"];
  298. [paraDict setObject:@[] forKey:@"TopicAddresseeUserIds"];
  299. [paraDict setObject:html forKey:@"HtmlContent"];
  300. [paraDict setObject:[self arrayToJSONString:jsonArray] forKey:@"Content"];
  301. SHOWLOADING
  302. [[HttpManager sharedHttpManager] POSTUrl:Host(APP_Topic_Add) parameters:paraDict responseStyle:DATA success:^(id _Nonnull responseObject) {
  303. NSLog(@"%@",responseObject);
  304. REMOVESHOW
  305. SHOWSUCCESS(@"创建成功");
  306. } failure:^(NSError * _Nonnull error) {
  307. REMOVESHOW
  308. SHOWERROR([ZYCTool handerResultData:error])
  309. }];
  310. }];
  311. }
  312. #pragma mark - 撤回
  313. - (void)deleteButtonHander:(NSInteger)deleteId{
  314. WS(weakSelf);
  315. SHOWLOADING
  316. NSString * url = [NSString stringWithFormat:@"%@%ld",Host(API_NOTICE_RECALL),(long)deleteId];
  317. [[HttpManager sharedHttpManager] PUTUrl:url parameters:@{} responseStyle:DATA success:^(id _Nonnull responseObject) {
  318. REMOVESHOW
  319. SHOWSUCCESS(@"撤回成功");
  320. [weakSelf.navigationController popViewControllerAnimated:YES];
  321. } failure:^(NSError * _Nonnull error) {
  322. REMOVESHOW
  323. }];
  324. }
  325. #pragma mark - 编辑
  326. - (void)editorButtonHander:(NSInteger)editId{
  327. H5ViewController * vc = [[H5ViewController alloc] init];
  328. [self.navigationController pushViewController:vc animated:YES];
  329. }
  330. - (NSString *)arrayToJSONString:(NSArray *)array
  331. {
  332. NSError *error = nil;
  333. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:array options:NSJSONWritingPrettyPrinted error:&error];
  334. NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  335. return jsonString;
  336. }
  337. - (void)dealloc {
  338. WKUserContentController *userCC = self.webView.configuration.userContentController;
  339. [userCC removeScriptMessageHandlerForName:@"chosePic"];
  340. [userCC removeScriptMessageHandlerForName:@"toPZ"];
  341. [userCC removeScriptMessageHandlerForName:@"toBJ"];
  342. [userCC removeScriptMessageHandlerForName:@"toSC"];
  343. [userCC removeScriptMessageHandlerForName:@"toWJ"];
  344. [userCC removeScriptMessageHandlerForName:@"delelteAction"];
  345. [userCC removeScriptMessageHandlerForName:@"delelteAction"];
  346. [userCC removeScriptMessageHandlerForName:@"postTopicDetailsData"];
  347. [[NSNotificationCenter defaultCenter] removeObserver:self];
  348. [self.webView stopLoading];
  349. self.webView.UIDelegate = nil;
  350. self.webView.navigationDelegate = nil;
  351. self.webView.scrollView.delegate = nil;
  352. [self.webView removeFromSuperview];
  353. }
  354. - (void)viewWillDisappear:(BOOL)animated
  355. {
  356. [super viewWillDisappear:animated];
  357. WKWebsiteDataStore *dateStore = [WKWebsiteDataStore defaultDataStore];
  358. [dateStore fetchDataRecordsOfTypes:[WKWebsiteDataStore allWebsiteDataTypes]
  359. completionHandler:^(NSArray * __nonnull records) {
  360. for (WKWebsiteDataRecord *record in records)
  361. {
  362. [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:record.dataTypes
  363. forDataRecords:@[record]
  364. completionHandler:^{
  365. NSLog(@"Cookies for %@ deleted successfully",record.displayName);
  366. }];
  367. }
  368. }];
  369. }
  370. @end